You need to sign in to do that
Don't have an account?
Kelly K
Upsert not working on cloned opp
I haven't seen this before and I'm having trouble figuring out why this isn't working. I have a class that handles adding OpportunityContactRoles to an opportunity on create, on account Id change, and on close of an opportunity - the test class for that passes, no issues has been working great for a few years.
I was working on new code that involves updating the opportunity record type, owner, and a few other fields and in some cases cloning the opportunity. My test class for this is failing. The error I'm getting is
System.DmlException: Update failed. First exception on row 0 with id 006180000023YfvAAE; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Upsert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: []: []: []
If I turn off the OpportunityContactRoles class I mentioned, my new test class passes 100%. So I've added comments all the way through the contact roles class and I've found that once it hits the upsert statement is what is causing this to happen.
Is there any particular reason why an opportunity being created using the .clone() method would cause issues with opportunityContactRoles?
Opportunity clonedOpp = opportunity.clone(false, false, true, false);
I was working on new code that involves updating the opportunity record type, owner, and a few other fields and in some cases cloning the opportunity. My test class for this is failing. The error I'm getting is
System.DmlException: Update failed. First exception on row 0 with id 006180000023YfvAAE; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Upsert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: []: []: []
If I turn off the OpportunityContactRoles class I mentioned, my new test class passes 100%. So I've added comments all the way through the contact roles class and I've found that once it hits the upsert statement is what is causing this to happen.
Is there any particular reason why an opportunity being created using the .clone() method would cause issues with opportunityContactRoles?
Opportunity clonedOpp = opportunity.clone(false, false, true, false);
All Answers
The issue isn't the Id of the opportunity, it's the Id of the opportunity contact role from the other class. If I comment out the call for the contact roles class in my after insert trigger, my test class for the new code passes fine.
Also - this is the original error: Update failed. First exception on row 0 with id 006180000023YkqAAE; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Attempt to de-reference a null object: []: []
The error I have listed above I had tried breaking the upsert into insert/update to see if that had any effect (it didn't).
I tried creating the opportunity manually instead of a clone to see if that resolves the error and it doesn't.
Just not sure why when I insert the opportunity record that the contact role upsert is throwing an error about a null contact role Id.
I know - I've already spent well over 12 hours getting to the core of the issue. So here's what I got. I'm sharing the opportunityaddcontactroles class and 3 other classes. The oppportunityPrequalOppConversion (this is runs in a before insert/update context), the OpportunityPrequalOppConvCreate (runs in after insert/update context), the OpportunityAddContactRoles (after insert/update context - excuse the comments), and the OpportunityPrequalOppConversionTest. At the end, I have a screenshot of the log and the error message.
I added a null check to the front of the for loop and it appears to have fixed both errors.
Also appears when I wrote the test class I didn't have it check for no contacts (thus causing a null map) either, so likely why I'm seeing it now.