Sub topic 3
Modeling inputs and outputs with @InvocableVariable
This chapter assumes you can write basic Apex classes, use collections and SOQL, and know that an @InvocableMethod is the entry point for an Agentforce action. Here you will model the data that goes into and comes out of that method.
Why wrapper classes
An invocable method accepts one parameter: a List of a single type. It returns either void or a List of a single type. To pass several named inputs, you define an inner class and mark its fields with @InvocableVariable. Agent Builder reads those fields and shows them as the action's inputs and outputs.
The usual pattern is two inner classes, Request and Response, inside the action class.
Declaring variables
Each field you want the agent to see needs the annotation. Fields without it are ignored.
public class Request {
@InvocableVariable(
label='Account Id'
description='The Salesforce Id of the account whose open cases should be listed'
required=true
)
public Id accountId;
@InvocableVariable(
label='Maximum Cases'
description='How many cases to include. Defaults to 5 if not provided'
)
public Integer maxCases;
}The annotation takes these parameters:
| Parameter | Purpose |
|---|---|
| `label` | Display name in Agent Builder. |
| `description` | Text the agent uses to decide what value to supply. |
| `required` | When `true`, the input must have a value. Default is `false`. |
| `placeholderText` | Example text shown for the input in Flow Builder. |
Descriptions get their own chapter. For now, write one for every variable.
Rules for the types
The platform restricts what you can declare. These are the ones that cause most errors:
- The field must be
publicorglobal, and it cannot bestaticorfinal. - Supported types include primitives (
String,Integer,Boolean,Decimal,Date,Datetime),Id, sObjects, Apex-defined types, and lists of these. - A
Mapor aSetis not supported as a field type. - The wrapper class must be
publicorglobal, with a public or global outer class. - Each wrapper class needs a public or global no-argument constructor. The implicit default constructor is enough.
- You cannot reuse the same wrapper class for two different invocable methods in one org, so keep a
RequestandResponseper action.
Using a generic sObject type is allowed in Flow, but it gives the agent little to work with. Prefer specific types such as Id or String.
Outputs
The response class follows the same rules. Mark only the fields the agent should read.
public class Response {
@InvocableVariable(
label='Open Case Count'
description='Total number of open cases for the account'
)
public Integer caseCount;
@InvocableVariable(
label='Case Summary'
description='One line per case with case number and subject, or a message if there are none'
)
public String summary;
}Returning a ready-to-read summary string is a deliberate choice. The agent can quote it to the user directly. Returning a raw list of Case records forces the model to format them, which is less predictable.
The order contract
The method signature ties the two classes together:
@InvocableMethod(label='Get Open Cases' description='Lists the open support cases for an account, newest first')
public static List<Response> getOpenCases(List<Request> requests) {
// build one Response per Request, in the same order
return new List<Response>();
}The platform matches responses to requests by position. If you receive three requests, return three responses in the same order. Skipping one, for example when an accountId is null, shifts every later answer onto the wrong request. Add a placeholder response instead.
Common errors
- A field does not appear in Agent Builder. The annotation is missing, or the field is not public.
- Save fails on an unsupported type. You probably used a
MaporSet. Use aListor a wrapper class. - Save fails on a duplicate class. Two invocable methods share one wrapper. Give each its own.
- The agent never supplies a value. The description is missing or vague.
Exercise
A teammate's invocable method receives a list of five requests. One request has a null accountId, so the loop uses continue and adds no response for it. What goes wrong, and how do you fix it?
Show answer
Responses are matched to requests by position, so the four returned responses are shifted. The fifth request gets no response, and requests after the skipped one receive the wrong answers. Fix it by always adding exactly one Response per Request, for example with a message such as "No account was provided" and a caseCount of 0 for the null case.