The Trailblazer Community in Slack is now in Open Beta—join and connect in real time. [Join us in Slack]
Invoke Apex methods annotated with @InvocableMethod and include custom parameters with @InvocableVariable.
This object is available in API version 33.0 and later.
URIs:
/services/data/vXX.X/actions/custom/apex/services/data/vXX.X/actions/custom/apex/action_nameFormats: JSON, XML
HTTP Methods: GET, HEAD, POST
Authentication: Authorization: Bearer token
Parameters: None
Example
This example invokes the Apex action called ActionTestWithSObject, which takes a list of Accounts, increases the employee count for each account by one, and returns an updated list of account IDs. The top-level key name in the JSON request body must be inputs.
1POST /services/data/vXX.X/actions/custom/apex/ActionTestWithSObject
2
3{
4
5 "inputs": [
6 {
7 "objects": {
8 "attributes" : {
9 "type" : "Account"
10 },
11 "Name": "Acme"
12 }
13 },
14 {
15 "objects": {
16 "attributes" : {
17 "type" : "Account"
18 },
19 "Name": "Global Media"
20 }
21 }
22 ]
23}Here’s the Apex code.
1public class ActionTestWithSObject {
2
3 @InvocableMethod(label='Action Test With sObject' description='Given a list of sObjects (Accounts), increase employee count by one and return account ID' category='Account')
4 public static List<String> getAccountNames(List<Account> objects) {
5
6 List<String> accountIds = new List<String>();
7 Map<Id, Account> accountsMap = new Map<Id, Account>(objects);
8 Map<Id, Account> retrievedAccountMap = new Map<Id, Account>([SELECT Id, NumberOfEmployees FROM Account WHERE Id in :accountsMap.keySet()]);
9
10 for (Account a : objects) {
11 Account ra = retrievedAccountMap.get(a.Id);
12 ra.NumberOfEmployees = ra.NumberOfEmployees == null ? 1 : ra.NumberOfEmployees + 1;
13 accountIds.add(ra.Id);
14 }
15 update retrievedAccountMap.values();
16 return accountIds;
17 }
18}The resource is the name of the Apex class, not the Apex method. In this example, the resource is /ActionTestWithSObject, not /getAccountNames.
@InvocableMethod annotation from the Apex class, you get a runtime error in the flow.public or global in a managed package. However, that action doesn’t appear in Flow Builder’s list of available Apex actions. Flows within the same managed package can still refer to these invocable actions. Global Apex invocable actions in a managed package can be used in flows outside the managed package, anywhere in the organization, and appear in Flow Builder’s list of available Apex actions.Supply input values that correspond to the Apex action.
A POST request body must use the JSON format specified in Invoking Actions.
Apex methods annotated with @InvocableMethod must take a List as an input and return a List or Null. For more information, see @InvocableMethod Annotation in the Apex Developer Guide.
Only the following primitive types are supported as inputs in a POST request body:
BlobBooleanDateDatetimeDecimalDoubleIDIntegerLongStringTimeConcrete types inherited from the sObject. In the previous example, the inherited concrete type is Account.
A user-defined type, containing variables of the supported types and 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.
The Apex InvocableMethod determines the output values.
See Also