Initially when a form is saved it inserts both parent and child object records in one DML Statement using parent external Id. later when same form is edited then it should insert as a form that is both parent and child saves as new record, at this point system is throwing following exception even after setting parent Id as 'Null'.

Insert failed. First exception on row 1; first error: INVALID_FIELD, Cannot specify both an external ID reference parent_Id__r and a salesforce id, parent_Id__c: []

Below is the method sets the parent Id as null.

 public static list<sobject> resetRecordIdsInList(list<sobject> sObjs){

        for(sobject s : sObjs){

            s.put('Id', Null);
            s.putSObject('parent_ID__r', new parent__c(External_Id__c = ExternalId));

         return sObjs; 
  • 1
    Is parent_Id__c a lookup or an external ID field. Seems you are trying to assign an external ID field to a Lookup field..
    – Eric
    Oct 29, 2015 at 17:08
  • Sorry, it is external Id field in parent object which i have updated in the post.
    – vraavi
    Oct 29, 2015 at 18:27
  • « When you edit the form » of an existing couple parent+child, are you sure you’re using update and not insert ?
    – altius_rup
    Dec 27, 2017 at 21:24

1 Answer 1


You can't specify both an ID (in this case, null), and an external ID (which presumably would generate a non-null value). Do not specify both Parent_Id__c and Parent_Id__r on the same record.

  • If i don't specify parent_Id__c system is throwing same error because it is retaining parentId at the time of loading the prior version of the form. Following is the query to display version of previous submit. when i debug the results system also prints child id and also parentid in child record. select Id,(select Name__c from child__r) from parent__c where id =: parentId. Next child records from query results are passed to the method mentioned in main post.
    – vraavi
    Oct 29, 2015 at 18:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.