( INVALID_FIELD_FOR_INSERT_UPDATE, Attempting to update (as part of an upsert) parent field)Upsert failed

I am using the upsert statement to update the custom object Customer__c with two master-detail fields. The fields reference custom Contact__c and Subject__c objects.

Customer__c c = new Customer__c();
c.Contact__c = ConId;
c.Subject__c = SubId;
c.External_Id__c = '100201';
upsert c External_Id__c;

While trying this, I am getting an exception:

System.DmlException: Upsert failed. First exception on row 0; first error: INVALID_FIELD_FOR_INSERT_UPDATE, Attempting to update (as part of an upsert) parent field Contact__c with new value 003g000000aaayLAAQ, current value is 003g0000009c9xyAAY: [Contact__c]

It’s working in a developer sandbox and not working in a full copy sandbox.

Note: In the full copy sandbox, Person Account is Enabled.

Answer

This error is telling you that you already have a record with field external_Id__c set to '100201'. When you do the upsert, it’s trying to update the existing record with a new value for Contact__c.

You need to have the checkbox for Child records can be reparented to other parent records after they are created selected on the Contact__c field for the upsert to work.

Attribution
Source : Link , Question Author : Venkatsforce , Answer Author : tomlogic

Leave a Comment