How to call apex class from salesforce flow

In this article, we are going to achieve a scenario where we update the “Account Primary Email” field (a custom field) by clicking a button on the Contact record page. This action will update the Contact email value to match the Account Primary Email value using a Screen Flow. We will also demonstrate how to call Apex in Flow by using the concept of an invocable method. This method allows you to call any type of Apex in any type of Flow using the same concept

  • Create a Primary Contact Email in Account Object

InvocableMethodClass

				
					public class InvocableMethodClass {
    @InvocableMethod(label='Update Account Primary Email')
    public static void updateAccountEmail(List<Id> recordId){
        Contact con = [Select Id,Email,AccountId From Contact Where Id =: recordId limit 1];
        Account acc = new Account();
        acc.Id = con.AccountId;
        acc.Primary_Contact_Email__c  = con.Email;
        Update acc;
    }
}

				
			

nvocableMethod Considerations

 

  • Implementation Notes
    • 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.

 

  • Inputs and Outputs

 

There can be at most one input parameter and its data type must be one of the following:

  • A list of a primitive data type or a list of lists of a primitive data type – the generic Object type isn’t supported.
  • A list of an sObject type or a list of lists of an sObject type.
  • A list of the generic sObject type (List<sObject>) or a list of lists of the generic sObject type (List<List<sObject>>).
  • A list of a user-defined type, containing variables of the supported types or user-defined Apex types, with the InvocableVariable annotation. To implement your data type, create a custom global or public Apex class. The class must contain at least one member variable with the invocable variable annotation.

If the return type isn’t Null, the data type returned by the method must be one of the following:

  • A list of a primitive data type or a list of lists of a primitive data type – the generic Object type isn’t supported.
  • A list of an sObject type or a list of lists of an sObject type.
  • A list of the generic sObject type (List<sObject>) or a list of lists of the generic sObject type (List<List<sObject>>).
  • A list of a user-defined type, containing variables of the supported types or user-defined Apex types, with the InvocableVariable annotation. To implement your data type, create a custom global or public Apex class. The class must contain at least one member variable with the invocable variable annotation.

Create a screen flow:

  • Go to setup home
  • Search flow in the quick-find box
  • Click on flows
  • Click on the new flow

It will navigate you to flow builder Canva, select ‘start from scratch’, click on next, and select screen flow

Now start adding elements to the flow

  • Click on the plus icon in Flow Canva and search for Invocable method with invocable label
  • Select the action, Name it, and pass the record ID (Contact ID)
  • Add a screen for the confirmation message
  • Add a display text and success message to the screen

Testing ScreenShots:

Leave a Comment