1

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

2

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"
        }
    ]
}
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.