Sub topic 2
Why Apex ignores sharing unless told otherwise
This chapter assumes you can read basic Apex classes and SOQL queries. It also assumes you know how organization-wide defaults, roles, and sharing rules decide which records a user can see, and that you know what profiles and permission sets are.
Apex runs in system context
When a user opens a list view or a record page, Salesforce checks what that user may see. Apex code does not work this way by default. Apex runs in system context, also called system mode. In this mode the platform does not apply the running user's record access to the code's queries and DML (data changes such as insert and update).
Imagine an organization where the Account organization-wide default is Private. Maria owns three accounts and can see only those three in the UI. Consider this class:
public class AccountLookup {
public static List<Account> getAccounts() {
return [SELECT Id, Name FROM Account];
}
}If Maria triggers this method from a Lightning component, the query can return every account in the org, not only her three. The class declares no sharing keyword, so nothing tells Apex to respect her record access.
Why the platform works this way
System context is a design choice, not a bug. Apex often needs to do work the current user could not do directly. A trigger may need to update a parent record the user cannot see, and a batch job runs unattended. Automation such as assignment logic must see all the data to make correct decisions. Because Apex is trusted code written by developers, the platform leaves access decisions to the developer.
The cost is that safety is opt-in. If you want your code to respect the user's record access, you must say so.
What system context ignores and what the keywords cover
System context affects three separate things:
| Concern | What it controls | Enforced in Apex by default? |
|---|---|---|
| Record-level sharing | Which records the user can see or edit | No. The sharing keywords control it. |
| Object permissions (CRUD) | Whether the user can read, create, edit, or delete a type of object | No |
| Field-level security (FLS) | Whether the user can see or edit a specific field | No |
The sharing keywords (with sharing, without sharing, and inherited sharing) address only the first row. A later chapter explains the other two rows and how user-mode queries enforce them. For now, remember that a keyword changes which records come back and not which objects or fields you may touch.
The first fix: declare `with sharing`
Adding with sharing to the class makes its queries and DML respect the running user's record access:
public with sharing class AccountReader {
public static List<Account> getAccounts() {
// Returns only the Account records the running user can see
return [SELECT Id, Name FROM Account];
}
}The query text is identical to the first example. Only the class declaration changed, and that alone changes the result Maria sees from every account to her three.
Where system context does not apply
The UI's own standard features, such as list views and standard record pages, enforce the user's access without any Apex. Only your custom Apex code starts in system context. The sections that follow show what each keyword does, what happens when a class declares none, and how the mode carries across classes that call each other.
Exercise
A user can see only 3 of 500 Accounts. A custom Apex method runs [SELECT Id FROM Account] in a class with no sharing keyword. Why might it return more than 3 rows, and what is the smallest change that makes the query respect the user's record access?
Show answer
Apex runs in system context by default, so record-level sharing is not applied unless a keyword asks for it. Declaring the class with sharing makes the query return only records the user can see. This does not enforce object permissions or field-level security.