Sub topic 4
Chaining Subrequests with Reference IDs
This chapter assumes you can send an authenticated REST call to a Salesforce org, read JSON, and know what an Account and a Contact are. It also assumes you have seen the basic shape of a composite request: a compositeRequest array of subrequests, each with a method, url, and body.
What a reference ID does
Every subrequest in a composite call needs a referenceId. It is a label you choose, and it must be unique within the request. Salesforce uses it for two things:
- It identifies the subrequest's result in the
compositeResponse. - It lets later subrequests read values from that result.
To read a value, write @{referenceId.field} inside a later subrequest's URL or body. Salesforce replaces the expression with the real value before it runs that subrequest. This is how you create an Account and then a Contact that points to it, without a round trip in between.
A worked example
The request below makes three subrequests in one transaction. It creates an Account, creates a Contact linked to that Account, then reads the Contact back.
{
"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 to the /composite endpoint:
curl -X POST "$INSTANCE_URL/services/data/v60.0/composite" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d @composite.jsonFollow the chain:
newAccountruns first. Salesforce returns a body such as{"id": "001...", "success": true, "errors": []}.- In
newContact,@{newAccount.id}becomes that new Account ID, so the Contact'sAccountIdis filled in. - In
readContact,@{newContact.id}appears inside the URL. References work in URLs as well as in bodies.
Where references can point
The field name after the dot comes from the response body of the earlier subrequest. For a create call, that is id. For a GET, it is any field in the returned record. You can also reach into nested data and lists. For example, a query subrequest can be referenced as @{myQuery.records[0].Id} to use the first record it returned.
Rules that cause most errors
- Order matters. A reference can only point to a subrequest that appears earlier in the array. Pointing forward fails.
- Unique IDs. Duplicate
referenceIdvalues make the request invalid. - A failed source breaks the chain. If the earlier subrequest fails, it has no result to read. The dependent subrequest cannot run and returns an error instead.
- Exact spelling.
@{newaccount.id}and@{newAccount.id}are different. Match thereferenceIdexactly, and use the same field name that the response contains. - Keep IDs simple. Use letters, numbers, and underscores in a
referenceId. Special characters make the@{...}expression harder to parse and are best avoided.
Checking the result in code
The outer HTTP call can return 200 even when a subrequest fails. Always read the per-subrequest status codes. This Python example posts a request and prints each referenceId with its status.
import os
import requests
base = os.environ['INSTANCE_URL']
headers = {'Authorization': 'Bearer ' + os.environ['ACCESS_TOKEN']}
payload = {
'allOrNone': True,
'compositeRequest': [
{
'method': 'POST',
'url': '/services/data/v60.0/sobjects/Account',
'referenceId': 'newAccount',
'body': {'Name': 'Acme Corp'},
}
],
}
resp = requests.post(base + '/services/data/v60.0/composite', headers=headers, json=payload, timeout=30)
resp.raise_for_status()
for sub in resp.json()['compositeResponse']:
print(sub['referenceId'], sub['httpStatusCode'])Exercise
In a composite request, a subrequest with referenceId "acct" creates an Account. Write the expression a later Contact subrequest should use in its body to set AccountId to the new Account's ID. Then say what happens if that later subrequest appears before "acct" in the array.
Show answer
Use "AccountId": "@{acct.id}". If the Contact subrequest comes before "acct", the reference points to a subrequest that has not run yet, so it cannot be resolved and that subrequest fails with an error.