Sub topic 15
with sharing, without sharing, and inherited sharing
This chapter assumes you can declare an Apex class, know the access modifiers, and understand that Apex runs inside a transaction. Sharing keywords decide whether a class respects the running user's record-level access when it queries or changes data.
What sharing keywords control
Apex normally runs in system context. It ignores object permissions and field-level security unless you enforce them. It also ignores record sharing unless the class says otherwise. Sharing keywords affect only record-level access, which comes from ownership, the role hierarchy, sharing rules, and manual shares. They do not enforce object permissions or field-level security.
You put the keyword between the access modifier and class:
public with sharing class AccountService {
public static List<Account> myAccounts() {
return [SELECT Id, Name FROM Account];
}
}The query returns only the accounts the running user can see.
The three keywords
| Keyword | Behavior |
|---|---|
| `with sharing` | Enforces the running user's record sharing in SOQL and DML. |
| `without sharing` | Ignores record sharing and sees all records. |
| `inherited sharing` | Uses the sharing mode of the class that called it. |
If you declare none of them, the class does not get a mode of its own. It takes the mode of the class that calls it. An entry point with no keyword, such as an anonymous Apex block, runs without sharing. A class with no keyword is therefore a risky default, so state your intent.
public without sharing class AuditReader {
public static Integer countAllAccounts() {
return [SELECT COUNT() FROM Account];
}
}How the mode carries across calls
The mode is set per class, and it follows the call chain.
- A method runs in the mode of the class that declares it.
- When a
with sharingclass calls awithout sharingclass, the called code runs without sharing. - A class with no keyword adopts the mode of its caller.
- Inner classes do not inherit sharing from their outer class. Declare it on the inner class if you need it.
- A subclass does not inherit the sharing mode of its parent. It uses its own keyword, and if it has none, it adopts its caller's mode.
inherited sharing makes the caller-dependent behavior explicit. It is useful for utility classes that should respect whatever context they are used in:
public inherited sharing class RecordLoader {
public static List<Contact> load(Id accountId) {
return [SELECT Id, LastName FROM Contact WHERE AccountId = :accountId];
}
}If a with sharing controller calls RecordLoader.load, the query enforces sharing. If a without sharing batch job calls it, the query sees everything. Unlike omitting the keyword, inherited sharing is safe when it is the entry point. In that case it defaults to with sharing.
Common mistakes
- Assuming
with sharingenforces field-level security. It does not. UseWITH USER_MODEin SOQL orSecurity.stripInaccessiblefor that. - Leaving the keyword off a class that queries data, then being surprised by the results when the caller changes.
- Using
without sharingon a whole class when only one method needs it. Put that method in a small separate class.
Exercise
A with sharing trigger handler calls a utility class declared inherited sharing. A scheduled job, which is a without sharing class, calls the same utility. In which mode does the utility's query run in each case?
Show answer
When the handler calls it, the query runs with sharing and returns only records the running user can see. When the scheduled job calls it, the query runs without sharing and returns all records. The utility takes the mode of its caller.