Sub topic 9
Choosing and limiting actions
An action is the part of an agent that touches data or systems. Topics and instructions shape what the agent says. Actions decide what the agent can do. This chapter covers how to pick actions for a topic, how to keep their inputs and outputs narrow, and how to check that each one respects the permissions of the user the agent runs as.
Prerequisites
This chapter assumes you already know the following:
- Basic Salesforce administration: users, profiles, and permission sets.
- How object permissions, field-level security, and record sharing combine to decide what a user can see.
- What an Agentforce agent is, and what topics and actions are.
- Enough Apex or Flow to read a short invocable action.
Earlier chapters covered the agent user, permission sets, and topics. Here we assume the agent already has a least-privilege permission set and a scoped topic, and we look at the actions inside that topic.
Why actions are the main risk
An agent can only affect Salesforce through its actions. A reply that is wrong or off-topic is a quality problem. An action that updates the wrong record, or returns a field the customer should never see, is a security problem.
Three layers limit an action:
- Assignment. The action must be added to a topic before the agent can choose it.
- Design. The inputs, outputs, and logic of the action decide what it can do even when it runs.
- Execution context. The user the action runs as decides which data it can read or change.
If any one layer is loose, the other two have to compensate. Good designs keep all three tight.
Choosing which actions a topic gets
Give each topic only the actions it needs to do its job. An action that is not in the topic cannot be chosen by the agent for that topic, so every extra action widens what a manipulated or confused agent can do.
Use this checklist when you decide whether to add an action:
- Does the topic's stated job require this action?
- Is there a read-only version that would be enough?
- Could the same result come from a narrower action, such as one that returns one field instead of a whole record?
- Does the action change data? If so, does the topic's instructions say when it may be used?
A topic that answers order-status questions needs one read action. It does not need a general "update order" action, even if one exists in the org and would be convenient later.
Standard, Flow, and Apex actions
Actions can be built on several kinds of logic. They differ in how much control you have over security.
| Action source | Strength | What to watch |
|---|---|---|
| Standard action | Maintained by Salesforce, quick to add | Check what it can read or write and confirm it fits the topic |
| Flow | Easy to read and change, no code | Confirm the flow's run mode and avoid broad Get Records or Update Records elements |
| Apex invocable method | Full control over queries and logic | You must enforce permissions yourself, because Apex does not do it by default |
| Prompt template | Good for generating text from grounded data | The data merged in is still limited by the running user and the template's inputs |
Prefer the simplest source that meets the need. A standard action or a short flow is easier to review than custom Apex. Use Apex when you need logic the others cannot express.
Designing narrow inputs and outputs
An agent fills in action inputs from the conversation, and it reads the outputs to form its reply. Both sides are places to apply limits.
Inputs
- Ask for the smallest identifier that works, such as an order number rather than free text.
- Mark required inputs as required so the agent asks the customer instead of guessing.
- Avoid inputs that accept a record ID or a query fragment. The agent could pass a value the customer never supplied.
- Avoid inputs that choose which field or object to act on. That moves a design decision to run time.
Outputs
- Return only the fields the agent needs for its reply. A status string is safer than an order record.
- Do not return fields only because they are available. Anything an action returns can end up in the agent's answer.
- Return an empty result when the user cannot see the record. Do not return an error message that confirms the record exists.
Read actions and write actions
Treat read actions and write actions differently.
- Read actions can leak data. Limit them with field selection, sharing, and field-level security.
- Write actions can change data. Limit them with narrow inputs, validation, and a confirmation step where the change matters.
If you are unsure whether the agent should write at all, start with read-only actions and add write actions later after testing. Removing an action after a problem is harder than adding one when it is needed.
Enforcing permissions inside the action
An action runs as a user. For an agent that runs as a dedicated agent user, that is the agent user. For an agent that runs as the logged-in user, that is the person talking to it. Either way, the action only respects that user's permissions if the logic enforces them.
Flows and Apex differ here:
- A flow can run in user context or in system context. System context ignores the user's object and field permissions and, depending on the setting, sharing. Use user context for agent actions unless you have a specific, reviewed reason not to.
- Apex runs in system mode by default. It ignores object permissions and field-level security unless you ask for enforcement. Declaring
with sharingapplies sharing rules, but it does not apply field-level security or object permissions.
The Apex example below puts both controls in place. It uses with sharing for record sharing and WITH USER_MODE for object permissions and field-level security.
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;
}
}Several design choices in this class are deliberate:
- One required input. The only input is the order number, and it is required, so the agent has to ask for it.
- One output. The result holds a status string. No other order fields reach the agent.
- Bulk-safe. The method takes a list of requests and runs one query, which is how invocable methods are expected to behave.
- Empty result on no access. If the user cannot see the order, the map has no entry, and
statusstays null. The action does not say whether the order exists. - User mode query. If the running user lacks read access to
Orderor toStatus, the query fails with an exception instead of returning data. Plan for this case. A missing permission shows up as a failed action, so test it.
The permission set from the earlier chapter pairs with this class. It gives read access to Order and Order.Status and nothing else:
<?xml version="1.0" encoding="UTF-8"?>
<PermissionSet xmlns="http://soap.sforce.com/2006/04/metadata">
<label>Agent Order Read</label>
<description>Read-only access to orders for the service agent user</description>
<hasActivationRequired>false</hasActivationRequired>
<objectPermissions>
<allowCreate>false</allowCreate>
<allowDelete>false</allowDelete>
<allowEdit>false</allowEdit>
<allowRead>true</allowRead>
<modifyAllRecords>false</modifyAllRecords>
<object>Order</object>
<viewAllRecords>false</viewAllRecords>
</objectPermissions>
<fieldPermissions>
<editable>false</editable>
<field>Order.Status</field>
<readable>true</readable>
</fieldPermissions>
</PermissionSet>The action and the permission set check each other. If someone later adds a field to the query, WITH USER_MODE blocks it until the permission set also grants that field. That is the behavior you want, because it forces a visible decision.
Tying the action to the right customer
The sample action looks up an order by number. If the agent user can see many orders, any customer who guesses or learns another order number could ask for it. Sharing alone does not prevent this, because the agent user is allowed to read those records.
Options to close the gap, from simplest to strongest:
- Instructions. Tell the agent to answer only for the customer's own orders. This helps but depends on the model following the rule.
- A second input. Require a value that only the real customer would know, such as the email on the order, and filter on both in the query.
- A verified identity. Pass the identity of the already verified customer from the conversation context into the action and filter by it, so the customer never types the matching value.
- Narrow sharing. Where the agent runs as the logged-in user, sharing rules already restrict records to that person. This is one reason to prefer that agent type for customer-specific data.
Instructions are a soft control. Query filters and sharing are hard controls. Use a hard control for anything where a mistake exposes another person's data.
Limiting what an action can change
For write actions, add limits the same way you do for reads:
- Restrict the fields the action sets. Do not accept a map of field names and values.
- Validate inputs in the action. Check the value against an allowed list before the update.
- Use
WITH USER_MODEorDatabase.update(records, AccessLevel.USER_MODE)so the update respects field-level security and object permissions. - Prefer actions that do one thing, such as "Add note to case", over general update actions.
- Where a change is hard to reverse, such as a cancellation or a refund, do not give it to the agent. Route the request to a human instead, as the sample topic instructions do.
The sample instructions for the order-status topic show how instructions and actions work together:
Only answer questions about the status of the customer's own orders.
Never share information about other customers.
Never offer refunds, price changes, or account changes. If the customer asks for one, tell them you are transferring them to a human agent.
If you cannot find the order, say so and ask the customer to confirm the order number.The instructions say what to refuse. The action set backs this up because no action exists that can issue a refund or change an account. A refusal enforced only by instructions can fail. A refusal enforced by the absence of an action cannot.
Common errors
| Mistake | Effect | Fix |
|---|---|---|
| Apex action without `with sharing` or `WITH USER_MODE` | Returns data the running user could not otherwise see | Add both and test with a user who lacks access |
| Flow action running in system context | Ignores field-level security | Run in user context |
| Output returns whole records | Extra fields appear in replies | Return only the fields needed |
| One broad action shared by many topics | Every topic gains every capability | Split into narrow actions per topic |
| Confirming that a record exists when access is denied | Leaks the existence of data | Return an empty result |
| Relying on instructions to prevent a write | The model may still call the action | Remove the action from the topic |
Testing actions
Test each action by itself before you test it through the agent.
- Run the Apex or Flow as the agent user with a record the user can see, and confirm the expected output.
- Run it with a record the user cannot see, and confirm the result is empty or the failure is handled.
- Remove field access from the permission set in a sandbox, and confirm the action fails instead of returning data.
- Then test through the agent with prompts that ask for out-of-scope actions, and confirm the agent declines or escalates.
A later chapter covers testing and reviewing agent activity in more depth.
Exercise
An Apex invocable action for an agent is declared public with sharing class and queries Order without WITH USER_MODE. The agent user's permission set does not grant read access to the Status field. What happens when the query selects Status, and what should you change?
Show answer
The query still returns Status. with sharing applies record sharing only, and Apex in system mode ignores object permissions and field-level security. Add WITH USER_MODE to the query so the missing field permission causes a failure instead of a data leak. Then decide whether the agent user should have the field, and grant it in the permission set only if the topic needs it.
Exercise
A topic for order status has an action called "Update Order" in addition to "Get Order Status". The instructions say never to change orders. Why is this still a weak design, and what is the stronger fix?
Show answer
Instructions are a soft control, and the model can still call an action that is available. The stronger fix is to remove "Update Order" from the topic, so the agent has no way to change an order. If a human needs to make the change, use an escalation.