Sub topic 5
without sharing: bypassing record-level sharing on purpose
This chapter assumes you know basic Apex syntax and SOQL. It also assumes you know how organization-wide defaults, roles, and sharing rules decide which records a user can see. The previous chapter covered with sharing. Here you learn the opposite keyword, when it is justified, and how to contain it.
What without sharing does
A class declared without sharing does not apply the running user's record-level sharing to its SOQL queries and DML. The code sees and changes records as if the user could access all of them.
public without sharing class AccountAdminReader {
public static List<Account> getAllAccounts() {
// Ignores record-level sharing for the running user
return [SELECT Id, Name FROM Account];
}
}If a user's role only gives access to 10 accounts, getAllAccounts() still returns every account in the org. This is the same behavior Apex has by default, because Apex runs in system context. The keyword states that intent explicitly.
The keyword applies per class
Each method runs under the sharing mode of the class that defines it. The caller's mode does not change that. A with sharing class that calls a without sharing class leaves record-level sharing off while the called method runs.
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() counts only the cases the user can see. countAllCases() counts every case, because the query runs inside CaseCounter. Control returns to CaseController afterward, and its own queries are filtered again.
When to use it
Use without sharing only when the code must act on records the user cannot see, and when that is the intended design. Typical cases:
- Counting or summing across all records to produce a total that does not expose individual rows.
- Checking for duplicates, where hidden records still matter.
- A system step, such as assigning ownership or writing an audit record on behalf of the user.
- A guest or community user action that needs to read one narrowly selected record.
In each case, keep the class small and give it one job. Do not mark a whole controller without sharing because one method needs it. Move that method into a dedicated class and call it from a with sharing controller.
Common errors
- Marking a controller
without sharingto fix a "missing records" bug. The real cause is usually a sharing rule or a role setup. Fix the access, not the class. - Assuming it bypasses field-level security. It does not, and it does not grant access either. Use user-mode operations when you need permissions enforced.
- Leaving it unexplained. Add a comment stating why the class bypasses sharing. Reviewers look for this.
- Forgetting that callers cannot restrict it. A
with sharingcaller does not protect you from awithout sharingcallee.
Exercise
A with sharing class named OrderController calls a static method in a without sharing class named OrderTotals. The method runs SELECT COUNT() FROM Order. The running user can see 5 of 200 orders. What does the count return, and why?
Show answer
It returns 200. The method runs under the sharing mode of the class that defines it, which is OrderTotals (without sharing), so record-level sharing is not applied to that query. The caller's with sharing keyword does not change this.
Exercise
A developer sets a whole controller to without sharing because one method must count all records. What is a safer design?
Show answer
Put the counting logic in a small, dedicated without sharing class that returns only the count. Keep the controller with sharing and have it call that class.