Call Apex Class From Lightning Flow

by Rijwan Mohmmed
call-apex-class-from-lightning-flow-techdicer

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 :

  1. The invocable method must be static, public, or global, and its class must be an outer class.
  2. Only one method in a class can have the InvocableMethod annotation.
  3. Triggers can’t reference Invocable methods.
  4. No other annotations can be used alongside the InvocableMethod annotation.
  5. 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.
  6. 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 :

call-apex-class-from-lightning-flow-process-techdicer-FLow Diagram
call-apex-class-from-lightning-flow-process-techdicer-FLow Diagram

Step 1: Go to Setup -> Flow -> Click New and then Select Screen Flow

call-apex-class-from-lightning-flow-process-techdicer-create-process

Step 2: In this step, we will create variables

call-apex-class-from-lightning-flow-process-techdicer-create-accountId-variable
call-apex-class-from-lightning-flow-process-techdicer-create-accountId-variable
call-apex-class-from-lightning-flow-process-techdicer-create-accountname-variable
call-apex-class-from-lightning-flow-process-techdicer-create-accountname-variable
call-apex-class-from-lightning-flow-process-techdicer-create-recordId
call-apex-class-from-lightning-flow-process-techdicer-create-recordId

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.

Assign Account Id
Assign Account Id

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.

Assign Account Name

Step 7: Now we create a screen to show the account name

Flow Screen 1

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.

Output :

Reference :

  1. Flow Element: Apex Action
What’s your Reaction?
+1
0
+1
0
+1
0
+1
0
+1
0
+1
1

You may also like

Leave a Comment