Sub topic 6
What happens when you declare no keyword
This chapter assumes you can read basic Apex and SOQL. It also assumes you know that with sharing applies the running user's record access and without sharing does not. Here you will see what a class does when it declares neither.
The omitted keyword is not a third mode
Leaving the keyword off does not give the class a sharing mode of its own. The class takes whatever sharing mode is already in effect when it is called. The result depends on who calls it.
| How the class is reached | Sharing behavior |
|---|---|
| Called from a `with sharing` class | Runs with sharing |
| Called from a `without sharing` class | Runs without sharing |
| Entry point of the transaction (trigger, Visualforce or Aura controller, REST endpoint, anonymous Apex) | Runs in system context, so record-level sharing is not applied |
The last row is the risky one. A class with no keyword that serves as an entry point behaves like without sharing. This is the system-context default described earlier in this tutorial.
Seeing it in code
Each top-level class goes in its own file. The first class below declares no keyword.
public class LegacyAccountQuery {
public static Integer countAccounts() {
return [SELECT COUNT() FROM Account];
}
}Two other classes call it.
public with sharing class SalesController {
public static Integer visibleCount() {
// LegacyAccountQuery inherits "with sharing" here
return LegacyAccountQuery.countAccounts();
}
}public without sharing class NightlyReport {
public static Integer totalCount() {
// LegacyAccountQuery inherits "without sharing" here
return LegacyAccountQuery.countAccounts();
}
}countAccounts() runs the same query in both cases. Called through SalesController, it returns only the accounts the running user can see. Called through NightlyReport, it returns every account. If any code calls LegacyAccountQuery.countAccounts() directly as the entry point, it also returns every account.
Why this is a problem
The same method can return different data depending on the caller. That makes the class hard to review, because the code alone does not say which mode it uses. A later change to a caller can also change what the class exposes, and no one edits the class itself.
The inherited sharing keyword, covered in a later chapter, makes the caller-decides behavior explicit. The two differ at the entry point. inherited sharing runs with sharing there, and an omitted keyword runs in system context.
When you will meet it
You will mostly find classes with no keyword in older code, in generated code, and in quickly written helpers. When you find one, decide which behavior you want and declare it.
- If the class reads or writes data for a user, add
with sharing. - If it is a shared helper that should follow its caller, add
inherited sharing. - If it must see every record, add
without sharingand document why.
Exercise
A class named PriceLookup declares no sharing keyword. A Visualforce controller declared with sharing calls PriceLookup, which runs a SOQL query. Later a scheduled job declared without sharing calls the same method. Which records does the query return in each case, and what happens if an Apex REST endpoint is PriceLookup itself?
Show answer
Called from the with sharing controller, the query returns only records the running user can see. Called from the without sharing scheduled job, it returns all records. If PriceLookup is itself the entry point, as an Apex REST endpoint would be, it runs in system context and ignores record-level sharing. Declaring with sharing or inherited sharing on the class removes this ambiguity.