2

Salesforce offers a way to upsert a record by a given External Id:

curl https://na1.salesforce.com/services/data/v20.0/sobjects/Account/customExtIdField__c/11999 -H "Authorization: Bearer token" -H "Content-Type: application/json" -d @newrecord.json -X PATCH

But if it doesn't find an Account with customExtIdField__c equaling to 11999, it will create a new record instead.

Is there a way to restrict the operation to only perform updates if a relevant record already exists, and throw an exception when it doesn't?

1 Answer 1

5

A reasonable workaround I found is to use a Composite request: in step 1, retrieve record Id, in step 2, update record by Id using normal PATCH method.

POST /services/data/v53.0/composite

{
    "compositeRequest":
    [
        {
            "method": "GET",
            "url": "/services/data/v53.0/sobjects/Account/customExtIdField__c/11999?fields=Id",
            "referenceId": "refAccount"
        },
        {
            "method": "PATCH",
            "url": "/services/data/v53.0/sobjects/Account/@{refAccount.Id}",
            "referenceId": "updatedAccount",
            "body":
            {
                "Name": "ACME"
            }
        }
    ]
}

If there is no record with given externalId, the request fails:

{
    "compositeResponse": [
        {
            "body": [
                {
                    "errorCode": "NOT_FOUND",
                    "message": "The requested resource does not exist"
                }
            ],
            "httpHeaders": {},
            "httpStatusCode": 404,
            "referenceId": "refAccount"
        },
        {
            "body": [
                {
                    "errorCode": "PROCESSING_HALTED",
                    "message": "Execution halted as previous operation (refAccount) was not successful."
                }
            ],
            "httpHeaders": {},
            "httpStatusCode": 400,
            "referenceId": "updatedAccount"
        }
    ]
}
2
  • Nice solution @mmm321321 Commented Jan 18, 2022 at 13:40
  • @mmm321321 Awesome solution! Thanks a lot for sharing!
    – Natallia
    Commented May 27, 2022 at 6:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .