Sub topic 11
When the Hierarchy Is Not Enough: Sharing Rules, Teams and Manual Sharing
This chapter assumes you know how org-wide defaults (OWD), record ownership and the role hierarchy combine. You should also be comfortable with basic SOQL and with reading Apex.
The hierarchy only opens access upward, from a record owner to the users above that owner. Many real needs run sideways or across branches. Support agents need to see accounts owned by sales. Two regional managers need to see each other's opportunities. One consultant needs access to a single deal. Salesforce has four mechanisms for these cases, and all of them only add access.
The rule that governs all of them
OWD sets the floor for record access. Sharing rules, teams, manual shares and Apex shares can only raise access above that floor. None of them can take away access that OWD, ownership or the hierarchy already grants. To restrict access, you tighten the OWD and then open access back up where needed.
This has two consequences:
- If an object's OWD is Public Read/Write, there is nothing left to open for that object, so sharing rules and the Sharing button have no effect on it.
- A sharing grant can never be lower than what the user already has. If the OWD is Public Read Only, a rule that grants Read Only changes nothing.
Sharing rules
A sharing rule grants access to a set of records, in bulk, to a public group, a role, or a role with its subordinates. You define rules in Setup under Sharing Settings. They apply to existing records and to new ones, and Salesforce recalculates them when you change them.
There are two types:
- Owner-based rules share records owned by members of a source group. Example: share all accounts owned by the "EMEA Sales" role with the "EMEA Support" public group, Read Only.
- Criteria-based rules share records whose field values match a condition, regardless of owner. Example: share every opportunity where
StageequalsClosed WonandAmountis above a set value with the "Finance" group.
Each rule grants Read Only or Read/Write. For accounts, a rule also sets the access level to the account's related contacts, opportunities and cases.
Use sharing rules for stable patterns that you can describe once and that affect many records. They are the most maintainable option because they are declarative and visible in one place.
Limits and gotchas
- Criteria-based rules support only certain objects, such as Account, Contact, Lead, Opportunity, Case, Campaign and custom objects. Check the current documentation for your object.
- A criteria-based rule can only compare fields. It can't evaluate formulas that reference other records.
- Rules are limited in number per object. Many narrow rules are a sign that a group, role or team would model the need better.
- A rule shares to groups and roles, not to arbitrary individual users. To target one person, put that user in a public group.
- Large changes trigger background recalculation, so new access may not appear instantly. Check the Sharing Settings page or the Background Jobs page for status.
Teams
A team is a per-record list of users who get access because they work on that record. Salesforce provides:
- Account teams, where each member has a team role and an access level for the account. You can also set access to the account's contacts, opportunities and cases.
- Opportunity teams, where each member has a role and an access level to that opportunity.
- Case teams, where each member has a case team role that carries Read Only or Read/Write access.
Teams must be enabled in Setup before they appear. Team members are added by the record owner, a user above the owner in the hierarchy, or an administrator. Users can also set up a default account or opportunity team that is copied to new records they own.
Use a team when the people change from record to record and belong to a working group, not a department. A sharing rule can't express "Priya is the solutions engineer on this deal".
Team access is stored as share rows with the row cause Team. Team members stay on the record when ownership changes.
Manual sharing
Manual sharing is the Sharing button on a record detail page. A user can share the record with another user, a group, a role, or a role with subordinates, at Read Only or Read/Write.
The button only appears when the OWD is Private or Public Read Only, since Public Read/Write leaves nothing to open. It is available to the record owner, to users above the owner in the hierarchy, and to administrators.
Manual shares are fragile by design. When the record owner changes, Salesforce deletes the manual shares on that record. If a record changes hands often, manual sharing is the wrong tool.
Comparing the mechanisms
| Mechanism | Who defines it | Scale | Survives owner change | Typical row cause |
|---|---|---|---|---|
| Sharing rule | Administrator | Many records | Recalculated automatically | `Rule` |
| Team | Owner, manager or administrator | One record, several people | Yes | `Team` |
| Manual share | Owner, manager or administrator | One record, one-off | No, deleted | `Manual` |
| Apex managed sharing | Developer | Any, driven by code | Yes | Custom sharing reason |
Seeing the result in the share table
Each of these mechanisms writes rows into the object's share table. For accounts the table is AccountShare. For a custom object it is MyObject__Share. The RowCause field tells you why each user has access.
SELECT Id, AccountId, UserOrGroupId, AccountAccessLevel, RowCause
FROM AccountShare
WHERE AccountId = '001XXXXXXXXXXXXXXX'You will usually see an Owner row, plus Rule, Team or Manual rows depending on how access was granted. The UserOrGroupId can be a user or a group, so a Rule row often points to a group, not a person.
Two limits apply to this query:
- Role hierarchy access is not stored as share rows. It is computed. A manager who sees a record through the hierarchy will not appear in
AccountShare. - Public Read Only and Public Read/Write access are also not stored as rows. They come from the OWD.
To see the final answer for one user, including all of these sources, query UserRecordAccess:
SELECT RecordId, HasReadAccess, HasEditAccess, HasDeleteAccess
FROM UserRecordAccess
WHERE UserId = '005XXXXXXXXXXXXXXX'
AND RecordId = '001XXXXXXXXXXXXXXX'Use AccountShare to find out why access exists. Use UserRecordAccess to find out whether it exists.
Apex managed sharing
When the access logic depends on something a rule can't express, such as a lookup to another record, you can create share rows in Apex. This is the programmatic version of the Sharing button:
public with sharing class AccountSharer {
public static void grantRead(Id accountId, Id userId) {
AccountShare share = new AccountShare();
share.AccountId = accountId;
share.UserOrGroupId = userId;
share.AccountAccessLevel = 'Read';
share.OpportunityAccessLevel = 'None';
share.RowCause = Schema.AccountShare.RowCause.Manual;
insert share;
}
}Choose a user who does not already have equal or higher access. For example, inserting a share for the record owner fails, because the owner's access can't be lowered or duplicated by a share row.
Standard objects only allow the Manual row cause for Apex-created shares. A Manual row created in code is deleted on owner change, exactly like one created with the button. On custom objects you can define an Apex sharing reason in Setup. Rows with that reason survive owner changes and can only be changed by code or by users with Modify All Data. Prefer this for logic that must persist.
The class above is declared with sharing, but that keyword controls which records its queries see. It does not stop it from writing share rows. Writing to a share table requires the right permissions on the parent record.
Exercises
Exercise
The Account OWD is Private. A support group needs Read Only access to every account where Type equals Customer, no matter who owns it. Which mechanism fits, and why is a manual share a poor choice?
Show answer
A criteria-based sharing rule on Account, sharing with the support group at Read Only. The need is a standing pattern across many records, and the rule applies to existing and new records automatically. Manual shares are one record at a time, need someone to click the Sharing button each time, and are deleted when a record's owner changes.
Exercise
A regional manager sees an account through the role hierarchy. You query AccountShare for that account and find no row for the manager. Is something wrong?
Show answer
No. Hierarchy access is calculated, not stored as share rows. To confirm the manager's access, query UserRecordAccess for that user and record and check HasReadAccess.