function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ranu Jain SFDCRanu Jain SFDC 

Unable to lock row

I am getting Unabe to Lock Row wile inseting reocrods - 

1. I am fetching Inventory reserve records form database by using SOQL query. Inventory reserve has a look up of sales order line. -> So I am gettinh SO Lines from inventory reserves.-> I am updating list of sales order lines 
I am gettig unable to lock row while updating sales order line.

I can use "FOR UPDATE" key word while quering Inventory reserve records, but my doubt that error is coming while updating sales order lines , so It will work??

Thanks
Ranu
 
Nishad KNishad K
Hi Ranu,

ROW_LOCK issue is very common if you have multiple users updating the record at the same time .Or say a batch job is running and is updating a record and same record another trigger or code snippet (usually a future method) is updating.
 
Account [] accts = [SELECT Id FROM Account LIMIT 2 FOR UPDATE];
Using FOR UPDATE keyword helps to achieve a lock a client end to prevent this locking issues .
Here is a small article on sharing model to avoid this granular locking:

https://help.salesforce.com/articleView?id=How-can-I-avoid-getting-lock-errors-in-my-organization-1327109108393&language=en_US&type=1


Regards,
Nishad KNishad K
if it's resolved, mark the answer  and keep the community clean!!
Mukesh Kumar 107Mukesh Kumar 107
Istead of using 'FOR UPDATE' blindly, I would recommend identifying the root cause of the proble. This will solve this problem for all. Not only that, there might be more underlying architectural problem that might be causing this issue.

Here are few hints/thoughts:
- When child record is updated (DML) both child and parent is LOCKED. Inventory has lookup(SO Lines).
- When you run your update, identify which all processes are running which is trying to modify SO Lines object.
- When does this problem occures? Always? certain time of day? That will hint towards other running processes.
- How many Inventory records are there? How many SO Lines records are there? What are the chances of other users using the same record update?
- Do you see any Data Skew scenario?
- Using 'FOR UPDATE' for simple reasons might put the system into resource constraint mode because of intentional record lock.