FOR UPDATE keyword in SOQL helps us to lock the records from being updating from another request.
Sample Code:
List<Contact> listCon = [SELECT Id, FirstName FROM Contact WHERE FirstName LIKE ‘%test%’ FOR UPDATE];
for(Contact con: listCon) {
…………………
…………………
}
Things to Know:
1. If you attempt to lock a record currently locked by another client, your process waits for the lock to be released before acquiring a new lock. If the lock isn’t released within 10 seconds, you will get a QueryException.
2. If you attempt to update a record currently locked by another client and the lock isn’t released within 10 seconds, you will get a DmlException.
3. The record locks that are obtained in Apex via FOR UPDATE clause are automatically released when making callouts.