+ Start a Discussion
ShayneOShayneO 

Apex Sharing for User Lookup

Need help with a Apex Sharing based on a User lookup.  I've followed the basic salesforce documentation, but need to be able to handle updates (if the user changes) and if the user lookup field is blank.

 

Right now the trigger works, but I get the following error when the Marketing_Coordinator__c field is left blank:

Error: Invalid Data. 
Review all error messages below to correct your data.
Unable to grant sharing access due to following exception: Required fields are missing: [User/Group]

 

Any input would be appreciated.

 

trigger TransactionApexSharing on Commdev3__Transaction__c (after insert) {
    
    if(trigger.isInsert){
        // Create a new list of sharing objects for Job
        List<Commdev3__Transaction__Share> ts  = new List<Commdev3__Transaction__Share>();
        
        // Declare variables for recruiting and hiring manager sharing
        Commdev3__Transaction__Share mktc;
        //Commdev3__Transcation__Share hmShr;
        
        for(Commdev3__Transaction__c t : trigger.new){
            // Instantiate the sharing objects
            mktc = new Commdev3__Transaction__Share();
           // hmShr = new Job__Share();
            
            // Set the ID of record being shared
            mktc.ParentId = t.Id;
            //hmShr.ParentId = job.Id;
            
            // Set the ID of user or group being granted access
            mktc.UserOrGroupId = t.Marketing_Coordinator__c;
            //hmShr.UserOrGroupId = job.Hiring_Manager__c;
            
            // Set the access level
            mktc.AccessLevel = 'edit';
            //hmShr.AccessLevel = 'read';
            
            // Set the Apex sharing reason for hiring manager and recruiter
            mktc.RowCause = Schema.Commdev3__Transaction__Share.RowCause.Marketing_Coordinator__c;
            //hmShr.RowCause = Schema.Job__Share.RowCause.Hiring_Manager__c;
            
            // Add objects to list for insert
            ts.add(mktc);
            //jobShrs.add(hmShr);
        }
        
        // 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(ts,false);
        
        // Create counter
        Integer i=0;
        
        // Process the save results
        for(Database.SaveResult sr : lsr){
            if(!sr.isSuccess()){
                // 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.
                    trigger.newMap.get(ts[i].ParentId).
                      addError(
                       'Unable to grant sharing access due to following exception: '
                       + err.getMessage());
                }
            }
            i++;
        }   
    }
    
}

 

 

 

 

 

Vinita_SFDCVinita_SFDC

Hello,

 

I think access on object Commdev3__Transaction__c is set to Private in OWD setting and you are using a public group in your code. So first of all add that user (new user) in that Public group for which you have written the code for sharing and then issue should be resolved.