Sub topic 6
Permissions and Sharing in Retrieval
This chapter assumes you know Salesforce objects, fields, profiles, permission sets, and sharing rules. It also assumes you have read the previous chapter on secure data retrieval. Here we look at one question: when an Agentforce agent builds a prompt, whose access decides which CRM data goes into it?
The core rule: the running user's access
An agent does not get a private, wider view of your org. When it retrieves records to ground a prompt, retrieval runs in the context of a user. For an employee-facing agent, that is the person who asked the question. The prompt can only contain data that user is allowed to see.
This matters because the model never enforces access control. The model reads whatever text it receives. If a restricted field reaches the prompt, the model can repeat it. Access control therefore has to happen before the prompt is built, at the retrieval step.
Two points are easy to get wrong:
- Retrieval uses the same Salesforce access model you already manage. The Trust Layer does not add a separate permission system for prompts.
- The Trust Layer does not widen access. It also cannot fix access that is already too broad.
Which layers of access apply
Salesforce decides access in several layers. Retrieval for grounding is expected to respect all of them for the running user.
| Layer | What it controls | Example effect on a prompt |
|---|---|---|
| Object permissions | Whether the user can read an object at all | A user without read access to Case gets no case data in the prompt |
| Field-level security | Which fields on an object the user can read | A hidden `Annual_Revenue__c` field is left out |
| Record sharing | Which records the user can see (org-wide defaults, roles, sharing rules, teams) | Only accounts the user owns or is shared on are retrieved |
| Data scoping in the action | What the agent action or flow actually queries | An action that queries only the open cases for one contact |
The first three come from your existing security model. The fourth depends on how the agent's actions are built. The exact enforcement can differ by action type, so check how each action you use handles access. For example, a flow can run in user mode or system mode, and Apex can run with or without sharing. Those choices affect what a given action returns.
Where enforcement can break down
The running-user rule is only as strong as the actions behind it. These are the common failure points.
System-context actions
A custom Apex action declared without sharing, or a flow that runs in system mode, can read records the user could not open in the UI. The agent then places those values in the prompt. The Trust Layer sees only the resulting text.
A defensive Apex query enforces access explicitly:
public with sharing class OpenCasesForContact {
public static List<Case> fetch(Id contactId) {
return [
SELECT Id, Subject, Status
FROM Case
WHERE ContactId = :contactId AND IsClosed = false
WITH USER_MODE
LIMIT 20
];
}
}with sharing applies record sharing. WITH USER_MODE applies object and field permissions for the running user. Use both when the action's results go into a prompt.
Over-broad permission sets
If a service agent's profile grants "View All Data" or "Modify All Data", retrieval returns everything. The agent behaves correctly according to the rules, and the result is still too much data. The fix is in the permission model, not in the agent.
Agents that run as a service identity
Some agents act for customers or other external parties and do not run as the person asking. In that case, the agent's own running identity and its permissions set the boundary. Give that identity only the objects and fields the agent's topics need. Treat it like an integration user, with least privilege.
Unrelated data in related lists
A query that follows relationships can pull in more than you intended. A prompt template that merges a related list of contacts brings in every contact the user can see on that account. Scope each merge to what the task needs.
Following one request
The diagram shows where the permission checks sit during retrieval.
A worked example
Two users ask the same agent: "Summarize the open cases for Northwind."
- Priya is a service rep. She can read Case and the account Northwind through the account team. She cannot read the field
Internal_Risk_Note__c. - Tom is on another team. He has Case read access, but Northwind is not shared with him.
The results differ:
| User | Cases retrieved | `Internal_Risk_Note__c` in prompt |
|---|---|---|
| Priya | Open cases for Northwind | No, hidden by field-level security |
| Tom | None, because the records are not shared | Not applicable |
Tom gets an answer such as "I could not find any cases." That answer is correct from his point of view. If he got a summary of the Northwind cases, that would indicate a sharing problem in your org or in the action, not a model problem.
What permissions do not cover
Permission-aware retrieval limits what enters the prompt. It does not decide what the user may do with data they are allowed to see.
- Masking is separate. A user may legitimately see a customer's email. Data masking, covered in a later chapter, still replaces it with a placeholder before the prompt leaves Salesforce. Permissions decide whether the value is retrieved. Masking decides whether the model provider sees it in the clear.
- Allowed data can still be summarized and shared. If a rep can see a record, the agent can summarize it for that rep. The rep can then paste the summary elsewhere. Permissions do not track that.
- Stored text is not checked for meaning. A permitted field that contains pasted secrets, such as a password in a case comment, goes into the prompt as written.
Practical checks
Use this list when you review an agent before launch.
- List the objects and fields each topic and action actually needs.
- Open the agent's running user, or a test user with the same permission sets, and compare their access to that list.
- Look for
without sharingApex and system-mode flows used by agent actions. Decide whether each one is justified. - Test with at least two users who have different sharing, one who should see data and one who should not.
- Check for "View All" or "Modify All" on any profile or permission set that agents or their users hold.
Exercise
A sales rep asks an agent for a summary of an account. The summary includes a field the rep cannot see in the Salesforce UI. The agent's action is a custom Apex class. What is the most likely cause, and where should you fix it?
Show answer
The Apex class most likely runs in system context, for example without sharing or without user-mode field checks, so it returns data the rep's permissions would block. Fix it in the action: declare the class with sharing and query with WITH USER_MODE (or enforce field-level security another way). The Trust Layer and the model are not the place to fix it, because the data was already in the prompt.
Exercise
Why does data masking not replace permission-aware retrieval, and why does permission-aware retrieval not replace masking?
Show answer
Permission-aware retrieval limits which data is pulled into the prompt, based on what the running user may see. Masking hides sensitive values that did get pulled in, so the model provider does not receive them in the clear. Without retrieval limits, users could get data they should not have. Without masking, permitted sensitive data would still be sent to the model provider as plain text.