[SalesForce] Salesforce REST upsert for externalId cause DUPLICATE_VALUE error

I send next upsert request to Salesforce:

PATCH /services/data/v41.0/sobjects/Task/uuid__c/a22ac4d6-e2c0-4e53-898d-ea1d0c807775 HTTP/1.1
Host: **********.my.salesforce.com
Content-Type: application/json; charset=UTF-8
Accept: application/json
Authorization: Bearer *******

{
  "Description": "Called C to discuss the reinstall.",
  "CallType": "Outbound",
  "OwnerId": "************",
  "Status": "Completed",
  "Subject": "Call",
  "isTransfer__c": false,
  "WhatId": "************",
  "CallDurationInSeconds": 597,
  "AudioFile__c": "outgoing/2017-10-31/03-10-59_ID-***********.mp3",
  "CallStarted__c": "2017-10-31T07:10:51Z",
  "CallHangup__c": "2017-10-31T07:20:56Z",
  "AsteriskId__c": "7794786",
  "ReasonText__c": "Success call"
}

uuid__c field is Unique, Case Insensitive and external id marked.

I receive next response from Salesforce:

[
    {
        "message": "duplicate value found: uuid__c duplicates value on record with id: *******************",
        "errorCode": "DUPLICATE_VALUE",
        "fields": []
    }
]

How it can be if UPSERT request must update object when duplicate found?

I have used this documentation for developing: Insert or Update (Upsert) a Record Using an External ID

Best Answer

After a month of discussions with Indian support of all tiers, we've got next:

Salesforce can't proceed too fast UPSERT queries. If you send two requests with delay about a second or less you'll get next:

  1. Your request 1 come to Salesforce and start to save into their DB.
  2. Your request 2 come while first request still in work.
  3. Salesforce return DUPLICATE_VALUE on second request because it think there is no such data in DB yet, and will try to save it also via UPSERT.
  4. You've got error above.

OFFICIAL SOLUTION: Send your requests with delay at least 2 second each. At least first save completed.

Also from my request, they created an article with description: Salesforce Article

P.S. They will not change anything, about this behaviour. We spent a month, trying to resolve this issue and find the reason with PREMIUM Salesforce support, but results you can see in this answer.

Related Topic