I'd like to update an Account and create a child Contact in a single transaction using REST API. Tried to use SObject Tree endpoint, but when I simply pass the Id in object body, I get:
Don’t specify the record ID. It’s created automatically and returned
in the response.
Is it even possible with SObject tree? Or perhaps the Composite resource with the mix of POST and PATCH with reference notation ("AccountId": "@{refAccount.id}"
) is the way to go? If so, is SObject Tree just a wrapper to Composite API?
Best Answer
I don't believe this can be done via the sObject Tree resource. The documentation indicates that it's create-only:
and POST-only:
(Emphasis mine).
Conversely, the sObject Composite takes a
POST
request for itself, but allows you to specify the method for each individual subrequest.Here's a variation on the example in the linked document and the example of Account-Contact junction linking above that successfully updates an existing Account and adds a new Contact:
Here, we actually have to specify the Id explicitly in both requests rather than using reference Ids. That's because the reference Id names the response body for each request, so if we did
@{NewAccount.id}
(which works fine in aPOST
create operation), we'd get an error since thePATCH
update response body is empty.