Sub topic 13
Creating and updating many records with Composite sObjects
This chapter assumes you can already create and update single records with POST and PATCH, and that you have SF_INSTANCE_URL, SF_ACCESS_TOKEN, and SF_API_VERSION set as in the earlier chapters.
Why use Composite sObjects
Single-record calls cost one HTTP request per record. Loading 150 accounts that way means 150 round trips and 150 API calls counted against your org's limits. The Composite sObjects resource accepts up to 200 records in one request. It is a good fit for batches of a few hundred records or fewer. Use Bulk API 2.0 for larger loads.
| Operation | Method and URL | Body |
|---|---|---|
| Create many | `POST /services/data/vXX.X/composite/sobjects` | `records` array |
| Update many | `PATCH /services/data/vXX.X/composite/sobjects` | `records` array, each with `Id` |
| Delete many | `DELETE /services/data/vXX.X/composite/sobjects?ids=id1,id2` | None |
Creating many records
The body has two parts. records is an array of records, and each record needs an attributes object that names its sObject type. allOrNone is a boolean that sets what happens when some records fail.
curl -X POST "$SF_INSTANCE_URL/services/data/$SF_API_VERSION/composite/sobjects" \
-H "Authorization: Bearer $SF_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"allOrNone": false,
"records": [
{"attributes": {"type": "Account"}, "Name": "Acme Corp", "Industry": "Technology"},
{"attributes": {"type": "Account"}, "Industry": "Banking"}
]
}'The second record has no Name, so it fails. The response is a JSON array with one result per record, in the same order as the request:
[
{
"id": "001xx000003DGb2AAG",
"success": true,
"errors": []
},
{
"success": false,
"errors": [
{
"statusCode": "REQUIRED_FIELD_MISSING",
"message": "Required fields are missing: [Name]",
"fields": ["Name"]
}
]
}
]Note that the error object uses statusCode, not the errorCode key you saw in single-record errors.
Reading the result correctly
The HTTP status of the whole call is 200 even when individual records fail. Checking response.ok is therefore not enough. You must inspect every item in the array.
allOrNone controls the failure behavior:
false(the default): each record succeeds or fails on its own. Good records are saved.true: if any record fails, Salesforce rolls back the whole request. The other records report an error with the codeALL_OR_NONE_OPERATION_ROLLED_BACK.
Updating many records
An update uses PATCH on the same URL. Each record carries its Id and its type, plus only the fields to change. The response has the same array shape as create.
import os
import requests
instance_url = os.environ["SF_INSTANCE_URL"]
token = os.environ["SF_ACCESS_TOKEN"]
version = os.environ.get("SF_API_VERSION", "v60.0")
url = f"{instance_url}/services/data/{version}/composite/sobjects"
headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json",
}
payload = {
"allOrNone": False,
"records": [
{"attributes": {"type": "Account"}, "Id": "001xx000003DGb2AAG", "Industry": "Banking"},
{"attributes": {"type": "Account"}, "Id": "001xx000003DGb3AAG", "Industry": "Energy"},
],
}
response = requests.patch(url, headers=headers, json=payload, timeout=30)
response.raise_for_status() # catches request-level problems such as 401
for record, result in zip(payload["records"], response.json()):
if result["success"]:
print(f"Updated {record['Id']}")
else:
print(f"Failed {record['Id']}: {result['errors']}")Here raise_for_status() handles request-level failures such as an expired token (401) or malformed JSON (400). The loop handles per-record failures. Matching results to inputs with zip works because the order is preserved.
Upserts and limits
Composite sObjects also supports upsert. Send PATCH to /composite/sobjects/Account/External_Id__c with an allOrNone flag and a records array that includes the external ID field in each record. To stay within the 200-record cap, split larger lists into chunks of 200 and send each chunk as its own request.
Exercise
You send a composite create with allOrNone set to false. The HTTP status is 200, but one of 50 records had a missing required field. How do you find out which record failed, and what happens to the other 49?
Show answer
Read the response array, which is in the same order as the request, and look for the item with "success": false. Its errors list gives the statusCode and message. The other 49 records are saved, because allOrNone is false.