+ Start a Discussion

New Users are not able to access the old records.....

Hi All,


I am working in salesforce for the past 2 months. I got a opportunity to develop a apex sharing trigger due to some requirment.  After some days I have created new user that user is not able to access the record. I got the following error "Unable to grant sharing access due to following exception : Required fields are missing: [User/Group] ". Can anyone please help me to solve this issue. Thanks in advance.



I think you have made that object as Private on OWD setting. So first of all add that user (new user) in that Public group for which you have written the code for sharing. I think in your Apex code you are using the Public Group. Please post your code so that can help you more.


Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 




Thanks for your reply. Yeah, I am using public groups in my apex trigger. 


trigger MCARApexSharing on MCAR__c (after insert,after update, before update) {

    Group NSNUser=[SELECT Id from group where name=:'MCAR_NSN_Users'];
        //Create a new list of Sharing object for MCAR
    List<MCAR__Share> mcarShares = new List<MCAR__Share>();
    //Declare the variables for Owner,Supplier sharing
    MCAR__Share OwnerShr;
    MCAR__Share CreatedByShr;
    MCAR__Share ContactShr;
    MCAR__Share MCARNSNUserShr;

    for(MCAR__c mcar : trigger.new){
   List<Contact> mcarContact = [SELECT Contact.MCAR_User__r.Id FROM Contact WHERE Contact.Account.Id=: mcar.MCAR_Supplier__c];
   system.debug('+++++++++++++++++++++++++++ With Duplicate Value+++++++++++++++++++++ '+mcarContact );
   List<Contact> DistinctCon = new List<Contact>();    
        if(mcar.MCAR_Status__c == 'Open' || mcar.MCAR_Status__c == 'Cancelled')
          //Instantite the sharing objects
            MCARNSNUserShr = new MCAR__Share();
            MCARNSNSUserShr = new MCAR__Share();
            MCARNSNInternalSubFactoryShr = new MCAR__Share();
            MCARESMUserShr = new MCAR__Share();
            MCARNSNUserShr.ParentId = mcar.Id;
            if(!DistinctCon .isEmpty())
                for(Contact con : DistinctCon )
                    ContactShr= new MCAR__Share();
                    ContactShr.ParentId = mcar.Id;
                    ContactShr.UserorGroupId = con.MCAR_User__r.Id;
                    ContactShr.AccessLevel = 'edit';
                    ContactShr.RowCause = Schema.MCAR__Share.RowCause.Supplier__c;
              if(NSNUser != null)
                MCARNSNUserShr.UserorGroupId = NSNUser.Id;
                MCARNSNUserShr.AccessLevel = 'edit';
                MCARNSNUserShr.RowCause = Schema.MCAR__Share.RowCause.MCAR_NSN_User__c;
        if(mcar.MCAR_Status__c == 'Draft')
            //Instantite the sharing objects
     OwnerShr = new MCAR__Share();
     CreatedByShr = new MCAR__Share();
     OwnerShr.ParentId = mcar.Id;
     CreatedByShr.ParentId = mcar.Id;
            if(mcar.Owner != null)
                OwnerShr.UserorGroupId = mcar.OwnerId;
                OwnerShr.AccessLevel = 'edit';
                OwnerShr.RowCause = Schema.MCAR__Share.RowCause.Owner__c;
            if(mcar.CreatedBy != null)
                CreatedByShr.UserorGroupId = mcar.CreatedById;
                CreatedByShr.AccessLevel = 'edit';
                CreatedByShr.RowCause = Schema.MCAR__Share.RowCause.CreatedBy__c ;
        Database.SaveResult[] lsr = Database.insert(mcarShares,false);
        //Create  Counter
        Integer i=0;
        //Process the Save Result
        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.
trigger.newMap.get(mcarShares[i].ParentId).addError('Unable to grant sharing access due to following exception :' + err.getMessage());