How to call Apex Method from Flow and Vice — Versa

Aman Garg
3 min readJan 21, 2023

--

To call the apex method from flow, use an Invocable Method Annotation. Use the Invocable Method annotation to identify methods that can be run as invocable actions.

Limitation of InvocableMethod Annotation -:

  • The invocable method must be static and public or global, and its class must be an outer class.
  • Only one method in a class can have the InvocableMethod annotation.
  • Other annotations can’t be used with the InvocableMethod annotation.

Supported Modifiers in InvocableMethod Annotation -:

  • Label : The action name in Flow Builder is the label for the method. Although we advise that you include a label, the method name is the defaults.
  • Description : the method’s explanation, which is displayed in Flow Builder as the action description. The standard is Null.
  • Callout : The callout modifier indicates whether or not the method calls an outside system. Add callout=true if the method calls an external system. False is the default setting.
  • Category : The action category in Flow Builder is the category for the method. In the absence of a category, actions are displayed under Uncategorized (by default).
  • ConfigurationEditor : When an administrator configures the action, the custom property editor that is registered with the method and displays in Flow Builder. Without this change, Flow Builder employs the default property editor.
  • iconName : The name of the icon that will be used as a unique icon for the action in the Flow Builder canvas. You have the option of specifying either a Salesforce Lightning Design System default icon or an SVG file that you supplied as a static resource.

Follow these steps to call Apex Method from Flows:

Step 1: Create an Apex Class.

Step 2: Create an Apex Method in the Apex class using the InvocableMethod Annotation.

public class AccountQueryAction {
@InvocableMethod(label='Get Account Names' description='Returns the list of account names corresponding to the specified account IDs.' category='Account')
public static List<String> getAccountNames(List<ID> ids) {
List<String> accountNames = new List<String>();
List<Account> accounts = [SELECT Name FROM Account WHERE Id in :ids];
for (Account account : accounts) {
accountNames.add(account.Name);
}
return accountNames;
}
}

Step 3: Quick Find Box -> Search Flow -> Click Flow -> Click New Flow.

Step 4: Create a Record-Triggered Flow.

Step 5: Create a Record-Triggered Flow.

  • Click Edit & Select Account Object.
  • Configure Trigger : When Record is Created
  • Set Entry Conditions:
  • Field: Id
  • Operator: Is Null
  • Value: False
  • Click Done.
  • Click (+) Icon & Search Action.
  • Search Apex Method in the Action Search Box.
  • Set the Label & API Name.
  • If record id is being passed in your method, then you have to pass record id in id field.

To call Apex Method flows, follow these steps:

Step 1: Create an auto-launched flow.

Step 2: Create an Apex Class.

Map<String, Object> params = new Map<String, Object>();
params.put('Routable_Id',objChatTranscript.Id);
Flow.Interview.Flow_Api_Name callingFlow =Flow.Interview.Flow_Api_Name(params);
callingFlow.start();

*Note: Routable_Id is the id used when the live chat begins to use the embedded service chat support. So, you will change this ID based on the object record.

originally published at http://amansfdc.wordpress.com on January 21, 2023.

--

--

Aman Garg
Aman Garg

Written by Aman Garg

Sr. Executive - Training || 6x Salesforce Certified || 2x Copado Certified || Salesforce Mentor || Founder of Salesforce Learners

No responses yet