Salesforce Apex Managed Sharing

1. Introduction

In this blog post, I am going to explain how to use apex managed sharing. Apex managed sharing allows developers to programmatically share custom objects. When you use Apex managed sharing to share the custom object, only users with the “Modify All Data” permission can add or change the sharing on the custom object’s record, and the sharing access is maintained across record owner changes.

Apex Manager sharing can be enabled on the object that is having private and public read-only access.

1.Apex Managed Sharing can not be enabled on public read-write objects
2.Each custom object is having its own sharing table with __share name if the object sharing is private or public read-only
3.Apex sharing reasons and Apex managed sharing recalculation are only available for custom objects.
4.Objects on the detail side of a master-detail relationship do not have an associated sharing object. The detail record’s access is determined by the master’s sharing object and the relationship’s sharing setting

2.Understanding Sharing Reason

The Reason field on a custom object specifies the type of sharing used for a record. This field is called rowCause in Apex or the Force.com API. you can use the Sharing reason in Force.com Managed Sharing, User Managed Sharing, Apex Managed Sharing

RowCause values depend on the type of sharing .its can be Owner, team, share, manual etc depends on the type of sharing the reason.

3.Access Levels

Access level is determining a user’s access to records. Most share objects support the following access levels: Supported Access levels are Private, Read Only, ReadWrite, Full Access
Every share object has the following properties:-
AccessLevel – Can be any of the Edit, Read, All
ParentID – The ID of the object
RowCause – The reason why the user or group is being granted access
UserOrGroupId – The user or group IDs to which you are granting access

4. User Managed Sharing Using Apex

Now we are going to create an apex class that is used to share the data based on sharing.It is possible to manually share a record to a user or a group using Apex or the SOAP API. If the owner of the record changes, the sharing is automatically deleted

 

Invoke the above code from the trigger to share the data as shown below.

 

 

5.Creating Apex Managed Sharing

Apex managed sharing enables developers to programmatically manipulate sharing to support their application’s behavior through Apex or the SOAP API

Apex managed sharing must use an Apex sharing reason. Apex sharing reasons are a way for developers to track why they shared a record with a user or group of users.
Apex sharing reasons are defined on an object’s detail page.
To Create a Apex Managed Sharing reason on the custom Object
go to –> setup -> create ->object ->Delivery__c -> under Apex Managed Sharing Reason ->Click New and create a sharing reason as shown below .

Apex sharing reason name: Custom Sharing Model
In order to use it in the code, you have to use it as shown below
Schema.Delivery__share.rowCause.Custom_Sharing_Model__c
Now Update the above code with the rowCause as shown below.