---
schema_version: "1"
id: cbeebdff0fbe3e369fb7efe4d002e7c31d65e0d53b55e330bef50c667c982e7c
title: Locking in a SOQL For Loop
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexcode/langCon_apex_locking_soql_for_loop.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_locking_soql_for_loop.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: df93a997d16b53a498605ec948a712fe0c43a6c81203ed7aa009c5d5978e5d8d
---
# Locking in a SOQL For Loop

The FOR UPDATE keywords can also be used within SOQL for loops. For example:

for (Account\[\] accts : \[SELECT Id FROM Account
                        WITH USER\_MODE FOR UPDATE\]) {
    // Your code
}

As discussed in [SOQL For Loops](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_loops_for_SOQL.htm "SOQL for loops iterate over all of the sObject records returned by a SOQL query."), the example above corresponds internally to calls to the query() and queryMore() methods in the SOAP API.

Note that there is no commit statement. If your Apex trigger completes successfully, any database changes are automatically committed. If your Apex trigger does not complete successfully, any changes made to the database are rolled back.