Sub topic 14
Sharing Behavior in Apex: with sharing and share records
This chapter assumes you know org-wide defaults (OWD), the role hierarchy, and basic Apex and SOQL. It covers two things: how Apex decides which records a query can see, and how to grant access by writing share rows.
Apex runs in system mode by default
Apex normally runs in system context. Object permissions, field-level security and record sharing are not enforced unless you ask for them. The sharing keyword on a class controls only record-level sharing. It does not enforce object or field permissions.
| Declaration | Record sharing behavior |
|---|---|
| `with sharing` | Enforces the running user's sharing: OWD, role hierarchy, rules, teams and manual shares |
| `without sharing` | Ignores sharing and returns all records |
| `inherited sharing` | Uses the sharing mode of the calling class |
| No keyword | Inherits from the caller. As an entry point, it runs without sharing |
Prefer an explicit keyword. Use with sharing unless you have a reason not to.
public with sharing class AccountReader {
public static List<Account> visibleAccounts() {
return [SELECT Id, Name, OwnerId FROM Account ORDER BY Name LIMIT 50];
}
}If the OWD for Account is Private, this returns the user's own accounts. It also returns accounts owned by users below them in the hierarchy, and accounts shared through other mechanisms. A user with View All Accounts sees every account.
Share rows grant access in code
Each object with a Private or Public Read Only OWD has a share table. For Account it is AccountShare, and for a custom object it is MyObject__Share. Inserting a row there grants access.
Account acc = [SELECT Id FROM Account LIMIT 1];
AccountShare share = new AccountShare();
share.AccountId = acc.Id;
share.UserOrGroupId = UserInfo.getUserId();
share.AccountAccessLevel = 'Read';
share.OpportunityAccessLevel = 'None';
share.RowCause = Schema.AccountShare.RowCause.Manual;
insert share;This has the same effect as a user clicking the Sharing button. Three points matter:
- The access level can only raise access above the OWD and hierarchy. A share row cannot take access away.
- The running user must be allowed to share the record. Typically that means being the owner, a user above the owner in the hierarchy, or an administrator.
Manualrows are removed when the owner changes. For access that must survive, define an Apex sharing reason on a custom object and use it as theRowCause. Standard objects only support theManualcause.
Checking the result
After inserting a share row, check the access it produced. You can query the share table for the row and its cause.
SELECT Id, AccountId, UserOrGroupId, AccountAccessLevel, RowCause
FROM AccountShare
WHERE AccountId = '001XXXXXXXXXXXXXXX'You can also ask for the end result across all sharing mechanisms.
SELECT RecordId, HasReadAccess, HasEditAccess, HasDeleteAccess
FROM UserRecordAccess
WHERE UserId = '005XXXXXXXXXXXXXXX'
AND RecordId = '001XXXXXXXXXXXXXXX'Common errors
- Assuming a missing keyword means
with sharing. An entry-point class with no keyword runs without sharing. - Using
without sharingto make a failing query "work". This hides a sharing design problem and exposes data. - Inserting share rows for an object whose OWD is Public Read/Write. No share table exists to write to, because everyone already has access.
- Writing a
Manualshare and expecting it to survive an ownership change.
Exercise
A class has no sharing keyword and is called directly from a Lightning component. The Account OWD is Private. What does its Account query return for a sales rep, and what is the fix if the rep should only see their permitted records?
Show answer
With no keyword, an entry-point class runs without sharing, so the query returns all accounts, not only the rep's. Declare the class with sharing so the query applies the OWD, role hierarchy and other sharing for the running user.