UNABLE_TO_LOCK_ROW retries from middleware are duplicating records — is upsert on an External ID the right fix?
0 reputation · 23 Sept 2025, 13:21 UTC
0 reputation · 23 Sept 2025, 13:21 UTC
We have an Apex integration where inbound middleware retries a request whenever the first attempt fails partway through — for example after a row lock contention error such as UNABLE_TO_LOCK_ROW, or when our own Queueable retry logic re-enqueues a job. The Apex side currently performs a plain insert, so every retry after a partially successful call creates a duplicate record.
My understanding is that upsert records External_Id__c can make a retried DML idempotent: the retry matches the existing record by key and updates it instead of inserting. But this only works if the chosen field is marked External ID and Unique, and I am unsure which business attribute is a genuinely stable, immutable idempotency key across our objects.
There is also the question of which failures deserve a retry at all. Using Database.insert(list, false) returns SaveResult objects, so retryable statuses could in principle be separated from validation failures that would loop forever.
A thoughtful contribution can make all the difference. Be the first to share one.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.