Contents
What is record locking in database?
Record locking is the technique of preventing simultaneous access to data in a database, to prevent inconsistent results. The classic example is demonstrated by two bank clerks attempting to update the same bank account for two different transactions. Clerks 1 and 2 both retrieve (i.e., copy) the account’s record.
How do I lock a record in SOQL query?
To lock records, simply use the FOR UPDATE keywords in your SOQL statements. You do not have to manually commit the records so if your Apex script finishes successfully the changes are automatically committed to the database and the locks are released.
How to avoid unable to lock row error?
You try to update the same record in your code and gets UNABLE_TO_LOCK_ROW error as the record is already locked. You just need to get the locked record Id and find out the second flow which is updating this record at the same time. You can also check best practices to avoid this issue at https://help.salesforce.com/articleView?id=000229525&type=1.
How to fix unable to lock row error in Salesforce?
You can enable Debug logs for the user who is facing the error, to find the offending trigger/flow/ValidationRules/ causing the issue. 2. Check for any dependent background Jobs that are running on the same object. If there is any, try to pause the jobs and then perform the actions to reduce row locks.
How to check for unable to lock row in apex?
To start debugging the issue, create debug logs and also check Apex Jobs at the same time. Apex job will have asynchronous calls in your flow and must have some DML which is updating same record on which you are getting UNABLE_TO_LOCK_ROW error.
Why is my record locked in apex jobs?
Apex jobs will have an asynchronous call which is already running and has put a lock on the record. You try to update the same record in your code and gets UNABLE_TO_LOCK_ROW error as the record is already locked. You just need to get the locked record Id and find out the second flow which is updating this record at the same time.