Sub topic 6
Sharing rules and field-level security for agents
This chapter covers the two settings that decide which records an agent user can see and which fields it can read on those records. It assumes you know how permission sets grant object and field access, and you know the basic sharing model (organization-wide defaults, role hierarchy, sharing rules). It also assumes you have read the earlier chapters on the agent user and on granting access with permission sets.
Two questions, two mechanisms
An agent that looks up data runs queries as a user. For an agent user, the platform answers two separate questions on every query.
- Which records can this user see? Sharing decides. It is a row-level control.
- Which fields on those records can this user read or edit? Field-level security (FLS) decides. It is a column-level control.
Object permissions come before both. If the user has no Read permission on Order, sharing and FLS never come into play. The query returns nothing or fails.
| Layer | Controls | Where you configure it |
|---|---|---|
| Object permission | Whether the user can read, create, edit, or delete the object at all | Permission sets, profiles |
| Sharing | Which records of that object the user can access | Organization-wide defaults, role hierarchy, sharing rules, teams, manual shares |
| Field-level security | Which fields the user can read or edit | Permission sets, profiles |
The layers do not substitute for each other. Sharing never grants access the object permission denies. FLS never reveals a record that sharing hides. An agent is safe only when all three layers are set correctly.
How sharing applies to the agent user
The rule is simple: sharing applies to the agent user the same way it applies to any other user. An agent that runs as a dedicated agent user sees only the records that user can see. An agent that runs as the logged-in user sees what that person sees. Which run-as context you have was covered earlier. Here the point is what each context does to record visibility.
Organization-wide defaults
The organization-wide default (OWD) for an object sets the baseline. If Order is Private, the agent user sees only the records it owns, plus whatever other sharing grants. If Order is Public Read Only, the agent user can read every order, whoever owns it. Check the OWD in Setup under Sharing Settings before you decide the agent's access is narrow. A permission set with only Read access still exposes every record when the default is public.
Role hierarchy
Users high in the role hierarchy can see records owned by users below them, if the object is set to grant access using hierarchies. Give the agent user no role, or a role at the bottom of the hierarchy. A role placed high up quietly widens what the agent can read. This is a common mistake when someone clones an existing admin-style user to create the agent user.
Sharing rules, teams, and manual shares
Criteria-based and owner-based sharing rules extend access to users in a role or public group. If the agent user is in a public group that a rule targets, it inherits that access. Review the group memberships of the agent user. The same applies to account teams, opportunity teams, and manual shares. Anything that names the agent user, or a group it belongs to, adds records to its view.
Permissions that bypass sharing
Some permissions override sharing entirely:
- View All Records and Modify All Records on an object give access to every record of that object.
- View All Data and Modify All Data do the same for all objects.
The permission set from the previous chapter sets viewAllRecords and modifyAllRecords to false for this reason. Keep them off for agent users. Treat any request to turn them on as a design problem to solve differently, for example by adjusting a sharing rule that scopes the records.
Field-level security in practice
FLS has two flags per field: readable and editable. Editable implies readable. Object Read permission does not give field access. Each field you want the agent to use needs its own fieldPermissions entry, as in this part of the permission set from the earlier chapter:
<fieldPermissions>
<editable>false</editable>
<field>Order.Status</field>
<readable>true</readable>
</fieldPermissions>With only this entry, the agent user can read Status on orders it can see. It cannot read TotalAmount, BillingStreet, or a custom Internal_Notes__c field, because no permission set grants them. Add fields one at a time, and only when an action needs them.
Some details catch people out:
- A field that is not readable disappears. In user mode a query that selects it fails, and with
Security.stripInaccessiblethe field is removed from the results. Either way, the agent cannot use it in a reply. - Required fields and Name fields. Some standard fields are always visible, and fields that are required at the database level cannot be hidden by FLS. Do not rely on FLS to hide them. Restrict the object or records instead.
- Relationship fields. A query such as
Account.Owner.Emailis checked against FLS on each object it passes through. The agent user needs access to each hop. - Formula fields. A formula can show a value derived from fields the user cannot read, depending on how the formula was built. Review formulas that the agent can read, because FLS on the source fields does not necessarily protect the result.
- Long text and sensitive data. If a free-text field might contain card numbers or personal notes, leave it out of the permission set. Do not count on instructions to keep the agent from repeating it.
Where enforcement can fail: Apex and Flow
Sharing and FLS are enforced for the agent user only if the code that runs the query respects them. Apex classes declared without sharing, or queries run in default system mode, ignore the running user's sharing and FLS. A standard Apex query does not check FLS at all unless you ask it to.
This anonymous Apex compares three ways to query. Run it as an admin first, then use the test approach in the last section to run it as the agent user.
String orderNumber = '00000100';
// System mode: ignores the user's sharing and FLS in code that
// runs without sharing, and ignores FLS in all cases.
List<Order> systemRows = [
SELECT OrderNumber, Status, TotalAmount
FROM Order
WHERE OrderNumber = :orderNumber
];
// User mode: enforces object permissions, FLS, and sharing.
// Throws a QueryException if the user cannot read a selected field.
List<Order> userRows = [
SELECT OrderNumber, Status
FROM Order
WHERE OrderNumber = :orderNumber
WITH USER_MODE
];
// Strip fields the user cannot read instead of failing.
SObjectAccessDecision decision = Security.stripInaccessible(
AccessType.READABLE,
systemRows
);
List<Order> strippedRows = (List<Order>) decision.getRecords();
System.debug('System mode rows: ' + systemRows.size());
System.debug('User mode rows: ' + userRows.size());
System.debug('Stripped rows: ' + strippedRows.size());WITH USER_MODE is the safer default for agent actions. It enforces sharing, object permissions, and FLS together, and it fails loudly when the agent user lacks a field. Use Security.stripInaccessible when you prefer partial data over an error. Keep the class itself with sharing as well. The earlier OrderStatusAction example does both.
For Flow actions, check the run context. A flow set to run in system context without sharing returns records the agent user could not see otherwise. Prefer user context, or system context with sharing, for flows the agent calls. Use system context without sharing only for narrow, reviewed cases, and then filter the output so the agent receives only what it should say.
Verifying access
Do not read the permission set and assume. Check the effective access.
- In Setup, open the agent user and use View Summary on the permission set assignments to see the combined object and field permissions.
- Check the org's sharing settings for each object the agent reads. Note the OWD and every sharing rule that names the agent user's role or groups.
- Log in as the agent user is not possible for an integration-style user, so test by calling the actions in a test class that uses
System.runAswith the agent user, or by running the agent in the Agent Builder test panel and checking what comes back. - Try to read a record the user should not see, and a field it should not read. The expected result is an empty list or an access error, not data.
A short list of expected results makes this repeatable. For the order status agent, those results might be: an order owned by another customer returns no status, and a query that selects TotalAmount in user mode throws an error.
Exercise
An agent user has a permission set with Read on Order and readable Order.Status. The Order OWD is Public Read Only. An Apex action selects OrderNumber and Status with WITH USER_MODE. A customer asks about an order that belongs to someone else. What does the action return, and what single change would limit the agent to relevant orders?
Show answer
The action returns the status of that order, because Public Read Only lets the agent user read every order, and the user has Read on the object and the Status field. WITH USER_MODE enforces sharing, but sharing allows this access. To limit the agent, change the sharing so the agent user sees fewer records, for example set the OWD to Private and share only the needed records. You can also filter in the action by the verified customer. Changing the permission set alone would not help, since Read is already required.
Exercise
Why is it a mistake to give the agent user a role near the top of the role hierarchy, and what should you do instead?
Show answer
Users higher in the hierarchy can see records owned by users below them when the object grants access using hierarchies, so a high role widens the agent's record access without any permission set change. Give the agent user no role, or the lowest role that works, and verify the result against the sharing settings.