Activity Forums Salesforce® Discussions Is it possible to insert a Salesforce Parent-Child record in one DML operation? Reply To: Is it possible to insert a Salesforce Parent-Child record in one DML operation?

  • shariq

    Member
    September 19, 2018 at 9:01 pm

    Hi,

    You can use external ID fields as foreign keys to create parent and child records of different sObject types in a single step instead of creating the parent record first, querying its ID, and then creating the child record. To do this:Create the child sObject and populate its required fields, and optionally other fields.
    Create the parent reference sObject used only for setting the parent foreign key reference on the child sObject. This sObject has only the external ID field defined and no other fields set.
    Set the foreign key field of the child sObject to the parent reference sObject you just created.
    Create another parent sObject to be passed to the insert statement. This sObject must have the required fields (and optionally other fields) set in addition to the external ID field.
    Call insert by passing it an array of sObjects to create. The parent sObject must precede the child sObject in the array, that is, the array index of the parent must be lower than the child’s index.

    Hope this helps.