In this session, We will be talk how to invoke/call Apex actions in flows and how to pass variables between a Flow and an Apex Action. Salesforce has taken programming into the next level by introducing Flows, which allows users to configure complex flows in a matter of minutes.

What are Invocable Actions

The invocable actions mechanism allows to create custom code and package it into components / actions available in declarative tools, such as Flow. You provide input values that the apex class does something with, and it gives you output values.

Before we create one, let's talk about the annotations - what exactly they do, what are their limitations as so on.

InvocableMethod Annotation

The InvocableMethod annotation tells Salesforce that this class should be exposed to users of invocable actions in the Flow Builder. It can be used to invoke a single Apex method.

Sample code

public class AccountAction {   @InvocableMethod(label='Get Account Names' description='Returns the list of account' category='Account')   public static List<Account> getAccount(List<ID> ids) {     // Do Something   } } 

InvocableMethod Considerations:

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

InvocableVariable Annotation

The InvocableVariable  identifies a class variable used as an input or output parameter for an InvocableMethod method's invocable action. The invocable variable annotation supports the modifiers shown in this example.

@InvocableVariable(label='yourLabel' description='yourDescription' required=(true | false))

This label appears in Flow Builder for the Action element that corresponds to an invocable method. This label helps admins understand how to use the variable in the flow.

InvocableVariable Considerations

  • Other annotations can't be used with the InvocableVariable annotation.
  • Only global and public variables can be invocable variables.
  • The invocable variable can't be one of the following:
    • A non-member variable such as a static or local variable.
    • A property.
    • final variable.
    • Protected or private.
  • The data type of the invocable variable must be one of the following:
    • A primitive
    • An sObject, either the generic sObject or a specific sObject
    • A list or a list of lists of primitives, sObjects, objects created from Apex classes, or collections
  • For managed packages:
    • Public invocable variables can be set in flows and processes within the same managed package.
    • Global invocable variables can be set anywhere in the subscriber org. Only global invocable variables appear in Flow Builder and Process Builder in the subscriber org.

Recording

Scenario : Create a Custom Picklist field on Account with Active, In Active & Hold. If the Account status is changed to In Active or Hold. Delete all the Closed Won Opportunity related to that Account. If the Case is Created where Origin is Email and Contact is Not Null then send an email to the related contact, also save that email as activity.

Agenda:

  1. Introduction to Invocable Apex
  2. Call the Apex Class From Flow
  3. Pass multiple parameters from flow to Apex
  4. Return the result from Apex to Flow

Conclusion

Invocable actions are amazing when you need to provide Salesforce administrators with additional functionality not available in declarative tools. They can open new ways for administrators and developers to work together

Check out the our YouTube, and don't forget to subscribe to our channel, so that you're notified right away when a new video is available. Check complete Salesforce flow builder training here.

Yeah! You know the way to go with that Invocable Apex and Call the Apex Class From flow now. Read more blogs and find out about other flow types too.