[SalesForce] Error: INVALID_FIELD, Cannot specify both an external ID reference and a salesforce id

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.put('parent_Id__c',Null);
            s.putSObject('parent_ID__r', new parent__c(External_Id__c = ExternalId));
        }

         return sObjs; 
    }

Best Answer

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.