Newer Version Available

This content describes an older version of this product. View Latest

InvocableMethod Annotation

Use the InvocableMethod annotation to identify methods that can be run as invocable actions.

Invocable methods are called with the REST API and used to invoke a single Apex method. Invocable methods have dynamic input and output values and support describe calls.

The following 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.')
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.')
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}

Invocable Method 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 is not supported.
  • A list of an sObject type or a list of lists of an sObject type – the generic sObject type is not supported.
  • A list of a user-defined type, containing variables of the supported types and with the InvocableVariable annotation. Create a custom global or public Apex class to implement your data type, and make sure your class contains at least one member variable with the invocable variable annotation.
If the return type is not 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 is not supported.
  • A list of an sObject type or a list of lists of an sObject type – the generic sObject type is not supported.
  • A list of a user-defined type, containing variables of the supported types and with the InvocableVariable annotation. Create a custom global or public Apex class to implement your data type, and make sure your class contains at least one member variable with the invocable variable annotation.
Managed Packages
  • You can use invocable methods in packages, but once 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 the Cloud Flow Designer and Process Builder in the subscriber org.

For more information about invocable actions, see Actions Developer’s Guide.