Sub topic 2
Writing an @InvocableMethod
This chapter assumes you can write basic Apex classes, use collections, and run SOQL queries. It also assumes you know what an Agentforce agent and its topics are. The goal here is the method itself: the annotation, its rules, and the shape of a correct implementation.
What the annotation does
@InvocableMethod marks an Apex method so that declarative tools can call it. Flow, Process Builder, and Agentforce all use it. When you register the class as an agent action, the agent calls this method with inputs it collected from the conversation. It then reads the returned values.
The annotation takes four common properties:
| Property | Purpose |
|---|---|
| `label` | The display name shown in tools such as Agent Builder. |
| `description` | Plain-language text explaining what the method does. The agent reads it. |
| `category` | A grouping name used to organize actions in the picker. |
| `callout` | Set to `true` if the method makes callouts. |
The rules the platform enforces
The compiler and the platform reject classes that break these rules:
- Each class can have only one
@InvocableMethod. - The method must be
staticandpublicorglobal. - It takes at most one parameter, and that parameter must be a
List. - It returns either
voidor aList. Use a list of your own response type to send data back. - The list elements must be primitives, sObjects, or Apex classes that use
@InvocableVariableon their fields. - The method cannot be declared in a generic class or use a generic type as the list element.
The next chapter covers the request and response classes in detail. For now, it is enough to know that you wrap inputs and outputs in small inner classes.
Anatomy of a method
This skeleton shows the required shape. It compiles, but it has no logic yet.
public with sharing class GetOpenCasesAction {
public class Request {
@InvocableVariable(label='Account Id' required=true)
public Id accountId;
}
public class Response {
@InvocableVariable(label='Open Case Count')
public Integer caseCount;
}
@InvocableMethod(
label='Get Open Cases'
description='Lists the open support cases for an account, newest first'
category='Service'
)
public static List<Response> getOpenCases(List<Request> requests) {
List<Response> responses = new List<Response>();
for (Request r : requests) {
Response res = new Response();
res.caseCount = 0;
responses.add(res);
}
return responses;
}
}The method receives a list even when the agent is handling one conversation turn. The platform can batch calls, for example when a Flow runs for many records. Write the method as if it will always receive several requests.
The one-to-one return contract
The platform matches each response to a request by position. Response 0 belongs to request 0, response 1 to request 1, and so on. Two mistakes break this:
- Returning fewer responses than requests, for instance by skipping a request that has a null
accountId. - Returning responses in a different order, for instance by iterating over a map.
Always loop over the original requests list and add exactly one response for each element. If a request cannot be processed, return a response that says so rather than leaving a gap.
The diagram below shows the flow of one call.
Common errors
- Two invocable methods in one class. The save fails. Put each action in its own class.
- Non-list parameter. A signature like
getOpenCases(Request r)is rejected. Wrap the type inList<>. - Missing
static. Instance methods cannot be invocable. - Mismatched list sizes. The save succeeds, but callers see wrong or missing results at run time.
- Querying inside the loop. It works with one request and hits the 100 SOQL query limit with a batch. Bulk handling is covered in a later chapter.
Exercise
A teammate's invocable method receives a list of 3 requests. One request has a null accountId, so the code skips it and returns a list of 2 responses. What goes wrong, and how should the code change?
Show answer
The platform matches responses to requests by position, so the second and third responses are attributed to the wrong requests, and the count no longer matches. The code should loop over every request and add exactly one response for each, using a response with a message such as "No account was provided" for the request with the null accountId.