Sub topic 1
Record-level sharing in a nutshell
Apex sharing keywords only make sense if you know what they enforce. This chapter reviews how Salesforce decides which records a user can see and edit. The next chapters cover how Apex interacts with that decision.
Prerequisites
This chapter assumes you already have the following:
- Basic Apex syntax: classes, methods, and SOQL queries.
- Familiarity with Salesforce record access at the admin level: organization-wide defaults, roles, and sharing rules.
- Awareness of profiles and permission sets.
You don't need to have written any Apex that deals with sharing. The goal here is a precise vocabulary, so the later chapters on with sharing, without sharing, and inherited sharing can use these terms without explanation.
Three layers of access
Salesforce checks three separate questions when a user touches data:
- Object permissions (CRUD). Can this user create, read, edit, or delete records of this object at all? Profiles and permission sets grant these.
- Field-level security (FLS). Can this user see or edit this particular field? Profiles and permission sets grant these too.
- Record-level sharing. Can this user see or edit this particular record? Ownership, the role hierarchy, sharing rules, teams, and manual shares grant this.
A user needs all three to work with a field value on a record. If any layer says no, the access is denied. The layers don't substitute for each other. Read access to Account as an object doesn't mean the user can see every Account record.
| Layer | Question | Granted by | Scope |
|---|---|---|---|
| Object permissions | Can the user work with this object? | Profiles, permission sets | Whole object |
| Field-level security | Can the user work with this field? | Profiles, permission sets | One field across all records |
| Record-level sharing | Can the user work with this record? | Ownership, hierarchy, rules, shares | One record |
This chapter is about the third row. The sharing keywords in Apex affect only that layer. A later chapter returns to the first two.
Start closed, then open up
Record-level sharing works as a baseline that you can loosen but never tighten below.
The baseline is the organization-wide default (OWD), set per object. It defines the most restrictive access any user has to records they don't own. The common settings are:
- Private: users see only their own records, plus whatever other mechanisms add.
- Public Read Only: everyone can see all records, but only owners and users with other grants can edit them.
- Public Read/Write: everyone can see and edit all records.
- Controlled by Parent: access follows the parent record in a master-detail relationship.
Some objects offer extra settings, such as Public Read/Write/Transfer on Leads and Cases. Which settings you see depends on the object.
Everything else widens access from that baseline. Sharing mechanisms never remove access that the OWD allows. For example, you can't use a sharing rule to hide a record from a user when the OWD is Public Read Only.
Mechanisms that grant access
Once the OWD is Private or Public Read Only, these mechanisms open records to more users:
- Ownership. The record owner always has full access to their own records. Owners can also be queues on some objects.
- Role hierarchy. Users higher in the hierarchy get access to records owned by users below them. On custom objects, an admin can turn this off with the Grant Access Using Hierarchies setting.
- Sharing rules. Admin-defined rules share records with groups, roles, or territories. They're based on the record owner or on record criteria.
- Teams. Account teams, opportunity teams, and case teams give named users access to a specific record.
- Manual sharing. The owner, or a user with Full Access, shares one record with a user or group by hand. Manual shares are removed when the record owner changes.
- Apex managed sharing. Code inserts rows into the object's share table, with a custom Apex sharing reason. This is available for custom objects.
- Territory management. Territory assignments can grant access to accounts and related records.
Two kinds of access sit outside this model. Users with the View All Data or Modify All Data system permissions skip record-level sharing entirely. The same is true of users with View All or Modify All on a single object. Admins sometimes grant these without realizing they make sharing rules irrelevant for those users.
Share tables: where grants are stored
Salesforce stores record-level grants in a share table for each object where sharing applies. A share table holds one row per grant. The naming follows a pattern:
- Standard objects use the object name plus
Share, such asAccountShareorOpportunityShare. - Custom objects use
ObjectName__Share.
Each row records which record is shared, who it's shared with, how much access they get, and why. The why is the row cause. Examples are Owner, Manual, Rule, Team, and custom Apex sharing reasons.
You can query a share table like any other object. This is useful for debugging why a user sees a record.
Id accountId = [SELECT Id FROM Account LIMIT 1].Id;
List<AccountShare> shares = [
SELECT UserOrGroupId, AccountAccessLevel, RowCause
FROM AccountShare
WHERE AccountId = :accountId
];
for (AccountShare s : shares) {
System.debug(
s.UserOrGroupId + ' has ' + s.AccountAccessLevel
+ ' because of ' + s.RowCause
);
}If your org has no accounts, the first query throws an exception, so run it in a sandbox with test data. The UserOrGroupId can point to a user or to a group, which is how role-based and rule-based grants are stored. Salesforce also recalculates and maintains these rows for you when roles, rules, or ownership change.
Share tables exist only when the OWD isn't Public Read/Write. When everyone can already edit everything, there is nothing to store.
Implicit sharing
Some access is granted by Salesforce itself, without any rule you configured. This is implicit sharing. The best-known cases:
- Access to an account's child records (contacts, opportunities, cases) can give a user read access to the parent account.
- Access to an account can give a user access to its children, within limits set by the account's role settings.
Implicit access doesn't appear as a row you can query in the usual way, so it can explain why a user sees a record that no share row justifies. Keep this in mind when debugging.
How a request is evaluated
The following diagram shows the order in which the layers apply for a user who requests a record.
The diagram simplifies some details, such as OWD settings of Public Read Only or Public Read/Write, which count as a grant in the record check. The principle is the same: a user passes through each layer independently.
A worked example
Suppose the Account OWD is Private, and your org has these users:
- Maya, a sales rep who owns Account A.
- Tom, a sales rep with no connection to Account A.
- Priya, Maya's manager, one level above her in the role hierarchy.
When each of them runs a report or opens the Accounts tab:
| User | Sees Account A? | Why |
|---|---|---|
| Maya | Yes | She is the owner. |
| Priya | Yes | The role hierarchy gives managers access to subordinates' records. |
| Tom | No | The OWD is Private and no rule, team, or share applies. |
Now an admin adds a criteria-based sharing rule: share all Accounts in the "Enterprise" segment with Tom's role. If Account A matches, Tom now sees it, and a share row with row cause Rule appears. If the admin later removes the rule, the row disappears and Tom loses access. Nothing about Maya's or Priya's access changes.
This is the behavior the Apex keywords will either respect or ignore, which is the subject of the next chapter.
What this means for Apex
Everything above describes what happens when a user works in the UI, a report, or an API client logged in as that user. Apex is different. By default, Apex code doesn't consult record-level sharing for the running user. The next chapter explains why, and the chapters after it show how with sharing, without sharing, and inherited sharing change that.
For now, keep three facts:
- Record-level sharing is one of three independent access layers, alongside object permissions and field-level security.
- OWD sets the floor, and every other mechanism only adds access.
- Users with View All Data, Modify All Data, or the per-object View All and Modify All permissions are exempt from record-level sharing.
Exercises
Exercise
The Opportunity OWD is Private. A sales rep named Sam doesn't own an opportunity and has no team, rule, or manual share on it. Sam's manager is above him in the role hierarchy. Can Sam see the opportunity? Can Sam's manager see it if Sam owns it?
Show answer
Sam cannot see it, because the Private OWD gives access only to owners and to those granted access by hierarchy, rules, teams, or shares, and none apply to Sam. If Sam owns the opportunity, his manager can see it through the role hierarchy, unless hierarchy access has been disabled for that object.
Exercise
An admin gives a user the View All Data system permission. The Account OWD is Private and there are no sharing rules. What can the user see on Accounts, and why do sharing rules not matter for this user?
Show answer
The user can see all Account records. View All Data bypasses record-level sharing, so ownership, hierarchy, rules, and shares are not checked for this user. Object permissions and field-level security still apply in the usual way, so the user still needs read permission to the Account object and visibility of the fields.
Exercise
Which of the three access layers (object permissions, field-level security, record-level sharing) does a row in AccountShare belong to, and what does the RowCause column tell you?
Show answer
A row in AccountShare belongs to record-level sharing. RowCause tells you why the grant exists, for example Owner, Manual, Rule, or Team, which helps you debug why a user can see a record.