Newer Version Available
Locking in a SOQL For Loop
The FOR UPDATE keywords can also be used within
SOQL for loops. For
example:
1for (Account[] accts : [SELECT Id FROM Account
2 FOR UPDATE]) {
3 // Your code
4}As discussed in SOQL For Loops, 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.