Newer Version Available
Create Nested Records
Use the SObject Tree resource to create nested records that share a root record type. For example, in a single
request, you can create an account along with its child contacts, and a second account along
with its child accounts and contacts. Once the request is processed, the records are created
and parents and children are automatically linked by ID. In the request data, you supply the
record hierarchies, required and optional field values, each record’s type, and a reference
ID for each record, and then use the POST method of the resource. The response body will
contain the IDs of the created records if the request is successful. Otherwise, the response
contains only the reference ID of the record that caused the error and the error
information.
The following example creates two sets of nested records. The first set includes an account and two child contact records. The second set includes an account, one child account record, and one child contact record. The record data is provided in newrecords.json.
- Example for creating two new accounts and their child records
-
1curl https://yourInstance.salesforce.com/services/data/v34.0/composite/tree/Account/ -H "Authorization: Bearer token -H "Content-Type: application/json" -d "@newrecords.json" - Example request body newrecords.json file for creating two new Accounts and their child records
-
1{ 2"records" :[{ 3 "attributes" : {"type" : "Account", "referenceId" : "ref1"}, 4 "name" : "SampleAccount1", 5 "phone" : "1234567890", 6 "website" : "www.salesforce.com", 7 "numberOfEmployees" : "100", 8 "industry" : "Banking", 9 "Contacts" : { 10 "records" : [{ 11 "attributes" : {"type" : "Contact", "referenceId" : "ref2"}, 12 "lastname" : "Smith", 13 "Title" : "President", 14 "email" : "sample@salesforce.com" 15 },{ 16 "attributes" : {"type" : "Contact", "referenceId" : "ref3"}, 17 "lastname" : "Evans", 18 "title" : "Vice President", 19 "email" : "sample@salesforce.com" 20 }] 21 } 22 },{ 23 "attributes" : {"type" : "Account", "referenceId" : "ref4"}, 24 "name" : "SampleAccount2", 25 "phone" : "1234567890", 26 "website" : "www.salesforce.com", 27 "numberOfEmployees" : "52000", 28 "industry" : "Banking", 29 "childAccounts" : { 30 "records" : [{ 31 "attributes" : {"type" : "Account", "referenceId" : "ref5"}, 32 "name" : "SampleChildAccount1", 33 "phone" : "1234567890", 34 "website" : "www.salesforce.com", 35 "numberOfEmployees" : "100", 36 "industry" : "Banking" 37 }] 38 }, 39 "Contacts" : { 40 "records" : [{ 41 "attributes" : {"type" : "Contact", "referenceId" : "ref6"}, 42 "lastname" : "Jones", 43 "title" : "President", 44 "email" : "sample@salesforce.com" 45 }] 46 } 47 }] 48} - Example response body after successfully creating records and relationships
-
1{ 2 "hasErrors" : false, 3 "results" : [{ 4 "referenceId" : "ref1", 5 "id" : "001D000000K0fXOIAZ" 6 },{ 7 "referenceId" : "ref4", 8 "id" : "001D000000K0fXPIAZ" 9 },{ 10 "referenceId" : "ref2", 11 "id" : "003D000000QV9n2IAD" 12 },{ 13 "referenceId" : "ref3", 14 "id" : "003D000000QV9n3IAD" 15 },{ 16 "referenceId" : "ref5", 17 "id" : "001D000000K0fXQIAZ" 18 },{ 19 "referenceId" : "ref6", 20 "id" : "003D000000QV9n4IAD" 21 }] 22}
Once the request is processed, all six records are created with the parent-child relationships specified in the request.