Hello friends, today we are going to discuss Call Apex Class From Lightning Flow. Here, we’ll look at how to use Apex actions in flows and how to pass variables between Apex actions and flows. Flows, introduced by Salesforce, lets users configure complex flows in just a few minutes.
Also check this: Safe Navigation Operator in Apex
By using invocable actions, you can create custom code and package it into components/actions available in declarative tools, such as Flow. Apex class does something with input values, and you get output values
InvocableMethod :
public class InvocaleCtrl {
@InvocableMethod(label='Get Conatct Names' description='Returns the list of account')
public static List<Contact> getContact(List<ID> ids) {
// Do Something
}
}
Key Highlights :
- The invocable method must be static, public, or global, and its class must be an outer class.
- Only one method in a class can have the InvocableMethod annotation.
- Triggers can’t reference Invocable methods.
- No other annotations can be used alongside the InvocableMethod annotation.
- There can be at most one input parameter and its data type must be a list of the following: primitive data type, sObject type, generis sObject type, user-defined type containing variables of the supported types above, or user-defined Apex types.
- The data type returned must also be a list.
Code :
ContactInvocaleCtrl.cls :
public class InvocaleCtrl {
@InvocableMethod(label='Get Account' description='Returns the list of Account')
public static List<Account> getAccountInfo(List<ID> ids) {
List<Account> acc = [SELECT Id, Name FROM Account WHERE Id IN : ids LIMIT 1];
return acc;
}
}
Flow Process Steps :
Step 1: Go to Setup -> Flow -> Click New and then Select Screen Flow
Step 2: In this step, we will create variables
Step 3: Here we will create Get Record Screen to fetch Contact record
Step 4: Here we will Assign Contact Account Id to accountId variable which we already defined in the above screens. And the Account Id value will come from the Get Contact Record variable.
Step 5: In this step, we will create an Action to call/invoke apex class and fetch account records
Step 6: Assign Account name to accountname variable which we already defined and the account name value comes from apex method.
Step 7: Now we create a screen to show the account name
Step 8: Here we put display text on the flow screen by drag and drop from the left side and putting accountname variable in the text component in the right bar.