Spring’20 brings one Invocable method for all
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);...
Read More