Sub topic 13
Designing Focused, Safe Actions
This chapter assumes you can write Apex classes, collections, and SOQL. It also assumes you have built an @InvocableMethod with @InvocableVariable wrapper types and registered it in Agent Builder. Here you will apply design rules to decide what an action should do and what it should never do.
Why design matters more for agents
A Flow or a button is triggered by a person who sees what will happen. An agent action is chosen by a reasoning engine from the action's label and description, and it fills in the inputs from a conversation. The action can run at any time the model decides it fits. That makes two properties important: each action must have a clear, narrow purpose, and each action must be safe when called with unexpected input.
Keep each action focused
An action should do one job that you can describe in a single sentence. GetOpenCasesAction from the earlier chapters is a good model. It reads open cases for an account and returns a summary. It does not close cases, send emails, or update the account.
Splitting by job helps in three ways:
- The agent picks the right action more reliably, because each description matches one intent.
- You can attach each action only to the topics that need it.
- You can test and secure each action on its own.
Avoid a single action with a mode or operation input, such as "get", "close", or "escalate". The agent has to guess valid values, and a wrong guess runs the wrong branch. Write separate actions instead.
| Design choice | Weak version | Focused version |
|---|---|---|
| Scope | `ManageCaseAction` with a `mode` input | `GetOpenCasesAction`, `CloseCaseAction` |
| Inputs | Free text like "the record" | A typed `Id` named `accountId` |
| Output | Raw `List<Case>` | A short `summary` and a `caseCount` |
| Side effects | Reads and writes in one call | Read actions kept apart from write actions |
Separate reads from writes
A read-only action is low risk. If the agent calls it by mistake, the user sees an odd answer. A write action changes data. If the agent calls it by mistake, the data is wrong.
Treat write actions with more care:
- Name the action and write its description so the effect is plain, for example "Closes a single case. Changes the case status."
- Require every input that identifies the target, and mark it
required=true. - Validate the input in Apex. Never rely on the agent to supply correct values.
- Make the topic instructions ask the user to confirm before the agent calls the action.
- Keep the change small and reversible when you can.
Validate inputs as untrusted
The agent builds inputs from user text, and users can type anything. Treat every field in the request as untrusted, the same as input from a public web form.
- Check for
nulland for out-of-range values, as the sample does withmaxCases. - Use typed fields such as
Id,Integer, andDateinstead ofStringwhere you can. The platform rejects values that do not parse. - Never build a dynamic SOQL string from an input. Use bind variables (
:accountIds) as in the example. - Cap results with
LIMITand a maximum you choose, so a large value cannot return unbounded data.
Let the platform enforce access
Run the action in the user's context. The example uses with sharing and WITH USER_MODE, so the agent cannot show a user records or fields that user could not open in Salesforce. Do not use without sharing or system mode to "make the action work". If a user lacks access, an empty result is the correct outcome, and your empty-result message explains it.
public with sharing class CloseCaseAction {
public class Request {
@InvocableVariable(
label='Case Id'
description='The Salesforce Id of the single case to close'
required=true
)
public Id caseId;
}
public class Response {
@InvocableVariable(
label='Result'
description='A message saying whether the case was closed'
)
public String message;
}
@InvocableMethod(
label='Close Case'
description='Closes one support case. This changes the case status to Closed'
category='Service'
)
public static List<Response> closeCases(List<Request> requests) {
Set<Id> ids = new Set<Id>();
for (Request r : requests) {
if (r.caseId != null) {
ids.add(r.caseId);
}
}
Map<Id, Case> casesById = new Map<Id, Case>([
SELECT Id, Status
FROM Case
WHERE Id IN :ids
WITH USER_MODE
]);
List<Case> toUpdate = new List<Case>();
for (Case c : casesById.values()) {
c.Status = 'Closed';
toUpdate.add(c);
}
update as user toUpdate;
List<Response> responses = new List<Response>();
for (Request r : requests) {
Response res = new Response();
res.message = casesById.containsKey(r.caseId)
? 'The case was closed.'
: 'No case was closed. It was not found or you cannot access it.';
responses.add(res);
}
return responses;
}
}This write action takes one typed Id, reads and updates in bulk, enforces user permissions on both the query and the update, and returns one response per request in order.
Return only what the agent needs
Return a short, readable result. Large or sensitive outputs waste the model's context and may expose data you did not intend to show. Return a summary and a count, not whole records with every field.
Exercise
A teammate proposes one action named UpdateAccount with a mode input that accepts "rename", "change owner", or "delete". Give two reasons to split it, and say how you would handle the delete case.
Show answer
The agent must guess valid mode values, so a wrong guess can run the wrong branch, and one vague description matches several intents, which makes action selection less reliable. Split it into RenameAccountAction and ChangeAccountOwnerAction, each with typed inputs and a precise description. Delete is the riskiest, so either leave it out of the agent entirely or make it its own write action that requires the account Id, uses user-mode security, and sits in a topic whose instructions require the user to confirm first.