Sub topic 8
Agent identity and access: agent user versus running user
This chapter assumes you know Salesforce permission sets, object and field permissions, and sharing basics. It also assumes you have read the earlier chapters on topics, instructions, and actions. The chapter covers which Salesforce identity an agent acts as, and how that identity limits the data the agent can read and change.
Why identity matters for agents
An agent does not read data directly. When the reasoning engine picks an action, the platform runs that action (a Flow, an Apex invocable method, a prompt template, or a query) as a specific Salesforce user. That user's permissions decide what the action can see and do. The identity choice is therefore a security decision, not a cosmetic setting.
Agentforce uses two models:
- Agent user. The agent runs as a dedicated Salesforce user that you create and assign. Service agents use this model.
- Running user. The agent runs as the person who is chatting with it. Employee agents use this model.
The agent user model (service agents)
A service agent talks to customers who usually have no Salesforce login. They may be website visitors, people on a messaging channel, or contacts in an Experience Cloud site. There is no Salesforce user to borrow, so the agent needs its own identity.
You create an agent user with the agent user license and assign it permission sets. You then select it as the agent's user when you configure the agent. Every action the agent runs executes as that user, no matter which customer is chatting.
Three consequences follow:
- The agent user's permissions are the ceiling. If the user cannot read the
Orderobject, no action can return order data, however good the instructions are. - Every customer gets the same access. The platform does not narrow the agent user's access to the current customer's records. Your instructions and actions must do that.
- Audit records show the agent user. Changes the agent makes appear as made by that user, so give it a clear name and use it for one agent only.
Granting least privilege
Build a dedicated permission set for each agent rather than cloning an administrator profile. Grant only what its actions need:
- Read access to the objects and fields the actions query.
- Create or edit access only where an action writes data.
- Access to the Apex classes and Flows the agent calls.
- Access to the knowledge articles or data it grounds on.
A small permission set also makes testing clearer, because a failure from a missing permission points to something you can fix.
The running user model (employee agents)
An employee agent is used by people who already have Salesforce accounts. It runs as whoever is logged in. When an employee asks "What are my open cases?", the action runs with that employee's permissions, sharing access, and field-level security.
This model has a useful property. Two employees can ask the same question and get different results, because each sees only what they could already see in Salesforce. You do not have to rebuild the access rules inside the agent.
Employees also need permission to use the agent itself. Grant that through a permission set, the same way you grant any other feature, so you can roll the agent out to one team first.
Comparing the two models
| Question | Agent user (service agent) | Running user (employee agent) |
|---|---|---|
| Who is the caller? | Usually an unauthenticated or external customer | An internal, logged-in employee |
| Which identity runs actions? | One dedicated agent user | The employee in the conversation |
| Who sets the data ceiling? | The agent user's permission sets | Each employee's own permissions |
| Do different callers see different data? | No, unless actions filter by caller | Yes, automatically |
| Main risk | Over-permissioned agent user exposes data to every customer | Instructions that assume more access than users have |
| Typical failure | Agent reveals another customer's record | Agent says "I can't find that" because the user lacks access |
Writing actions that respect the identity
Whichever identity runs the action, your code must honor that identity's access. In Apex this needs care, because the keywords do different jobs:
with sharingapplies record-level sharing rules for the running identity.- It does not enforce object permissions or field-level security.
For those you need user-mode database operations. The invocable method from the topic spine uses with sharing and a bulk-safe query. The version below adds WITH USER_MODE, so the query also fails if the identity lacks read access to the object or fields.
public with sharing class GetOrderStatusAction {
public class Request {
@InvocableVariable(label='Order Number' required=true)
public String orderNumber;
}
public class Response {
@InvocableVariable(label='Order Status')
public String status;
}
@InvocableMethod(
label='Get Order Status'
description='Returns the status of an order for a given order number'
)
public static List<Response> getStatus(List<Request> requests) {
Set<String> orderNumbers = new Set<String>();
for (Request r : requests) {
orderNumbers.add(r.orderNumber);
}
Map<String, String> statusByNumber = new Map<String, String>();
for (Order o : [
SELECT OrderNumber, Status
FROM Order
WHERE OrderNumber IN :orderNumbers
WITH USER_MODE
]) {
statusByNumber.put(o.OrderNumber, o.Status);
}
List<Response> results = new List<Response>();
for (Request r : requests) {
Response res = new Response();
res.status = statusByNumber.containsKey(r.orderNumber)
? statusByNumber.get(r.orderNumber)
: 'Order not found';
results.add(res);
}
return results;
}
}A user-mode query that hits a missing permission throws an exception instead of returning an empty list. Test both outcomes. "No such order" and "no permission to look" are different situations, and the agent will treat them differently.
Identity in topic instructions
Instructions should match the identity model. The two topic examples from this tutorial show the difference.
The service agent topic handles a missing record by asking the customer to recheck the number once, then offers a human:
Topic: Order Status
Classification description: Customer asks where an order is or what state it is in.
Scope: Look up the status of a single order using the Get Order Status action.
Instructions:
- Ask for the order number if the customer has not given one.
- Call the Get Order Status action with that order number.
- Report only the status returned by the action.
- If the action returns 'Order not found', ask the customer to check the number once, then offer to connect them with a human agent.
- Do not discuss refunds or cancellations in this topic.The employee agent topic assumes the user's own permissions govern the results:
Topic: My Open Cases
Classification description: An employee asks about support cases assigned to them or their team.
Scope: Summarize cases the employee already has permission to view.
Instructions:
- Use the Get Open Cases action to retrieve cases for the current user.
- Present each case with its number, subject, and status.
- If the employee asks about data they cannot access, say that you cannot show it and suggest contacting their administrator.The last instruction in the employee topic is only possible because the running user model enforces access. The agent can say "I cannot show it" because the platform already withheld the data. A service agent has no equivalent safety net, so its restrictions have to live in the agent user's permissions and in the action logic.
Common errors
- Reusing an administrator-level user as the agent user. The agent can then read almost everything. Create a dedicated user and permission set.
- Sharing one agent user across several agents. Permissions pile up, and audit trails become hard to read.
- Forgetting Apex class or Flow access. The agent user needs access to the invocable class. A missing grant shows up as the action failing at runtime.
- Testing as an administrator. Admin access hides permission gaps. Test with the real agent user, and for employee agents with a user in the target role.
- Expecting
with sharingto enforce field security. It does not. Use user mode. - Assuming a service agent knows who the customer is. The agent user is not the customer. Identity verification is a separate step that you build into the flow or action.
A checklist before you activate
- The agent user exists, has the agent user license, and is used by one agent only.
- Its permission set grants only the objects, fields, classes, and Flows the actions need.
- Every Apex action runs in user mode or otherwise enforces sharing and field security.
- Actions that return customer data filter by a verified customer, not by a value the customer typed.
- For employee agents, the target employees have the access permission and the underlying data access the topics assume.
- You tested a denied case, such as a user without access, and confirmed the agent responds sensibly.
Exercise
A service agent runs as an agent user with read access to every Case. A customer asks, "What is the status of case 00001234?" and the action returns a case that belongs to a different customer. Which model's rule explains why this happened, and name two changes that would prevent it.
Show answer
The agent user model explains it. The action runs as the single agent user, whose broad read access sets the ceiling, and the platform does not narrow access to the current customer. Two fixes: (1) reduce the agent user's permission set and sharing access to the minimum the agent needs, and (2) change the action so it filters cases by the verified customer (for example, by the contact or account confirmed in the conversation) and returns nothing for a case that belongs to someone else. Instructions alone would not prevent this.
Exercise
An employee agent runs the Get Open Cases action. One employee sees five cases and a colleague sees none, although both use the same agent. Is this a defect?
Show answer
No. Employee agents use the running user model, so each action runs as the employee who is chatting, with that employee's permissions and sharing access. Different employees can see different results. Check the second employee's case access before changing the agent.