You need to sign in to do that
Don't have an account?
Michael 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;
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;
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
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
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;
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?
You may change you code as below and try:
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.