+ Start a Discussion
Stacey McDonaldStacey McDonald 

Apex Managed Sharing

I have created an apex trigger to share a custom object record based on user lookup fields.  Works great on the initial load of the record.  Here are a couple of things I would like to have happen.


1.  When one of the user lookup fields change to remove the person that had sharing access before.

2.  Grant sharing access to the new user.


I don't know if this makes a difference but, all creation of new records and updates are being done through an informatica cloud integration.  Users are not adding records modifying the user lookup fields.  Any assistance would be greatly appreciated!


Below is my current code:


trigger TeamMemberAccess on Quote__c (after insert,after update) {
        // Create a new list of sharing objects for Quote 
        List<Quote__Share> quoteShrs  = new List<Quote__Share>();
        // Declare variables for specifying rep, purchasing rep, and ap rep sharing 
        Quote__Share specifyingShr;
        Quote__Share purchasingShr;
        Quote__Share apShr;
        for(Quote__c quote : trigger.new){
            // Instantiate the sharing objects 
            specifyingShr = new Quote__Share();
            purchasingShr = new Quote__Share();
            apShr = new Quote__Share();
            // Set the ID of record being shared 
            specifyingShr.ParentId = quote.Id;
            purchasingShr.ParentId = quote.Id;
            apShr.ParentId = quote.Id;
            // Set the ID of user or group being granted access 
            specifyingShr.UserOrGroupId = quote.specifying_rep__c;
            purchasingShr.UserOrGroupId = quote.purchasing_rep__c;
            apShr.UserOrGroupId = quote.ap_rep1__c;
            // Set the access level 
            specifyingShr.AccessLevel = 'edit';
            purchasingShr.AccessLevel = 'edit';
            apShr.AccessLevel = 'edit';
            // Set the Apex sharing reason for specifying rep, purchasing rep, and ap rep 
            specifyingShr.RowCause = Schema.Quote__Share.RowCause.specifying_rep__c;
            purchasingShr.RowCause = Schema.Quote__Share.RowCause.purchasing_rep__c;
            apShr.RowCause = Schema.Quote__Share.RowCause.ap_rep__c;
            // Add objects to list for insert 
        // Insert sharing records and capture save result  
        // The false parameter allows for partial processing if multiple records are passed  
        // into the operation  
        Database.SaveResult[] lsr = Database.insert(quoteShrs,false);
        // Create counter 
        Integer i=0;
        // Process the save results 
        for(Database.SaveResult sr : lsr){
                // Get the first save result error 
                Database.Error err = sr.getErrors()[0];
                // Check if the error is related to a trivial access level 
                // Access levels equal or more permissive than the object's default  
                // access level are not allowed.  
                // These sharing records are not required and thus an insert exception is  
                // acceptable.  
                if(!(err.getStatusCode() == StatusCode.FIELD_FILTER_VALIDATION_EXCEPTION  
                                               &&  err.getMessage().contains('AccessLevel'))){
                    // Throw an error when the error is not related to trivial access level. 
                       'Unable to grant sharing access due to following exception: '
                       + err.getMessage());