How many ways we can share a record?

Overview

Salesforce provides multiple mechanisms to share records so users can access the data they need while maintaining security and compliance. This post lists the main sharing methods, explains when to use them, and includes a simple Apex sharing code example.

Key sharing mechanisms in Salesforce

1. Organization-Wide Defaults (OWD)

OWD defines the baseline level of access for each object (Private, Public Read Only, Public Read/Write). It is not a sharing method per se, but it determines whether further sharing is required.

2. Role Hierarchy

The role hierarchy grants access to records owned by users lower in the hierarchy. Use it when managers need implicit access to their subordinates’ records.

3. Sharing Rules

Sharing rules open access based on criteria or owner-based rules. They are declarative and are useful for broad, automated sharing (e.g., share opportunities in Region = “EMEA” with the EMEA Sales group).

4. Manual Sharing

Users can manually share individual records from the record detail (when manual sharing is enabled). It’s appropriate for one-off exceptions where specific users need access.

5. Apex Managed Sharing

Apex sharing allows programmatic creation of share records (e.g., CustomObject__Share). Use Apex sharing for complex, dynamic, or transaction-based sharing logic that cannot be accomplished declaratively.

6. Account Teams / Opportunity Teams / Case Teams

Teams let you share account/opportunity/case records with predefined roles and access levels. They are great for multi-person collaboration on a single account or opportunity.

7. Sharing via Groups and Queues

Share records with public groups, queues, or territories. Queues are often used for lead, case, or custom object record ownership and processing.

8. Territory Management

Territory Management offers territory-based access to accounts (and related records) for complex sales structures. Use this when access follows territory assignments rather than role hierarchy.

9. Portal / Experience Cloud Sharing (Sharing Sets & Guest User)

Customer and partner portal users have their own sharing models. Sharing Sets allow access based on user-to-account relationships. Guest user access must be used carefully for public data.

10. Implicit Sharing (Parent-Child)

Some objects inherit access implicitly. For example, access to an Account can grant access to its related Contacts and Opportunities depending on sharing model and settings.

Quick comparison (when to use)

– Use OWD + Role Hierarchy for organizational defaults and manager access.
– Use Sharing Rules for broad, declarative sharing by criteria or owner.
– Use Manual Sharing for ad-hoc, record-level exceptions.
– Use Apex Managed Sharing for custom, conditional, or transactional sharing logic.
– Use Teams and Territories for collaborative or territory-based models.

Example: Simple Apex Managed Sharing

Below is a minimal Apex example that creates a share record for a custom object (replace CustomObject__c with your object API name):

public static void shareCustomRecord(Id recordId, Id userOrGroupId) {
CustomObject__Share sr = new CustomObject__Share();
sr.ParentId = recordId; // the custom object record Id
sr.UserOrGroupId = userOrGroupId; // user or group to grant access
sr.AccessLevel = 'Edit'; // Read, Edit (depends on object)
sr.RowCause = Schema.CustomObject__Share.RowCause.Manual; // or a custom reason
insert sr;
}

Summary

In short, there are many ways to share records in Salesforce — declarative (OWD, Role Hierarchy, Sharing Rules, Teams, Territories, Manual Sharing) and programmatic (Apex Managed Sharing). Choose the method that best balances security, maintainability, and business requirements.

Further reading

Refer to Salesforce documentation on Record-Level Security and Apex Managed Sharing for advanced scenarios.