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
Sabah FarhanaSabah Farhana 

Test Class for simple trigger error:insufficient access to cross reference entity Id

Hi,

I have an apex trigger that updates the event custom field with opportunity Stage Value.The trigger is working fine.Now i want to move it to production.But having issues with the test class.

When i run the test class i get the error First Exception on row 0 insufficient access to cross reference entity Id.
Please help! Iam very bad at test classes.


The trigger:
trigger trigeventOpp on Event (before insert, before update) {

   
    Set<Id> opIds = new Set<Id>();
    for(Event t : trigger.new){
        String wId = t.WhatId;
                if(wId!=null && wId.startsWith('006') && !opIds.contains(t.WhatId))
                { opIds.add(t.WhatId);
                } }
    // Pull in opp ids and related field to populate Event record
    List<Opportunity> taskOps = [Select Id, StageName from Opportunity where Id in :opIds];
    Map<Id, Opportunity> opMap = new Map<Id, Opportunity>();
    for(Opportunity o : taskOps)
    { opMap.put(o.Id,o);  } // Update custom task field with  opp field
    for(Event t : trigger.new)
    { String wId = t.WhatId;
     if(wId!=null && wId.startswith('006'))
     { Opportunity thisOp = opMap.get(t.WhatId);
      if(thisOp!=null){t.Opportunity_stage__c = thisOp.StageName;}
                                                 } }
}

The test class

@isTest
private class TheTestClassEvent {
  
public static testMethod void testocc(){
   
     // GET USER FROM USER TABLE...
     User u = [select name, Id  FROM user LIMIT 1];
      Account acc = new Account(
       name = 'temp validation account',
       Type = 'Prospect',
             Sub_Type__c='Merchant - Body',
             BillingCity='Dubai',
             BillingCountry='United Arab Emirates'
            
       );
         insert acc;
       
        Account accCreated = [Select Name From Account Where Id =: acc.Id LIMIT 1];
     // RUN AS USER...
     system.runAs(u)
     {
            Opportunity od = new Opportunity(
       Name = 'Distributor',
                AccountId=accCreated.id,
       Type='New Business',
                StageName='Pending',
                Stage_Notes__c='test opp',
                Edition__c='2014',
                LeadSource='Distributor',
                CloseDate=System.Today()
       );
         insert od;
           
           
           
            // TEST ADDING A NEW Event...                       
      Event eve = new Event(
       subject = 'Meeting test',
                whatId=od.Id
      
       );
         insert eve;
        

        
     }
     system.debug('----->>> RUNNING AS USER: ' + u.name);

   }       
}
Vivek Deepak11Vivek Deepak11
Hi

Does your runAs() user have access to opportunity because you as creating account by other user and opportunity by other user. And if that is not the problem does it give the line number where the problem is ?
Vinit_KumarVinit_Kumar
What is the API version of your test class ??