As per Salesforce documents, I understand that when using SELECT FOR UPDATE the records will not released until the end of transaction.

I use SELECT FOR UPDATE but still find error UNABLE_TO_LOCK_ROW when I call UPDATE after it. Is it possible to get the error if the records still being locked?

I doubt that my records were released before transaction end. Perhaps because the SELECT FOR UPDATE is in separated method? Will the locked rows release after method exit?

My code is like

Public static void updateSalesOrder(Order so){
   //prepare value for update so here
   Order record; 
       record = LockRecord(so.id); // call method to lock so by id
   } catch(QueryException e) {
       //log error                            
       update so;
Public static Order LockRecord(Id soId){
    return [SELECT Id FROM Order WHERE Id =: soId FOR UPDATE];
  • What is the complete text of the error and/or log context? The Order may not be the record that is subject to the row lock exception.
    – David Reed
    Commented Feb 16, 2021 at 3:44

1 Answer 1


There are two sentences in this documentation that might be the cause of your problem. They are related to a specific scenario but I think they apply to your case nevertheless.

The locking process returns a fresh copy of the record from the database through the SELECT statement. The second client can use this copy to make new updates.

Meaning: Your passing in an Order (so). Then you're locking a record in the database and retrieving a fresh copy into the record variable. Then you're updating your "old" order.

Try applying your changes to record and update that.


    // Replace this by the fields that actually changed or loop over all fields.
    record.AccountId = so.AccountId;
    // Now update the locked record.
    update record;

You must log in to answer this question.

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