Sub topic 8
Grant Access Using Hierarchies and Custom Objects
This chapter covers the Grant Access Using Hierarchies setting. It is the switch that decides whether the role hierarchy opens record access upward for a given object. I assume you know how org-wide defaults (OWD) set baseline access, and that you know how roles form a hierarchy. You also need to be able to open Setup and read the Sharing Settings page.
What the setting does
The role hierarchy is not always active. Each object has a Grant Access Using Hierarchies option on the Sharing Settings page. When it is on, users in a role get the same access to a record as the users below them in the hierarchy. A sales manager can then read and edit records owned by their direct reports, because the manager's role sits above the reports' roles.
When it is off, the hierarchy is ignored for that object. A manager gets access only in these ways:
- They own the record.
- The OWD grants it. For example, the OWD is Public Read Only or Public Read/Write.
- A sharing rule, team or manual share grants it.
- They have a permission that bypasses sharing, such as View All or Modify All.
The hierarchy applies to more than ownership. If a record is shared with a subordinate through a sharing rule, the subordinate's managers also gain access, but only while this setting is on.
Standard objects and custom objects
The rule differs by object type.
| Object type | Can the setting be cleared? | Behavior |
|---|---|---|
| Standard objects (Account, Contact, Opportunity, Case, and so on) | No | The hierarchy is always applied. |
| Custom objects | Yes | The box is selected by default, and you can clear it. |
| Custom objects on the detail side of a master-detail relationship | Not applicable | Access is "Controlled by Parent", so the parent's setting decides. |
You can only turn the hierarchy off for a custom object whose OWD is not Controlled by Parent. The default for a new custom object is on, so a custom object with a Private OWD behaves like Opportunity unless you change it.
When to turn it off
Turning it off is for data that should not flow up the management chain. Typical cases:
- HR records such as performance reviews, salary adjustments or disciplinary notes. A manager's manager should not see these just because of the org chart.
- Records owned by a team whose managers are not meant to see the details, such as legal or compliance cases.
- Records where the role hierarchy models sales territory or reporting, but not who is entitled to the data.
Suppose an Employee_Review__c object has a Private OWD. With the setting on, every manager above the reviewer can read and edit the review. With it off, only the owner (and anyone you explicitly share with) can.
Changing the setting
To change the setting:
- In Setup, open Sharing Settings.
- In the Organization-Wide Defaults section, click Edit.
- Clear Grant Access Using Hierarchies for the custom object.
- Save.
Saving starts a sharing recalculation, because Salesforce has to add or remove implicit access for every affected record. In orgs with large data volumes, this can take a while and may lock certain sharing operations while it runs. Make the change outside peak hours, and test it in a sandbox first.
Why you cannot find hierarchy access in the share table
Access that comes from the hierarchy is implicit. Salesforce computes it from the role hierarchy at access time. It does not write a share row for each manager.
So if you query the share table for the custom object, you will see rows for the owner and for explicit shares, but not for managers who get access through roles. For a custom object Employee_Review__c, the share object is Employee_Review__Share:
SELECT Id, ParentId, UserOrGroupId, AccessLevel, RowCause
FROM Employee_Review__Share
WHERE ParentId = 'a01XXXXXXXXXXXXXXX'Expect a row with RowCause = 'Owner' and perhaps manual or rule-based rows. Do not expect a row for the owner's manager. To check whether the manager can actually reach the record, query UserRecordAccess, which evaluates every mechanism, including the hierarchy:
SELECT RecordId, HasReadAccess, HasEditAccess, HasDeleteAccess
FROM UserRecordAccess
WHERE UserId = '005XXXXXXXXXXXXXXX'
AND RecordId = 'a01XXXXXXXXXXXXXXX'Run it for the manager's user ID before and after you clear the setting. With the setting on, a manager above the owner returns HasReadAccess = true. With it off (and no other grant), it returns false.
What hierarchy access gives you
Hierarchy access mirrors what the subordinate has to the record, subject to the manager's own object permissions. A few points to keep in mind:
- Object permissions still apply. A manager needs Read on the object to see the record and Edit to change it. The hierarchy opens record access, not object access. If the manager's profile or permission sets lack Read on the object, the hierarchy does nothing.
- Both users need a role. If the record owner has no role, there is no position in the hierarchy to inherit from. The hierarchy opens access upward through roles, and users without a role are outside it.
- Access flows up, not sideways or down. A manager sees subordinates' records. Peers do not see each other's, and subordinates do not see their manager's.
- Delete is not guaranteed. Whether a manager can delete depends on the object permission and on the access level that the hierarchy conveys. Check
HasDeleteAccessrather than assuming.
Common mistakes
- Looking for the checkbox on a standard object. It cannot be cleared there, so use a Private OWD plus careful ownership if you need to restrict managers. For truly sensitive data, a custom object with the setting off is the usual answer.
- Assuming "off" means "locked down." Turning the hierarchy off does not hide records from users with View All, Modify All, View All Data or Modify All Data. Those bypass sharing entirely.
- Forgetting that shared access also stops flowing up. With the setting off, a manager of a user who received a manual share does not inherit that share.
- Expecting a share row. Verifying hierarchy access by looking at the share table finds nothing. Use
UserRecordAccessor Sharing on the record, as in the next sections of this tutorial.
Apex and the setting
Apex code does not change the hierarchy rules. A class declared with sharing evaluates the running user's access, including the hierarchy where it is on:
public with sharing class ReviewReader {
public static List<Employee_Review__c> visibleReviews() {
return [SELECT Id, Name, OwnerId FROM Employee_Review__c ORDER BY Name LIMIT 50];
}
}When a manager runs this and the setting is on, the query returns reviews owned by their reports. When it is off, it returns only the manager's own reviews and ones shared with them.
Exercise
An Employee_Review__c object has a Private OWD and Grant Access Using Hierarchies cleared. A director's profile gives Read and Edit on the object, and the director is two roles above the review's owner. The director cannot open a review. Name two ways to give the director access without turning the setting back on. Then name one permission that would already have given access, and say why the share table would not show a row for the director if the setting were on.
Show answer
Two options are a sharing rule (for example, share records owned by the owner's role with the director's role or group) and a manual share or team on the specific review. A permission that bypasses sharing is View All on the object, or View All Data for the user. If the setting were on, the director's access would be implicit and computed from the role hierarchy at access time, so no share row is written. You would confirm it with a UserRecordAccess query instead.
Exercise
Why can you not clear Grant Access Using Hierarchies for the Opportunity object, and what is the usual way to keep sensitive records away from managers?
Show answer
Standard objects always apply the hierarchy, so the checkbox cannot be cleared. For data that managers must not see, store it in a custom object with the setting cleared and a Private OWD. Share it only through explicit mechanisms.