Sub topic 9
Run context, permissions, and data access
This chapter assumes you know Flow Builder basics (variables, Get Records, Update Records), the Salesforce data model, and how permission sets and profiles grant object and field access. It covers what identity a flow-based agent action runs as, what that identity can read and change, and how to limit the damage a mistaken or manipulated agent call can do.
Why the run context matters for agents
A normal flow runs because a person clicked something or a record changed. An agent action runs because a language model decided to call it, based on a conversation. The model can misread a request, and a customer can phrase a request to push the agent toward things it should not do. The flow is the last place where you can enforce rules in a deterministic way.
Two questions decide how safe an action is:
- Which user and permissions does the flow run with?
- Which records and fields can the flow reach with those permissions?
Treat the agent's choice to call an action as a suggestion. The permissions and the logic inside the flow decide what actually happens.
Two layers of identity
An agent has its own running identity, and the flow has a run mode. They interact.
- Agent user. An internal (employee) agent runs as the user who is chatting with it, so the user's own access applies. A service agent, for example one that answers customers on a website, runs as a dedicated agent user that you assign in the agent's settings. That user is a normal Salesforce user with a profile and permission sets. The exact setting names vary by release, so check the Agent Builder settings in your org.
- Flow run mode. Each flow has a "How to Run the Flow" setting. It decides whether the flow respects the running user's permissions or ignores them.
For a service agent, the agent user is the identity that matters. It is not the customer. The customer has no Salesforce user in most designs, so any customer-specific restriction must be built into the flow logic.
Flow run modes
Flow Builder offers these choices for autolaunched flows:
| Run mode | Object and field permissions | Sharing rules |
|---|---|---|
| User or System Context: Depends on How the Flow Is Launched | Usually enforces the running user's access when launched by a user, but the details depend on the launch method | Depends on launch method |
| System Context with Sharing: Enforce Record Access | Not enforced for objects and fields | Enforced |
| System Context without Sharing: Access All Data | Not enforced | Not enforced |
Check the current labels in your org's Flow Builder, because Salesforce has renamed these settings between releases. The behavior to remember is that object and field permissions are not enforced in either system context mode. A flow in system context can read a field that the agent user cannot see in the UI.
Choosing a mode
Prefer the narrowest mode that works.
- Use user context when the agent user already has the access the action needs. This is the safest choice because the permission set is the single source of truth.
- Use system context with sharing when the action needs fields the agent user should not have in general, but record visibility should still follow sharing rules.
- Use system context without sharing only when the action must see records the agent user cannot see, and you have written explicit filters in the flow to compensate.
A common mistake is picking "Access All Data" because a test failed with a permission error. That hides the problem and gives the model an action that can reach everything. Fix the permission set, or narrow the flow, instead.
Least privilege for the agent user
Build a dedicated permission set for the agent user and grant only what your actions need.
- Object permissions: read on Case, not edit, if your actions only look cases up.
- Field permissions: list the fields explicitly. Do not grant "View All Fields" style access.
- Apex class and flow access where required. Autolaunched flows used by an action generally need the agent user to be able to run them.
Then review the list against every action on the agent. When you add an action, update the permission set, and when you remove one, remove the access it needed.
Permission set: Case_Lookup_Agent_Access
Object Case: Read
Field Case.CaseNumber: Read
Field Case.Status: Read
Field Case.ContactId: Read
Object Contact: Read
Field Contact.Email: ReadLimiting data access inside the flow
Permissions control what the agent user can do in general. The flow logic controls what this action does for this request. Both are needed, because the model supplies the input values.
Filter on the input and on the requester
Consider the case lookup action from earlier chapters. Its input is caseNumber. If the flow only filters on that number, anyone who guesses or types a valid number gets that case's status. For a public-facing agent, you usually want the caller to prove they own the case.
Add a second input, such as the customer's email address, and require both to match.
<recordLookups>
<name>Get_Case</name>
<label>Get Case</label>
<object>Case</object>
<filterLogic>and</filterLogic>
<filters>
<field>CaseNumber</field>
<operator>EqualTo</operator>
<value>
<elementReference>caseNumber</elementReference>
</value>
</filters>
<filters>
<field>ContactEmail</field>
<operator>EqualTo</operator>
<value>
<elementReference>customerEmail</elementReference>
</value>
</filters>
<getFirstRecordOnly>true</getFirstRecordOnly>
<outputAssignments>
<assignToReference>caseRecord</assignToReference>
</outputAssignments>
<storeOutputAutomatically>false</storeOutputAutomatically>
</recordLookups>Note that the email is still text typed in a conversation. It reduces guessing, but it is not strong authentication. For sensitive data, rely on a verified identity step in your channel (for example a signed-in session) instead of a value the customer types.
Return only what is needed
Output variables are sent to the language model and may be repeated to the customer. Return the status, not the whole record. Avoid outputs such as an entire Contact, internal notes, or fields like cost or discount.
- Define a small set of text outputs.
- Do not mark record variables as available for output unless every field in them is safe to show.
- Keep internal comments and owner details inside the flow.
Limit writes
Update actions carry more risk than reads.
- Update a specific record found by a filter, never a collection built from a loose filter.
- Set only the fields the action is for. Do not copy every input into a record.
- Use a Decision element to reject unsafe values, such as a status the agent should never set.
- Consider whether the action should ask the customer for confirmation first. You set this in the action instructions, covered later.
How the pieces fit
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Flow works in Flow Builder debug but fails in the agent | Debug runs as you, an admin with broad access. The agent user has less. | Test as the agent user's permissions, then adjust the permission set. |
| Get Records returns nothing for a record you can see | Sharing rules hide it from the agent user | Review sharing, or use system context with sharing and explicit filters |
| Agent can see a field you wanted hidden | System context ignores field permissions | Remove the field from outputs, or switch to user context |
| Action fails with "insufficient access" | The agent user lacks access to the flow or to an object | Add the access to the permission set |
| Update works on the wrong record | Filter used a model-supplied value that was not unique | Filter on a unique key and check that one record was found |
Edge cases
- Fault paths hide detail. When access fails, the flow raises a fault. The fault message can contain object and field names. Return a short, safe message as the output, not
$Flow.FaultMessage, when the agent faces customers. - Subflows. A subflow generally follows the run mode of the calling flow in most cases, but verify this in your release. Do not assume a subflow is more restricted than its parent.
- Apex called from the flow. Apex runs in system mode by default unless the class uses
with sharingand your code enforces field access. See the later chapter on combining flows with Apex. - Record-triggered side effects. An update from the action can fire triggers and other flows. Those run in their own context and may do more than you expect. Review what fires on the objects you update.
Checklist before registering the action
- The run mode is the narrowest that works.
- The agent user has a dedicated permission set with only the needed objects and fields.
- Filters use unique values, and the flow checks that exactly one record matched.
- Outputs contain only values safe to show the customer.
- Fault paths return safe messages.
- Writes touch one record and set only intended fields.
Exercise
A service agent has an action that looks up a case by case number. The flow runs in system context without sharing, and it returns the Case record as an output. Name two risks and one change for each.
Show answer
Risk 1: any customer who supplies a valid case number can see that case, because system context without sharing ignores record access, and nothing checks who is asking. Change: add a second input such as the contact email, filter on both, and prefer a verified identity from the channel. Risk 2: returning the whole Case record sends every field to the model, including internal ones, and field permissions are not enforced in system context. Change: return only specific text outputs such as the status, and switch to system context with sharing or user context if the access allows it.
Exercise
A flow action works when you debug it in Flow Builder but returns "insufficient access" in the agent preview. What is the most likely cause, and how do you confirm and fix it?
Show answer
Debug runs with your own broad access, while the agent runs as the agent user, who has fewer permissions. Confirm by checking the agent user's permission set for the object, the fields, and access to the flow. Fix by adding the missing object, field, or flow access to a dedicated permission set, not by switching to Access All Data.