Sub topic 3
Anatomy of a composite request
This chapter assumes you can send authenticated REST calls to a Salesforce org. You need an OAuth access token and the instance URL. You should also know HTTP methods, JSON bodies, and the sObject endpoints such as /sobjects/Account.
One envelope, many subrequests
A composite request is a single POST to the /composite resource. Its body wraps a list of ordinary REST calls. Salesforce runs them in order on the server and returns one response containing one result per call.
POST {instance_url}/services/data/v60.0/compositeThe top-level fields
The request body is a JSON object with these fields:
| Field | Required | Purpose |
|---|---|---|
| `compositeRequest` | Yes | The ordered array of subrequests. |
| `allOrNone` | No | If `true`, any failure rolls back the whole request. The default is `false`. |
| `collateSubrequests` | No | If `true`, Salesforce may group independent subrequests to run them faster. The default is `false`. |
This chapter focuses on compositeRequest. The allOrNone flag gets its own chapter.
The fields of a subrequest
Each element of compositeRequest describes one REST call:
| Field | Required | Purpose |
|---|---|---|
| `method` | Yes | The HTTP method: `GET`, `POST`, `PATCH`, `PUT`, or `DELETE`. |
| `url` | Yes | The resource path, starting at `/services/data/vXX.X/`. |
| `referenceId` | Yes | A name for this subrequest, unique within the request. |
| `body` | For writes | The JSON payload, as in a normal call. |
| `httpHeaders` | No | Extra headers for this subrequest only. |
The url is a path, not a full address. Leave out the host, because the composite call already knows the org.
The referenceId identifies the subrequest in the response. It also lets later subrequests use its results. The next chapter covers that.
A complete example
This request creates an Account, creates a Contact under it, and reads the Contact back. The @{newAccount.id} value is a reference to the first subrequest's result.
{
"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": "Rivera", "AccountId": "@{newAccount.id}" }
},
{
"method": "GET",
"url": "/services/data/v60.0/sobjects/Contact/@{newContact.id}?fields=Id,LastName,AccountId",
"referenceId": "readContact"
}
]
}Save it as composite.json and send it:
curl -X POST "$INSTANCE_URL/services/data/v60.0/composite" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d @composite.jsonWhat comes back
The response has one top-level property, compositeResponse. It is an array in the same order as your subrequests. Each entry contains:
referenceId, which matches the one you sent.httpStatusCode, such as201for a create or200for a read.body, the payload a standalone call would have returned.httpHeaders, such as theLocationheader on a create.
Use referenceId to match results to requests. Do not rely on position alone.
Common mistakes
- Duplicate
referenceIdvalues. Each one must be unique, or the request is rejected. - A full URL in
url. Use the path that starts with/services/data/. - Mixed API versions. Keep the version in the subrequest URLs consistent with the version in the composite endpoint.
- Putting a write payload in
richInput. That name belongs to Composite Batch. Composite usesbody.
Exercise
A composite request contains two subrequests that both use the referenceId "rec1". What is wrong, and which field of a subrequest should you change?
Show answer
Each referenceId must be unique within the request, so Salesforce cannot tell the two subrequests apart. Change the referenceId on one of them to a different name, such as "rec2".