Newer Version Available
InvocableMethod Annotation
Invocable methods are called with REST API and used to invoke a single Apex method. Invocable methods have dynamic input and output values and support describe calls.
This code sample shows an invocable method with primitive data types.
1public class AccountQueryAction {
2 @InvocableMethod(label='Get Account Names' description='Returns the list of account names corresponding to the specified account IDs.' category='Account')
3 public static List<String> getAccountNames(List<ID> ids) {
4 List<String> accountNames = new List<String>();
5 List<Account> accounts = [SELECT Name FROM Account WHERE Id in :ids];
6 for (Account account : accounts) {
7 accountNames.add(account.Name);
8 }
9 return accountNames;
10 }
11}This code sample shows an invocable method with a specific sObject data type.
1public class AccountInsertAction {
2 @InvocableMethod(label='Insert Accounts' description='Inserts the accounts specified and returns the IDs of the new accounts.' category= 'Account')
3 public static List<ID> insertAccounts(List<Account> accounts) {
4 Database.SaveResult[] results = Database.insert(accounts);
5 List<ID> accountIds = new List<ID>();
6 for (Database.SaveResult result : results) {
7 if (result.isSuccess()) {
8 accountIds.add(result.getId());
9 }
10 }
11 return accountIds;
12 }
13}This code sample shows an invocable method with the generic sObject data type.
1public with sharing class GetFirstFromCollection {
2 @InvocableMethod
3 public static List <Results> execute (List<Requests> requestList) {
4 List<SObject> inputCollection = requestList[0].inputCollection;
5 SObject outputMember = inputCollection[0];
6
7 //Create a Results object to hold the return values
8 Results response = new Results();
9
10 //Add the return values to the Results object
11 response.outputMember = outputMember;
12
13 //Wrap the Results object in a List container
14 //(an extra step added to allow this interface to also support bulkification)
15 List<Results> responseWrapper= new List<Results>();
16 responseWrapper.add(response);
17 return responseWrapper;
18 }
19
20 public class Requests {
21 @InvocableVariable(label='Records for Input' description='yourDescription' required=true)
22 public List<SObject> inputCollection;
23 }
24
25 public class Results {
26 @InvocableVariable(label='Records for Output' description='yourDescription' required=true)
27 public SObject outputMember;
28 }
29}This code sample shows an invocable method with a custom icon from an SVG file.
1global class CustomSvgIcon {
2 @InvocableMethod(label='myIcon' iconName='resource:myPackageNamespace__google:top')
3 global static List<Integer> myMethod(List<Integer> request) {
4 List<Integer> results = new List<Integer>();
5 results.add(1);
6 return results;
7 }
8}This code sample shows an invocable method with a custom icon from the Salesforce Lightning Design System (SLDS).
1public class CustomSldsIcon {
2
3@InvocableMethod(iconName='slds:standard:choice')
4public static void run() {}
5
6}Supported Modifiers
All modifiers are optional.
- label
- The label for the method, which appears as the action name in Flow Builder. The default is the method name, though we recommend that you provide a label.
- description
- The description for the method, which appears as the action description in Flow Builder. The default is Null.
- callout
- The callout modifier identifies whether the method makes a call to an external system. If the method makes a call to an external system, add callout=true. The default value is false.
- category
- The category for the method, which appears as the action category in Flow Builder. If no category is provided (by default), actions appear under Uncategorized.
- configurationEditor
- The custom property editor that is registered with the method and appears in Flow Builder when an admin configures the action. If you don’t specify this modifier, Flow Builder uses the standard property editor.
- iconName
- The name of the icon to use as a custom icon for the action in the Flow Builder canvas. You can specify an SVG file that you uploaded as a static resource or a Salesforce Lightning Design System standard icon.
InvocableMethod 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. Create a custom global or public Apex class to implement your data type, and make sure that your class contains at least one member variable with the invocable variable annotation.
- 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. Create a custom global or public Apex class to implement your data type, and make sure that your class contains at least one member variable with the invocable variable annotation.
- Managed Packages
-
- You can use invocable methods in packages, but after you add an invocable method you can’t remove it from later versions of the package.
- Public invocable methods can be referred to by flows and processes within the managed package.
- Global invocable methods can be referred to anywhere in the subscriber org. Only global invocable methods appear in Flow Builder and Process Builder in the subscriber org.
For more information about invocable actions, see Actions Developer Guide.