Sub topic 7
Which sharing mode applies when classes call each other
This chapter assumes you can read basic Apex and SOQL. It also assumes you know what with sharing and without sharing do on a single class. Real code spreads logic across several classes, so you need to know which keyword is in force at each line.
The rule: the defining class decides
Sharing is not a setting on the whole transaction. Each method runs under the sharing mode of the class that defines it. The class that calls it does not change that mode, with two exceptions covered below.
Here is the example from the topic plan:
public with sharing class CaseController {
public static Integer countVisibleCases() {
return [SELECT COUNT() FROM Case];
}
public static Integer countAllCases() {
// The method runs under the sharing mode of the class that defines it
return CaseCounter.countAll();
}
}
public without sharing class CaseCounter {
public static Integer countAll() {
return [SELECT COUNT() FROM Case];
}
}countVisibleCases runs in CaseController, so the query respects the user's record access. countAllCases also starts in CaseController. It then calls CaseCounter.countAll, which is defined in a without sharing class, so that query ignores record-level sharing.
In the opposite direction, a without sharing class can call a with sharing class. The called method then enforces the user's sharing.
Switching and returning
The mode switches when execution enters a method of another class. It switches back when that method returns. Code in the caller after the call is unaffected.
How the three declarations behave in a chain
| Declaration on the called class | Mode used when called from another class |
|---|---|
| `with sharing` | Always with sharing |
| `without sharing` | Always without sharing |
| `inherited sharing` | The caller's mode |
An explicit keyword is a fixed choice. The caller cannot override it. Only inherited sharing adopts the caller's mode. The next chapter covers it in detail.
The entry point matters
For inherited sharing, and for a class with no keyword, the mode comes from the entry point of the transaction. The entry point is the first class that runs, such as a controller, a REST resource, or a queueable.
- If the entry point is
with sharing, a chain of inheriting classes below it runs with sharing. - If the entry point is
without sharing, the same chain runs without sharing. - If an
inherited sharingclass is itself the entry point, it runswith sharing.
So the same utility class can behave differently depending on who reaches it. This is why you trace the chain from the entry point instead of reading one class alone.
Tracing a chain: a method
To find the mode for a given line of code, work in this order:
- Find the class that defines the method.
- If it declares
with sharingorwithout sharing, that is the mode. Stop. - If it declares
inherited sharing, or has no keyword, look at the class that called it and repeat the question for that caller. - If the class is the entry point, apply the entry-point rules above.
Exercise
A with sharing controller calls ReportHelper.run(). ReportHelper is declared without sharing and, inside run(), calls AccountQuery.fetch(). AccountQuery is declared inherited sharing. Which sharing mode applies to the query inside fetch(), and why?
Show answer
Without sharing. AccountQuery inherits the mode of its immediate caller, ReportHelper. ReportHelper is explicitly without sharing, so the controller's with sharing no longer applies by that point.