Sub topic 9
Implementing an Action with Apex
This chapter shows how to turn an Apex class into an action the agent can call. It assumes you know basic Apex and have seen what an Agentforce topic and action are. The agent reads the labels and descriptions you write, so the metadata matters as much as the logic.
How Apex becomes an action
An agent action can target an invocable method. An invocable method is a static Apex method marked with @InvocableMethod. Agentforce Builder reads the method, its request class, and its response class. Each @InvocableVariable becomes an input or an output of the action.
Three rules shape the design:
- The method takes a
Listof request objects and returns aListof response objects. - Each class has public fields marked with
@InvocableVariable. - Only one
@InvocableMethodis allowed per class.
The label and description values on the annotations are what the agent reads. Write them for the agent, not for a developer.
A complete example
This class looks up an order by number. Read the descriptions as instructions to the agent.
public with sharing class GetOrderStatus {
public class Request {
@InvocableVariable(
label='Order Number'
description='The order number the customer provided, for example 00000101. Ask the customer if it was not given. Do not guess.'
required=true
)
public String orderNumber;
}
public class Response {
@InvocableVariable(
label='Order Status'
description='Current status of the order, such as Draft or Activated. Empty if no order was found.'
)
public String status;
@InvocableVariable(
label='Result Message'
description='A short plain-language message describing the outcome, including when no order matches the number.'
)
public String message;
}
@InvocableMethod(
label='Get Order Status'
description='Looks up the status of one order by its order number. Use when the customer asks where their order stands.'
)
public static List<Response> getStatus(List<Request> requests) {
Set<String> numbers = new Set<String>();
for (Request r : requests) {
numbers.add(r.orderNumber);
}
Map<String, Order> byNumber = new Map<String, Order>();
for (Order o : [SELECT OrderNumber, Status FROM Order WHERE OrderNumber IN :numbers WITH USER_MODE]) {
byNumber.put(o.OrderNumber, o);
}
List<Response> results = new List<Response>();
for (Request r : requests) {
Response res = new Response();
Order o = byNumber.get(r.orderNumber);
if (o == null) {
res.message = 'No order was found with number ' + r.orderNumber + '.';
} else {
res.status = o.Status;
res.message = 'Order ' + r.orderNumber + ' has status ' + o.Status + '.';
}
results.add(res);
}
return results;
}
}What each part does
- Method description. It says what the action does and when to use it. The agent uses this text to choose between actions.
- Input description. It gives an example value and says what to do when the value is missing.
required=truemarks the input as one the agent must have before it calls the action. - Output descriptions. They tell the agent what the values mean, including that
statusis empty when nothing matches. messagefield. It carries the outcome in plain language, so the agent has something to say in the not-found case.WITH USER_MODE. The query runs with the permissions of the user, so the agent cannot read records the user cannot see.
Bulk safety
Apex may pass many requests in one call, so the method collects all order numbers first and runs one query. Querying inside the for loop risks hitting governor limits. The method also returns one response per request, in the same order. Keep that pairing, or results will be matched to the wrong requests.
Common errors
- Vague descriptions. A label such as
Idwith the descriptionThe idgives the agent nothing to match against the conversation. Say which identifier, give an example, and say whether to ask the user. - Throwing an exception for a missing record. The agent gets a generic failure. Return a message instead, as the example does.
- Unsupported field types. Keep fields to simple types, such as strings, numbers, booleans, dates, and records or lists of them. Complex custom structures are harder for the agent to fill.
- Returning too much. Return only the fields the agent needs to answer. Extra fields add noise.
- Changing a signature without refreshing. After you edit the class, re-add or refresh the action in Agentforce Builder so it picks up the new metadata.
Exercise
In the GetOrderStatus example, why does the method return a message for the not-found case instead of throwing an exception?
Show answer
A thrown exception reaches the agent as a generic failure with no useful detail. A returned message states the outcome in plain language, so the agent can tell the customer that no order matches the number and can ask them to check it.
Exercise
The method runs a single SOQL query before its loop over the requests. What problem does this avoid, and what must you preserve in the returned list?
Show answer
It avoids running a query per request, which can exceed governor limits when many requests arrive together. The method must return one response for each request, in the same order as the requests.