Professional Documents
Culture Documents
=============
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.
All the Sharing Information will get resides inside the Share Objects. For each
Object salesforce will provides a Share object by default as below.
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.
3. AccountAccessLevel:
It contains the Level of Access has been granted to the user on
the record.
5. CaseAccessLevel:
It contains the Level of Access has been granted to the user on
the Related Case Records.
6. RowCause:
It contains the Reason for the Sharing of the record.
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.
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>();
accShare.AccountId = acc.Id;
accShare.UserOrGroupId = acc.Share_Record_To_User__c;
accShare.RowCause = 'Manual';
accShare.OpportunityAccessLevel = 'READ';
accShare.CaseAccessLevel = 'READ';
if(! shareAccountRecords.isEmpty())
{
Insert shareAccountRecords;
}
}
}
}