LinkedIn

How to develop reusable Invocable Apex methods for Flows

 Salesforce is adding many new features and capabilities to Flows. But there are still some scenarios where you need to call apex methods from Flows to execute complex operations - when Flow is not capable of doing that or the performance will be low when you implement that using flows.

Let us see how we can build reusable apex methods for Flows. For this let us consider 2 sample scenarios:

Scenario 1 - Quick Create Account/Contact

I want to build a screen, where the user can select the object needs to be created, and then create a record of that object by entering the required details. Also if the record creation is getting failed, I want to create an exception record with details.

Solution

Let us see how we can utilize Apex action for this.

For this first let us create an Apex Invocable method, which can accept a list of wrapper class as shown below:




You can see, this invocable method is accepting a List of wrapper class variables and also returning a list of wrapper class variables. The wrapper class is defined as shown below:


Advantage of Using Wrapper Class

Wrapper class gives you more flexibility on the types and complexity of data that you can handle between Flows and Apex classes.

Method Logic

1. Wrapper Class with Sobject input Type

This Invocable method accepts wrapper class list and executes INSERT DML operation of the input records.

If you see the wrapper class definition you can see that one of the parameters is:

 @InvocableVariable

   public List<sObject> sobjs;

This consumes the Spring 20 release update from Salesforce where we can pass sObject data type to apex class method from Flows. 

Advantage of passing sObject Type

This enables the apex method to be consumable from multiple flows for multiple object types.

2. INSERT DML Statement

Below statement execute the INSERT operation for accepted records:

 Database.SaveResult[] results = Database.insert(completesObjlist, false);

Advantages of Using Database.insert

1. You will be able to perform a partial database save if needed. That is out of 10 records that are getting inserted - if 5 are failing, the rest 5 records can be still successful.

2. If each record is failing because of different reasons you will be able to log the Exception details of each of this failure.

3. Exception Logging

The below statement calls the exception logging logic:

 ExceptionUtil.publishException(request[0].ObjectType,'FlowException',results );

Below is the publishException method and you can refer to the below blog post to understand how you can utilize platform events to log Exception.

https://meera-r-nair.blogspot.com/2020/06/flows-exception-logging-using-fault.html 


So the above code will iterate over database.saveresult and if it is not successful will publish the details to a platform event. Later the trigger on platform event will log the details to Exception Object.


Flow Screen to Create Records

I have implemented the below Flow to create a record based on the selected object type:


Flow Details

1. Screen element to select Object



2. Populate required details



3. Insert Record

We implement this using invocable method and the same function we can reuse for inserting Account, Contact, or any other object.

See below how I have called Invocabelmethod.


Assigning response from InvocableMethod to flow variable

More details can be seen in the demo added at the end.

4. Response On the Screen


5. Created Account


The same approach is used to create a new Contact record also:

1. Select Contact

2. Populate Required Fields


3. Response from Apex


4. Exception Record


Scenario - 2

Create Task and Assign to Marketing Rep on new Lead Creation

I have created a new field in Lead called Marketing Rep, which is a user lookup. Whenever a new Lead is getting created, I want to create a new Task and assign it to this Marketing Rep.

Below is the Flow implemented for this.


In this also the same apex action is called used to insert Task as shown below:


1. New Lead Creation


2. Automated Task Creation


Below Demo shows this in Action:



Note - As per the latest release, only if you add a specific Apex class to the Profile, then only those profile users will be able to access Invocable Actions from Flows.

The Flows and apex class used in this blog is available in the below repository path:

https://github.com/MeeraRNair/FlowReusableApexMethods 

References:

http://acknowledgecloud.blogspot.com/2017/07/salesforce-invoke-apex-from-visual-flows.html - referred for invoking apex 

 

Comments

  1. https://www.tipsandtricksndiys.com/2020/12/tips-to-develop-good-presence-of-mind.html?showComment=1627902427200#c3875180871446252336

    ReplyDelete
  2. Hi Meera, Could you pls provide screen shots for the flow. Thanks.

    ReplyDelete

Post a Comment

Popular posts from this blog

Subscribing to Salesforce Platform Events using External Java Client - CometD

Salesforce Security - Restriction Rules and Scoping Rules