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
Bryan Revelant 7Bryan Revelant 7 

Test Class INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY

Having an issue with the below test class. I have an insert trigger for the sharing object upon a field change. When the field next_step = submit then the code grabs a look up to user on the child, and gives the user visibility to the parent. 

I keep getting an error: 
System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: []

Class.TestADRShareSubmitV2.ADRNonApprovalObjectUpdateChildRecordwithSubmit: line 136, column 1



@istest
public  class TestADRShareSubmitV2 {

    public static testmethod void ADRNonApprovalObjectUpdateChildRecordwithSubmit()
    { 
       
       
        //buidling the ADR Master Object   
User u =[select id from user where Username = 'bryan.revelant@delmonte.com.saa'];
          ADR__c newadr = new ADR__c();

        newadr.Review__c ='test account';
        newadr.Affiliate_Company__c = 'Test';
        newadr.Apex_Status__c = 'Testing Vendor';
        newadr.Next_Step__c = '';    

        newadr.OwnerId = u.id;
   
           insert newadr;
          
          
         ADR__Share newadrshare3 = new ADR__Share();
       
        newadrshare3.ParentId = newadr.id;
        newadrshare3.AccessLevel = 'Edit';
        newadrshare3.UserOrGroupId = newadr.OwnerId;      
       // newadrshare.UserOrGroupId = NEWADRVOU.User__c;
   
      //  insert newadrshare3; 

           //Building the attachement


      ADR_Non_Approval_Object__c NEWADRNONAO = new ADR_Non_Approval_Object__c();
     //User u =[select id from user where isActive=true limit 1];    
         
        NEWADRNONAO.ADR__c = newadr.id;
        NEWADRNONAO.User__c = u.id;
        NEWADRNONAO.Approval__c = ''; 
        NEWADRNONAO.Approval_Status_Date_Time__c = 'Testing Vendor';
        NEWADRNONAO.CheckReadAccess__c = true;
        NEWADRNONAO.Comments__c = 'Testing cat';
       
        insert NEWADRNONAO;
       
           
     ADR_VIew_Only_Users__c NEWADRVOU = new ADR_VIew_Only_Users__c();
         User ua =[select id from user where isActive=true limit 1];
          NEWADRVOU.User__c = ua.id;
         NEWADRVOU.ADR__c = newadr.id;
        NEWADRVOU.Status__c = '';
      
         insert NEWADRVOU;
       
    newadr.Next_Step__c = 'Submit';
     NEWADRNONAO.Approval__c = 'Submit';
    NEWADRVOU.Status__c = 'Submit';
      try {
            update newadr;
            update NEWADRVOU;
            update NEWADRNONAO;
        } catch (DMLException ex) {
            System.assert(true, 'Please Attach The Required Document Below In Notes and Attachment Section.');
        }
   

         ADR__Share newadrshare1 = new ADR__Share();
       
        newadrshare1.ParentId = NEWADRNONAO.ADR__c;
        newadrshare1.AccessLevel = 'Edit';
        //newadrshare.UserOrGroupId = u.id;      
        newadrshare1.UserOrGroupId = NEWADRVOU.User__c;
       
       
       
      insert newadrshare1;
  
             list<ADR__Share> Results = new list<ADR__Share>
        ([SELECT ParentId, AccessLevel, UserOrGroupId FROM ADR__Share]);
system.assert(Results.size()>0, 'No Answes found: '+ Results);
         } 
    }
pradeep naredlapradeep naredla
Hi,
   The error is basically coming due to sharing the inserted record back with the owner of the record who has full permissions on it.

If you see while creating the record, the logged in user is the owner as well as Branch Manager of the record. Then in the trigger its again shared back with the Branch Manager who is the owner of the record.
Basically, you cannot restrict the access to the owner of a record.

I will suggest you to create a user and assign it to Branch Manager and then insert it. In the trigger share it with UserInfo.getUserId().
Let me know if you have  any questions.

regards,
pardeep.
Bryan Revelant 7Bryan Revelant 7
Hmm, I am pretty sure that we dont use Roles though. Does that matter. 

If it doesnt could you provide a sample of what you are menaing? 
pradeep naredlapradeep naredla
Hi,
Make sure the profile and role can access the object.

Bryan Revelant 7Bryan Revelant 7
The user object can access the Role and Object, I guess I can point it to the system admin
Bryan Revelant 7Bryan Revelant 7
How do you create a user. I dont see anything where you can do this. When testing for the user record, it appears that the object is just queried with the data rather than created in the test class from what i read if you add @istest (SeeAllData=true) you can see all the data
Bryan Revelant 7Bryan Revelant 7
Nevermind i figured it out. It was just nice to know why the error was happening - thanks On Thu, Jun 12, 2014 at 10:50 AM, Bryan Revelant wrote: