Have you ever asked this question to yourself – Why I have to write multiple invocable methods for the same functionality working on different objects? Definitely not a very clean approach. Thankfully Salesforce came up with the enhanced version of Invocable methods which now can work on multiple objects doing similar types of work. 

With this new enhancement, you can now write Invocable methods which will accept generic sObject data type. As a result, you will write one single Invocable method which will now work for multiple sObjects. The advantage is that if in your org, you have multiple Invocable methods doing similar operations (you just created them because they need to work on different sObjects), it the time to merge them to make a single Invocable method and make your org look clean.

In this post, I am going to show the example of creating one Invocable method which will be used to create Lead and Account when calling from different flows.

Here is the Invocable method:

public class createRecords {
label = ‘Create Records’
description = ‘Update records or create new records’)
public static List<myResponse> createRecords(List<myRequest> request){
List<SObject> records = new List<SObject>();
for(myRequest singleRequest : request){
List<myResponse> response = new List<myResponse>();
myResponse singleResponse = new myResponse();
Schema.SObjectType sObjectType = records.get(0).getSObjectType();
if(sObjectType != null){
String listType = ‘List<‘+sObjectType+‘>’;
List<SObject> castRecords = (List<SObject>)Type.forName(listType).newInstance();
upsert castRecords;
singleResponse.records = castRecords;
return response;
public class myRequest {
@InvocableVariable(label = ‘Records to create’)
public List<SObject> records;

public class myResponse {
@InvocableVariable(label = ‘Inserted/Upserted records’)
public List<SObject> records;

As you can see, this above Apex code is written in such a way that it can work on any sObjects. In the actual Invocable method, we are typecasting the input records (myRequests -> records) to the actual sObject.

Below are two flows from where I am going to call this Invocable method passing the type of sObject against which the function needs to work.

Account Create Flow

Lead Create Flow

As you can see in both the flows, I have used the same Invocable method, just method the sObject name against which it will operate. I think this enhancement is going to make development very clean, reducing the surface area od code maintenance and increasing code reusability.