Sub topic 9
Bulk operations with sObject Collections
This chapter assumes you can send authenticated REST calls and read the compositeResponse format from earlier chapters. sObject Collections is a separate resource under /composite/sobjects. It runs one operation on up to 200 records in a single call.
What Collections does
A Composite request chains different operations. A Collections request does the same operation many times. One call counts as one API request, whether it touches 2 records or 200.
| Operation | Method and URL | Input |
|---|---|---|
| Create | `POST /composite/sobjects` | `records` array |
| Update | `PATCH /composite/sobjects` | `records` array, each with `Id` |
| Upsert | `PATCH /composite/sobjects/Account/External_Id__c` | `records` array with the external ID field |
| Retrieve | `GET /composite/sobjects/Account?ids=...&fields=...` | Query parameters |
| Delete | `DELETE /composite/sobjects?ids=...` | Query parameter |
Creating records
Each record in the body needs an attributes.type entry that names its object. The allOrNone flag sits at the top level of the body.
{
"allOrNone": false,
"records": [
{ "attributes": { "type": "Account" }, "Name": "Bulk Account 1" },
{ "attributes": { "type": "Account" }, "Name": "Bulk Account 2" }
]
}Send it to POST /services/data/v60.0/composite/sobjects. Updates use the same shape. Each record must also carry an Id, and only the fields you include change.
Reading the results
The response is a JSON array, not a compositeResponse object. The results appear in the same order as the records you sent. Each entry has success, an errors array, and an id for created records.
import os
import requests
base = os.environ['INSTANCE_URL']
headers = {'Authorization': 'Bearer ' + os.environ['ACCESS_TOKEN']}
payload = {
'allOrNone': False,
'records': [
{'attributes': {'type': 'Account'}, 'Name': 'Bulk Account 1'},
{'attributes': {'type': 'Account'}}, # missing Name, will fail
],
}
resp = requests.post(
base + '/services/data/v60.0/composite/sobjects',
headers=headers, json=payload, timeout=30,
)
resp.raise_for_status()
for record, result in zip(payload['records'], resp.json()):
if result['success']:
print('created', result['id'])
else:
print('failed', result['errors'][0]['statusCode'], result['errors'][0]['message'])Matching by position lets you tell which input produced which error.
allOrNone in Collections
The default for allOrNone is false, which is the opposite of what many developers assume.
- With
false, valid records save and invalid ones return errors. The HTTP status is still 200 in most cases. - With
true, one failure rolls back the whole call. The failing record reports its real error. Every other record reportsALL_OR_NONE_OPERATION_ROLLED_BACK.
For DELETE, pass allOrNone as a query parameter: ?ids=001...,001...&allOrNone=true.
Limits and gotchas
- The limit is 200 records per call. Split larger sets into chunks of 200 or fewer.
- A call that exceeds the limit is rejected as a whole.
- Triggers, flows, and validation rules still run. Heavy automation can hit governor limits when it processes 200 records in one transaction.
- Collections cannot reference earlier results, so there is no
@{referenceId}syntax. To create a parent and its children, use sObject Tree or Composite instead. - Always check
successon each element. A 200 status does not mean every record saved.
Exercise
You send 150 Account records to POST /composite/sobjects with allOrNone set to true. Record 40 fails a validation rule. What does the response contain for the other 149 records, and what should your client do next?
Show answer
Each of the other 149 entries has success false and the error code ALLORNONEOPERATIONROLLED_BACK, and record 40 reports its real validation error. Nothing was saved. The client should fix record 40 and resend the batch. It can also resend with allOrNone false if partial success is acceptable.