function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
TheReportDoctorTheReportDoctor 

Mother - Child relationship

I want to add a new mother object record and add a new child record.  I cannot insert the mother record until the end of the batch.  There will be multiple mother records.  How can I give the child record the mother's ID before the mother is saved?  Can I add the mother and child in the same DML add function?  It doesn't work for me currently.

 

 

Anand@SAASAnand@SAAS
Since the "parent" and child are two different entities/objects you will need two DML "insert" calls. You can not have a composite "List" in an "insert" Once you insert the "mother", you can assign the Id of corresponding field on the child and then insert the child.
TheReportDoctorTheReportDoctor
Having to successful insert the mother before the child causes the too man 21 DML error.
Anand@SAASAnand@SAAS

Some code would help to understand your problem. In this case, if you are inserting the parent record in a loop you might have the too many DML governor issue. Try doing it in bulk.

  

TheReportDoctorTheReportDoctor
Anand@SAAS, you have described the problem.  If you do the parent in bulk how do you know the parent ID that belongs to each child?  You have to insert the parent first, get the parent ID and then put the parentID in the child.  The child can be done in bulk but the parent has to be done one at a time.  The soultion would be if the when you "add" the parent record before you insert the bulk if you could get an Id or a temporary link.