Newer Version Available

This content describes an older version of this product. View Latest

Execute Dependent Requests in a Single API Call

The following example uses the Composite resource to execute several dependent requests all in a single call. First, it creates an account and retrieves its information. Next it uses the account data and the Composite resource’s reference ID functionality to create a contact and populate its fields based on the account data. Then it retrieves specific information about the account’s owner by using query parameters in the request string. Finally, if the metadata has been modified since a certain date, it retrieves account metadata. The composite.json file contains the composite request and subrequest data.
Execute dependent requests in a single API call
1curl https://MyDomainName.my.salesforce.com/services/data/v60.0/composite/ -H "Authorization: Bearer token -H "Content-Type: application/json" -d "@composite.json"
Request body composite.json file
1{
2    "allOrNone" : true,
3    "compositeRequest" : [{
4        "method" : "POST",
5        "url" : "/services/data/v60.0/sobjects/Account",
6        "referenceId" : "NewAccount",
7        "body" : {  
8            "Name" : "Salesforce",
9            "BillingStreet" : "Landmark @ 1 Market Street",
10            "BillingCity" : "San Francisco",
11            "BillingState" : "California",
12             "Industry" : "Technology"
13        }
14    },{
15        "method" : "GET",
16        "referenceId" : "NewAccountInfo",
17        "url" : "/services/data/v60.0/sobjects/Account/@{NewAccount.id}"
18    },{
19        "method" : "POST",
20        "referenceId" : "NewContact",
21        "url" : "/services/data/v60.0/sobjects/Contact",
22        "body" : {  
23            "lastname" : "John Doe",
24            "Title" : "CTO of @{NewAccountInfo.Name}",
25            "MailingStreet" : "@{NewAccountInfo.BillingStreet}",
26            "MailingCity" : "@{NewAccountInfo.BillingAddress.city}",
27            "MailingState" : "@{NewAccountInfo.BillingState}",
28            "AccountId" : "@{NewAccountInfo.Id}",
29            "Email" : "jdoe@salesforce.com",
30            "Phone" : "1234567890"
31        }
32    },{
33        "method" : "GET",
34        "referenceId" : "NewAccountOwner",
35        "url" : "/services/data/v60.0/sobjects/User/@{NewAccountInfo.OwnerId}?fields=Name,companyName,Title,City,State"
36    },{
37        "method" : "GET",
38        "referenceId" : "AccountMetadata",
39        "url" : "/services/data/v60.0/sobjects/Account/describe",
40        "httpHeaders" : {
41            "If-Modified-Since" : "Tue, 31 May 2016 18:13:37 GMT"
42        }
43    }]
44}
Response body after successfully executing the composite request
1{
2    "compositeResponse" : [{
3        "body" : {
4            "id" : "001R00000033JNuIAM",
5            "success" : true,
6            "errors" : [ ]
7        },
8        "httpHeaders" : {
9          "Location" : "/services/data/v60.0/sobjects/Account/001R00000033JNuIAM"
10        },
11        "httpStatusCode" : 201,
12        "referenceId" : "NewAccount"
13    },{
14        "body" : {
15            all the account data
16        },
17        "httpHeaders" : {
18            "ETag" : "\"Jbjuzw7dbhaEG3fd90kJbx6A0ow=\"",
19            "Last-Modified" : "Fri, 22 Jul 2016 20:19:37 GMT"
20        },
21        "httpStatusCode" : 200,
22        "referenceId" : "NewAccountInfo"
23    },{
24        "body" : {
25            "id" : "003R00000025REHIA2",
26            "success" : true,
27            "errors" : [ ]
28        },
29        "httpHeaders" : {
30            "Location" : "/services/data/v60.0/sobjects/Contact/003R00000025REHIA2"
31        },
32        "httpStatusCode" : 201,
33        "referenceId" : "NewContact"
34    },{
35        "body" : {
36            "attributes" : {
37            "type" : "User",
38            "url" : "/services/data/v60.0/sobjects/User/005R0000000I90CIAS"
39            },
40            "Name" : "Jane Doe",
41            "CompanyName" : "Salesforce",
42            "Title" : Director,
43            "City" : "San Francisco",
44            "State" : "CA",
45            "Id" : "005R0000000I90CIAS"
46        },
47        "httpHeaders" : { },
48        "httpStatusCode" : 200,
49        "referenceId" : "NewAccountOwner"
50    },{
51        "body" : null,
52        "httpHeaders" : {
53            "ETag" : "\"f2293620\"",
54            "Last-Modified" : "Fri, 22 Jul 2016 18:45:56 GMT"
55         },
56        "httpStatusCode" : 304,
57        "referenceId" : "AccountMetadata"
58    }]
59}