Sub topic 10
Enforcing permissions inside Apex and Flow actions
This chapter assumes you can read basic Apex and Flow, and that you know what permission sets, sharing, and field-level security (FLS) do. An agent action that runs Apex or Flow can bypass the agent user's permissions unless you write it not to. This chapter shows how to prevent that.
Why actions can leak data
Apex normally runs in system mode. That means it ignores object permissions and FLS, and it applies sharing only if the class says so. If an action reads an order in system mode, the agent can return data the agent user could never open in the UI. The permission set from the earlier chapter would not matter.
You need two settings in the action code:
with sharingon the class, so record sharing applies.WITH USER_MODEon the query, so object permissions and FLS apply.
with sharing alone does not enforce FLS. WITH USER_MODE alone does not replace the class-level choice for the rest of the code. Use both.
An action that respects the running user
The class below returns an order's status. It uses the sharing and user-mode settings described above.
public with sharing class OrderStatusAction {
public class Request {
@InvocableVariable(required=true label='Order Number')
public String orderNumber;
}
public class Result {
@InvocableVariable(label='Order Status')
public String status;
}
@InvocableMethod(label='Get Order Status' description='Returns the status of an order the running user can access')
public static List<Result> getStatus(List<Request> requests) {
Set<String> numbers = new Set<String>();
for (Request r : requests) {
numbers.add(r.orderNumber);
}
Map<String, String> statusByNumber = new Map<String, String>();
for (Order o : [
SELECT OrderNumber, Status
FROM Order
WHERE OrderNumber IN :numbers
WITH USER_MODE
]) {
statusByNumber.put(o.OrderNumber, o.Status);
}
List<Result> results = new List<Result>();
for (Request r : requests) {
Result res = new Result();
res.status = statusByNumber.get(r.orderNumber);
results.add(res);
}
return results;
}
}Three details matter here:
- The method takes and returns lists. Invocable methods are bulk-safe, so the platform can pass several requests in one call.
- The query runs once, outside the loop that builds results.
- If the user cannot see the order, the query returns no row. The status stays
nulland the agent can say it found nothing. It does not receive the data.
If the query selects a field the user cannot read, WITH USER_MODE throws an exception instead of silently dropping the field. Select only the fields the action needs, and make sure the permission set grants read access to them.
How the pieces fit together
Flow actions
Flows have a similar setting. A flow can run in user context or system context. For flows called by an agent, choose the narrowest option that works.
- Run the flow in user mode when you can. Object permissions, FLS, and sharing then apply to the agent user.
- Use system mode only for a step that truly needs elevated access. Keep that flow small and give it a single purpose, so the agent cannot use it for anything else.
- Avoid screen-based design assumptions. Agents call autolaunched flows, so there is no person to confirm a step.
Check the flow's run mode setting in Flow Builder before you add it as an action.
Common errors
| Mistake | Effect | Fix |
|---|---|---|
| Class declared `without sharing` or with no keyword | Agent sees records the user should not | Use `with sharing` |
| Query without `WITH USER_MODE` | Ignores FLS and object permissions | Add `WITH USER_MODE` |
| Selecting extra fields | Exception if the user lacks read access | Select only needed fields |
| Query inside a loop | Hits governor limits in bulk calls | Collect keys, then query once |
| Broad system-mode flow | Agent reaches data beyond its role | Use user mode or narrow the flow |
Test the permissions
Write an Apex test that runs the action as a user holding only the read-only permission set. Then run it as a user without the permission set. The second case should return no status or throw a handled exception. A passing test for the privileged user alone proves nothing about the guardrail.
Exercise
An Apex action queries Order inside a class declared with sharing, but the SOQL has no WITH USER_MODE. What does the action still fail to enforce, and how do you fix it?
Show answer
It still fails to enforce object permissions and field-level security, because with sharing only applies record sharing rules. Add WITH USER_MODE to the query so the agent user's object and field permissions apply too.
Exercise
The agent user lacks read access to a field that the action's SOQL selects with WITH USER_MODE. What happens, and what are two valid responses?
Show answer
The query throws an exception. Either remove the field from the query if the action does not need it, or add read access to that field in the agent user's permission set if the access is intended.