Sub topic 7
Designing Outputs the Agent Can Explain
This chapter assumes you can read Apex or Flow, know what an Agentforce topic and action are, and have already written input descriptions for an action. Here you design the other half: what the action returns.
Why outputs matter
After an action runs, the agent reads its outputs and writes a reply to the customer. The agent sees each output's label, description, data type, and value. It doesn't see your code. If an output is vague or missing, the agent fills the gap with a guess.
Good outputs have three traits:
- They contain only what the agent needs to answer.
- They carry clear labels and descriptions.
- They say what happened, including when nothing was found.
Return only what the agent needs
It is tempting to return a whole record. Resist that. Every extra field is more text the agent must sift through, and it may mention a field the customer never asked about.
Ask what question the action answers. "Where is my order?" needs a status and a short message. It does not need the account owner, internal notes, or 40 other fields.
| Approach | Result |
|---|---|
| Return the whole Order record | The agent may quote irrelevant or sensitive fields |
| Return `status` and `message` | The agent answers directly and stays on topic |
Describe each output
Compare a weak output with a stronger one:
Weak output description:
Label: Result
Description: Output of the action
Stronger output description:
Label: Order Status
Description: Current status of the order, such as Draft or Activated. Empty if no order was found.The stronger version names the value, gives example values, and says what an empty value means. The agent can then tell "no status because no order exists" from "the status is blank."
Add a message output
A plain-language message output gives the agent a sentence it can rely on. It is especially useful for outcomes that a single field can't express, such as "no order matches this number."
The Apex class below returns both a status and a message. Its labels and descriptions tell the agent how to read each one.
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;
}
}Note three design choices:
- The method returns one
ResponseperRequest, in the same order, as invocable methods require. - A missing order is not an exception. It produces an empty
statusand an explanatorymessage. - Only two outputs are returned, and both are used in the reply.
The flow of an action result
The diagram shows how an output reaches the customer.
Common mistakes
- Returning too much. Extra fields invite the agent to mention things you didn't intend.
- Generic labels. "Result" or "Output" tell the agent nothing about meaning.
- Silent empty results. An empty list or null with no message leaves the agent to guess, and it may claim success.
- Mismatched descriptions. If the description says "Activated" but the code returns "Active", the agent may misreport the value.
Exercise
An action returns a full Order record with 40 fields and no message. A customer asks where their order is, and the agent mentions an internal note. What two changes to the outputs would you make?
Show answer
Return only the fields needed, such as Order Status, and add a Result Message output. Removing the extra fields stops the agent from quoting internal data. The message gives it a clear sentence to use, including when no order is found.
Exercise
Why does the sample code set only message when no order matches, and leave status empty?
Show answer
An empty status, described as "Empty if no order was found", tells the agent that no value exists. The message explains why, so the agent can tell the customer that no order matched instead of guessing.