Sub topic 2
Why combine requests
This chapter explains the problem the Composite resources solve. It assumes you can call the Salesforce REST API with an OAuth access token and instance URL. It also assumes you know HTTP methods, status codes, JSON bodies, and the sObject endpoints. Later chapters cover the request format itself.
The cost of one call per operation
The standard REST API takes one operation per HTTP request. Creating an Account, creating a Contact under it, and reading the Contact back takes three calls. Each call has a cost:
- Network round trip. Every request waits for a response before the next can start. If a round trip takes 150 ms, three sequential calls spend about 450 ms on the network alone.
- API call usage. Each request counts against the org's daily API request allocation. Three operations use three calls.
- Client-side glue code. Your code must read the new Account ID from the first response and put it into the second request. It must also decide what to do if a step fails halfway through.
- No shared transaction. Separate requests commit separately. If the Contact insert fails, the Account is already saved, so your client must clean it up.
Composite resources address these costs by letting one HTTP request carry several operations.
What a composite call gives you
A single request to /services/data/v60.0/composite contains a list of subrequests. Salesforce runs them in order on the server and returns one response. This gives you three things:
- Fewer round trips. The client waits on the network once instead of once per operation.
- Fewer API calls. The whole composite request counts as one API call toward your allocation, even though it holds several subrequests. The Composite resources also enforce their own limits on subrequests, which a later chapter covers.
- Dependent operations in one step. A later subrequest can use a value produced by an earlier one, such as a new record ID, without the client in the middle. Reference IDs make this work, and the next chapters cover them.
With allOrNone set to true, the subrequests also run as one transaction, so a failure rolls back the earlier ones. A later chapter covers that flag.
The same work, before and after
Without Composite, the client runs this sequence. The IDs it passes between steps are the part Composite removes.
ACCOUNT_ID=$(curl -s -X POST "$INSTANCE_URL/services/data/v60.0/sobjects/Account" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"Name": "Acme Corp"}' | python3 -c 'import sys, json; print(json.load(sys.stdin)["id"])')
curl -s -X POST "$INSTANCE_URL/services/data/v60.0/sobjects/Contact" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d "{\"LastName\": \"Rivera\", \"AccountId\": \"$ACCOUNT_ID\"}"That is two API calls, two round trips, and a shell variable to carry the ID. If the second call fails, the Account remains. The composite version of this work sends one body to one endpoint and lets Salesforce pass the ID along.
Common errors in this thinking
- Assuming every composite resource is transactional. Only some are. Composite Batch subrequests run independently, so one failure does not undo another. The final chapter compares the resources.
- Expecting speed from server-side work. Composite saves network time and API calls. It does not make each operation cheaper to run on the server.
- Treating one HTTP 200 as full success. The outer call can succeed while a subrequest fails. Always inspect each subrequest result. A later chapter shows how.
Exercise
A client creates an Account, then a Contact for that Account, then reads the Contact back, using three separate REST calls. Name two costs that a single composite request reduces, and one problem it helps with that separate calls leave to the client.
Show answer
It reduces network round trips (one wait instead of three) and API call usage (one call counted instead of three). With separate calls, the client must also copy the new Account ID into the Contact request and clean up the Account itself if the Contact insert fails. A composite request passes the ID server-side through a reference ID, and with allOrNone set to true it rolls back earlier subrequests when a later one fails.