---
schema_version: "1"
id: e1f0d757fe12d6506896f88891ebcebc732c09e76ab2fa42b177081ed847b2d1
title: FOR UPDATE
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/platform/salesforce-soql-sosl/guides/soql-sosl/sforce-api-calls-soql-select-for-update.md
canonical_url: https://developer.salesforce.com/docs/platform/salesforce-soql-sosl/guide/sforce-api-calls-soql-select-for-update.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: bd3bb45eb85cd973a633965c8f2a4945cedc16902550f4b7319dcf95749f38e5
products:
  - SOQL and SOSL Reference
  - Platform Developer Content
---

# FOR UPDATE

In Apex, you can use `FOR UPDATE` to lock sObject records while they’re being updated in order to prevent race conditions and other thread safety problems.

While an sObject record is locked, no other client or user is allowed to make updates either through code or the Salesforce user interface. The client locking the records can perform logic on the records and make updates with the guarantee that the locked records won’t be changed by another client during the lock period. The lock gets released when the transaction completes.

**note:** You can’t use the `ORDER BY` clause in any SOQL query that uses locking.

To lock a set of sObject records in Apex, embed the keywords `FOR UPDATE` after any inline SOQL statement. This example queries for two accounts and also locks the accounts that are returned.

```apex
Account [] accts = [SELECT Id FROM Account LIMIT 2 FOR UPDATE];
```

**note:** Use care when setting locks in your Apex code. For details, see [Locking Records ](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_locking_records.htm) in the _Apex Developer Guide_.
