Sub topic 3
Implementing an Agentforce Action with Apex
Assume you know Salesforce Setup, Agentforce topics and actions, and basic Apex or Flow. An Agentforce action is an operation the agent can choose, such as looking up an order. The action’s metadata helps the agent select it and populate its inputs.
Implementing the action with Apex
An Apex action uses an @InvocableMethod. Its request and response fields use @InvocableVariable. Labels and descriptions are part of the agent’s usable interface, so write them as instructions, not as developer shorthand.
public with sharing class GetOrderStatus {
public class Request {
@InvocableVariable(
label='Order Number'
description='Order number provided by the customer, for example 00000101. Ask for it if missing. Do not guess.'
required=true
)
public String orderNumber;
}
public class Response {
@InvocableVariable(
label='Status'
description='Current order status. Empty when no order matches.'
)
public String status;
@InvocableVariable(
label='Message'
description='Plain-language result message for the customer.'
)
public String message;
}
@InvocableMethod(
label='Get Order Status'
description='Looks up one order by order number when a customer asks about its status.'
)
public static List<Response> getStatus(List<Request> requests) {
Set<String> numbers = new Set<String>();
for (Request request : requests) numbers.add(request.orderNumber);
Map<String, Order> orders = new Map<String, Order>();
for (Order order : [
SELECT OrderNumber, Status
FROM Order
WHERE OrderNumber IN :numbers
WITH USER_MODE
]) {
orders.put(order.OrderNumber, order);
}
List<Response> results = new List<Response>();
for (Request request : requests) {
Response result = new Response();
Order order = orders.get(request.orderNumber);
if (order == null) {
result.message = 'No order was found with number ' + request.orderNumber + '.';
} else {
result.status = order.Status;
result.message = 'Order ' + request.orderNumber +
' has status ' + order.Status + '.';
}
results.add(result);
}
return results;
}
}Implementing the action with Flow
A Flow action exposes input and output variables. Mark an input as required when the action cannot work without it. Add descriptions that explain its source, format, and allowed values. Use a text output for a customer-facing message and separate outputs for values the topic may need to reference.
Returning errors and empty results clearly
Return an expected “not found” result as structured output. Do not make the agent infer that an empty status means failure. For unexpected failures, configure the Flow fault path or handle the Apex exception and return a short, safe message. Do not expose stack traces or internal identifiers.
Aligning actions with topic instructions
The action description and topic instructions should agree. The topic can say when to use the action and how to explain its result. The action metadata should define the exact input and output behavior. Avoid putting required business rules in only one location.
Testing and refining definitions
Test with complete, missing, invalid, and unmatched inputs. Review the agent transcript:
- Did it choose the action?
- Did it ask for a missing value?
- Did it preserve the input format?
- Did it explain an empty result accurately?
- Did it show only intended outputs?
Exercise
Why should a “not found” response include a message instead of only an empty status?
Show answer
The message gives the agent explicit information it can explain to the user without guessing what the empty status means.