function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
cduncancduncan 

How do I upsert using Id as the External ID with the REST API?

The REST API documentation states that you can use the Salesforce record ID as an external ID for upserting data.

This example uses the POST method as a special case to insert a record where the Id field is treated as the external ID. Because the value of Id is null, it’s omitted from the request. This pattern is useful when you’re writing code to upsert multiple records by different external IDs and you don’t want to request a separate resource. POST using Id is available in API version 37.0 and later.
https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_upsert.htm

How is this any benefit? Upsert calls with a true external ID all use the PATCH method, but using a Salesforce ID requires POST for new records and PATCH for updating records. With a Salesforce ID, you cannot PATCH a new record, and you cannot POST to an existing record, and the /Id/ part of the path appears to be redundant and unnecessary. How is this pattern actually useful?

SwethaSwetha (Salesforce Developers) 
HI cduncan ,
If you've got the Salesforce record ID, you'll have to use PATCH to update the record. You can't use POST to insert a record by ID as the ID is generated by Salesforce.
 
I'm trying to understand the pattern you want to use. Can you clarify? Thanks
cduncancduncan
It seems to me that a true upsert is not possible using the REST API and a Salesforce record ID, so I don't understand why it's part of the API at all. Consider the following:
  • To insert using Salesforce ID as an external ID, which implies your ID value is null
    • POST to [instance_url]/services/data/v51.0/Account/Id
  • To update using Salesforce ID as an external ID
    • PATCH to [instance_url]/services/data/v51.0/Account/Id/[salesforce_id]
Now, forget about the "using Salesforce ID as an external ID" part:
  • To insert
    • POST to [instance_url]/services/data/v51.0/Account
  • To update using a Salesforce ID
    • PATCH to [instance_url]/services/data/v51.0/Account/[salesforce_id]
The only difference between the two scenarios is the presence of "/Id/" in the URL path…so why even include it at all? Why is it described in the documentation about "upserting" when such an action is not actually possible using a Salesforce record ID?