Utility class for Apex Sharing

Ever faced a situation where you need to share records and it cannot be achieved through standard sharing rules? In such cases, Apex Managed Sharing comes to your rescue. Let us first try to understand what sharing means.

In an org where the OWD is restricitive (OWD is set other than public read/ write or public read only), you may have some requirements where you need to share records with some users. Sharing is the act of granting a user or group of users permission to perform a set of actions on a record or set of records. Sharing access can be granted using the Salesforce user interface and Force.com, or programmatically using Apex.

To access sharing programmatically, you must use the share object associated with the standard or custom object for which you want to share. For example, AccountShare is the sharing object for the Account object, ContactShare is the sharing object for the Contact object, and so on. In addition, all custom object sharing objects are named as follows, where MyCustomObject is the name of the custom object:
MyCustomObject__Share


It is very common to implement Apex sharing due to the complexity of requirements which need to be implemented. Instead of writing the same code in different ways across projects, I thought of writing a utility class which can be used across projects to create share records. 


Utility Class : 

/** Author : Ravi Dutt Sharma

*  Created Date : 6/10/2016
*  Description : Utility class for Apex managed sharing
**/
public class ApexSharingUtils {
    
    /** Method name : createLeadShare
     * Arguments : Id leadId - Record id of the lead record which needs to be shared
     * Id userOrGroupId - Record id of user or public group with which the leads record needs to be shared
     * String leadAccessLevel - Record sharing access level - Read, Edit, All
     * Return type : LeadShare
     * Description : Creates lead share record as per the parameters passed to the method
    **/
    public static LeadShare createLeadShare(Id leadId, Id userOrGroupId, 
                                                  String leadAccessLevel){
        LeadShare share = new LeadShare();
        share.LeadId = leadId;
        share.UserOrGroupId = userOrGroupId;                                              
        share.LeadAccessLevel = leadAccessLevel;
        return share;
    }
    
    /** Method name : createAccountShare
     * Arguments : Id accountId - Record id of the account record which needs to be shared
     * Id userOrGroupId - Record id of user or public group with which the account record needs to be shared
     * String accountAccessLevel - Record sharing access level - Read, Edit, All
     * String opportunityAccessLevel - Record sharing access level of child opportunities present under the account- Read, Edit, All
     * String caseAccessLevel - Record sharing access level of child cases present under the account- Read, Edit, All
     * Return type : AccountShare
     * Description : Creates account share record as per the parameters passed to the method
    **/
    public static AccountShare createAccountShare(Id accountId, ID userOrGroupId, 
                                                  String accountAccessLevel,String opportunityAccessLevel, String caseAccessLevel){
        AccountShare share = new AccountShare();
        share.AccountId = accountId;
        share.UserOrGroupId = userOrGroupId;
        share.AccountAccessLevel = accountAccessLevel;
        share.OpportunityAccessLevel = opportunityAccessLevel;
        share.CaseAccessLevel = caseAccessLevel;                                       
        return share;
    }
    
     /** Method name : createContactShare
     * Arguments : Id contactId - Record id of the contact record which needs to be shared
     * Id userOrGroupId - Record id of user or public group with which the contact record needs to be shared
     * String contactAccessLevel - Record sharing access level - Read, Edit, All
     * Return type : ContactShare
     * Description : Creates contact share record as per the parameters passed to the method
    **/
    public static ContactShare createContactShare(Id contactId, Id userOrGroupId, 
                                                  String contactAccessLevel){
        ContactShare share = new ContactShare();
        share.ContactId = contactId;
        share.UserOrGroupId = userOrGroupId;                                              
        share.ContactAccessLevel = contactAccessLevel;
        return share;
    }
    
    /** Method name : createOpportunityShare
     * Arguments : Id opportunityId - Record id of the opportunity record which needs to be shared
     * Id userOrGroupId - Record id of user or public group with which the opportunity record needs to be shared
     * String opportunityAccessLevel - Record sharing access level - Read, Edit, All
     * Return type : OpportunityShare
     * Description : Creates opportunity share record as per the parameters passed to the method
    **/
    public static OpportunityShare createOpportunityShare(Id opportunityId, Id userOrGroupId, 
                                                  String opportunityAccessLevel){
        OpportunityShare share = new OpportunityShare();
        share.OpportunityId = opportunityId;
        share.UserOrGroupId = userOrGroupId;                                              
        share.OpportunityAccessLevel = opportunityAccessLevel;
        return share;
    }
    
    /** Method name : createCaseShare
     * Arguments : Id caseId - Record id of the case record which needs to be shared
     * Id userOrGroupId - Record id of user or public group with which the case record needs to be shared
     * String caseAccessLevel - Record sharing access level - Read, Edit, All
     * Return type : CaseShare
     * Description : Creates case share record as per the parameters passed to the method
    **/
    public static CaseShare createCaseShare(Id caseId, Id userOrGroupId, 
                                                  String caseAccessLevel){
        CaseShare share = new CaseShare();
        share.CaseId = caseId;
        share.UserOrGroupId = userOrGroupId;                                              
        share.CaseAccessLevel = caseAccessLevel;
        return share;
    }
    
    /** Method name : createCustomObjectShare
     * Arguments : Id recordId - Record id of the custom object record which needs to be shared
     * Id userOrGroupId - Record id of user or public group with which the custom object record needs to be shared
     * String recordAccessLevel - Record sharing access level - Read, Edit, All
     * Return type : sObject - share object specific to the custom object
     * Description : Creates custom object share record as per the parameters passed to the method
    **/
    public static sObject createCustomObjectShare(Id recordId, Id userOrGroupId,
                                                  String recordAccessLevel){
        String sObjLabel = recordId.getSObjectType().getDescribe().getLabel();
        sObject sObj = Schema.getGlobalDescribe().get(sObjLabel + '__Share').newSObject();
        sObj.put('ParentId',recordId);
        sObj.put('UserOrGroupId',userOrGroupId);
        sObj.put('AccessLevel',recordAccessLevel);
        return sObj;
    }

}

Comments

Popular posts from this blog

Salesforce Lightning: Countdown timer

Salesforce Hacks: System.LimitException: Too many queueable jobs added to the queue: 2

Building an Org Role Hierarchy component in LWC