Sub topic 1
Record-level sharing in a nutshell
Prerequisites
This chapter assumes you know the following:
- Basic Apex syntax: classes, methods, and SOQL queries.
- Salesforce record access at a working level: organization-wide defaults, roles, and sharing rules.
- What profiles and permission sets are.
You do not need to have used the Apex sharing keywords yet. This chapter sets up the model that the keywords build on.
What record-level sharing decides
Salesforce answers two separate questions when a user touches data:
- Can this user work with this kind of object, and with these fields? Profiles and permission sets answer this.
- Can this user see or edit this particular record? Record-level sharing answers this.
This chapter covers the second question. Two users with the same profile can see different Account records, because record access is calculated per user and per record.
How a user gets access to a record
A user can reach a record through several layers. Each layer can only open access wider than the one before it.
| Layer | What it does |
|---|---|
| Organization-wide defaults (OWD) | Set the baseline access for each object, such as Private or Public Read Only. |
| Role hierarchy | Gives users access to the records owned by users below them in the hierarchy. |
| Sharing rules | Open access to groups of records based on owner or field criteria. |
| Manual and team sharing | Grant access to a single record for a specific user or group. |
| Record ownership | The owner of a record has full access to it. |
The OWD is the floor. If an object is Private, a user sees only the records they own, plus whatever the other layers add.
Where Apex fits in
When a user opens a list view or a record page, Salesforce applies these rules for you. Apex code is different. By default, Apex runs in system context. That means a SOQL query in Apex does not filter records by the running user's sharing.
The same query can therefore return different results depending on how the class is declared. The next chapters cover why this happens and how the sharing keywords change it. For now, keep one point in mind: the sharing model above exists in the platform, and Apex has to opt in to it.
Here is a small example of the opt-in. This class applies the running user's record access to its query.
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];
}
}Suppose the Account OWD is Private. A sales user who owns 20 accounts and has no other access calls AccountReader.getAccounts(). The query returns those 20 accounts, even if the org holds thousands. A user higher in the role hierarchy would get their own accounts plus those owned by users beneath them.
Check your understanding
Exercise
An org sets the Account organization-wide default to Private. A user owns 5 accounts, and a sharing rule gives that user read access to 10 more. Without considering Apex, how many accounts can the user see, and which layers produced that access?
Show answer
The user can see 15 accounts. Five come from record ownership, and 10 come from the sharing rule. The Private default sets the baseline, and the other layers add access on top of it.
Exercise
Which question does record-level sharing answer: "Can this user use the Account object?" or "Can this user see this particular Account record?"
Show answer
It answers "Can this user see this particular Account record?" Whether the user can use the Account object, and which of its fields, is decided by profiles and permission sets.