Posts

Showing posts with the label Lock Records

Lock Records Using Apex in Salesforce

Locking Statements In Apex, you can use FOR UPDATE to lock sObject records while they’re being updated to prevent race conditions and other thread safety problems. Account [] accts = [SELECT Id FROM Account LIMIT 2 FOR UPDATE]; Locking Consideration When records are locked by a client, that client can modify the record's field values in the database within the same transaction. Other clients must wait until the transaction is completed and the records are no longer locked before they can update the same records. However, other clients can still query the locked records. If a record is already locked by another client, a process will wait for a maximum of 10 seconds for the lock to be released before acquiring a new lock. If the wait exceeds 10 seconds, a QueryException is thrown. Similarly, if a record is locked by another client and the lock is not released within 10 seconds, a DmlException is thrown. If a client tries to modify a locked record, the update can succeed if the lock ...