function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
rsenthil86rsenthil86 

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.

Navatar_DbSupNavatar_DbSup

Hi,


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. 

rsenthil86rsenthil86

Hi,

 

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'];
    
    
 if(trigger.isInsert){
        //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;
                    mcarShares.add(ContactShr);
                }            
                
            }
              if(NSNUser != null)
               {
                MCARNSNUserShr.UserorGroupId = NSNUser.Id;
                MCARNSNUserShr.AccessLevel = 'edit';
                MCARNSNUserShr.RowCause = Schema.MCAR__Share.RowCause.MCAR_NSN_User__c;
                mcarShares.add(MCARNSNUserShr);
               }
              }            
        }
        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;
                mcarShares.add(OwnerShr);
            }
            if(mcar.CreatedBy != null)
            {
                CreatedByShr.UserorGroupId = mcar.CreatedById;
                CreatedByShr.AccessLevel = 'edit';
                CreatedByShr.RowCause = Schema.MCAR__Share.RowCause.CreatedBy__c ;
                mcarShares.add(CreatedByShr);
            }
            
         }
   }
        Database.SaveResult[] lsr = Database.insert(mcarShares,false);
        
        //Create  Counter
        
        Integer i=0;
        
        //Process the Save Result
        
        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(mcarShares[i].ParentId).addError('Unable to grant sharing access due to following exception :' + err.getMessage());
            }
        }
          i++;
    
    }
  }