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
AbAb 

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 Ab
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.