Sub topic 3
The agent user
This chapter covers the dedicated user that some Agentforce agents run as. It assumes you can administer Salesforce users, profiles, and permission sets, and that you know what an agent, a topic, and an action are. You should also know how object permissions and record sharing combine. The Apex example near the end is short, and you can follow the chapter without running it.
What the agent user is
An agent user is a Salesforce user record that an agent runs as. When the agent reads a record, updates a field, or calls a flow, Salesforce checks permissions against this user. The agent has no access of its own beyond what this user has.
Three properties follow from that:
- The agent can only reach data and features the agent user can reach.
- Records the agent creates or edits show the agent user as the creator or last modifier.
- Changing the agent user's permissions changes the agent's reach immediately, with no change to the agent's topics or instructions.
The agent user is a normal user record, so you manage it with the same tools as any other user: permission sets, sharing, field-level security, and audit fields. Everything else in this tutorial about limiting access builds on this one user.
Which agents use an agent user
Not every agent runs this way. The previous chapter compared agent types in detail, so here is the short version.
- Agents that serve people outside your company, such as a service agent answering customers, run as a dedicated agent user. The customer is not a Salesforce user, so there is no one to inherit permissions from.
- Agents that help your own employees run as the logged-in employee. They see what that employee sees.
The rest of this chapter is about the first case, where you must create and configure the user yourself.
What the agent user controls
The agent user's identity affects four things. Each matters for security.
| Area | What the agent user determines |
|---|---|
| Object and field access | Which objects the agent can read, create, edit, or delete, and which fields it can see or change |
| Record access | Which individual records are visible, through ownership, role hierarchy, sharing rules, and manual shares |
| Ownership and audit | Who appears as owner, creator, and last modifier on records the agent touches |
| Feature access | Which Salesforce features and Apex classes or flows the agent can run, through permission sets |
A common mistake is to think of the agent user as only an object-permission question. Record access matters just as much. An agent user with Read on Order can still see only the orders that sharing allows. The Sharing and field-level security chapter covers that in depth.
The user license and profile
Salesforce provides a license and a profile designed for agent users. In current orgs the license is named Einstein Agent and the profile is named Einstein Agent User. Names and defaults change between releases, so confirm them in Setup before you rely on them.
An agent user created this way has these characteristics:
- It is not meant for interactive sign-in. Nobody should log in as it, and it needs no one to hold its password.
- The profile gives very little. Almost all useful access comes from permission sets that you assign.
- It does not consume a standard employee license, which makes a dedicated user cheap to create.
Because the profile is nearly empty, the agent user starts with close to no access. That default suits least privilege. Your task is to add only the access the agent's job requires.
Creating the agent user
You can create the agent user in one of two ways:
- During agent setup. The agent creation flow can create a new agent user for you or let you pick an existing one.
- In Setup, under Users. Create the user yourself, choose the Einstein Agent license and Einstein Agent User profile, and select it later in the agent's settings.
Creating it yourself gives you control over naming and order of operations. For production agents, that is usually the better choice. Follow these steps:
- Give the user a name that identifies the agent, such as "Order Status Agent". Use an email address that a team monitors, since Salesforce sends system notices to it.
- Assign the Einstein Agent license and Einstein Agent User profile.
- Assign the permission sets that grant the agent's required access. The next chapters cover how to design these.
- Select the user as the agent's run-as user.
- Test the agent with the user's exact permissions, as shown later in this chapter.
If your org has a managed package or feature that requires a particular base permission set for agent users, assign it too. The agent setup screens and Salesforce Help list these requirements for your release.
One agent user per agent
Give each agent its own agent user. Sharing one user across several agents causes problems:
- You cannot reduce one agent's access without affecting the others.
- Audit fields do not tell you which agent changed a record.
- A permission added for one agent's needs quietly becomes available to all of them.
A one-to-one mapping keeps the permission model readable. Name the user after the agent, and use the same name in the permission sets you build for it, such as "Agent Order Read".
Inspecting agent users
You can find agent users and review their access with queries. Run these in the Developer Console query editor or in any SOQL tool. This first query lists active users with the agent profile:
SELECT Id, Name, Username, IsActive, Profile.Name
FROM User
WHERE Profile.Name = 'Einstein Agent User'This second query shows what permission sets a given agent user holds. Replace the placeholder with an Id from the first query:
SELECT PermissionSet.Label, PermissionSet.Name, PermissionSet.IsOwnedByProfile
FROM PermissionSetAssignment
WHERE AssigneeId = '005000000000000AAA'The rows with IsOwnedByProfile set to true come from the profile. The others are assignments you made. Review this list on a schedule. Access tends to grow when someone adds a permission set "just to get it working" and never removes it.
Seeing the run-as user from inside an action
When an agent behaves unexpectedly, the first question is which user the code ran as. The following invocable Apex action returns the user's name and Id. You can add it to a test agent to confirm the run-as context.
public with sharing class WhoAmIAction {
public class Result {
@InvocableVariable(label='Running User Name')
public String userName;
@InvocableVariable(label='Running User Id')
public String userId;
}
@InvocableMethod(label='Who Am I' description='Returns the user the action runs as')
public static List<Result> whoAmI() {
Result res = new Result();
res.userName = UserInfo.getName();
res.userId = UserInfo.getUserId();
return new List<Result>{ res };
}
}For a service agent, the name returned should be the agent user. For an employee agent, it should be the person chatting with it. If the result is someone else, such as an administrator who set the agent up, stop and fix the configuration. Remove this action from production agents once you are done, because it adds an unnecessary capability.
Common errors
Using an administrator as the run-as user. An agent running as an administrator can read and change almost anything, so a single prompt injection or badly worded instruction could expose data across the org. Never use a System Administrator or a user with Modify All Data.
Cloning an employee user. Copying a real user copies roles, groups, and sharing context that the agent does not need. Create the agent user from scratch.
Granting access to fix a single failed test. When the agent returns nothing, add the missing permission for the specific object or field, not a broad one like View All. Check sharing first, because the failure is often record visibility and not object permission.
Assuming the agent sees what you see. In testing, you may see an order that the agent cannot. Your administrator access and the agent user's access differ. Test with the agent's actual run-as user, not your own.
Deactivating or freezing the user. An inactive or frozen agent user stops the agent from working. Do this deliberately when you want to disable an agent quickly, and be careful not to do it by accident during user cleanup.
Leaving ownership unplanned. Records the agent creates are owned by the agent user by default. If your sharing model or queues depend on ownership, decide how those records are reassigned, for example by an assignment rule or flow.
Checklist for a new agent user
- The user has the agent license and profile, and no other special profile.
- The name and email identify the agent and a monitored owner.
- Only the required permission sets are assigned, and each has a stated purpose.
- No Modify All Data, View All Data, or administrative permissions are included.
- Only one agent uses this user.
- You tested the agent with this user, and confirmed that restricted records and fields stay hidden.
Exercise
A customer-facing service agent answers questions as an administrator's user because "it was the quickest way to get it working." Name two specific risks and what you would change.
Show answer
Risk one: the agent can read and modify nearly all data, so a mistaken action or prompt injection could expose or alter records far beyond the agent's purpose. Risk two: audit fields show the administrator as the actor, so you cannot tell which changes came from the agent. Change it by creating a dedicated agent user with the Einstein Agent license and profile, assigning only the permission sets the agent needs, and selecting that user as the agent's run-as user.
Exercise
You run the WhoAmI action in an employee-facing agent, and it returns the name of the employee who is chatting. You run it in a customer-facing service agent and it returns the name of the agent user. Is this the expected behavior? Why?
Show answer
Yes. An employee agent runs as the logged-in user, so permissions and the running identity are that employee's. A service agent serves people who are not Salesforce users, so it runs as the dedicated agent user chosen in its configuration.