Newer Version Available

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

InvocableVariable Annotation

Use the InvocableVariable annotation to identify variables used by invocable methods in custom classes.

The InvocableVariable annotation identifies a class variable used as an input or output parameter for an InvocableMethod method’s invocable action. If you create your own custom class to use as the input or output to an invocable method, you can annotate individual class member variables to make them available to the method.

The following code sample shows an invocable method with invocable variables.

1global class ConvertLeadAction {
2  @InvocableMethod(label='Convert Leads')
3  global static List<ConvertLeadActionResult> convertLeads(List<ConvertLeadActionRequest> requests) {
4    List<ConvertLeadActionResult> results = new List<ConvertLeadActionResult>();
5    for (ConvertLeadActionRequest request : requests) {
6      results.add(convertLead(request));
7    }
8    return results;
9  }
10
11  public static ConvertLeadActionResult convertLead(ConvertLeadActionRequest request) {
12    Database.LeadConvert lc = new Database.LeadConvert();
13    lc.setLeadId(request.leadId);
14    lc.setConvertedStatus(request.convertedStatus);
15
16    if (request.accountId != null) {
17        lc.setAccountId(request.accountId);
18    }
19
20    if (request.contactId != null) {
21      lc.setContactId(request.contactId);
22    }
23
24    if (request.overWriteLeadSource != null && request.overWriteLeadSource) {
25      lc.setOverwriteLeadSource(request.overWriteLeadSource);
26    }
27
28    if (request.createOpportunity != null && !request.createOpportunity) {
29      lc.setDoNotCreateOpportunity(!request.createOpportunity);
30    }
31
32    if (request.opportunityName != null) {
33      lc.setOpportunityName(request.opportunityName);
34    }
35
36    if (request.ownerId != null) {
37      lc.setOwnerId(request.ownerId);
38    }
39
40    if (request.sendEmailToOwner != null && request.sendEmailToOwner) {
41      lc.setSendNotificationEmail(request.sendEmailToOwner);
42    }
43
44    Database.LeadConvertResult lcr = Database.convertLead(lc, true);
45    if (lcr.isSuccess()) {
46      ConvertLeadActionResult result = new ConvertLeadActionResult();
47      result.accountId = lcr.getAccountId();
48      result.contactId = lcr.getContactId();
49      result.opportunityId = lcr.getOpportunityId();
50      return result;
51    } else {
52      throw new ConvertLeadActionException(lcr.getErrors()[0].getMessage());
53    }
54  }
55
56  global class ConvertLeadActionRequest {
57    @InvocableVariable(required=true)
58    global ID leadId;
59
60    @InvocableVariable(required=true)
61    global String convertedStatus;
62
63    @InvocableVariable
64    global ID accountId;
65
66    @InvocableVariable
67    global ID contactId;
68
69    @InvocableVariable
70    global Boolean overWriteLeadSource;
71
72    @InvocableVariable
73    global Boolean createOpportunity;
74
75    @InvocableVariable
76    global String opportunityName;
77
78    @InvocableVariable
79    global ID ownerId;
80
81    @InvocableVariable
82    global Boolean sendEmailToOwner;
83  }
84
85  global class ConvertLeadActionResult {
86    @InvocableVariable
87    global ID accountId;
88
89    @InvocableVariable
90    global ID contactId;
91
92    @InvocableVariable
93    global ID opportunityId;
94  }
95
96  class ConvertLeadActionException extends Exception {}
97}

The following code sample shows an invocable method with invocable variables that have 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
21public class Requests {
22  @InvocableVariable(label='Records for Input' description='yourDescription' required=true)
23  public List<SObject> inputCollection;
24  }
25
26public class Results {
27  @InvocableVariable(label='Records for Output' description='yourDescription' required=true)
28  public SObject outputMember;
29  }
30}

Supported Modifiers

The invocable variable annotation supports the modifiers shown in this example.

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

All modifiers are optional.
label
The label for the variable. The default is the variable name.

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.

Tip

description
The description for the variable. The default is Null.
required
Specifies whether the variable is required. If not specified, the default is false. The value is ignored for output variables.

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

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