Sub topic 11
Returning errors and empty results clearly
This chapter assumes you can write an Apex invocable method or a Flow, and that you know what an Agentforce topic and action are. It also assumes you have read the earlier chapters on output design. The agent only knows what your action returns. If a lookup finds nothing or fails, the agent needs a clear signal to explain it. Otherwise it may invent an answer or stay silent.
Why silence and exceptions fail
An action can end in three ways: it finds data, it finds nothing, or it fails. The agent handles the first well. The other two are where definitions tend to break.
- Empty output. If an action returns a blank result with no text, the agent can't tell "no match" from "the action didn't run." It may retry, guess, or make up a status.
- Unhandled exception. The agent receives a generic failure. It can't tell the user what went wrong or what to try next.
- Technical error text. A raw message like
List has no rows for assignment to SObjectreaches the agent, and probably the user. It is correct but not useful.
The fix is to treat "not found" and "failed" as normal outcomes and return them as data.
Return a message the agent can repeat
Add a plain-language message output to every action. Fill it in for every outcome, not only for errors. Describe it in the output description so the agent knows it can quote or paraphrase it.
The GetOrderStatus method from earlier shows the pattern:
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 + '.';
}Three details matter here:
- The status output stays empty when nothing is found, and its description says so ("Empty if no order was found").
- The message names the value that was searched for, so the agent can confirm it with the user.
- The message output description says it covers the no-match case.
Separate empty results from errors
"Nothing matched" and "something broke" call for different agent behavior. For an empty result, the agent should ask the user to check the input. For an error, it should say it can't complete the request right now.
A simple approach is to add an outcome field with a small set of values. List the values in the description.
| Outcome | Meaning | What the agent should do |
|---|---|---|
| `FOUND` | A record matched | Report the data |
| `NOT_FOUND` | The query ran, no match | Ask the user to confirm the input |
| `ERROR` | The action could not complete | Apologize and offer another route |
public class SafeResponse {
@InvocableVariable(
label='Outcome'
description='One of FOUND, NOT_FOUND, or ERROR. NOT_FOUND means the lookup worked but nothing matched. ERROR means the lookup could not run.'
)
public String outcome;
@InvocableVariable(
label='Result Message'
description='A short plain-language message the agent can share with the customer.'
)
public String message;
}Catch failures and keep details out of the message
Wrap the risky work in try/catch. Return a friendly message to the agent. Log the technical detail for administrators instead of passing it on.
try {
// query or DML here
res.outcome = 'FOUND';
} catch (Exception e) {
System.debug(LoggingLevel.ERROR, e.getMessage());
res.outcome = 'ERROR';
res.message = 'The order system could not be reached. Please try again later.';
}Don't put stack traces, record IDs, or internal field names in the message. The agent may repeat them to the user.
In Flow, use a fault path on the elements that can fail. Assign values to the output variables (for example, outcome and message) on that path, then end the flow normally.
Check the behavior
Test each outcome in the agent's test conversation: a valid input, an input that matches nothing, and a forced failure. Check that the agent's reply matches the message you returned and that it doesn't guess.
Exercise
A lookup action returns an empty status and an empty message when no record matches. The agent tells the user "Your order is being processed." What went wrong, and how do you fix it?
Show answer
With no signal, the agent filled the gap with a guess. Return a message such as "No order was found with number X" for the no-match case. Add to the output descriptions that the status is empty when nothing matches, and consider an outcome field with NOT_FOUND.