Sub topic 9
Choosing and limiting actions
This chapter assumes you can administer Salesforce users and permission sets, and that you know what an Agentforce topic and an action are. You should also be able to read basic Flow or Apex. Earlier chapters covered the agent user, permission sets, and topics. Here you decide which actions an agent gets and how to keep each one narrow.
Why actions are the main risk
Instructions guide what an agent says. Actions are what it does. An action reads or changes data, so each one extends the agent's reach. The agent user's permissions set the upper limit, but the actions you attach decide how much of that limit the agent can actually use.
An action can be a Flow, an invocable Apex method, a prompt template, or a standard action. Whatever the type, the same rules apply: give the agent the fewest actions that do the job, and make each action do one thing.
Choosing actions
Start from the tasks the topic covers, not from the actions that are available. For each task, ask whether the agent needs to read, create, update, or delete data.
- Prefer read actions. A status lookup carries far less risk than an update.
- Attach an action only to the topics that need it. An action assigned to a topic is available only when that topic is active.
- Avoid general-purpose actions, such as "run any query" or "update any record." They are hard to constrain.
- Build narrow actions with explicit inputs. An action that takes an order number and returns a status can't be misused to browse other data.
The table compares a broad design with a narrow one.
| Design | Inputs | Output | Risk |
|---|---|---|---|
| Broad: "Update Order" | Any field and value | Updated record | Agent can change fields you never intended |
| Narrow: "Get Order Status" | Order number | Status text | Agent can only read one field |
Limiting what an action returns
Return only what the agent needs to answer. If the action returns a full record, the agent may repeat fields the customer should not see. Define a small result class with just the fields required.
The Apex action below follows this pattern. It takes one input, returns one value, and runs in user mode so object permissions, field-level security, and sharing apply.
public with sharing class OrderStatusAction {
public class Request {
@InvocableVariable(required=true label='Order Number')
public String orderNumber;
}
public class Result {
@InvocableVariable(label='Order Status')
public String status;
}
@InvocableMethod(label='Get Order Status' description='Returns the status of an order the running user can access')
public static List<Result> getStatus(List<Request> requests) {
Set<String> numbers = new Set<String>();
for (Request r : requests) {
numbers.add(r.orderNumber);
}
Map<String, String> statusByNumber = new Map<String, String>();
for (Order o : [
SELECT OrderNumber, Status
FROM Order
WHERE OrderNumber IN :numbers
WITH USER_MODE
]) {
statusByNumber.put(o.OrderNumber, o.Status);
}
List<Result> results = new List<Result>();
for (Request r : requests) {
Result res = new Result();
res.status = statusByNumber.get(r.orderNumber);
results.add(res);
}
return results;
}
}If the agent user cannot see an order, the query returns nothing and status stays null. The agent then tells the customer it can't find the order, as the topic instructions say.
The action's description matters too. The agent reads it to decide when to call the action, so state plainly what the action does and what it does not do.
How the layers fit together
Common mistakes
- Using
without sharingor system mode in an action, which bypasses the agent user's limits. - Giving the agent user broader permissions because one action needs them. Fix the action instead.
- Attaching every available action to every topic.
- Writing vague action descriptions, so the agent calls the wrong action.
Exercise
A topic about order status has an action that accepts an order ID and returns the whole Order record. Name two changes that would limit it.
Show answer
Return only the fields the agent needs, such as the status, by using a small result class. Query in user mode (WITH USER_MODE) so field-level security and sharing apply. Removing any update capability from the action also helps.