+ Start a Discussion

Creating Parent Child Record in one dml ?



i have parent child relationship between contract line item (its custom object not the sfdc standard object), i like to know how to create both parent and child in one dml statement and let salesforce do the work of maping relationship.


example of what I'm expecting, where parentId refers parent contractlineitem record


ContractLineItem parent = new ContractLineItem();

ContractLineItem child = new ContractLineItem();

child.parentId = parent;

isnert new List<ContractLineItem>{parent, child};



2. also jsut curious, if the same case is possible for cross objects, i mean account/cases. assuming account is parent of cases ?









You cannot do this.


You have to create the parent then the child. mapping the relationship yourself


You can do this, but you need to use external id fields.  I wrote up a blog post on this last weekend:




seems like resonable, still the only problem is you need the external Id to be "Unique" . The externalId can be unique if its coming from thrid party source or through integeration, but  if we have to generate one, its going to be tricky.


Thanks to both of you.