![]() |
VOOZH | about |
Role hierarchy in Salesforce is a powerful mechanism that defines the access and visibility of records within an organization. By default, role hierarchies allow users higher in the hierarchy to access records owned by users below them. While this feature ensures data transparency and accessibility for managers and team leads, there are scenarios where you may want to override this behavior to enforce stricter record-level access controls.
In this article, we will explore how role hierarchy works, why overriding it may be necessary, and the various methods to achieve this in Salesforce. This guide is tailored for intermediate to advanced developers, covering both declarative and programmatic approaches with detailed examples.
Role hierarchies in Salesforce are part of the organization-wide defaults (OWD) and define record visibility based on a user’s role. For example:
This default behavior simplifies data access but may not align with all business requirements, such as when sensitive data must be restricted even from higher roles.
There are several scenarios where you might need to override role hierarchy:
Salesforce allows you to disable the "Grant Access Using Hierarchies" setting on custom objects. This ensures that the role hierarchy does not apply to the object, and record-level access must be explicitly granted using sharing rules, manual sharing, or Apex sharing.
Sharing rules allow you to define specific conditions to grant access to records, overriding the default role hierarchy behavior.
Example: Suppose you have a custom object Confidential_Document__c with sensitive information. You want to grant access only to specific roles, irrespective of the role hierarchy.
1. Set OWD to Private:
Confidential_Document__c to Private.2. Create a Sharing Rule:
Confidential_Document__c sharing settings, click New Sharing Rule.Define the rule:
Confidential_Level__c = 'High'.Apex sharing provides a programmatic way to grant or revoke access to records, allowing you to implement complex logic that overrides the default role hierarchy.
Example: Let’s assume you need to share records of Confidential_Document__c with specific users based on custom criteria, such as department or seniority level.
Confidential_Document__Share object is used to define sharing settings for the custom object.ParentId: Specifies the record to be shared.UserOrGroupId: Specifies the user or group with whom the record is shared.RowCause: Specifies the reason for sharing (e.g., Manual or a custom reason).You can invoke the above logic in a trigger:
If your goal is to restrict access to specific fields rather than entire records, consider using Field-Level Security or Validation Rules. These features complement role hierarchy overrides by providing finer control.
Overriding role hierarchy in Salesforce is a crucial capability for organizations requiring strict control over record access. By disabling "Grant Access Using Hierarchies", leveraging sharing rules, and employing Apex sharing, you can enforce complex access controls tailored to your business needs.
Understanding these techniques and implementing them correctly ensures data security and compliance while maintaining flexibility in how records are shared within your Salesforce org. With the right combination of declarative and programmatic tools, you can customize Salesforce access controls to suit any scenario.