Apex Sharing in Salesforce How to share Record To Diffrent User and Group Using Apex Sharing.

Apexsharing

  • When the Object's OWD has been assigned as "Private", then the Record will be visible to the  "Record Owner and his Manager". Other users inside the organization will not have access to the record.
To make the record visible to the other users inside the Organization, we have to use the below features.

1. Manual Sharing (Achieved by using the "Sharing" Button)
2. Automated Sharing (Sharing Rules)
We can create our own automated rules, to share the records to the required users based on the specified conditions with the required "Level Of Access".

Drawbacks above Manual & Automated Sharing:
1. We need to define the conditions, upon creating the rule. (i.e. We can't add the conditions at  runtime)
2. We need to select the users / Public Group to share the record, upon creating the Rule.
3. We need to hardcode the "Level Of Access" upon configuring the Rule.

To avoid these problems, we can use "Apex Sharing", through which we can share the record to a User / Group of Users (Public Group) based on the need with the required "Access Level".

using Apex sharing On a Single Record, Multiple users can have Different Access Levels.

  --> User1 (Read Only Access)
|
Account Record --- -> User2 (Owner: Full Access)
|
  --> User3 (Read / Write Access)

All the Sharing information will get resides inside the "Share" object provided by the salesforce.

For each Standard / Custom object, salesforce provides a Share Object as below.

Object Name Share Object Name
------------------------------------------------
Account                 AccountShare
Contact                 ContactShare
Opportunity                 OpportunityShare
Lead                 LeadShare
Case                 CaseShare
...
...
Position__c                 Position__Share
Candidate__C                 Candidate__Share
Hiring_Manager__c                 Hiring_Manager__Share
...
...

Share Objects can be accessible, if the associated Parent object's OWD has been assigned as either "Private / Public Read-only".
Upon sharing the record through Programmatically, we have to use the "associated Share Classes" provided by the salesforce.

Each Share Class contains the below properties..

Ex:
Position__Share posShare = new Position__Share();

Properties:
-----------
1. ParentID = "Position Record ID"
This property is used to specify the "Record ID", which needs to be get shared to the users.
Ex:
posShare.ParentId = "Position Record Id";
2. AccessLevel ="EDIT / READ".
This property is used to specify the Level Of Access to be granted to the users on the record.
EDIT --> Grant Read and Write Permissions.
READ --> Grant Only View Permission on the Record.
Ex:
posShare.AccessLevel = 'EDIT';

3. UserOrGroupID = "User / Group ID"
This property is used to specify the "User ID / Public Group ID", to whome the record should get shared.
Ex:
posShare.UserOrGroupID = 'User ID (005)';
posShare.UserOrGroupID = 'Public Group ID (00G)';

4. RowCause = 'Sharing Reason Name'
This property is used to specify the Sharing Reason, because of which the record is visible to the users.
Ex:
posShare.RowCause = Schema.Position__Share.RowCause.Manual;
(i.e. Manual Sharing)

The Developer  / Administrator can create their own sharing reasons based on the need.

Click on the "Required Object's Tab". (Ex: Positions)

        1. Expand the "Force.com Quick Access Menu".
        2. Click on "View Object" link.
        3. Go to the "Sharing Reason Section".
        4. Click on "New" button, to create a New Sharing Reason. 
        5. Enter the Sharing Reason Name and the Label.
                (Ex: Label : Requested By Client Name : Requested_By_Client__c)
        6. Click on "Save" button.



Like our post & bookmark this blog for your future learning. Any suggestions and improvements are most welcome, please comment your suggestions and improvement in the comment box. 😍😍Happy Coding Sharing is Caring 😍😍

Post a Comment

Post a Comment (0)

Previous Post Next Post