Sub topic 4
Exposing fields with @InvocableVariable
This chapter assumes you can write Apex classes with inner classes and that you have used Flow Builder variables. It also assumes you know how @InvocableMethod and its signature rules work.
What the annotation does
@InvocableVariable marks a field of a wrapper class as visible to Flow. Without it, Flow ignores the field. Flow Builder reads the annotated fields of the method's input class and shows each one as an input on the Apex action. It reads the annotated fields of the return class and offers each one as an output you can use later in the flow.
You use it on fields of the classes that your invocable method accepts and returns. The next chapter covers how to design those classes. Here the focus is on the annotation itself.
Attributes
| Attribute | Purpose | Notes |
|---|---|---|
| `label` | Name shown in Flow Builder | Defaults to the field name if omitted |
| `description` | Help text for the flow author | Shown in the action configuration |
| `required` | Forces the author to supply a value | Defaults to `false`. Applies to inputs |
| `defaultValue` | Value used when the author supplies none | Written as a string, even for numbers or Booleans |
| `placeholderText` | Hint text in an empty input | Only affects the display |
Use label and description on every field. Flow authors often work without seeing your code, and these two attributes are all the documentation they get.
Example
public with sharing class DiscountCalculator {
public class Request {
@InvocableVariable(label='Opportunity Id' description='Opportunity to price' required=true)
public Id opportunityId;
@InvocableVariable(label='Discount Percent' description='Percent from 0 to 100' defaultValue='10')
public Decimal discountPercent;
@InvocableVariable(label='Round Result' defaultValue='true')
public Boolean roundResult;
// No annotation: Flow cannot see or set this field.
public String internalNote;
}
public class Result {
@InvocableVariable(label='Discounted Amount')
public Decimal discountedAmount;
}
@InvocableMethod(label='Calculate Discount')
public static List<Result> calculate(List<Request> requests) {
Set<Id> oppIds = new Set<Id>();
for (Request req : requests) {
oppIds.add(req.opportunityId);
}
Map<Id, Opportunity> opps = new Map<Id, Opportunity>(
[SELECT Id, Amount FROM Opportunity WHERE Id IN :oppIds]
);
List<Result> results = new List<Result>();
for (Request req : requests) {
Result res = new Result();
Decimal amount = opps.containsKey(req.opportunityId)
? opps.get(req.opportunityId).Amount
: null;
if (amount != null) {
res.discountedAmount = amount * (100 - req.discountPercent) / 100;
if (req.roundResult) {
res.discountedAmount = res.discountedAmount.setScale(0);
}
}
results.add(res);
}
return results;
}
}In Flow Builder, this action shows three inputs: Opportunity Id, Discount Percent, and Round Result. Opportunity Id is marked required. internalNote does not appear.
Rules for annotated fields
- The field must be an instance variable. Static fields are not exposed.
- The field must be
publicorglobal. The class that contains it must also be accessible to Flow. - Supported types include primitives,
Id,Date,Datetime, sObjects, andListcollections of those types. A field can also use another Apex class whose own fields are annotated. SetandMapfields are not supported.- The class needs a public no-argument constructor. The default one is enough, so avoid adding a constructor that takes parameters and leaves no default.
Common errors
- Missing annotation. The field compiles but never appears in Flow Builder. When an input is missing from the action, check this first.
requiredon an output field. The attribute only controls what an author must supply. It does nothing for values you return.- A
defaultValuethat does not match the type. A value likedefaultValue='ten'on aDecimalfield causes a problem when the flow runs. Keep it parseable as the field's type. - Relying on defaults for null checks. An unset optional input with no
defaultValuearrives asnull. Guard against it in code, as withString.isBlankin earlier examples. - Renaming fields after use. Flows store the field's API name. Changing it can break existing flows that reference the action. Change
labelfreely, but treat field names as a contract.
Exercise
A teammate adds public Integer retryCount; to a Request class and says it does not show up on the Apex action in Flow Builder. The class and method are public. What is the most likely cause, and how do you fix it?
Show answer
The field has no @InvocableVariable annotation, so Flow ignores it. Add @InvocableVariable(label='Retry Count' description='Number of retries') above the field. Use defaultValue if a missing value should have a fallback.