Sub topic 12
Building an Invocable Apex Method for an Action
This chapter assumes you can read basic Apex, know what a topic and an action are, and have seen how action inputs and outputs are described. You will build one Apex class that an agent can call from a topic.
Why Apex is an action type
Flow covers many tasks. Apex fits when you need logic that is awkward in Flow, such as complex queries or calls to existing Apex services. Agentforce Builder can only use Apex that is marked as invocable. Two annotations make that work:
@InvocableMethodmarks the one method the agent can call.@InvocableVariablemarks the fields of the request and response classes that become action inputs and outputs.
The label and description values are not decoration. The agent reads them to decide what to put in each input and what each output means.
The example class
This class powers the "Get Order Status" action used in the Order Status topic.
public with sharing class GetOrderStatus {
public class Request {
@InvocableVariable(label='Order Number' description='The order number the customer provided' required=true)
public String orderNumber;
}
public class Response {
@InvocableVariable(label='Status' description='Current status of the order, or Not found')
public String status;
}
@InvocableMethod(label='Get Order Status' description='Looks up the status of an order by its order number')
public static List<Response> getStatus(List<Request> requests) {
List<Response> results = new List<Response>();
for (Request r : requests) {
Response res = new Response();
List<Order> orders = [
SELECT Status FROM Order
WHERE OrderNumber = :r.orderNumber
WITH USER_MODE
LIMIT 1
];
res.status = orders.isEmpty() ? 'Not found' : orders[0].Status;
results.add(res);
}
return results;
}
}What each part does
- Request and Response classes. An invocable method takes a list of one wrapper type and returns a list. The wrapper classes carry the named inputs and outputs. Each
@InvocableVariablebecomes one field in Builder. required=true. The agent must supply the order number before it can run the action. This pairs with the instruction "ask for the order number if they have not given one."with sharingandWITH USER_MODE. Sharing rules apply, and the query also enforces object and field permissions for the running user. This supports the instruction never to share other customers' orders.- The
'Not found'value. The method returns a predictable string instead of throwing an error. The topic instruction "If the action returns 'Not found'..." depends on this exact text.
Adding it to a topic
- Deploy the class to your org.
- Open the topic in Agentforce Builder and add an action.
- Choose the Apex action type and select Get Order Status.
- Check that the input and output labels read clearly.
- Reference the action by name in the topic instructions.
Common errors
- The class does not appear in Builder. Check that the method is
public static, annotated with@InvocableMethod, and deployed. A class can have only one invocable method. - The agent fills inputs wrongly. Vague descriptions are usually the cause. "The order number the customer provided" works better than "Order".
- Unexpected empty results. User mode hides records the running user cannot see. An order that exists may return
Not found. Test with the same user profile the agent runs as. - Limit errors. The example runs one query per request, which is fine for a single conversation turn. If the method may receive many requests in one call, collect the order numbers first and query once with
IN :orderNumbers. Salesforce limits the number of queries per transaction.
Exercise
The agent keeps asking customers for "the order" instead of an order number. Which part of the Apex class would you change first, and why?
Show answer
Change the label and description on the orderNumber variable. The agent reads these to decide what input to collect, so a specific description such as "The order number the customer provided" makes the request clearer.