To call the apex method from flow use 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 default.
- 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 displayed 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 Apex Class.
Step 2: Create Apex Method in Apex class using 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 and select Account Object.
- Configure Trigger: When Record is Created
- Set Entry Conditions:
- All Conditions are Met (AND)
- Field: Id
- Operator: Is Null
- Value: False
- All Conditions are Met (AND)
- Click Done.
- Click the (+) Icon and search Action.
- Search Apex Method in the Action Search Box.
- Set the Label & API Name.
- If the record ID is being passed in your method, then you have to pass the record id in the id field.
- Click Done.
- Click Save.
Follow these Steps to Call Flows from the Apex Method
Step 1: Create AutoLaunched Flow.
Step 2: Create Apex Class.
Map<String, Object> params = new Map<String, Object>();
params.put('Routable_Id',objChatTranscript.Id);
Flow.Interview.Flow_Api_Name callingFlow = new Flow.Interview.Flow_Api_Name(params);
callingFlow.start();
Note* -: Here, Routable_Id is the id when the Live chat Starts using the Embedded Service Chat Support. So, you will change this ID based on the object record.
One Response
Thank you Aman for sharing this usefull article.