You are on page 1of 3

Apex Sharing:

=============
By using this feature, we can share the Records to the required Users / Public
Groups dynamically at runtime through apex programming. We can decide the required
level of access on the records to the users at runtime.

Object: OWD : Private --> Owner, Manager.


|
--> Sharing.
|
--> 3 Types.
1. Manual
Sharing --> "Sharing" Button.
2. Automated
Sharing
3. Apex
Sharing

All the Sharing Information will get resides inside the Share Objects. For each
Object salesforce will provides a Share object by default as below.

Object Name Share Object Name


-----------------------------------------------
Account ------> AccountShare
Contact ------> ContactShare
Lead ------> LeadShare
Case ------> CaseShare
Campaign ------> CampaignShare
....
....
Position__c ------> Position__Share
Hiring_Manager__C ----> Hiring_Manager__Share
Customer__C ------> Customer__Share
....

AccountShare Object:
====================
This object contains all the Account Records Sharing Details.

Fields:
-------
1. AccountID:
It Contains the Account Record ID, which has been shared /
created.

2. UserOrGroupID:
It contains the User Id / Public Group ID, to whom the record has
been shared.

User ID --> 005


Public Group --> 00G

3. AccountAccessLevel:
It contains the Level of Access has been granted to the user on
the record.

ReadOnly --> READ


Read/Write --> EDIT
Full Access--> ALL
4. OpportunityAccessLevel:
It contains the Level of Access has been granted to the user on
the Related Opportunity Records.

ReadOnly --> READ


Read/Write --> EDIT
Full Access--> ALL

5. CaseAccessLevel:
It contains the Level of Access has been granted to the user on
the Related Case Records.

ReadOnly --> READ


Read/Write --> EDIT
Full Access--> ALL

6. RowCause:
It contains the Reason for the Sharing of the record.

1. Manual --> Manual Sharing


2. Owner --> Owner of the Record
3. Rule --> Due to Account Sharing Rule

AccountShare accShare = new AccountShare();

accShare.AccountID = '0012v000032srQt';
accShare.UserOrGroupID = '0052v00000h4dBB';
accShare.RowCause = 'Manual';
accShare.AccountAccessLevel = 'EDIT';
accShare.OpportunityAccessLevel = 'READ';
accShare.CaseAccessLevel = 'READ';

insert accShare;

UseCase:
========
Configure an Automate Sharing Process to Share the Record to the Selected
User dynamically at runtime based on the "Lookup Field".

Grant the Edit Access to the User, if the Account Record's Annual
Revenue is more than 50,00,000. Else grant the Read Only Access on the Record.

Object Name : Account Object


Event Name : After Insert

Pre-Requisite: Create a Lookup Relationship field on Account object, by


selecting
the "User Object" as the Parent.

Trigger Code:
-------------
trigger ShareAccountRecordsTrigger on Account (After insert)
{
if(Trigger.isAfter && Trigger.isInsert)
{
AccountsShareHandler.AfterInsert(Trigger.New);
}
}

Handler Class:
--------------
public class AccountsShareHandler
{
Public static void AfterInsert(List<Account> lstAccounts)
{
if(! lstAccounts.isEmpty())
{
List<AccountShare> shareAccountRecords = new List<AccountShare>();

for(Account acc : lstAccounts)


{
if(acc.Share_Record_To_User__c != null)
{
// Prepare the Share Details..
AccountShare accShare = new AccountShare();

accShare.AccountId = acc.Id;
accShare.UserOrGroupId = acc.Share_Record_To_User__c;
accShare.RowCause = 'Manual';
accShare.OpportunityAccessLevel = 'READ';
accShare.CaseAccessLevel = 'READ';

if(acc.AnnualRevenue >= 5000000)


accShare.AccountAccessLevel = 'EDIT';
else
accShare.AccountAccessLevel = 'READ';

// Add the Record to Collection for Bulkification..


shareAccountRecords.Add(accShare);
}
}

if(! shareAccountRecords.isEmpty())
{
Insert shareAccountRecords;
}
}
}
}

You might also like