I have a batch process which updates Accounts and sometimes when i run this batch i get the error.

UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record

I did read through Mohit's answer on why this is happening here Can anybody explain the UNABLE_TO_LOCK_ROW error?

Based on this article http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_locking_statements.htm

Using the FOR UPDATE locks the whole set of records. The question i have is that is it a good practice to do this on a batch process, as the batch process could run for sometime and till then all the records would be locked.

In my batch process i am querying for all the accounts in the start method and in the execute method i am checking for certain conditions deciding if these records need to be updated or not. I cant perform these checks using WHERE clause as it depends on combination of factors dependent on other objects.


FOR UPDATE has to be written and that would avoid the error.Only record that batch context will process will be locked for very minimal amount of time .

Only those on which DML will happen at current time will be locked so prefer using FOR UPDATE keyword

  • awesome.. just wanted to confirm that
    – Prady
    Mar 11 '14 at 6:53

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.