+ Start a Discussion
Amrender sainiAmrender saini 

UNABLE_TO_LOCK_ROW unable to obtain exclusive access to this record or 1 records

Howdy Salesforce developers
I'm getting this error often. I'm running my batches in queue.
Any one have any idea how can we prevent this error. This error occur when Single record try to update by 2 jobs at same time.

Update failed. First exception on row 0 with id a145000000C6g5HAAR; first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record or 1 records: a145000000C6g5HAAR: []
Best Answer chosen by Amrender saini
Akhil AnilAkhil Anil
Hi Amrender,

When you have multiple jobs running that update the same set of records, it's recommended to lock the records using the FOR UPDATE keyword. It will ensure that the record is locked and no other job can update the same record at that point in time. The conflicting job will then wait untill the locking on the record is released and then perform the required operaiton. This will avoid conflicts between jobs that are running concurrently and will not throw up this exception. You can refer the below link to learn more about record locking.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_locking_statements.htm

kindly mark it as an answer if that resolves your query !
 

All Answers

pranoti patilpranoti patil
When salesforce updating any record, it prevents to update the same record using UI or through code i.e. that record gets locked. The lock gets released when the transaction completes.

When two users or two different apex code tries to update the same record, then salesforce throws an error “UNABLE_TO_LOCK_ROW unable to obtain exclusive access to this record”.

Using FOR UPDATE keyword helps to achieve a lock a client end to prevent this locking issues.
List<Account> accountList = [SELECT Id FROM Account FOR UPDATE];

Here is an article.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_locking_statements.htm
Akhil AnilAkhil Anil
Hi Amrender,

When you have multiple jobs running that update the same set of records, it's recommended to lock the records using the FOR UPDATE keyword. It will ensure that the record is locked and no other job can update the same record at that point in time. The conflicting job will then wait untill the locking on the record is released and then perform the required operaiton. This will avoid conflicts between jobs that are running concurrently and will not throw up this exception. You can refer the below link to learn more about record locking.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_locking_statements.htm

kindly mark it as an answer if that resolves your query !
 
This was selected as the best answer
Deanna Aaron 11Deanna Aaron 11
Amrender or Akhil--
Where do I insert the "FOR UPDATE keyword". One of my process builders keeps sending this error via email.

I sincerely appreciate your help!
Graciana NallemGraciana Nallem
I arrived at this thread cause i was having the same issue. The above didn't help for me either, so I attempted to copy the data and paste it as plain text, and converting to number a field that required this data. That helped, so adding it here in case it might be useful for someone else. 
Devair TononDevair Tonon
What can I do if I received this same "UNABLE_TO_LOCK_ROW" error when I try to consume a standard Salesforce SOAP API?