Newer Version Available
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}InvocableVariable Modifiers
The invocable variable annotation has three available modifiers, as 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.
- description
- The description for the variable. The default is Null.
- required
- 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 an Apex class or a list of Apex classes.
- 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’s Guide.