Sub topic 5
Secure data retrieval
This chapter covers the step in the Trust Layer where CRM data is fetched for a prompt. It assumes you know Salesforce objects, fields, and sharing, and that you know what a prompt is. It also assumes you have read the earlier chapters on the Trust Layer and the request flow.
What secure data retrieval means
An LLM does not have access to your org. It only sees the text in the prompt. Whatever data is placed in that text is the only data the model can use or repeat. Secure data retrieval is the rule that controls what goes into that text: data is fetched at the moment of the request, on behalf of the person making it, and only if that person is allowed to see it.
Three ideas make up the rule:
- Retrieval happens at runtime. The data is read when the agent needs it, not copied ahead of time into a model or a training set.
- Retrieval uses the user's access. Object permissions, field-level security (FLS), and record sharing apply to the lookup.
- Retrieval is separate from the model. Salesforce reads the records and builds the prompt. The model provider never queries your org.
Secure retrieval is the first protective step. Masking, prompt defense, and the gateway work on what retrieval has already produced. If retrieval returns a field the user should not see, later steps will not know that was a mistake.
Where retrieval happens
Agentforce can pull data into a prompt in several ways. Each has its own access behavior.
| Source | How data reaches the prompt | Access to check |
|---|---|---|
| Prompt template merge fields | Fields from a record are inserted into the template text | The running user's object access and FLS |
| Flow | A flow action runs queries and returns values | Whether the flow runs in user mode or system mode |
| Apex action | An invocable method runs SOQL and returns values | Sharing keywords and whether the query enforces FLS |
| Data Cloud and retrieval over unstructured content | Indexed data is searched and snippets are returned | Access controls configured on the data and the index |
The first row is the simplest case. The others are code or configuration that you or a developer wrote, so you control whether they enforce access. That is where most mistakes happen.
The path of one lookup
The diagram shows what happens when an agent needs a record to answer a question.
Two details matter here. First, the check is per field, so one record can contribute some fields and not others. Second, a blocked lookup produces missing data, not a refusal. The model then answers from whatever is left. Depending on the action, that can mean a short answer, a request for more information, or an error.
Writing an action that enforces access
Prompt template merge fields give you access checks without extra work. Custom Apex does not. Apex runs in system mode by default, so it can read fields the user cannot. Add the checks yourself.
The invocable method below returns a case subject and status. It uses with sharing so record sharing applies, and WITH USER_MODE so object permissions and FLS apply to the query.
public with sharing class CaseDetailsAction {
public class Request {
@InvocableVariable(required=true label='Case Id')
public Id caseId;
}
public class Result {
@InvocableVariable(label='Subject')
public String subject;
@InvocableVariable(label='Status')
public String status;
}
@InvocableMethod(
label='Get Case Details'
description='Returns the subject and status of a case the running user can see.'
)
public static List<Result> getCaseDetails(List<Request> requests) {
Set<Id> ids = new Set<Id>();
for (Request r : requests) {
ids.add(r.caseId);
}
Map<Id, Case> visible = new Map<Id, Case>(
[SELECT Id, Subject, Status
FROM Case
WHERE Id IN :ids
WITH USER_MODE]
);
List<Result> results = new List<Result>();
for (Request r : requests) {
Result res = new Result();
Case c = visible.get(r.caseId);
if (c != null) {
res.subject = c.Subject;
res.status = c.Status;
}
results.add(res);
}
return results;
}
}Points to notice:
with sharingfilters out cases the user cannot see. Those IDs are absent fromvisible, so the result for them is empty.WITH USER_MODEthrows aQueryExceptionif the user lacks read access to the Case object or to a queried field. The error reaches the agent as a failed action, not as silent data.- The method returns one result per request, in order. Invocable methods must keep that alignment.
- The method returns only two fields. Returning less is a retrieval control too. Fields that are never selected cannot reach the prompt.
Common errors
Assuming the agent has its own broad access. Teams sometimes test as an admin, see good answers, and ship. A regular user then gets thin answers or errors, because the same lookup now runs under their access. Test with a user who has the real profile and permission sets.
Relying on instructions instead of permissions. An agent instruction such as "never mention salary" is a request to the model. It is not an access control. If the salary field is in the prompt, it can leak. Remove the field from retrieval instead.
Leaving flows and Apex in system mode. A flow that runs in system context, or Apex without sharing and FLS enforcement, can return data the user could not open in the UI. This is the most common gap between "respects permissions" in principle and in practice.
Selecting too many fields. Pulling an entire record "for context" widens what the model sees and what could be repeated back. It also adds cost and noise. Select the fields the task needs.
Confusing retrieval with masking. Retrieval decides whether a value is allowed into the prompt. Masking, covered later, replaces allowed sensitive values with placeholders before the prompt leaves Salesforce. A value the user may see can still be masked. A value the user may not see should never be retrieved.
What secure retrieval does not do
It does not judge whether data is accurate or relevant. It does not stop a user from asking for something they are allowed to see but should not share. It does not check data that you put into the prompt by other means, such as a long free-text instruction pasted into a template. It also depends on your permission model. If a profile grants broad access, the agent inherits it.
Exercise
A support agent returns a customer's order total correctly when an admin tests it, but a service rep gets an answer with no total. The prompt template and the agent have not changed. What is the most likely cause, and how would you confirm it?
Show answer
The rep's field-level security or record sharing does not allow access to the total field or the order record. Retrieval runs as the running user, so the admin's broader access hid the problem. To confirm, test as the rep, or use "Login as" or a test user with the same profile and permission sets, and check read access on the field and sharing on the record. Then decide whether to grant access or accept that the agent should not return the total for that role.
Exercise
Your Apex action runs SELECT Id, Name, SSN__c FROM Contact WHERE Id = :contactId in a class declared without sharing. Name two changes that make retrieval safer.
Show answer
Declare the class with sharing so record sharing applies, and add WITH USER_MODE to the query so object permissions and FLS apply. Also remove SSN__c from the query unless the task needs it, because a field that is never selected cannot reach the prompt.