+ Start a Discussion
SandrineSandrine 

Error | INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []

Hello,

I have a code which updates an  Old__c which is a object in managed package. it has only one record type which is accessible to system admin

but for line 
//update lstOldToUpdate;

It gives ERROR like
//
Exception : Update failed. First exception on row 0 with id a1eb00000015PrAAAU; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []
As data volume may be large, go for batch to achiev this :
global class  BatchClassDemo implements  Database.Batchable<sObject> 
{
    global  BatchClassDemo()
      {

      }
      
      global Database.QueryLocator start(Database.BatchableContext BC){
      
         return Database.getQueryLocator('SELECT Id__c FROM New__c');
      }
      
      global void execute(Database.BatchableContext BC, List<New__c> scope)
      {
         Set<String> setStrId = new Set<String>();
         List<Old__c> lstOldToUpdate = new list<Old__c>();
         for(New__c objNew : scope)
            if(String.isNotblank(objNew.Id__c))
                setStrId.add(objNew.Id__c)
         for(Old__c objOld : [SELECT Id, checkbox__c FROM Old__c WHERE Id IN: setStrId])
         {
            
            lstOldToUpdate.add(new Old__c(Id=objOld.Id, checkbox__c = true));
         }
         
         if(!lstOldToUpdate.isEmpty())
            update lstOldToUpdate;
      }
      global void finish(Database.BatchableContext BC){
        
      }
}

 
Best Answer chosen by Sandrine
Martijn SchwarzerMartijn Schwarzer
Hi Sandrine,

Can you please try the following code:
 
As data volume may be large, go for batch to achiev this :
global class  BatchClassDemo implements  Database.Batchable<sObject> 
{
    global  BatchClassDemo()
      {

      }
      
      global Database.QueryLocator start(Database.BatchableContext BC){
      
         return Database.getQueryLocator('SELECT Id__c FROM New__c');
      }
      
      global void execute(Database.BatchableContext BC, List<New__c> scope)
      {
         Set<String> setStrId = new Set<String>();
         List<Old__c> lstOldToUpdate = new list<Old__c>();
         for(New__c objNew : scope)
            if(String.isNotblank(objNew.Id__c))
                setStrId.add(objNew.Id__c)
         for(Old__c objOld : [SELECT Id, checkbox__c FROM Old__c WHERE Id IN: setStrId])
         {
            objOld.checkbox__c = true;
            lstOldToUpdate.add(objOld));
         }
         
         if(!lstOldToUpdate.isEmpty())
            update lstOldToUpdate;
      }
      global void finish(Database.BatchableContext BC){
        
      }
}

Hope this helps! Please let me know if that works for you.

Best regards,
Martijn Schwärzer

Ps. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.