Sub topic 1
Object, field and record access: where sharing fits
Salesforce decides what a user can do with data in several independent layers. Org-wide defaults and the role hierarchy belong to only one of them, the record layer. This chapter maps all the layers so you know what sharing controls and what it does not. The later chapters rely on this map.
Prerequisites
This chapter assumes you already know:
- How to move around Setup and find a user, profile or permission set.
- The standard objects Account, Contact, Opportunity and Case.
- What a profile and a permission set are, at a basic level.
- Simple SOQL, meaning
SELECTfieldsFROMan objectWHEREa condition.
You do not need to know anything about roles, org-wide defaults or share tables yet. They are introduced in later chapters.
The layers of data access
A user's ability to work with data is the result of several checks. Each check answers a different question.
| Layer | Question it answers | Main tools |
|---|---|---|
| Organization | Can this user log in at all, from here and now? | Login hours, login IP ranges, authentication settings |
| Object | Can this user create, read, edit or delete records of this type? | Profiles, permission sets |
| Field | Can this user see or change this particular field? | Field-level security on profiles and permission sets |
| Record | Can this user see or change this particular row? | Ownership, org-wide defaults, role hierarchy, sharing rules, teams, manual sharing |
The layers are independent. A user can have full access to the Account object and still be unable to open a specific account. A user can also see an account but find that its Annual Revenue field is hidden.
Sharing is the record layer. It answers one question: given that the user is allowed to work with this object, which of its records are visible and editable?
Object permissions
Object permissions come from profiles and permission sets. They are Create, Read, Edit and Delete, plus the two override permissions View All and Modify All. A user's effective object permissions are the combination of their profile and every permission set assigned to them. Permissions only add up here. A permission set can grant access but cannot take it away.
Object permissions have no idea which record is involved. "Edit on Account" means "may edit accounts the record layer allows", not "may edit every account".
Field-level security
Field-level security sets each field to hidden, read-only or editable for each profile or permission set. It applies on top of record access. Even for a record you own, a hidden field stays hidden.
Field-level security does not decide which records you see. Hiding the Phone field does not hide any account, and sharing an account does not reveal hidden fields.
Record access and the sharing model
The record layer starts from a baseline and then opens access. These are the mechanisms, in the order the rest of this tutorial covers them:
- Record ownership. Every record has an owner, and the owner can always work with their own record, subject to object permissions.
- Org-wide defaults (OWD). The baseline access that users get to records they do not own. It is set per object.
- Role hierarchy. Opens access upward, so managers can reach records owned by people below them.
- Sharing rules, teams and manual sharing. Open access further for specific groups, people or single records.
Org-wide defaults are the most restrictive setting in the model. Every other mechanism can only widen access. You cannot use a sharing rule or the hierarchy to take access away. To make a record more private, you tighten the default and then open access back up where needed.
The baseline only matters for records a user does not own. For their own records, ownership already grants access.
Where the layers interact
Three interactions cause most of the confusion.
Object access is required but not enough
If a user lacks Read on Opportunity, no sharing setting will show them an opportunity. The object check comes first, and sharing cannot override a missing object permission. The reverse is also true. Read on Opportunity does not show them every opportunity.
Two permissions bypass sharing
View All and Modify All on an object let a user read or edit every record of that object, whatever the sharing model says. The user-level equivalents, View All Data and Modify All Data, do the same for every object. These are administrative overrides, so they are granted sparingly. A later chapter covers them.
Apex runs outside the layers unless told otherwise
Apex code normally runs in system mode. It ignores the running user's object permissions, field-level security and sharing. The with sharing keyword on a class makes its queries respect record sharing:
public with sharing class AccountReader {
public static List<Account> visibleAccounts() {
return [SELECT Id, Name, OwnerId FROM Account ORDER BY Name LIMIT 50];
}
}The keyword enforces only the record layer. It does not enforce object or field permissions. A class that is with sharing can still read a field the user cannot see, unless you add field-level checks. Do not assume with sharing makes a class fully safe.
Seeing the layers at work
You can check the record layer directly. The UserRecordAccess object reports whether a given user can read, edit or delete a given record, after all sharing mechanisms are applied:
SELECT RecordId, HasReadAccess, HasEditAccess, HasDeleteAccess
FROM UserRecordAccess
WHERE UserId = '005XXXXXXXXXXXXXXX'
AND RecordId = '001XXXXXXXXXXXXXXX'Replace the placeholder IDs with a real user ID and a real record ID. The query needs both conditions, a single user and a single record, or a small set of records. Run it in the Developer Console Query Editor or in Apex. If HasReadAccess is false for a user who has object Read permission, the problem is in the record layer.
The result reports the combined outcome of the record layer. It does not say which mechanism granted the access. A later chapter shows how to find that with share tables.
Worked example: three users, one account
Suppose the Account default is Private. Priya owns an account called Northwind. Three users try to open it.
- Priya owns the record. She has access through ownership, assuming her profile gives her Read on Account.
- Marcus has Read, Edit and Delete on Account through his profile, but he is not Priya's manager and no sharing applies. He cannot see Northwind. The object layer says yes, but the record layer says no.
- Dana has View All on Account through a permission set. She sees Northwind even though the default is Private and she is not in Priya's chain, because View All bypasses sharing.
Marcus's case is the most common source of "but he has the permission" tickets.
Common mistakes
- Fixing a record problem with an object permission. Granting Edit on Account to a user who cannot see one account changes nothing, because the record check still fails. Check record access first.
- Using profiles to limit which records users see. Profiles control object and field access. Limiting rows is the job of org-wide defaults and the mechanisms that open them.
- Expecting field-level security to hide a record. It hides fields only.
- Treating
with sharingas a complete security check. It enforces record sharing and nothing else. - Granting View All or Modify All for convenience. These permissions silently remove the protection the sharing model provides for that object.
Summary of the model
Object permissions say what kind of operations a user may perform on a type of record. Field-level security says which fields they may use. Sharing says which records they may reach. A user succeeds only when the object check and the record check both pass, and then field-level security shapes what appears. The next chapters take the record layer apart, starting with ownership.
Exercise
A user has Read and Edit on the Opportunity object through their profile. The Opportunity default is Private. They cannot find an opportunity owned by a colleague in another team, who is neither above nor below them in the role hierarchy. Which layer blocks them, and name two ways to give them access that do not change their profile.
Show answer
The record layer blocks them. Object permission is already granted, but under a Private default they get no access to records they do not own, and nothing else shares the record with them. Options that leave the profile alone include a sharing rule, manual sharing of that record, adding them to the opportunity team, or giving View All on Opportunity through a permission set. View All is the broadest of these because it opens every opportunity.
Exercise
A developer writes a with sharing Apex class that returns Contact records, including a field that is hidden from the running user by field-level security. Does with sharing stop the hidden field from being returned? Explain.
Show answer
No. The with sharing keyword enforces only record-level sharing. It does not enforce object permissions or field-level security, so the field can still be returned. The developer must add separate field-level checks.