+ Start a Discussion
Prakhar KPrakhar K 

Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: []

Hi All,
I am getting  an error while inserting record of OpportunitylineitemSchedule in test class for trigger and is unable to figure out the cause.Please help?  
error:
Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: []

Trigger
trigger trgUpdatePrdScheduleDate on OpportunityLineItem (after insert) {
   
    Set<Id> opids = new Set<Id>();
    
    for(OpportunityLineItem ol :trigger.New){
        opids.add(ol.Id);
    }
    
    list<OpportunityLineItemSchedule> lstScheduleToUpdate = new list<OpportunityLineItemSchedule>();
    map<id,OpportunityLineItemSchedule> mapScheduleDate = new map<id,OpportunityLineItemSchedule>();
    
    for(OpportunityLineItemSchedule opps:[Select id,Type,ScheduleDate,Revenue,
                                                 Quantity,OpportunityLineItemId from opportunityLineItemSchedule where 
                                                             OpportunityLineItemId IN:trigger.NewMap.KeySet()]){
        if(!mapScheduleDate.containsKey(opps.OpportunityLineItemId))
            mapScheduleDate.put(opps.OpportunityLineItemId,opps);   
    }
                                         
                                        
      for(OpportunityLineItem op : trigger.New){                                 
          If(op.Campaign_Start_Date__c != null && mapScheduleDate.containsKey(op.Id)){
             OpportunityLineItemSchedule opSchd1 = mapScheduleDate.get(op.Id);
             opSchd1.ScheduleDate = op.Campaign_Start_Date__c;
             lstScheduleToUpdate.add(opSchd1);
          }
      }
      
      if(lstScheduleToUpdate != null && lstScheduleToUpdate.size()>0)
          update lstScheduleToUpdate;
    
}
below is the code for my test class:
@isTest(SeeAllData=true)
private class TesttrgUpdatePrdScheduleDate {

    static testMethod void trgUpdateProductScheduleTest () {
        Test.startTest();
        User usr1 = [select id, Name, ProfileId  from User where Profile.Name = 'System Administrator' and isActive = true limit 1 ];
        System.runAs(usr1) { 
        Account acc = new Account();
        acc.Name = 'TestCompany1234';
        insert acc;
        System.assertEquals(acc.Name, 'TestCompany1234');

        Pricebook2 standardPB = [SELECT Id, Name FROM PriceBook2 Limit 1];
        
        Opportunity opp = new Opportunity();
        opp.Name = ' Testing123';
        opp.Pricebook2ID = standardPB.Id ;
        opp.AccountId = acc.Id;
        opp.StageName = 'Qualification';
        opp.CloseDate = date.parse('12/31/2012');
        opp.Start_Date__c = date.today();
        opp.Next_Steps__c = 'get steps';
        insert opp;
        System.assertEquals(opp.Pricebook2ID, standardPB.Id);
        System.assertEquals(opp.AccountId, acc.Id);
        
        Product2 p = new Product2();
        p.ProductCode = 'Test';
        p.Name = 'TestName';
        p.description ='testing product';
        p.isactive=true;
        insert p;
        System.assertEquals(p.isactive, true);
        
        PricebookEntry pe = new PricebookEntry();
        pe.Product2Id = p.Id;
        pe.Pricebook2Id = standardPB.Id;
        pe.UnitPrice = 1000;
        pe.IsActive = true;
        insert pe;
        System.assertEquals(pe.Product2Id, p.Id);
                
        OpportunityLineItem oli = new OpportunityLineItem();
        oli.OpportunityId = opp.Id;
        oli.PricebookEntryId = pe.Id;
        oli.Quantity = 1;
        oli.UnitPrice = 1000;
        insert oli;   
        
        update opp;
           System.assertEquals(oli.OpportunityId,opp.Id);
           System.assertEquals(oli.PricebookEntryId, pe.Id);
           system.debug('oli.OpportunityId----------'+oli.OpportunityId);
           system.debug('oli.PricebookEntryId----------'+oli.PricebookEntryId);
               
        
      /*  List<OpportunityLineItemSchedule> olie = new List <OpportunityLineItemSchedule>();
        olie = [select Quantity,ScheduleDate,OpportunityLineItemId from OpportunityLineItemSchedule
                          where OpportunityLineItemId =: oli.Id  limit 1 ];
        system.debug('##############'+olie);
      */
      
        OpportunityLineItemSchedule olis = new OpportunityLineItemSchedule();
        olis.OpportunityLineItemId = oli.id;
        system.debug('olis.OpportunityLineItemId++++++++++++'+oli.id);
        olis.quantity= 40;
        olis.revenue= 100.00;
        olis.type= 'Both';
        olis.ScheduleDate = Date.Today();
        insert olis;   
        
        System.assertEquals(olis.OpportunityLineItemId,oli.id);
        Test.stopTest();
    }
  }
}
------------------------------------------------------------------------------
 When I see the debug log i am getting the ids:

