Sub topic 6
What happens when you declare no keyword
This chapter assumes you can read basic Apex (classes, static methods, SOQL). It also assumes you know how organization-wide defaults, roles, and sharing rules decide which records a user can see. Earlier chapters covered with sharing, without sharing, and why Apex runs in system context by default. This chapter covers the third option, which is declaring nothing.
The short answer
A class with no sharing keyword has no sharing mode of its own. What it does depends on how it was reached:
- If another class calls it, it runs under the sharing mode already in effect. A
with sharingcaller gives youwith sharingbehavior. Awithout sharingcaller gives youwithout sharingbehavior. - If it is the entry point of the transaction, it runs without sharing. Record-level sharing is not applied.
Many developers assume an omitted keyword means with sharing. It does not. The same class can show different behavior depending on who calls it.
public class AccountCounter {
public static Integer countAccounts() {
return [SELECT COUNT() FROM Account];
}
}AccountCounter declares no keyword, so the result of countAccounts() depends on its caller.
The two situations
Called from another class
When code in a class with a sharing keyword calls a method in a class with no keyword, the current sharing rules stay in effect. The method does not reset the mode. It continues with whatever the caller established.
public with sharing class SharedCaller {
public static Integer run() {
// AccountCounter has no keyword, so it runs with sharing here
return AccountCounter.countAccounts();
}
}
public without sharing class OpenCaller {
public static Integer run() {
// The same method now ignores record-level sharing
return AccountCounter.countAccounts();
}
}Suppose the running user can see 10 of the org's 50 accounts. SharedCaller.run() returns 10 and OpenCaller.run() returns 50. The code inside AccountCounter is identical in both calls.
As the entry point
An entry point is the first piece of Apex code that runs in a transaction. Examples include:
- A trigger
- An
@AuraEnabledmethod - A Visualforce controller action
- An
@RestResourcemethod - A
Queueable,Batchable, orSchedulablemethod that the platform invokes - Code run from the Developer Console as anonymous Apex
If the entry-point class declares no keyword, nothing sets a mode, and the class runs without sharing. Suppose a Lightning component calls this class:
public class AccountListController {
@AuraEnabled(cacheable=true)
public static List<Account> getAccounts() {
// No keyword and this is the entry point:
// the query returns Accounts the user may not be able to see
return [SELECT Id, Name FROM Account];
}
}The component shows every account that matches the query, including records the user has no access to. Nothing in the code looks wrong, and no error is raised. This is why an omitted keyword is a security risk.
Why the behavior is a trap
The same source file behaves in two ways. You cannot tell from reading AccountCounter alone whether it enforces sharing. You have to trace every caller. A class that looks safe in code review can become unsafe when a developer later adds a new entry point that calls it.
The exposure also changes without any edit to the class. If a with sharing class calls AccountCounter today and someone later wires AccountCounter straight to an @AuraEnabled method, its behavior flips from limited to unlimited.
How it differs from inherited sharing
inherited sharing is easy to confuse with an omitted keyword because both follow the caller. They differ only at the entry point.
| Situation | No keyword | `inherited sharing` |
|---|---|---|
| Called from a `with sharing` class | Runs with sharing | Runs with sharing |
| Called from a `without sharing` class | Runs without sharing | Runs without sharing |
| Entry point of the transaction | Runs without sharing | Runs with sharing |
The entry-point row is the whole difference. inherited sharing falls back to the safe mode and an omitted keyword falls back to the unsafe one. A later chapter covers inherited sharing in depth.
Decision flow
The diagram shows how Apex picks the mode for a method in a class with no keyword.
Tracing a longer call chain
Modes carry down the chain until a class sets a new one. Consider three classes:
public with sharing class OrderController {
public static Integer visibleCases() {
return CaseService.countCases();
}
}
public class CaseService {
public static Integer countCases() {
return CaseCounter.countAll();
}
}
public without sharing class CaseCounter {
public static Integer countAll() {
return [SELECT COUNT() FROM Case];
}
}Follow a call to OrderController.visibleCases() from an @AuraEnabled method elsewhere:
OrderControlleriswith sharing, so sharing is enforced.CaseServicehas no keyword and continues inwith sharing.CaseCounteriswithout sharing. The method runs under the mode of the class that defines it, so the query ignores record-level sharing.
The omitted keyword in the middle did not cause the exposure. The explicit without sharing in CaseCounter did. Had CaseCounter also omitted the keyword, the query would have run with sharing. Each method runs under the mode of the class that defines it. A class with no keyword defines no mode, so it keeps the current one.
What the omitted keyword does not change
Some parts of the picture stay the same whether or not you declare a keyword:
- Object permissions (CRUD) and field-level security are not enforced by any sharing keyword. A class with no keyword does not check them, and neither does
with sharing. See the chapter on sharing versus CRUD and FLS. - Code in a class with no keyword is not blocked from running. The platform does not warn you at save time or at run time.
- Inner classes do not inherit the keyword of the outer class. An inner class with no keyword follows the same caller-or-entry-point rule as a top-level class.
Finding classes with no keyword
You can search your source for class declarations that lack a keyword. This command lists them in an SFDX project:
grep -rLE "(with|without|inherited) sharing" force-app/main/default/classes --include="*.cls"grep -L prints files that do not contain a match. Test classes with @IsTest often appear in the output. They are usually fine to leave alone, and you can filter them out if you want a cleaner list. Static analysis tools such as Salesforce Code Analyzer (PMD) include a rule that flags classes with no sharing declaration. Salesforce's AppExchange security review also expects every class to declare its sharing mode, so unannotated classes cause review findings.
Guidelines for choosing instead of omitting
Always declare a keyword. The safe default is with sharing. Use inherited sharing for helper classes that must adapt to their caller. Use without sharing only for narrow, documented cases such as a system task that must read records the user cannot see.
When you find an existing class with no keyword, do not change it blindly:
- List the entry points that reach it.
- Decide which behavior each entry point needs.
- Add
with sharingif all callers should enforce sharing. Addinherited sharingif callers differ and the safe fallback is right. - Test as a user with limited access, before and after the change, because adding
with sharingcan hide records that a screen or process relied on.
Summary of the behavior
- No keyword means no mode of its own.
- Called from another class, the class uses the caller's mode.
- As the entry point, the class runs without sharing.
inherited sharingbehaves the same except at the entry point, where it runs with sharing.- Declare a keyword on every class so the behavior does not depend on who calls it.
Exercise
A class named LeadFinder has no sharing keyword and contains [SELECT Id FROM Lead]. Give the sharing mode it uses in each case: (a) an @AuraEnabled method in a with sharing class calls it, (b) a trigger handler declared without sharing calls it, (c) it is itself the controller class for an @AuraEnabled method.
Show answer
(a) With sharing, because it keeps the mode of its caller. (b) Without sharing, for the same reason. (c) Without sharing, because it is the entry point and no mode has been set. This is why you should declare with sharing or inherited sharing on LeadFinder.
Exercise
How does a class with no keyword differ from a class declared inherited sharing when it is the entry point of a transaction, and which one is safer?
Show answer
A class with no keyword runs without sharing at the entry point. A class declared inherited sharing runs with sharing there. inherited sharing is safer because its fallback enforces the user's record access.
Exercise
In the chain OrderController (with sharing) calls CaseService (no keyword), which calls CaseCounter (also no keyword), which runs a SOQL query, which sharing mode applies to the query, and why?
Show answer
With sharing. Neither CaseService nor CaseCounter sets a mode, so each keeps the mode set by OrderController.