Sub topic 10
Independent subrequests with Composite Batch
This chapter assumes you can send an authenticated REST call to a Salesforce org, read JSON, and interpret HTTP status codes. It also assumes you have read the earlier chapters on the /composite resource, referenceId, and allOrNone.
What Composite Batch does
Composite Batch runs up to 25 subrequests in one HTTP call to /services/data/v60.0/composite/batch. It saves round trips, like /composite, but it makes different trade-offs:
- Subrequests are independent. One subrequest cannot use another's output, so there is no
@{referenceId.field}syntax. - There is no
allOrNoneflag. Each subrequest commits or fails on its own. - Each subrequest counts toward your API limits.
Use it when you have many unrelated operations, such as fetching metadata, reading a few records, and creating one record.
Request structure
The body has a batchRequests array and an optional haltOnError flag.
{
"batchRequests": [
{
"method": "GET",
"url": "v60.0/sobjects/Account/describe"
},
{
"method": "POST",
"url": "v60.0/sobjects/Account",
"richInput": { "Name": "Batch Account" }
}
],
"haltOnError": false
}Three details differ from /composite:
- The
urlis relative and starts at the version, as inv60.0/sobjects/Account. Do not add/services/data/. - The request data goes in
richInput, notbody. - There is no
referenceId. You match results to requests by their position in the array.
Sending the request
curl -X POST "$INSTANCE_URL/services/data/v60.0/composite/batch" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d @batch.jsonReading the response
The response has a top-level hasErrors boolean and a results array. Each entry holds a statusCode and a result. The results come back in the same order as the requests.
import os
import requests
base = os.environ['INSTANCE_URL']
headers = {'Authorization': 'Bearer ' + os.environ['ACCESS_TOKEN']}
payload = {
'batchRequests': [
{'method': 'GET', 'url': 'v60.0/limits'},
{'method': 'POST', 'url': 'v60.0/sobjects/Account',
'richInput': {'Name': 'Batch Account'}},
],
'haltOnError': False,
}
resp = requests.post(base + '/services/data/v60.0/composite/batch',
headers=headers, json=payload, timeout=30)
resp.raise_for_status()
body = resp.json()
print('hasErrors:', body['hasErrors'])
for i, item in enumerate(body['results']):
print(i, item['statusCode'])The outer HTTP status is usually 200 even when a subrequest fails. Always check hasErrors and each statusCode. Without that check, a failed subrequest goes unnoticed.
Controlling failure with haltOnError
haltOnError decides what happens after a failure.
| Setting | Behavior after a subrequest fails |
|---|---|
| `false` (default) | Later subrequests still run. |
| `true` | Later subrequests are not run and return status 412. |
Setting haltOnError to true only stops later work. It does not roll back earlier subrequests. If request 1 created an Account and request 2 failed, the Account stays in the org.
How it compares
Common mistakes
- Using
bodyinstead ofrichInput. The subrequest then runs without its data and fails validation. - Starting the URL with
/services/data/v60.0/.... In a batch, usev60.0/.... - Trying to reference
@{newAccount.id}. The batch has no reference IDs, so the literal text is sent as a value. - Sending more than 25 subrequests. The call is rejected, so split large jobs into several batches.
- Treating a 200 response as proof that every subrequest succeeded.
Exercise
A batch has three subrequests with haltOnError set to true. The first creates an Account (201), the second creates a Contact with an invalid field (400), and the third queries limits. What status does each subrequest return, and is the Account still in the org?
Show answer
The first returns 201, the second returns 400, and the third is not run and returns 412. The outer call is still HTTP 200 with hasErrors true. The Account stays in the org because Composite Batch never rolls back earlier subrequests.
Exercise
You want to create an Account and a Contact linked to it in one call. Why can't Composite Batch do this, and which resource fits instead?
Show answer
The Contact needs the new Account's ID, and a batch subrequest cannot use another subrequest's result. Use /composite with an @{referenceId.id} reference, or sObject Tree for a parent and child hierarchy.