I often encounter usecases where i need to insert nested objects.
While i know that it is feasible to leverage external Ids to achieve the insert in a single DML operation
Account a = new Account(Name='Phil Household', theExternalId__c = 'externalValue');
Contact c = new Contact (FirstName = 'Phil', LastName = 'Uyt');
c.Account = new Account(theExternalId__c = 'externalValue');
toInsert.add(a);
toInsert.add(c);
insert toInsert;
I see that using the REST api composite tree, i am able to perform similar operation EXCEPT that i don't need to create external ID fields across the objects to be able to nest them in a single operation.
Indeed, documentation states that with this composit tree request :
The request can contain the following:
- Up to a total of 200 records across all trees
- Up to five records of different types
- sObject trees up to five levels deep
Does anyone know if there is an exivalent to that action in APEX that would greatly simplify my (our?) lives?
refs :
Sobject tree : https://developer.salesforce.com/docs/atlas.en-us.234.0.api_rest.meta/api_rest/resources_composite_sobject_tree.htm
Nested insert : https://developer.salesforce.com/docs/atlas.en-us.234.0.api_rest.meta/api_rest/dome_composite_sobject_tree_create.htm