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.

  • can you post the comment here? Nov 25, 2013 at 13:26
  • Hi crmprogdev Can you kindly help... Nov 25, 2013 at 14:06

2 Answers 2


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.

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]

The above message is telling you that you're trying to use an invalid field to perform an update via the upsert command.

Looks like you may have some formatting issues. It appears as though Customer__c is an object? If so, I'd expect your code to look more like the following:

<Customer__c> c=new <Customer__c>();

c.Contact__c = ConId; // is ConId already assigned a value via the M-D? 
// Since this is a Master Detail relationship, this one and the one below could be creating the conflict, esp if it's affected by the external Id.

c.subject__c =SubId; // is SubId already assigned a value via the M-D? The same comment above applies regarding the M-D relationship

c.external_Id__c = '100201';
// I believe the external ID would be a field of Customer__c, and should be assigned as shown.

upsert c;
// I think you just need to upsert c without using the format you posted above.
  • Thanks crmprogdev for your reply. already we tried what you said but that throw duplicate exception because we are used external_Id__c. can you kindly help Nov 25, 2013 at 14:27
  • Have you perhaps created a many to many relationship with your junction object? As in which direction does the M-D go between the junction object to the other objects? By adding the external ID to the junction object, I think you've made it unique and not an actual child!
    – crmprogdev
    Nov 25, 2013 at 14:31
  • i am not got your idea. can you kindly explain bit more.. Nov 25, 2013 at 14:48
  • Which is the Master and which is the Detail on each of the Objects? Contacts can only be a Detail, not a Master. If Contact__c is the Master to the other object, you created a many to many relationship between the two, not a 1 to 1. If Contact__c is Detail, then you would have a 1 to 1 between the other object and Contacts. The external ID on Contact__c also adds an additional constraint to the junction object.
    – crmprogdev
    Nov 25, 2013 at 15:03
  • Contact is Master Object and Subject__c is Master Object we are created junction object called "Customer__c". in Customer__c object Contact and Subject__c fields are Master detail Relationship.. we are created external_id__c field in Customer__c object for preventing the duplicate records. while trying to create a records we got above exception Nov 25, 2013 at 15:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .