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


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 :{
        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 :
    { 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

private class TheTestClassEvent {
public static testMethod void testocc(){
     User u = [select name, Id  FROM user LIMIT 1];
      Account acc = new Account(
       name = 'temp validation account',
       Type = 'Prospect',
             Sub_Type__c='Merchant - Body',
             BillingCountry='United Arab Emirates'
         insert acc;
        Account accCreated = [Select Name From Account Where Id =: acc.Id LIMIT 1];
     // RUN AS USER...
            Opportunity od = new Opportunity(
       Name = 'Distributor',
       Type='New Business',
                Stage_Notes__c='test opp',
         insert od;
            // TEST ADDING A NEW Event...                       
      Event eve = new Event(
       subject = 'Meeting test',
         insert eve;

     system.debug('----->>> RUNNING AS USER: ' +;

Vivek Deepak11Vivek Deepak11

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 ?
What is the API version of your test class ??