Saturday 7 November 2020

Episode 11:Sharing the Record using Apex in Salesforce

In this blog, we will learn about sharing the records programmatically using the apex code. But the question here arises is why do we need apex sharing when we have the sharing rules to provide additional access of records to users in salesforce.

There are certain complex business requirements where sharing rules will not work, in those scenarios, we will use apex sharing using code.

Let's take one of the use cases of those scenarios and understand how it works.
For Example, you want to share the custom object "CompetencyCourse__c with the Employee. Let's consider that every CompetencyCourse__c  has a lookup to the Employee object. In this case, sharing rules will not work as you want to give access to records that are on the related list to some users. One way to solve this problem is by manually sharing the record but it will need the interference of the record owner, which will be a hectic and manual process to do for each record thus we need the automation using code.


Before you Proceed,

To access sharing using code you have to use share objects associated with every custom as well as a standard object.
For Example, ContactShare is the sharing object for Contact Object, AccountShare is the sharing object for Account Object, and so on. Moreover, all custom objects sharing object is named as "MyCustomObject__share" where MyCustomObject is the name of the custom object.
For the scenario use case, CompetencyCourse__Share is the shared object associated with a CompetencyCourse__c  custom object.


Points To Remember:

  • Objects on the detail side of the master-detail relationship do not have an associated sharing object because in a master-detail relationship the child is tightly coupled with the parent hence detail record access will be determined by the masters sharing object and the relationship's sharing setting.
  • The object's Organization-wide default access level must not be set to the most permissive access level i.e Public Read/Write because in this case there is no need to provide additional access.

Share Object

Every share object must have the below properties defined for them:

  • ParentId: The Id of the record being shared.
  • UserOrGroupId: The Id of the user or the group with whom the objects are being shared.
  • Access Level: The permission level given to the user or the group.
  • RowCause: The reason why the group or the user has been granted sharing access.







Apex Sharing Reason:

Apex sharing reasons help the developers to track the reason why the records are shared with the user or group of users and also prevent the standard users from deleting the sharing.
Note: An Apex Sharing Reason is not available for the standard objects.RowCause is Manual for the standard object i.e apex sharing will be lost once the owner is changed.

Steps to create an apex sharing reason:

  • Click on  SetUp
  • Then go to Build->Create->Objects
  • Select the  Object(CompetencyCourse__c in our example) for which you want to create sharing reasons.
  • Then go to Apex Sharing Reasons related list.
  • Click on new, the below window will appears.

  • Enter the label for the sharing reason(EmployeeCourseAccess in our example)
  • Enter the Name for the sharing reason(EmployeeCourseAccess in our example)
  • Finally, click on the Save button.

Sharing Reason is programmatically  referenced using the below code:

Schema.CustomObject__Share.rowCause.SharingReason__c

Want to know more about Schema Class, check out the link-Schema Class

Scenario: Want o share the custom object "CompetencyCourse__c with the Employee and let's consider that every CompetencyCourse__c  has a lookup to the Employee object.

Code for Sharing the Record using Apex in Salesforce

trigger CompetencyCourse_Share on CompetencyCourse__c (after insert) {
    if(Trigger.isInsert){

        /** CompetencyCourse__Share is the sharing object for the CompetencyCourse__c object.
            courses in the list of share table       **/

        List<CompetencyCourse__Share> courses= new List<CompetencyCourse__Share>();
        //for Each CompetencyCourse__c object do the following

        for(CompetencyCourse__c c:Trigger.New){
            
            CompetencyCourse__Share employeeCourse= new CompetencyCourse__Share();

            //assigning all the properties of share object with the values as explained earlier

            employeeCourse.parentid=c.id;
            employeeCourse.UserOrGroupId=c.employee__c;
            employeeCourse.accessLevel='edit';

            //EmployeeCourseAccess is the sharing reason created for the CompetencyCourse__c object.

employeeCourse.rowCause=
Schema.CompetencyCourse__Share.rowCause.EmployeeCourseAccess__c;

            //Adding the new created share object to the list defined earlier
            courses.add(employeeCourse);

        }
        /**inserting the newly created share objects in the database and
        returning the list of Database.SaveResult which can further use to calculate the no of successful or failed records **/

        Database.SaveResult[] result= Database.insert(courses,false);
        
    }  
    

Thanks for reading my blog. Stay tuned, till then keep learning, and sharing your knowledge.













No comments:

Post a Comment

If you have any doubts ,Please let me know.

Episode 13 : RollUp Summary Trigger for Lookup Relationship

 As we all know we can use roll-up summary field logic only for the master-detail relationship, but in case you have the business requiremen...