Newer Version Available

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

InvocableVariable Annotation

To identify variables used by invocable methods in custom classes, use the InvocableVariable annotation.

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.

This 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}

This 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
20public class Requests {
21  @InvocableVariable(label='Records for Input' description='yourDescription' required=true)
22  public List<SObject> inputCollection;
23  }
24
25public class Results {
26  @InvocableVariable(label='Records for Output' description='yourDescription' required=true)
27  public SObject outputMember;
28  }
29}

Supported Modifiers

All modifiers are optional.

Default values, labels, and placeholder text appear in Flow Builder for the Action element that corresponds to an invocable method. These modifiers help admins understand how to use variables in the flow.

Tip

defaultValue
The default value for the variable. Valid invocable variable data types are:
  • Boolean
  • Decimal
  • Double
  • Integer
  • Long
  • String
description
The description for the variable. The default is Null.
label
The label for the variable. The default is the variable name.
placeholderText
Provides examples or additional guidance about the invocable variable, such as examples of values that can set the invocable variable. Valid invocable variable data types are:
  • Double
  • Integer
  • String
required
Specifies whether the variable is required. If not specified, the default is false. The value is ignored for output variables.

The defaultValue modifier throws an error when used with required.

Note

Example

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

1@InvocableVariable(label='yourLabel'
2 description='yourDescription' placeholderText='yourPlaceholderText'
3 required=(true | false))

The invocable variable annotation supports defaultValue in this example.

1@InvocableVariable(defaultValue='yourDefaultValue')
2        global Boolean createOpportunity;

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 any of these:
    • 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 these:
    • A primitive other than Object
    • 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
  • The invocable variable name in Apex must match the name in the flow. The name is case-sensitive.
  • 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.