Spring’20 brings one Invocable method for all

by Sudipta Deb | Aug 19, 2020 | Apex, flow, Invocable, Optimize, Spring 20 | 0 comments

 

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 {
@InvocableMethod(
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){
records.addAll(singleRequest.records);
}
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();
castRecords.addAll(records);
upsert castRecords;
singleResponse.records = castRecords;
response.add(singleResponse);
}
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. 

Written by

Related Posts

New Apex Method | Extracting Picklist Values Based on Record Type Inside Apex

New Apex Method | Extracting Picklist Values Based on Record Type Inside Apex

For years, Salesforce developers have faced a common challenge: programmatically retrieving picklist values that are specific to a certain Record Type directly within Apex. The “solutions” often involved either complex SOQL queries against metadata, relying on the UI API (with its associated callout limits and serialization overhead), or maintaining clunky custom metadata/settings.

Good news, Technical Potpourri readers! The Salesforce Spring ’26 release is bringing a game-changer that will significantly simplify your Apex code and improve performance: native Apex methods to filter picklist values by Record Type!

read more...
Apex Cursor and Its Advantages Over Batch Processing

Apex Cursor and Its Advantages Over Batch Processing

Struggling with large datasets and SOQL governor limits in Salesforce? In this video, I will dive deep into Apex Cursors, the powerful new feature that revolutionizes how we process millions of records.

Apex Cursors allow developers to navigate through query results in manageable chunks within a single transaction or across Queueable jobs. In this video, I will show you the correct way to implement Apex Cursor and also when this is an exception, how to handle that and perform retry.

read more...
Perform Inline Edit And Sort Inside Flow Data Table

Perform Inline Edit And Sort Inside Flow Data Table

I’ll show you exactly how to use the new data table component features from Salesforce’s Spring 26 release. Learn how to implement inline editing and column sorting directly within your screenflow, making your workflow automation more efficient. This update offers significant enhancements for any salesforce developer working with flowbuilder.

Also I have shown how to save the inline edit changes properly in the salesforce.

read more...

0 Comments

Leave a Reply