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 

INVALID_CROSS_REFERENCE_KEY error | Lookup or master detail to opportunity

Hello,

I am copying records from one object to another.
On the old object there exisit a lookup(ManagedPackage__Opportunity__c) to opportunity object, and in New object there exisit a Masterdetail(opportunity__c ) to opportunity

x.opportunity__c = c.ManagedPackage__Opportunity__c;

this line gives error when i try to insert like below
User-added image
de|"common.apex.runtime.impl.DmlExecutionException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []"|0x4951b823
 
Best Answer chosen by Ab
John Pipkin 14John Pipkin 14
Sandrine, 

If that is the only thing you are inserting into the object, then that may be your issue. Serializing the object like I posted should give you attributes about the object, along with the fields. But without more information, I cannot say what is going on. The error message you are receiving is extremely generic and cannot be diagnosed with a one line example of your code. 

My advice would be to run an anonymous block of code to test it out. Manually build a new record of the new object. Call Database.setSavePoint() before the insert and Database.rollback() after the insert if you are testing this in production. If you can manually insert the record in the anonymous block, then add a series of debug statements to print ALL attributes of the record you are inserting to see where your code is diffing. If you are still stuck, we will need more information in order to help you debug this. 
 

All Answers

John Pipkin 14John Pipkin 14
Sandrine, 

It looks like it could be because the old record's lookup was blank. Are you ensuring that the lookup field is not null before copying and inserting to the new object?

This error can mean a lot of different things so it would be helpful to add some debug statements for the new record.

Hope that helps
AbAb
i have already verified for blank field, so i am sure this is not the reason
John Pipkin 14John Pipkin 14
Before you insert the record, print a debug statement like so
System.debug(Logginglevel.ERROR,'====>>> new record: '+JSON.serializePretty(x));

assuming that "x" is the variable of the object you are inserting. Check all the lookup fields for invalid entries.
AbAb
After i print this line, for opportunity part, i get below correspnding lines


09:46:35:000 USER_DEBUG   "opportunity__c" : "006b0000004m28yAAA"

09:46:35:000 USER_DEBUG   "opportunity__c" : "006b0000005TWMmAAO"

09:46:35:000 USER_DEBUG   "opportunity__c" : "006b0000005TkYnAAK"

09:46:35:000 USER_DEBUG   "opportunity__c" : "006b0000005TkYnAAK"



I have a list which i am updating 
John Pipkin 14John Pipkin 14
Sandrine, 

If that is the only thing you are inserting into the object, then that may be your issue. Serializing the object like I posted should give you attributes about the object, along with the fields. But without more information, I cannot say what is going on. The error message you are receiving is extremely generic and cannot be diagnosed with a one line example of your code. 

My advice would be to run an anonymous block of code to test it out. Manually build a new record of the new object. Call Database.setSavePoint() before the insert and Database.rollback() after the insert if you are testing this in production. If you can manually insert the record in the anonymous block, then add a series of debug statements to print ALL attributes of the record you are inserting to see where your code is diffing. If you are still stuck, we will need more information in order to help you debug this. 
 
This was selected as the best answer