Sub topic 3
What the sharing keywords control
This chapter assumes you know basic Apex (classes, methods, SOQL), how record access is built from organization-wide defaults, roles, and sharing rules, and what profiles and permission sets are.
The question the keywords answer
When Apex runs a query, Salesforce has to decide which records the code may see. By default, Apex runs in system context, so the running user's record access does not limit it. The sharing keywords let you change that for a class.
A sharing keyword is a modifier on a class declaration. It sets whether the class's SOQL queries and DML statements respect the running user's record-level sharing. Record-level sharing is the access that comes from organization-wide defaults, the role hierarchy, sharing rules, teams, and manual shares.
The three keywords
| Keyword | Effect on record-level sharing |
|---|---|
| `with sharing` | Enforced for the running user |
| `without sharing` | Not enforced. The code sees records regardless of the user's access |
| `inherited sharing` | Takes the mode of the calling class. When the class is the entry point of a transaction, it runs `with sharing` |
You can also declare no keyword. That case depends on how the class is called, so a later chapter covers it.
One query, two results
The same query can return different rows depending on the keyword. Suppose the running user can see 10 of the 100 Account records in the org.
public with sharing class AccountReader {
public static Integer countVisible() {
// Counts only the Accounts the running user can see: 10
return [SELECT COUNT() FROM Account];
}
}
public without sharing class AccountAdminReader {
public static Integer countAll() {
// Ignores record-level sharing: 100
return [SELECT COUNT() FROM Account];
}
}The queries are identical. Only the keyword on the enclosing class differs.
What the keywords do not control
Three limits are worth knowing now.
- Only record access. The keywords do not check object permissions (create, read, edit, delete) or field-level security. A
with sharingclass can still read a field the user's profile hides, unless you add user-mode enforcement. A later chapter covers this. - Only the class's own code. The keyword applies to the code in the class where it is declared. It is not a transaction-wide switch, and it does not turn sharing on for everything that class calls.
- Not the whole system. Some things stay in system mode even in a
with sharingclass. For example, Apex still has access to its own class definitions and managed code you call.
Where the keyword goes
The keyword sits in the class declaration, after the access modifier and before class:
public with sharing class OrderService {
public static List<Order> openOrders() {
return [SELECT Id, Status FROM Order WHERE Status = 'Draft'];
}
}Because the mode attaches to the class, you can read a class and know how its own queries and DML behave. That is the main reason to declare a keyword explicitly instead of leaving it out.
Exercise
A with sharing class runs [SELECT Id, Salary_c FROM Employeec]. The running user can see the Employeec record but has no field-level access to Salary_c. Does with sharing hide the field? What would you add to enforce the field permission?
Show answer
No. with sharing only filters which records the user can see, so the field is still returned. Add WITH USERMODE to the query (or use AccessLevel.USERMODE for DML) so object permissions and field-level security are enforced too.