Sub topic 5
Reducing round trips with Composite and Batch resources
This chapter assumes you can authenticate with OAuth 2.0, run SOQL, and create records with the REST API. It builds on those skills to cut the number of HTTP calls an integration makes.
Why round trips matter
Each REST call has network latency and counts against your org's API request limit. Creating an Account and then a Contact takes two calls, and the second needs the Id returned by the first. Composite and Batch resources let you send several subrequests in one HTTP call.
Three resources, three behaviors
| Resource | Path | Subrequests | Dependencies between them | Rollback |
|---|---|---|---|---|
| Composite | `/composite` | Up to 25 | Yes, through reference IDs | Optional with `allOrNone` |
| Batch | `/composite/batch` | Up to 25 | No, each is independent | No |
| sObject Collections | `/composite/sobjects` | Up to 200 records | No | Optional with `allOrNone` |
Salesforce can change these limits between releases. Check the REST API Developer Guide for your API version.
Composite: chained requests
Composite sends an ordered list of subrequests. Each has a method, a url, a referenceId, and usually a body. Later subrequests can read the results of earlier ones with the syntax @{referenceId.field}.
The request below creates an Account and then a Contact linked to it. Send it with POST /services/data/v60.0/composite.
{
"allOrNone": true,
"compositeRequest": [
{
"method": "POST",
"url": "/services/data/v60.0/sobjects/Account",
"referenceId": "newAccount",
"body": { "Name": "Acme Corp" }
},
{
"method": "POST",
"url": "/services/data/v60.0/sobjects/Contact",
"referenceId": "newContact",
"body": { "LastName": "Smith", "AccountId": "@{newAccount.id}" }
}
]
}The Contact's AccountId comes from the first result. With "allOrNone": true, a failure in either subrequest rolls back both. With false, the successful subrequests are kept.
Batch: independent requests
Batch runs subrequests that do not depend on each other, such as fetching several unrelated records. A failed subrequest does not stop the others. Each result carries its own statusCode, so the client must check each one.
Common errors
- Reading only the HTTP status. A composite call can return
200 OKwhile individual subrequests failed. Always inspect each entry incompositeResponse. - Misspelled reference IDs. A reference to a name that does not exist fails that subrequest. Subrequests that depend on it fail too.
- Forgetting
allOrNone. The default isfalse, so a partial success can leave orphaned records, such as an Account with no Contact. - Exceeding the subrequest cap. Split larger workloads into several calls, or use Bulk API 2.0 for large volumes.
What counts against limits
A composite call counts as one API request against the daily limit, even with many subrequests. The subrequests still consume the same governor resources, such as DML and query limits, as if you had sent them individually. Composite reduces latency and call counts. It does not make the underlying work cheaper.
Exercise
An integration must create an Account, then a Contact that references the new Account's Id, and must leave no data behind if either step fails. Which resource and setting do you use, and why?
Show answer
Use the Composite resource with allOrNone set to true. A reference ID (@{newAccount.id}) passes the Account Id to the Contact subrequest. allOrNone rolls back both records if either fails. Batch cannot pass results between subrequests and has no rollback.
Exercise
A Composite call returns HTTP 200. Can you conclude every subrequest succeeded?
Show answer
No. The HTTP status describes the composite call itself. You must check the status code of each entry in compositeResponse, because subrequests can fail individually.