Sub topic 16
Choosing the right mechanism and avoiding common mistakes
This chapter assumes you know the pieces covered earlier: org-wide defaults (OWD), the role hierarchy, public groups, sharing rules, manual sharing, account, opportunity and case teams, share tables, recalculation, and the troubleshooting steps. Here you will decide which mechanism fits a requirement and which mistakes to avoid.
The core principle: use the least complex mechanism
Every sharing mechanism adds rows to share tables, adds recalculation work, and adds one more place an administrator must look when access is unexpected. The goal is to meet the requirement with the mechanism that is easiest to predict and maintain.
Work from the broadest, most stable mechanism to the narrowest, most ad hoc one:
- Set the OWD to the most restrictive level that the business can accept.
- Use the role hierarchy where reporting lines match who should see what.
- Use sharing rules for access that follows a stable pattern.
- Use teams for access that varies record by record but follows a repeatable shape.
- Use manual sharing for one-off exceptions.
- Use Apex sharing only when no declarative option can express the requirement.
Comparing the mechanisms
| Mechanism | Best for | Who maintains it | Main cost |
|---|---|---|---|
| OWD | Setting the baseline for every record of an object | Administrator | Too open exposes data. Too closed forces more sharing. |
| Role hierarchy | Managers seeing their reports' records | Administrator | Roles that mirror org charts rather than data needs. |
| Owner-based rule | Sharing all records owned by a group of users | Administrator | Recalculation when membership or rules change. |
| Criteria-based rule | Sharing records that match field values | Administrator | Access changes as field values change. |
| Team | Collaborators that differ per record | Record owner or team members | Users must keep teams current. |
| Manual sharing | Rare exceptions | Record owner or user with full access | Shares are removed when ownership changes. |
| Apex sharing | Logic that clicks cannot express | Developer | Code to test, deploy, and maintain. |
Matching requirements to mechanisms
Consider three requirements and the reasoning behind each choice.
- "All sales managers must see their team's opportunities." This is the role hierarchy. If the OWD for Opportunity is Private, managers already see records owned by users below them, provided the object's Grant Access Using Hierarchies setting is on. No rule is needed.
- "Finance managers must read all overdue invoices, regardless of owner." This depends on a field value, so use a criteria-based rule. The Metadata API form from the earlier chapter, sharing
Status__cequalsOverduewith theFinance_Managersgroup, is the whole solution. Access appears when a record becomes overdue and disappears when the status changes. - "For this one large account, a solutions engineer and a legal reviewer need to collaborate." The people differ per account, so use an account team. Pick a team role and an access level for each member.
If someone asks for access to a single record for a single person, manual sharing is fine. If you find yourself doing it for dozens of records, that is a sign a rule or team is missing.
Common mistakes
Opening the OWD instead of adding sharing
When a few users cannot see a record, it is tempting to change the OWD to Public Read Only. That exposes every record of the object to every internal user. Keep the OWD restrictive and open access selectively. Remember that sharing can only grant more access than the OWD. It can never remove access, so a baseline that is too open cannot be tightened by adding rules.
Expecting sharing to override object permissions
Sharing controls which records a user can see. Profiles and permission sets control which objects and fields the user can work with. A user who lacks Read on Invoice will not see invoices no matter what share rows exist. When troubleshooting, check object permissions and field-level security first, then record access.
Building roles to solve sharing problems
Roles open access upward. Creating extra roles only to push access to certain users also gives access to everyone above them, and makes the hierarchy deep and hard to reason about. If the need is "these peers should see each other's records," use a public group and a sharing rule instead.
Depending on manual shares that disappear
Manual shares are removed when the record owner changes, and only the owner or a user with full access can create or remove them. A process that relies on them will quietly break after a reassignment. Use a rule or team when the access should survive ownership changes. If code must grant access in that situation, use a custom Apex sharing reason (a row cause you define for the object) instead of the Manual row cause, because custom reasons are kept when ownership changes.
Using criteria-based rules on volatile fields
A criteria-based rule re-evaluates when the field changes. If Status__c flips often, users gain and lose access repeatedly, and every change generates share-table work. Choose criteria on fields that change rarely, or accept that access will be dynamic and tell users so.
Forgetting that access is a union
A user's access to a record is the most permissive result of all mechanisms. Removing one rule does not remove access if a team, the hierarchy, or another rule also grants it. Before concluding that a revoke worked, check the other sources. The share query below lists every grant and its row cause.
SELECT Id, ParentId, UserOrGroupId, AccessLevel, RowCause
FROM Invoice__Share
WHERE ParentId = '<invoice record Id>'Rows with RowCause of Owner, Manual, or a rule cause tell you which mechanism supplied each grant. Role hierarchy access is implicit and does not always appear as a row for the managers, so also check the hierarchy by hand. For account teams, query the team directly.
SELECT Id, AccountId, UserId, TeamMemberRole, AccountAccessLevel
FROM AccountTeamMember
WHERE AccountId = '<account record Id>'Ignoring data volume and recalculation
Large changes, such as editing a rule, changing role membership, or changing the OWD, trigger recalculation across many records. Plan these changes for off-peak hours, change one thing at a time, and check recalculation status before making the next change. Skewed data makes this worse. A single owner with hundreds of thousands of records, or a single account with a very large number of children, can slow recalculation and cause lock errors. Spread ownership across several users or integration users where the business allows.
Writing Apex sharing without handling failures
Apex sharing is code you must test and maintain. Insert shares with the partial-success form so one failure does not abort the whole batch, and read the errors:
Invoice__Share share = new Invoice__Share();
share.ParentId = invoiceId;
share.UserOrGroupId = userId;
share.AccessLevel = 'Read';
share.RowCause = Schema.Invoice__Share.RowCause.Manual;
Database.SaveResult result = Database.insert(share, false);
if (!result.isSuccess()) {
System.debug(result.getErrors()[0].getMessage());
}Remember that this example uses the Manual row cause, so the share is removed on owner change. It also needs the OWD to be Private or Public Read Only, because shares on an object that is already Public Read/Write have no effect, and the share object is not available at all for some object configurations.
A review checklist before you ship a sharing design
- State the baseline: the OWD for each object involved, and why it cannot be tighter.
- List each access requirement and the mechanism that meets it, preferring the order given earlier.
- For each rule, name the target group, role, or user, and the access level granted.
- Identify what happens to the access when the owner changes, a field value changes, or a user changes role.
- Check for data skew and estimate the recalculation impact.
- Test with real user accounts using login-as, and confirm both the cases that should see the record and the cases that should not.
- Document the design, so the next administrator knows why each mechanism exists.
Exercises
Exercise
A support lead needs to read cases owned by any agent in the "EMEA Support" public group, and the Case OWD is Private. The lead is not in the agents' management chain. Which mechanism do you choose, and why not manual sharing?
Show answer
Use an owner-based sharing rule that shares cases owned by the EMEA Support group with the lead, preferably through a public group or role the lead belongs to. The requirement covers all current and future records, so manual sharing would need repeating for every case and would be lost when ownership changes.
Exercise
Your integration reassigns invoices to a new owner each quarter. Users complain that colleagues they shared invoices with now lose access. What is happening, and what are two ways to fix it?
Show answer
Manual shares (and Apex shares with the Manual row cause) are removed when the owner changes. Fix it by replacing them with a sharing rule or team that does not depend on the owner, or by having Apex create shares with a custom Apex sharing reason, which survives ownership changes.
Exercise
You remove a sharing rule, but a user can still open the record. List three other places you would check.
Show answer
Check the role hierarchy (the user may be above the owner), the record's team (account, opportunity, or case team), and the share table for manual or other rule-based rows. Also check whether the OWD already grants access, and whether the user holds View All or Modify All permissions on the object.