Sub topic 11
Sharing and Data Access
This chapter assumes you can write an invocable method with request and response classes, and that you know the basics of Apex sharing keywords and Flow run contexts. Here you will learn what data access rules apply when Flow calls your Apex, and how to make them explicit.
Flow context does not decide Apex access
An invocable method runs as Apex. The way the flow was started does not control what the Apex code can see. Two settings matter instead:
- The sharing keyword on the class:
with sharing,without sharing, orinherited sharing. - Field-level security (FLS) and object permissions, which Apex does not enforce by default.
A flow can run in user context or system context. A record-triggered flow, for example, can run in system context without sharing. Your Apex class is a separate layer. If you declare the class with sharing, record sharing is enforced for the running user, even when the calling flow ignores sharing.
Choosing a sharing keyword
| Keyword | Behavior | Use when |
|---|---|---|
| `with sharing` | Enforces the running user's record access | The default choice for most invocable classes |
| `without sharing` | Ignores record sharing | The method must see records the user cannot, and you have a documented reason |
| `inherited sharing` | Uses the caller's sharing mode | The class is shared between several entry points |
Be careful with inherited sharing. When Flow calls the method directly as the entry point, the class runs with sharing. That default is safe, but it is easy to forget when you reuse the class from other contexts.
Enforcing object and field access
Use WITH USER_MODE in SOQL and as user in DML to apply the running user's permissions, including FLS. The following method renames accounts and fails if the user lacks access.
public with sharing class AccountRenamer {
@InvocableMethod(label='Trim Account Names')
public static void trimNames(List<Id> accountIds) {
List<Account> accounts = [
SELECT Id, Name
FROM Account
WHERE Id IN :accountIds
WITH USER_MODE
];
for (Account acc : accounts) {
acc.Name = acc.Name.trim();
}
update as user accounts;
}
}If the user cannot read the Name field, the query throws a QueryException. If the user cannot edit it, the update throws a DmlException. Both exceptions reach the flow as a fault, so you can handle them on a fault path.
Records the user cannot see
A with sharing query silently returns fewer rows when the user lacks access to some records. This has a side effect on invocable methods. Your method receives a list of IDs, queries them, and may get back fewer records than it was given. If you return a result list by position, the sizes no longer match.
Build results from the request list, not from the query results.
public with sharing class AccountStatusLookup {
public class Request {
@InvocableVariable(required=true)
public Id accountId;
}
public class Result {
@InvocableVariable
public String industry;
@InvocableVariable
public Boolean found;
}
@InvocableMethod(label='Get Account Industry')
public static List<Result> getIndustry(List<Request> requests) {
Set<Id> ids = new Set<Id>();
for (Request req : requests) {
ids.add(req.accountId);
}
Map<Id, Account> visible = new Map<Id, Account>(
[SELECT Id, Industry FROM Account WHERE Id IN :ids WITH USER_MODE]
);
List<Result> results = new List<Result>();
for (Request req : requests) {
Result res = new Result();
Account acc = visible.get(req.accountId);
res.found = acc != null;
res.industry = acc?.Industry;
results.add(res);
}
return results;
}
}The loop walks requests, so the output always has one entry per input, in order. Records the user cannot see produce found = false instead of a missing entry.
Using without sharing safely
Sometimes a method must work beyond the user's access, such as writing to a log object the user cannot edit. Keep the elevated code small. Put it in a private inner class with without sharing, and leave the outer invocable class with sharing. The elevated part is then easy to review.
Exercise
A with sharing invocable method queries 50 account IDs without WITH USER_MODE. The user can see only 40 of them and has no read access to the Industry field. What happens, and how do you build a result list of 50 entries?
Show answer
The query returns 40 accounts because with sharing filters out the 10 invisible records. It still returns Industry, because record sharing does not enforce field-level security. Add WITH USER_MODE to enforce FLS, which makes the query throw an exception that reaches the flow's fault path. To get 50 results, loop over the request list and look each ID up in a Map of the queried records. Use a "not found" result for the 10 missing entries.