Sub topic 5
Granting access with permission sets
This chapter assumes you can create a permission set in Setup, you know the difference between object permissions and field-level security, and you have read the earlier chapters on the agent user and least privilege. Some examples use Salesforce DX metadata and the sf CLI, but you can do the same steps in Setup.
Why permission sets carry the agent's access
An agent that runs as a dedicated agent user can do only what that user can do. The agent has no separate access model. If the agent user can read Orders, the agent can read Orders. If the agent user can delete Cases, a badly written action or a successful prompt manipulation can delete Cases.
You therefore decide what the agent can touch by deciding what its user can touch. Salesforce gives you two tools for this: the profile and permission sets.
- The profile is the base. For an agent user it is normally the minimal Einstein Agent User profile. Keep it minimal and do not add permissions to it.
- Permission sets add access on top. Each one is small, named for a purpose, and can be reviewed and removed separately.
This split is the main reason to use permission sets. Permissions are additive, so the agent user's access is the sum of its profile and every assigned permission set. If you keep the profile empty, the list of assigned permission sets becomes a complete, auditable list of what the agent can do.
What a permission set can grant
Not every permission set setting matters for an agent. These are the ones to check.
| Setting | What it controls | Agent relevance |
|---|---|---|
| Object permissions | Create, read, edit, delete, view all, modify all, per object | Defines which records an action can query or change |
| Field permissions | Read and edit access per field | Hides sensitive fields from queries and from the model's context |
| Apex class access | Which Apex classes the user can run | Required for invocable Apex actions |
| Flow access | Which flows the user can run | Required for flow-based actions |
| System permissions | Org-wide abilities such as View All Data or Modify All Data | Almost never appropriate for an agent |
| Custom permissions | Named flags your own logic can check | Useful for gating one action |
View All and Modify All on an object bypass sharing for that object. View All Data and Modify All Data do the same for every object. Both undermine the sharing model that the next chapter relies on, so treat them as red flags in any agent permission set.
A read-only permission set for order status
The following permission set gives the agent user read access to Order and to one field, 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>Points to notice:
- Every create, edit, delete, and view-all flag is written out as
false. Omitting a flag has the same effect, but explicit values make a review easier because the reader can see that someone made a decision. hasActivationRequiredisfalse, so this is an ordinary permission set that applies as soon as it is assigned. A session-based permission set is a different kind that activates only inside a session. An agent user normally needs standing access, so use ordinary assignment.- The field list names only
Order.Status. Any other Order field that is not covered by another permission set stays hidden from the agent user. Standard name fields, which normally include the Order number, follow object read access and do not need a field entry. Check this in your own org before you rely on it.
Save the file as force-app/main/default/permissionsets/Agent_Order_Read.permissionset-meta.xml. The file name sets the API name Agent_Order_Read.
Adding access for the actions the agent runs
Object and field permissions are not enough when the agent calls Apex. The agent user also needs permission to run the class. Add a class access entry to the same permission set, or to a separate one if you want to keep data access apart from action access.
<?xml version="1.0" encoding="UTF-8"?>
<PermissionSet xmlns="http://soap.sforce.com/2006/04/metadata">
<label>Agent Order Actions</label>
<description>Lets the service agent user run the order status action</description>
<hasActivationRequired>false</hasActivationRequired>
<classAccesses>
<apexClass>OrderStatusAction</apexClass>
<enabled>true</enabled>
</classAccesses>
</PermissionSet>Splitting the two has a practical benefit. When you retire an action, you remove its permission set from the agent user and the data permissions stay in place for other actions. When you add a new object, you change only the data set.
A rule of thumb for naming and structure:
- One permission set per business capability, such as "Agent Order Read" or "Agent Case Create".
- One permission set per kind of access when a capability grows, such as data versus action.
- A description on every permission set that says why the agent needs it.
Assigning the permission sets
Assign the permission sets to the agent user the same way you would for a person. Setup works, and so does the CLI, which is easier to repeat across sandboxes.
sf project deploy start --source-dir force-app/main/default/permissionsets
sf org assign permset --name Agent_Order_Read --on-behalf-of [email protected]
sf org assign permset --name Agent_Order_Actions --on-behalf-of [email protected]Replace the username with the username of your agent user. The agent user's username is unique across all Salesforce orgs, so it usually includes your org or sandbox name.
Agent setup in Setup may also assign standard permission sets to the user. Do not remove these without checking what they grant, because the agent may stop working. Review them as part of your audit, as described below.
How the layers combine
The license sets the ceiling. A permission set cannot grant a permission that the user's license does not allow, and Salesforce either rejects the assignment or ignores the permission. Permissions in the profile and the permission sets are added together. Sharing is then applied record by record. Permission sets decide whether the agent user can work with Orders at all. They do not decide which Orders. That is the job of sharing, covered in the next chapter.
Common errors and edge cases
The action returns nothing, and no error appears. This is the most common symptom of a missing permission. A query in user mode returns only records the user can see, and it throws an error if the user lacks object read access or field access for a field you named in the query. An action written to return an empty status when nothing is found hides the cause. Check object read, field read, and class access in that order.
The agent works for you but not as the agent user. Administrators usually have broad access, so testing as an admin hides gaps. Test as the agent user, or use the agent testing tools that run with the agent's identity.
The agent can see more than you expected. Look for these causes:
- A standard permission set assigned by agent setup that includes broader access.
- A permission set group that includes a set you did not review.
- View All or Modify All on an object, or View All Data or Modify All Data.
- A role or sharing rule that gives the agent user's role access to records.
Deployment succeeds but the permission is not there. Field permissions in a permission set deploy only if the field exists in the target org. Required fields cannot have read turned off. Permission sets that refer to a class that does not exist fail to deploy, so deploy the class first or in the same package.
Changing a permission set changes every agent that uses it. If two agents share one agent user, or one permission set is assigned to several users, a change for one agent applies to all. Prefer one agent user per agent, so you can see exactly what each agent can do.
Auditing what the agent user can do
After you assign permission sets, confirm the result instead of trusting your configuration:
- In Setup, open the agent user and list the assigned permission sets and permission set groups.
- Open each one and check object permissions, system permissions, and Apex class access.
- Use the field accessibility and object access views to see what the user can reach.
- Run each action as the agent user with a record the user should be able to see and one it should not.
Repeat this audit when you add an action, change the profile, or upgrade the agent. Permission drift is slow and easy to miss.
Exercise
An agent calls an invocable Apex action that queries Order with WITH USER_MODE. The agent user has the Agent Order Read permission set from this chapter, and the action always returns an empty status for orders you know exist. The class is declared with sharing. What is the first permission you should check, and what should you avoid doing to fix the problem?
Show answer
Check whether the agent user has access to the Apex class itself, since the Agent Order Read set grants only object and field access and the class needs its own class access entry (for example in the Agent Order Actions set). Then check record visibility through sharing. Avoid fixing it by granting View All Data, Modify All Data, or View All on Order, because that bypasses sharing and gives the agent far more access than the action needs.
Exercise
Why is it better to keep the agent user's profile minimal and put all added access in separate permission sets?
Show answer
Permissions are additive, so with a minimal profile the assigned permission sets are the complete list of what the agent can do. This makes the access easy to audit, easy to change one capability at a time, and easy to remove, and it avoids hidden access coming from the profile.