+ Start a Discussion
Michael MMichael M 

How to solve error message?

Hello,
I'm getting this error when I use the below code. The bold part below is the line where it says the error occurred. Here is the error message: 

"execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0 with id 00Q3l00000vUpy4EAC; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: [] "


List<id> refToDelete = new List<id>();
Lead refToUpdate;      
Set<Id> oldReferral =  new Set<id>();
Id cldId;
      for (lead thisLead : trigger.new){
          if (thislead.Existing_Referral__c !=null  && thislead.Community_Lead__c !=null ){
              refToDelete.add(thisLead.id);
              oldReferral.add(thislead.existing_referral__c);
              cldId = thislead.Community_Lead__c;
          }
      }
     //delete new referral
          if (!refToDelete.isempty()){
              system.debug('*****DELETED REFERRAL:  ' + reftodelete);
              database.delete(refToDelete);     }
      
     //populate community lead of existing referral
        if (!oldReferral.isempty()){
refToUpdate = [Select Id, community_lead__c from lead where id in :oldReferral];
            list<lead> updateExisting = new list<lead>();
            if (refToUpdate.community_lead__c == null){
              refToUpdate.Community_Lead__c = cldId;
              //updateExisting.add(l);
           system.debug('****************EXISTING LeadTO UPDATE:  ' + reftoupdate);
            } update refToUpdate;  
Best Answer chosen by Michael M
David Zhu 🔥David Zhu 🔥
I believe this is a partial sandbox. When sampling the data at refreshing sandbox, not all records will be copied to partial sandbox.
In your case, the Lead record is copied but some reference record of the lead record is not copied. This is normal on Partial sandbox.
You can confirm this by open the Lead record in question, find the lookup field, it won't allow you save and tell you which field has missing reference.

All Answers

Anthony McDougaldAnthony McDougald
Hello Michael,
Hope that your day is off to an amazing start. It would seem that you are trying to assign an Id to a field that doesn't accept that's objects Id. Safe to assume that the community_lead__c field is a lookup field that's not related to the same object as the Id in CIdId. Can you add some debug lines to the code to get a better idea what is missing or occuring during the update job.
Best,
Anthony
Michael MMichael M
HI Anthony, I set up some debug logs, and it seems to be that CLid is the same as community_lead__c... 
Here are the screenshots:
     for (lead thisLead : refList){
          if (thislead.Existing_Referral__c !=null  && thislead.Community_Lead__c !=null ){
              refToDelete.add(thisLead.id);
              oldReferral.add(thislead.existing_referral__c);
              cldId = thislead.Community_Lead__c;
          }
      }
     //delete new referral
          if (!refToDelete.isempty()){
              system.debug('*****DELETED REFERRAL:  ' + reftodelete);
              database.delete(refToDelete);     }
      
     //populate community lead of existing referral
        if (!oldReferral.isempty()){
refToUpdate = [Select Id, community_lead__c from lead where id in :oldReferral];
            list<lead> updateExisting = new list<lead>();
            if (refToUpdate.community_lead__c == null){
                system.debug ('**CLID::::::::' + clid);
              refToUpdate.Community_Lead__c = cldId;
              //updateExisting.add(l);
           system.debug('****************EXISTING REFERRAL TO UPDATE:  ' + reftoupdate);
            } update refToUpdate;  

User-added image
 
Michael MMichael M

I also tried removeing the line of the code where I assign "refToUpdate.Community_Lead__c = cldId;", and still got the same error. Any idea what the issue could be?

David Zhu 🔥David Zhu 🔥
You defined refToUpdate as a Lead, I think it shuld be List<Lead> based on the query.

You may change you code as below and try:
 
// remove line "List<Lead> refToUpdate;"  at the top

    //populate community lead of existing referral
    if (!oldReferral.isempty()){
        List<lead> refToUpdate = [Select Id, community_lead__c from lead where id in :oldReferral];
        list<lead> updateExisting = new list<lead>();
        for (Lead l : refToUpdate) {
            if (l.community_lead__c == null){
                l.Community_Lead__c = cldId;
                //updateExisting.add(l);
                system.debug('****************EXISTING LeadTO UPDATE:  ' + l);
            } 
        }
        update refToUpdate;  
    }

 
Michael MMichael M
Hi David, thanks so much for the suggesiton. I just tried that, but i still got the same error. 
David Zhu 🔥David Zhu 🔥
Field community_lead__c  seems a lookup field to another object. Is there filter on the lookup field?
Michael MMichael M
Community_lead__c is a lookup to the community_lead__c object, and there is no filter on the lookup field. 
Michael MMichael M
After more testing, it seems that this error only comes when I try to manipulate Leads that came into the Sandbox when we refreshed our Sandbox. Would that make sense?
David Zhu 🔥David Zhu 🔥
I believe this is a partial sandbox. When sampling the data at refreshing sandbox, not all records will be copied to partial sandbox.
In your case, the Lead record is copied but some reference record of the lead record is not copied. This is normal on Partial sandbox.
You can confirm this by open the Lead record in question, find the lookup field, it won't allow you save and tell you which field has missing reference.
This was selected as the best answer
Michael MMichael M
Interesting. Yes, when I tried to edit any of those Leads that came through the refresh, it won't even let me edit anything. Instead, I immediately get this screen:
User-added image