oli.OpportunityId----------006S0000008LaogIAC
oli.PricebookEntryId----------01uS0000006ftMEIAY
olis.OpportunityLineItemId++++++++++++00kS0000006FqA0IAK


 
Roy LuoRoy Luo
Likely it is a permission issue. 

You may run the codes in workbench https://workbench.developerforce.com/execute.php, and likely you would see clues there. 

 
Account acc = new Account();
        acc.Name = 'TestCompany1234';
        insert acc;
      //  System.assertEquals(acc.Name, 'TestCompany1234');

        Pricebook2 standardPB = [SELECT Id, Name FROM PriceBook2 Limit 1];
        
        Opportunity opp = new Opportunity();
        opp.Name = ' Testing123';
        opp.Pricebook2ID = standardPB.Id ;
        opp.AccountId = acc.Id;
        opp.StageName = 'Qualification';
        opp.CloseDate = date.parse('12/31/2012');
        opp.Start_Date__c = date.today();
        opp.Next_Steps__c = 'get steps';
        insert opp;
      //  System.assertEquals(opp.Pricebook2ID, standardPB.Id);
     //   System.assertEquals(opp.AccountId, acc.Id);
        
        Product2 p = new Product2();
        p.ProductCode = 'Test';
        p.Name = 'TestName';
        p.description ='testing product';
        p.isactive=true;
        insert p;
       // System.assertEquals(p.isactive, true);
        
        PricebookEntry pe = new PricebookEntry();
        pe.Product2Id = p.Id;
        pe.Pricebook2Id = standardPB.Id;
        pe.UnitPrice = 1000;
        pe.IsActive = true;
        insert pe;
      //  System.assertEquals(pe.Product2Id, p.Id);
                
        OpportunityLineItem oli = new OpportunityLineItem();
        oli.OpportunityId = opp.Id;
        oli.PricebookEntryId = pe.Id;
        oli.Quantity = 1;
        oli.UnitPrice = 1000;
        insert oli;   
        
        update opp;
           //System.assertEquals(oli.OpportunityId,opp.Id);
           System.assertEquals(oli.PricebookEntryId, pe.Id);
           system.debug('oli.OpportunityId----------'+oli.OpportunityId);
           system.debug('oli.PricebookEntryId----------'+oli.PricebookEntryId);
               
        
      /*  List<OpportunityLineItemSchedule> olie = new List <OpportunityLineItemSchedule>();
        olie = [select Quantity,ScheduleDate,OpportunityLineItemId from OpportunityLineItemSchedule
                          where OpportunityLineItemId =: oli.Id  limit 1 ];
        system.debug('##############'+olie);
      */
      
        OpportunityLineItemSchedule olis = new OpportunityLineItemSchedule();
        olis.OpportunityLineItemId = oli.id;
        system.debug('olis.OpportunityLineItemId++++++++++++'+oli.id);
        olis.quantity= 40;
        olis.revenue= 100.00;
        olis.type= 'Both';
        olis.ScheduleDate = Date.Today();
        insert olis;