Accessibility and data security are crucial in Salesforce. Sharing rules let you give people more access based on certain conditions, while the organization-wide defaults (OWD) set the minimum degree of access. When teams must work together across departments or hierarchies without sacrificing data integrity, these guidelines are crucial in real-world business situations. In this article we will be discussing “What Are Sharing Rules, and How Are They Tested in Salesforce?”
What Are Sharing Rules in Salesforce?
Salesforce uses sharing rules to automatically extend access to records based on roles, public groups, or territories. These rules ensure a secure baseline before granting access and apply only when the OWD is set to either Private or Read-Only.
Salesforce supports two types of sharing rules:
-
- Owner-based Sharing Rules: These grant access to records owned by specific roles or groups to other roles, groups, or territories.
- Criteria-based Sharing Rules: These provide access to records that meet certain field value criteria, such as “Region equals East” or “Status equals Approved.”
Why Are Sharing Rules Important?
Users usually need access to records they do not own in large businesses.You can automate this procedure more easily with sharing rules than with manual sharing. For example, while default settings may restrict access, a regional sales manager might still need to monitor opportunities that sales representatives in a specific region own.
By implementing sharing rules, administrators can:
-
- Avoid excessive manual sharing.
- Maintain data confidentiality while enabling collaboration.
- Simplify security management for large user bases.
So, what are sharing rules? And how are they tested?
How to Test Sharing Rules in Salesforce
You must test sharing rules to ensure that the proper users have the necessary access without overexposing sensitive information. A detailed tutorial on What are sharing rules? And how are they tested? can be found below:
1. Set Up the Test Environment
To begin, set the organization-wide defaults for the object you wish to test (Account, Opportunity, or Custom object) to the proper level (Private or Read-Only).
In order to test the sharing scenario, create the required roles, users, and public groups. To correctly validate access control, make sure users are part of the various levels of the role hierarchy.
2. Define and Implement Sharing Rules
Create sharing rules based on criteria or owners. In Setup, under Security Controls > Sharing Settings,
Example: Share all Account records where the Industry equals “Healthcare” with the “Support Team” public group.
3. Prepare Test Records
Create sample records that match and do not match the sharing rule criteria. For example:
-
- Record A: Industry = Healthcare (should be shared)
- Record B: Industry = Education (should NOT be shared)
Assign these records to users who are not in the same role or hierarchy as the users who should gain access through the sharing rule.
4. Log in as target users.
To access Salesforce as users who should (and shouldn’t) have access, use the “Login As” option. Verify visibility on relevant reports, list views, and tabs.
For example, if a user from the “Support Team” logs in, they should see Record A but not Record B.
5. Validate Permissions Using Apex (Optional)
For automated validation, write test classes in Apex using System.runAs() to simulate different user contexts. Use Test.startTest() and Test.stopTest() to measure system behavior.
Example code snippet:
System.runAs(supportUser) {
Account acc = [SELECT Id FROM Account WHERE Industry=’Healthcare’ LIMIT 1];
System.assertNotEquals(null, acc, ‘User should have access to Healthcare account’);
}
6. Check Audit Logs and Debug Logs
Review the debug logs to confirm that the sharing rule works as expected. Additionally, use the Sharing button on the record detail page (if enabled) to identify who has access and why.
Best Practices for Testing Sharing Rules
-
- To replicate production environments, test using data and user roles that are realistic.
- To identify records made for testing, use naming conventions.
- Audit sharing rules on a regular basis to prevent overexposure from outdated standards.
Conclusion
Salesforce’s sharing rules are an effective tool that make record-level access safe, scalable, and automated. You can make sure that users can access just the data they require—neither more nor less—by carefully checking these rules using Apex automation and manual checks.
A well-executed and proven sharing model improves cooperation within your Salesforce business, safeguards sensitive data, and promotes user trust.
Follow me on Linkedin