Sub topic 7
Setting up the agent user and permissions
This chapter covers the identity an Agentforce service agent runs as in the target org, and the permissions that identity needs. It assumes you can use permission sets, can run Salesforce CLI commands, and have already retrieved the agent and built a manifest in earlier chapters. You should also know which Apex classes and Flows the agent's actions call.
Why permissions are a separate step
Deploying metadata copies definitions. It does not copy users, and it does not copy permission set assignments. An agent can deploy with no errors and then fail when it runs, because the identity it runs as can't reach the objects, fields, Apex classes, or Flows its actions use.
Two things have to be right in the target org before activation:
- The agent user that the agent runs as exists and is active.
- That user has the permissions the agent's actions need.
How an agent runs: the agent user
Agents that serve customers, such as service agents, run as a dedicated agent user. Salesforce does not treat this user as a person. It is created with the Einstein Agent User profile and an agent-specific license. Everything the agent does at runtime (reading records, calling Apex, running Flows) happens with this user's access.
Employee-facing agents work differently. They run as the employee who is talking to them, so they use that employee's own permissions. For these agents, grant access to the employees' permission sets instead of to an agent user.
| Agent type | Runs as | Where to grant access |
|---|---|---|
| Customer-facing service agent | A dedicated agent user | Permission sets assigned to the agent user |
| Employee-facing agent | The employee in the conversation | Permission sets assigned to employees |
This difference matters when you deploy. Employee-facing agents only need the right permission sets in the target org. Service agents also need the agent user to exist first.
Where the agent user is referenced in metadata
The Bot metadata stores the agent user as a username, in the botUser field of the retrieved bot file. Usernames are unique across all Salesforce orgs. The username in your source sandbox therefore can't be reused as it is in the target org, and sandbox usernames carry a suffix with the sandbox name.
You can check which username a retrieved agent expects:
grep -r "botUser" force-app/main/default/botsIf the target org has no active user with that exact username, the deployment can fail, or the agent can deploy but not run correctly. You have two ways to handle this:
- Create a user in the target org with the same username. This works only if the username is available.
- Change
botUserin the retrieved file to the username of an agent user in the target org, then deploy.
The second option is more common, because the username must be different per environment anyway. The next chapters cover how to keep such environment-specific values out of your source files.
Creating the agent user in the target org
Create the agent user in the target org before deploying. In Setup, you can do this from the agent's configuration, or by creating a user yourself. Doing it yourself gives you control over the username, which makes it simpler to script.
When you create it, check these points:
- The profile is Einstein Agent User.
- The license is the agent user license that your org provides. Your org must have these licenses available. If it doesn't, ask your Salesforce account team.
- The user is active.
- The username follows your naming convention for the environment.
You can confirm the user exists with a query:
sf data query \
--query "SELECT Username, IsActive, Profile.Name FROM User WHERE Profile.Name = 'Einstein Agent User'" \
--target-org target-orgRun the same query against source-org and compare the results. Differences in the active users, or a missing user, are the first thing to look at when an agent works in the sandbox and not in the target org.
What permissions the agent user needs
Think of the agent user's access in layers. Each action in the agent has its own requirements, and the user needs the union of all of them.
- Apex classes. If an action calls an invocable Apex method, the user needs access to that Apex class through a permission set.
- Flows. If an action runs a Flow, the user needs permission to run it. Check whether the Flow runs in user context or system context, because that decides which object access matters.
- Objects and fields. Actions that read or write records need object permissions and field-level security. Missing field access is a common cause of an action that returns empty data and no error.
- Agent-specific permissions. Salesforce provides permission sets for agent users. Look in Setup for the ones in your org and assign the base set for agent users, since the names can differ between releases.
- Data sharing. Org-wide defaults and sharing rules still apply. If the agent should see records owned by other users, the sharing setup in the target org must allow it.
A good practice is to put the agent's custom access in one permission set that you own, such as Agent_Action_Access in this tutorial, and deploy it with the agent. That permission set travels in your manifest as PermissionSet. Assigning it is a separate step, which you do after the deployment.
What deploys and what does not
| Item | Deployed by the manifest? | Notes |
|---|---|---|
| Permission set definition | Yes | Include `PermissionSet` in the manifest. |
| Permission set assignment | No | Assign it in each org. |
| Agent user record | No | Create it in each org. |
| Licenses and permission set licenses | No | The target org must already have them. |
| `botUser` username | Yes, as text | The username must match a user in the target org. |
Assigning the permission set
After the permission set is deployed, assign it to the agent user. The sf org assign permset command normally assigns to the user you authenticated with. To assign to a different user, such as the agent user, use the --on-behalf-of flag:
sf org assign permset \
--name Agent_Action_Access \
--on-behalf-of [email protected] \
--target-org target-orgReplace the username with the real agent user's username. You can pass --on-behalf-of more than once for several users. Run the command for each permission set the agent needs, including the agent-specific base set that Salesforce provides.
If you assign a permission set to yourself instead of the agent user, the command succeeds and tells you nothing is wrong. The agent still won't have access. Always check which user got the assignment.
The order of operations
The order matters, because later steps depend on earlier ones.
Creating the user first lets the deployment find the username in botUser. Assigning permission sets after the deployment ensures the permission set exists. Activation comes last, once the agent has everything it needs.
Activation uses the agent's API name:
sf agent activate --api-name My_Service_Agent --target-org target-orgVerifying the setup
Before you run a full test, check the pieces separately.
- Confirm the agent user is active, with the query above.
- List the permission set assignments for that user:
sf data query \
--query "SELECT PermissionSet.Name, Assignee.Username FROM PermissionSetAssignment WHERE Assignee.Profile.Name = 'Einstein Agent User'" \
--target-org target-org- Compare the result with the same query in the source org. The list of permission sets should match, apart from sandbox-specific ones.
- Start a test conversation that triggers each action once, and watch for permission errors.
Common problems
- The deployment fails and mentions the bot user. The username in
botUserdoesn't match an active user in the target org. Fix the username or create the user. - The agent deploys but an action returns nothing. Field-level security or sharing likely hides data from the agent user. Compare field access in the source and the target.
- An action fails with an access error on an Apex class or Flow. The permission set that grants the class or Flow access was never assigned to the agent user.
- You can't create the agent user. The org doesn't have the needed license, or all copies of it are in use.
- The same setup works for admins but not for the agent. You tested with your own access. Admins usually have wider permissions than the agent user, so test as the agent does.
Exercise
You deploy a service agent from a sandbox to production. The deployment finishes, but the agent can't read Case records. The permission set definition was in the manifest. What should you check first, and why?
Show answer
Check whether the permission set was assigned to the agent user in production. A manifest deploys the permission set definition but not its assignments, so the agent user may have no access. Then check field-level security and sharing for the Case fields the action uses.
Exercise
The retrieved Bot file has a botUser value that ends with the sandbox name. Give two ways to make the deployment to the target org work.
Show answer
Create a user in the target org with that exact username, if it is available. Or edit botUser to the username of an active agent user in the target org before deploying. The second option is more practical because the username differs per environment.
Exercise
Which command assigns the permission set Agent_Action_Access to a user named [email protected], and why is the --on-behalf-of flag needed?
Show answer
sf org assign permset --name Agent_Action_Access --on-behalf-of [email protected] --target-org target-org. Without the flag, the command assigns the permission set to the user you authenticated as, not to the agent user.