2

I have a trigger and I am calling a queueable job from trigger. Sometimes I get this error:

Update failed. First exception on row 0 with id a0VN00*********; first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record or 200 records:

Below is my query before updating the recods:

list<MyObject> lst = [select id, Account_Id__c from MyObject where 
                            Account_Id__c in:setofAccountID FOR UPDATE];    

I have referred to Can anybody explain the UNABLE_TO_LOCK_ROW error? and used FOR UPDATE. Still, I get the error.

1 Answer 1

6

You'll want to read this answer for more information, but the gist is that just because you use FOR UPDATE, does not mean you will eliminate this error. Instead, if the lock does not get acquired after approximately ten seconds, the transaction throws an exception. If you absolutely must try to obtain a row lock, you can do so in a do-try-catch-while loop (or alternatives):

SObject[] results;

do {
  try {
    results = [select ... from ... for update];
  } catch(QueryException e) {
  }
} while(results == null);

You may also want to limit the query to a number of retries, but usually your loop won't need to go more than once or twice before you acquire the row lock for your records.

I would personally only consider repeating a row lock in asynchronous code; if you can't get a row lock in a trigger or other real-time context, just return a friendly error so the user can try again later.

3
  • The waiting time isn't 10 seconds now? referring here: help.salesforce.com/s/articleView?id=000338933&type=1 Commented Dec 15, 2021 at 16:12
  • 1
    @EnrikHysko That help topic is from 2020, this answer is from 2018. That said, I just tested it again using the original code I wrote in the linked answer, and I found that indeed, the lock timed out at 10.009 seconds; I'll amend this.
    – sfdcfox
    Commented Dec 15, 2021 at 16:23
  • uh that was fast :D excellent @sfdcfox Commented Dec 15, 2021 at 16:32

You must log in to answer this question.

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