+ Start a Discussion
sumithasumitha 

ERROR: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id

Hi Everyone,

The Following error occurs in Apex trigger.

      Error: Apex trigger DistributorDiscountChangeTrigger caused an unexpected exception, contact your administrator: DistributorDiscountChangeTrigger: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0QLL00000001CRNOA2; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []: ()
 QuoteLineItem ID: 0QLL00000001CRNOA2 

DistributorDiscountChangeTrigger :(used to update the discount ,which enter in account and should updte the value in related QuoteLineItem)
trigger DistributorDiscountChangeTrigger on Account (after update) 
{
    if(checkRecursive.runOnce())
{
    Map<Id, Account> accByIds = new Map<Id, Account>();
       for ( Account acc: Trigger.NEW) 
    {
        System.debug('Inside FOR Loop');
        if ( acc.Distributor_Discount_Service__c != Trigger.oldMap.get(acc.Id).Distributor_Discount_Service__c || acc.Distributor_Discount_Product__c != Trigger.oldMap.get(acc.Id).Distributor_Discount_Product__c || acc.Distributor_Discount_Renewal__c != Trigger.oldMap.get(acc.Id).Distributor_Discount_Renewal__c) {
            accByIds.put( acc.Id, acc);
          System.debug('Account ID' +accByIds);    
        System.debug('Account ID' +accByIds.size());    
        }
    }
    
    Map<Id, Opportunity> oppByIds = new Map<Id,Opportunity>( [ SELECT Id,Distributor__c FROM Opportunity WHERE Distributor__c IN: accByIds.keySet() AND IsClosed = FALSE]);        
       System.debug('Opportunity ID' +oppByIds.size());
 List<QuoteLineItem> qlitmp = [select id,quote.opportunity.Distributor__c,quote.opportunity.Override_Renewal_Discount__c,quote.opportunity.RecordType.name,Distributor_Discount_1__c,quote.opportunity.Distributor__r.Distributor_Discount_Renewal__c,product2.family,quote.opportunity.Distributor__r.Distributor_Discount_Service__c ,quote.opportunity.Distributor__r.Distributor_Discount_Product__c from quotelineitem where quote.opportunity.ID IN: oppByIds.keySet()]; 
     List<Opportunity> oppqt = [ SELECT Id,RecordType.name ,Override_Renewal_Discount__c,Distributor__r.Distributor_Discount_Renewal__c,Distributor__r.Distributor_Discount_Service__c,Distributor_Discount_Product_Temp__c,Distributor_Discount_Service_Temp__c,Distributor__c FROM Opportunity WHERE Id IN: oppByIds.keySet()];
      System.debug('Opportunity -list'+oppqt.size());   
     
    
    if ( !accByIds.isEmpty() ) 
    {
        System.debug('Inside if Loop');
        
       
         for ( Opportunity qt: oppqt ) 
         {
        /******* NST's Record Type == Service Renewal ***************/   //opp
               
                  if(qt.RecordType.name=='Service Renewal' && qt.Override_Renewal_Discount__c==false)
        { 
            System.debug('inside if');
            qt.Distributor_Discount_Service_Temp__c=  qt.Distributor__r.Distributor_Discount_Renewal__c;
        
             System.debug('Distributor_Discount_Renewal__c '+  qt.Distributor_Discount_Service_Temp__c); 
          
        }
      else  if(qt.RecordType.name=='Service Renewal' && qt.Override_Renewal_Discount__c==true)
        { 
            System.debug('inside if');
            qt.Distributor_Discount_Service_Temp__c=  qt.Distributor__r.Distributor_Discount_Service__c;
        
             System.debug('Distributor_Discount_Service__c '+  qt.Distributor_Discount_Service_Temp__c); 
          
        }
             
              /*******EOF NST's Record Type == Service Renewal ***************/
//
    else if ( qt.Distributor__c != null) 
    {
                qt.Distributor_Discount_Product_Temp__c = accByIds.get( qt.Distributor__c ).Distributor_Discount_Product__c;
               System.debug('opportunity.Distributor_Discount_Product_Temp__c '+  qt.Distributor_Discount_Product_Temp__c); 
               qt.Distributor_Discount_Service_Temp__c= accByIds.get( qt.Distributor__c ).Distributor_Discount_Service__c;
             System.debug('opportunity.Distributor_Discount_Service_Temp__c '+  qt.Distributor_Discount_Service_Temp__c); 

           }   
               
         }
         update oppqt;//opp
        
        
    }    
          for(quotelineitem qlitemp: qlitmp)
             {
                 system.debug('1');
                 if(qlitemp.quote.opportunity.RecordType.name=='Service Renewal' && qlitemp.quote.opportunity.Override_Renewal_Discount__c==false)
        { 
            system.debug('1-false');
           
           qlitemp.Distributor_Discount_1__c=qlitemp.quote.opportunity.Distributor__r.Distributor_Discount_Renewal__c;
         }
              else if(qlitemp.product2.family=='Service' && qlitemp.quote.opportunity.Distributor__c != null)
        {
            system.debug('12');
                 qlitemp.Distributor_Discount_1__c=qlitemp.quote.opportunity.Distributor__r.Distributor_Discount_Service__c ; 
        }
                 else if(qlitemp.product2.family=='product' && qlitemp.quote.opportunity.Distributor__c != null)
                 {
                     system.debug('13');
                     qlitemp.Distributor_Discount_1__c=qlitemp.quote.opportunity.Distributor__r.Distributor_Discount_Product__c ; 
                 }
                
         }  
        system.debug('qlitmp'+qlitmp);
        update qlitmp; //qli 
             
 
     
        
        /************************** NST Product Service Update to TEMP ***********************/
        Map<Id, NST__c> nstdisByIds = new Map<Id,NST__c>( [ SELECT Id, Distributor_Account__c FROM NST__c WHERE  Distributor_Account__c IN: accByIds.keySet() AND NST_Status__c = 'Draft']);        
       System.debug('nstdisByIds ID' +nstdisByIds.size());

     List<NST__c> nstdis = [ SELECT Id,recordtype.id,recordtype.name,Distributor_Account__r.Distributor_Discount_Renewal__c,Distributor_Account__r.Distributor_Discount_Product__c,Distributor_Account__r.Distributor_Discount_Service__c, Product_Contractual_Discount_Temp__c,Service_Contractual_Discount_Temp__c,Distributor_Account__c FROM NST__c WHERE Id IN: nstdisByIds.keySet()];
    System.debug('nstdis -list'+nstdis.size());   
           for ( NST__c qt: nstdis ) 
         {
            System.debug('Inside FOr');
             /******* NST's Record Type == Service Renewal ***************/
        if(qt.RecordType.name=='Service Renewal')
        { 
            System.debug('inside if');
            qt.Service_Contractual_Discount_Temp__c=  qt.Distributor_Account__r.Distributor_Discount_Renewal__c;
             System.debug('Distributor_Discount_Renewal__c '+  qt.Service_Contractual_Discount_Temp__c); 
        }
        
         /******* End of NST's Record Type == Service Renewal ***************/
    else if ( qt.Distributor_Account__c != null) 
    {
                qt.Product_Contractual_Discount_Temp__c = accByIds.get( qt.Distributor_Account__c ).Distributor_Discount_Product__c;
               System.debug('opportunity.Product_Contractual_Discount_Temp__c '+  qt.Product_Contractual_Discount_Temp__c); 
               qt.Service_Contractual_Discount_Temp__c=  accByIds.get( qt.Distributor_Account__c ).Distributor_Discount_Service__c;
             System.debug('opportunity.Service_Contractual_Discount_Temp__c '+  qt.Service_Contractual_Discount_Temp__c);  
           }       
         }

      update nstdis;  
        system.debug('Updated'+nstdis.size());
           system.debug('Updated'+nstdis);
        /*************************************************************************************/
        
        
    }
}

Please advice,

Thanks
Sumitha P

 
NagendraNagendra (Salesforce Developers) 
Hi Sumitha,

Normally you will get this error whenever you have assigned a wrong id to a field. For Example.

in contact object, we have a lookup to account, If I try to create a new contact and give an invalid id in the AccountID of contact you will get the error.

Please Print the qlis before updating and see if there is an ID assigned to it. So that we will know which ID is invalid.

Please let us know if you have any further issues.

Mark this as solved if it;s resolved.

Thanks,
Nagendra