
In Salesforce, sharing rules define which records a user can access. While most sharing can be managed through configuration (like organization-wide defaults, role hierarchy, and sharing rules), there are scenarios where you need to programmatically share records.
This is where Apex Sharing in Salesforce comes into play. It allows developers to write code to create, modify, or delete sharing records for finer control over data access.
What is Apex Sharing in Salesforce?
Apex Sharing refers to creating or managing record-level access programmatically using Apex code. Salesforce stores record-sharing information in share tables (also called share objects) that end with __Share
for custom objects or <StandardObjectName>Share
for standard objects.
For example:
-
AccountShare – for the Account object
-
CustomObject__Share – for a custom object
When to Use Apex Sharing?
You should use Apex Sharing when:
-
You want to share a record based on complex business logic not achievable through declarative sharing rules.
-
Record access needs to be assigned dynamically at runtime.
-
Sharing should happen instantly after certain conditions are met.
Example Use Case:
A record is automatically shared with a specific user when a status field is updated to “Approved”.
Types of Apex Sharing
-
Declarative Sharing (Configured via setup – no code)
-
Role hierarchy
-
Sharing rules
-
Manual sharing
-
-
Programmatic Sharing (Apex Sharing)
-
Controlled via Apex code
-
Uses share objects
-
Allows inserting, updating, or deleting share records
-
Structure of a Share Object
A share object generally has these fields:
-
ParentId – The ID of the record being shared
-
UserOrGroupId – The ID of the user or group the record is being shared with
-
AccessLevel – Level of access (
Read
,Edit
,All
) -
RowCause – The reason for sharing (Manual, Rule, Apex, etc.)
Apex Sharing Example
Here’s a sample code snippet to share a record programmatically:
Key Notes:
-
RowCause
can beManual
,Owner
, or a custom Apex sharing reason for custom objects. -
For standard objects, you cannot create new custom row causes.
Best Practices for Apex Sharing
-
Use with sharing keyword in Apex classes to enforce sharing rules.
-
Always check if the record is already shared before inserting a new share record to avoid duplication.
-
For custom objects, define Apex Sharing Reasons to track why a record was shared.
-
Avoid excessive DML operations; group sharing inserts in bulk.
Limitations of Apex Sharing
-
Only works if the object’s organization-wide default (OWD) is Private or Public Read-Only.
-
For standard objects, RowCause values are predefined and cannot be customized.
-
Large-scale sharing may require asynchronous processing (Batch Apex or Queueable).
Conclusion
Apex Sharing in Salesforce is a powerful feature that allows developers to implement highly customized data access logic beyond what is possible with declarative sharing rules. By leveraging share objects and programmatic sharing, you can ensure that the right people have access to the right data at the right time.