Salesforce Flow: Use Invocable Actions

When Declarative Tools Aren’t Enough

Salesforce Flow is an amazing declarative tool that enables administrators to create complex automations. Standard elements and components allow for a lot things to be done without ever touching the code. You can freely modify records, use predefined actions, and power your processes with custom logicBut when a business requirement goes beyond what’s available in standard tools, there’s simply no choice but to ask for a developer’s help.This doesn’t mean you need to give up on building a flow and leave all the work to the more tech savvy people. It’s possible to invoke a code from a flow using a custom action.Let’s look at how this can be done.

What are Invocable Actions

The invocable actions mechanism allows to create custom code and package it into componentes/actions available in declarative tools, such as Flow.The idea is simple. You provide input values that the apex class does something with, and it gives you output values.Invocable actions are perfect when you need to build a process with a flow (so that users with no coding experience can still maintain it), but some important functionality is missing in the Flow Builder.Before we create one, let’s talk about the annotations - what exactly they do, what are their limitations as so on.

About InvocableMethod

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.The supported modifiers of InvocableMethod are:

  • label - appears as the action name in the Flow Builder.
  • description - same as the label
  • callout - identifies whether the method makes a call to an external system (true/false).
  • category - defines the action’s category.
  • configurationEditor - the custom property editor that is registered with the method and appears in Flow Builder when an admin configures the action.

Considerations:

  • The invocable method must be static, public, or global, and its class must be an outer class.
  • Only one method can be invocable.
  • 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.

About InvocableVariable

The InvocableVariable annotation identifies a class variable used as an input or output parameter for an InvocableMethod method’s invocable action.The supported modifiers for InvocableVariable are:

  • label - this appears in Flow Builder to help admins identify the purpose of the variable
  • description - same as before
  • required - true or false

Here’s a couple of considerations to remember when using InvocableVariables:

  • The data type of the invocable variable can only be: a primitive, an sObject, or a list or a list of list of primitives, sObjects, objects created Apex classes, or collections
  • Only public and global variables can be invocable variables
  • It can’t be a property, a non-member variable such as a static or local variable, a final variable, protected or private.

Creating an Invocable Action

First of all, we need an Apex Class. For the purpose of this tutorial, let’s use a class that we’ve already created in a different article. This class takes a Decimal value that represents a EUR price and converts it to USD by making a callout to an external API.Let’s adjust it so we can use it in the Flow Builder.The class needs to be global instead of public.global class CurrencyConverterInvocableNow, we can create our InvocableVariables. One of them is going to hold the requested price to be converted, and the other is going to hold the result. Add two global classes as below:

global class Requests {@InvocableVariableglobal DecimaleurPrice;}

global class Results {@InvocableVariableglobal DecimalusdPrice;}

Let’s use those classes in the method. Firstly, we need to annotate the method with InvocableMethod. Secondly, we need to change the data type of the returned value, and out input. Remember, the data type of the input parameter and the return value must be a list.

@InvocableMethod(label='Convert EUR to USD' description = 'Converts a decimal EUR into a decimal USD' category='Account')public static List convertCurrencyEURToUSD(List eur) {In the original version of this class we used a Decimal variable named “usd” to store the result. This time we are returning a value from the class we’ve created.

Results usd = new Results();

Because the input value is a list, we use the index to get what we need.usd.usdPrice = eur[0].eurPrice

* conversionRate;

Finally, let’s create a list to store the return value. Next, add the calculated value to it, and return it.

List resultsList = new List();resultsList.add(usd);return resultsList;And that’s it. Here’s how your final code should look:

invocable action final code

Now, the newly created class will be available in the list of Actions. We can use the invocable variables to store input from users, and calculate the converted currency value.

use apex action in 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. Make sure to try them when you get the chance!Read more Salesforce-related articles on our blog.