Apex Manual Sharing

To access sharing programmatically, you must use the shared object associated with the standard or custom object for which you want to share. Every object has a sharing object, API for standard object add ‘Share’ to the object name and for custom object add ‘__Share’ e.g. AccountShare and Customobject__Share.

A shared object includes records supporting all three types of sharing: managed sharing, user managed sharing, and Apex managed sharing. Sharing granted to users implicitly through organization-wide defaults, the role hierarchy, and permissions such as the “View All” and “Modify All” permissions for the given object, “View All Data,” and “Modify All Data” are not tracked with this object.

Use the properties of apex sharing:

CustomObject__Share CustomShr  = new CustomObject__Share();

ParentId: The ID of the object. This field cannot be updated.

CustomShr.ParentId = recordId;

UserOrGroupId: The user or group IDs to which you are granting access.

 CustomShr.UserOrGroupId = userOrGroupId;

AccessLevel: The level of access that the specified User or Group has been granted. Valid values for Apex managed sharing are: Edit, Read.

 CustomShr.AccessLevel = 'Read';

RowCause: The reason determines the type of sharing, which controls who can alter the sharing record. This field cannot be updated. Manual is the default value for sharing objects.

 CustomShr.RowCause = Schema.Job__Share.RowCause.Manual;

See how to use custom object in apex Sharing.

  • Open object detail

  • Find the related list apex sharing reason

  • Create new apex sharing reason

  • Write your Apex code to Apex Sharing

trigger customobjApexSharing on A__c (after insert) {
    
    if(trigger.isInsert){
        // Create a new list of sharing objects for A
        List<A__Share> Alst  = new List<A__Share>();        
        // Declare variables for sharing reason
        A__Share testShr; 
        
        for(A__c a: trigger.new){
	    testShr = new A__Share();
            // Set the ID of record being shared
            testShr.ParentId = a.Id;           
            
            // Set the ID of user or group being granted access
            testShr.UserOrGroupId = a.User1__c;           
            
            // Set the access level
            testShr.AccessLevel = 'edit';          
            
            // Set the Apex sharing reason for hiring manager and recruiter
            TestShr.RowCause = Schema.A__Share.RowCause.Test_reason__c;           
            
            // Add objects to list for insert
            Alst.add(TestShr);
            
        }
        // The false parameter allows for partial processing if multiple records are passed into the operation 
        Database.SaveResult[] lsr = Database.insert(Alst,false);
}
}

If you are facing error invalid type for share object then go to Setup =>Security Controls => Sharing Settings. In the list click edit and make the Custom object (A__c) permission to Private. You get the error because you have full permission to Object.

Note: If the owner of record is changed, all the Apex-based sharing will not be lost, unlike manual sharing.

Leave a Reply

Your email address will not be published. Required fields are marked *