Sub topic 7
Which sharing mode applies when classes call each other
This chapter assumes you can read basic Apex (classes, static methods, SOQL) and know the three keywords from the previous chapters: with sharing, without sharing, and a class with no keyword. You should also know that organization-wide defaults, roles, and sharing rules decide which records a user can see.
Real transactions rarely run in one class. A controller calls a service, the service calls a data-access class, and the data-access class calls a utility. Each class may declare a different keyword, so you need a rule for predicting which sharing mode applies to each query at each step.
The core rule: the code that runs decides
Sharing is applied per piece of code, not per transaction. A SOQL query or DML statement follows the sharing mode of the class that contains that statement. The caller's mode does not carry over to a class that declares its own keyword.
This gives three rules:
- A class declared
with sharingenforces the running user's record access for every statement in its methods, constructors, and initialization code. - A class declared
without sharingignores record-level sharing for every statement in its own code. - A class with no keyword takes on the sharing mode of whatever called it.
inherited sharingalso takes the caller's mode, but you declare it on purpose (the next chapter covers it).
When a method returns, control goes back to the caller, and the caller's mode applies again to the caller's next statement.
A worked call chain
Suppose the running user can see 10 of the 500 Case records in the org. Four classes read Cases.
public with sharing class ReportController {
public static List<Integer> countCases() {
Integer own = [SELECT COUNT() FROM Case];
Integer viaAdmin = AdminCounter.countAll();
Integer viaHelper = HelperCounter.countAll();
Integer afterCalls = [SELECT COUNT() FROM Case];
return new List<Integer>{ own, viaAdmin, viaHelper, afterCalls };
}
}
public without sharing class AdminCounter {
public static Integer countAll() {
return [SELECT COUNT() FROM Case];
}
}
public class HelperCounter {
public static Integer countAll() {
return [SELECT COUNT() FROM Case];
}
}Calling ReportController.countCases() returns 10, 500, 10, 10.
ownis 10 becauseReportControlleriswith sharing.viaAdminis 500 becauseAdminCounterdeclareswithout sharing. Thewith sharingcaller does not override it.viaHelperis 10 becauseHelperCounterdeclares nothing, so the caller's mode stays in effect.afterCallsis 10 because control returned toReportController, and its own mode applies again.
Now change the entry point. If a different without sharing class called HelperCounter.countAll(), the result would be 500, because the helper follows its caller.
Chains of several classes
The same rule applies at every hop. The table shows what happens to the sharing mode as control moves down a chain. "Effective mode" is what the last class's own statements run under.
| Chain (entry point first) | Effective mode in the last class |
|---|---|
| `with sharing` → no keyword | `with sharing` |
| `without sharing` → no keyword | `without sharing` |
| `with sharing` → `without sharing` → no keyword | `without sharing` |
| `without sharing` → `with sharing` → no keyword | `with sharing` |
| `with sharing` → `without sharing` → `with sharing` | `with sharing` |
| no keyword as the entry point, then no keyword | Not enforced (system context) |
The nearest class above you that declares a keyword sets your mode, if you declare nothing yourself. If no class in the chain declares a keyword, nothing enforces sharing.
The entry point has no caller
Every transaction starts somewhere: a Visualforce or Lightning controller action, an @AuraEnabled method, a REST resource, a trigger, a batch job, or a scheduled job. This first class has no caller to inherit from.
- If it declares
with sharingorwithout sharing, that keyword applies. - If it declares nothing, it runs in system context, so record-level sharing is not enforced. This is the default behavior covered earlier in the tutorial.
- If it declares
inherited sharing, it runs aswith sharing, because there is no caller to inherit from.
This is why a keyword-less utility class is safe or unsafe depending on who calls it. A keyword-less controller is unsafe no matter what.
public class LooseController {
@AuraEnabled
public static List<Account> getAccounts() {
// No keyword and no caller: system context, so every Account is returned
return [SELECT Id, Name FROM Account];
}
}Data already in memory stays there
Sharing is checked when a statement runs. It is not attached to the records.
If AdminCounter returned a List<Case> rather than a count, the with sharing caller would hold all 500 records. The caller's keyword would not filter that list afterward. It only affects the caller's own queries and DML.
public without sharing class AdminReader {
public static List<Case> getAllCases() {
return [SELECT Id, Subject FROM Case];
}
}
public with sharing class LeakyController {
public static List<Case> getCases() {
// The list holds every Case, even though this class is with sharing
return AdminReader.getAllCases();
}
}LeakyController looks safe because of its keyword, but it hands the user data the user could not query directly. When you call a without sharing class from a with sharing one, check what the callee returns.
The same logic applies to DML. A with sharing class that passes records to a without sharing method that updates them performs the update without record-level checks.
Other edge cases
- Constructors and static initializers: these run under the keyword of the class that defines them, the same as methods.
- Dynamic SOQL:
Database.queryfollows the class that contains the call. Building the query string in another class does not change this. - Triggers: a trigger has no sharing keyword and runs in system context. If it calls a
with sharingclass, that class enforces sharing for its own statements. The trigger's own DML is not filtered. - Inner classes: an inner class does not take its mode from the outer class that contains it. The next chapters on
inherited sharingand inner classes explain this in detail. - Mode switching is not visible in the call:
AdminCounter.countAll()looks like any other call. You must open the callee to see its keyword.
Reading a chain during review
When you review a call chain, work through it step by step:
- Find the entry point and note its keyword. No keyword means system context.
- For each class called next, check whether it declares a keyword. If it does, that keyword applies inside it. If it doesn't, carry the previous mode forward.
- At each boundary where a
without sharingclass returns data, check whether the caller exposes that data to the user. - Repeat for every query and DML statement, not for the transaction as a whole.
Exercise
A user can see 5 of 100 Opportunity records. Class A (with sharing) calls class B (without sharing), which calls class C (no keyword) and then runs its own query. Class C then runs a query, and A runs a final query after B returns. How many records does each of these queries see: B's own query, C's query, and A's final query? What would C see if A (with sharing) called C directly?
Show answer
B's own query sees 100, because B declares without sharing. C's query sees 100, because C declares nothing and follows its caller B. A's final query sees 5, because control returned to A and its with sharing mode applies again. If A called C directly, C would see 5, because it would follow the with sharing caller.
Exercise
A with sharing controller calls a without sharing class that returns a List<Case> from an unfiltered query. The controller returns that list to a Lightning component. Does the controller's with sharing keyword protect the user from seeing records they shouldn't? Explain.
Show answer
No. Sharing is applied when each statement runs, not stored on the records. The unfiltered query ran in the without sharing class, so the list already holds every Case. The controller's keyword only affects the controller's own queries and DML, so it does not filter the list it received.