Build Invocable Actions That Work for Multiple Objects

Now, developers can create reusable Apex actions that use the generic sObject and List<sObject> data types. Build one action that works for multiple objects, rather than one for each individual object. Developers can build a filter or sort action that works with any collection of records, from accounts and contacts to custom objects. Previously, developers couldn't use polymorphic Apex structures in invocable actions because generic data types weren't supported.

Where: This change applies to Lightning Experience and Salesforce Classic in Enterprise, Performance, Unlimited, and Developer editions.

How: Create a custom action, or edit an existing one. Use the sObject or List<sObject> data type in invocable methods and attributes.

public with sharing class GetFirstFromCollection {  
  
    @InvocableMethod
    public static List <Results> execute (List<Requests> requestList) {

        List<SObject> inputCollection = requestList[0].inputCollection;
       
        SObject outputMember = inputCollection[0];
        
        //Create a Results object to hold the return values
        Results response = new Results();

        //Add the return values to the Results object
        response.outputMember = outputMember;

        //Wrap the Results object in a List container 
        //(an extra step added to allow this interface to also support bulkification)
        List<Results> responseWrapper= new List<Results>();
        responseWrapper.add(response);
        return responseWrapper;    
    }

    public class Requests {

      @InvocableVariable(required=true)
      public List<SObject> inputCollection;
    }    

    public class Results {     

      @InvocableVariable
      public SObject outputMember;
    }
}

Previously, the example class would have been tied to the Account or Contact or myCustomObject__c object. But now developers create one action, and the Flow Builder admin chooses the object each time they use that action.

A new action screen with contact specified as the object for “recordsToCheck” input, and Account as the object for “recordsToReturn”.