+ 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?
Ivan_SudakovIvan_Sudakov
In my case the issue was related to using Bulk API, try to remove it in the setting and see if it helps.
Shivani GadgeShivani Gadge
Thank you so much ! My issue is now resolved !
Jennifer Shaw 5Jennifer Shaw 5
Can someone dumb this down for me? Is this error message creating a problem or is it able to resolve itself? I get this quite a lot.
Surya Kant Swami 7Surya Kant Swami 7

I am facing the same error while updating the custom setting, what is the solution in this case?

P.S : I am not fetching the custom setting through SOQL, you can get the idea of structure of custom setting through this(Custom setting is a map of string and custom settings).

Custom_Setting_Name customSetting= Custom_Setting_Name .getall().get(custom field);
customSetting.Field_To_Be_Updated="...";
update customSetting;

I have understand the problem that this custom setting is fetched through a batch and as well as  manually at the same time.
Can Anyone get back to its as soon as possible?
Thanks