Invoking Apex with Flow Using Salesforce
In the Spring of 2019, Salesforce introduced Lightning Flows to their platform, expanding upon the already useful process builder and workflow automation tools. Flows have similar qualities to both of these tools, but to be frank, flows are more intuitive and are better! However, what if a flow can not solve your business process issue? What should you do then?
Any seasoned admin will tell you how much flows have improved their work experience. The user interface is pretty intuitive, even with little platform knowledge. Being able to follow the diagram’s ‘flow’ of operations makes it simple to see what is happening. Combined with the drag and drop functionality, creating powerful business processes has never been easier. For various reasons, many organizations prefer to use this feature, especially before writing any complex code. A few of these reasons are:
Although Lightning Flows have added immense value to the Salesforce platform, like all things in life, it is not perfect. Here are some limitations for each flow interview:
100 SOQL queries issued
150 DML statements
50,000 records retrieved
10,000 records modified by DML
10,000 millisecond CPU Time
12 duplicates per batch
The same record can be modified up to 12 times
2000 executed elements at runtime
Can easily be reached if you have a loop in your flow. Say you have a flow that has 400 records, multiply that by the number of elements in your loop. For example, if you have 5 actions in the loop, the flow will fail because you exceeded the limits. 400(# of records) x 5(# of actions in loop) = 2000.
With that being said, if we build a flow and run into one of these issues, should we scrap it entirely? Well, the short answer is, no! We can combine flows with Salesforce’s native programming language, Apex, to solve many of these use cases!
Along Comes Apex
Being able to delegate much of the complex logic where the declarative tools fall short is extremely powerful. But how do we pass our data to Apex? We do so by using invocable methods and variables. The concept is: send data to your invocable method and have it perform DML or some other logic. Then have the method optionally return or not return specified variables that your flow can use.
How to Invoke Apex
Now that we know the reasoning behind invoking Apex, how do we actually call it? By following a few simple steps.
Create an Invocable Method.
Creating an invocable method begins in the Apex class. Set the @invocableMethod annotation to the function you wish to use.
Things to note about Invocable Methods:
Only one method per class can use the InvocableMethod annotation.
If you’re using the InvocableMethod annotation, you may not use another annotation with that method.
Invocable methods may only contain one parameter.
For this reason, it is wise to pass in a list of variables, which we can then extract in the class and use. To do so, we use custom types to pass in more than one variable. If a custom type is used it has to be as a list.
For the InvocableMethod we set the label and description.
The label appears in flow builder for the action element.
The description gives a short explanation of the method.
2. Create Invocable Variables.
We passed in custom lists as the input, and are receiving a custom list as the output as well. This is how we will get around the single parameter limitation using custom types. We define these by using classes of InvocableVariables.
Things to Note about Invocable Variables:
They support Modifiers, like invocable methods.
Label, Description, Required
Other annotations can not be used with invocable variables.
Static, Local, Final, Protected, Private
For this example, we want to pass in a list of contact records and a list of account records. Then we will perform some data manipulation on these records, and as a result, receive a list of updated Contact Names and Emails.
3. Add Logic to Code.
After setting up our method and variables, the final step we need to do is get the input/output values in our class and perform whatever level of complex logic we want.
4. Invoke Apex From Flow.
Now that our Apex class is all set up, how do we incorporate it into our flow?
After collecting the data or records you want to pass in, add an Apex Action to your flow.
Select your invocable method: the label you created will appear as an option.
Set the input parameters from what you collected in your flow.
Set the output parameters as variables you defined.
Finally, you are done! Now you have successfully created an invocable method to provide support to your flow! What is great about this is once the Apex logic is set up, an admin has full control over how to use it. They can determine the data that is passed in, and do whatever they want with the data that is returned, without having to write any code.
Being able to combine two of the most powerful features the Salesforce platform provides, Apex and Lightning Flows, will definitely come in handy. Not all situations require invoking Apex from flows, but many can benefit from it. They both have their own useful features, as well as their own flaws. Fusing the two helps create efficient orgs which makes life easier for admins, developers, and most importantly, users.