SOQL for loops differ from standard SOQL statements because of the method they use to retrieve sObjects. While the standard queries discussed in SOQL and SOSL Queries can retrieve either the count of a query or a number of object records, SOQL for loops retrieve all sObjects, using efficient chunking with calls to the query and queryMore methods of the SOAP API. Developers should always use a SOQL for loop to process query results that return many records, to avoid the limit on heap size.
Note that queries including an aggregate function don't support queryMore. A run-time exception occurs if you use a query containing an aggregate function that returns more than 2,000 rows in a for loop.
SOQL For Loop Formats
SOQL for loops can process records one at a time using a single sObject variable, or in batches of 200 sObjects at a time using an sObject list:
- The single sObject format executes the for loop's <code_block> once per sObject record. Consequently, it is easy to understand and use, but is grossly inefficient if you want to use data manipulation language (DML) statements within the for loop body. Each DML statement ends up processing only one sObject at a time.
- The sObject list format executes the for loop's <code_block> once per list of 200 sObjects. Consequently, it is a little more difficult to understand and use, but is the optimal choice if you need to use DML statements within the for loop body. Each DML statement can bulk process a list of sObjects at a time.
For example, the following code illustrates the difference between the two types of SOQL query for loops:
01 | // Create a savepoint because the data should not be committed to the database |
02 | Savepoint sp = Database .setSavepoint(); |
03 |
04 | insert new Account[]{ new Account(Name = 'yyy' ), |
05 | new Account(Name = 'yyy' ), |
06 | new Account(Name = 'yyy' )}; |
07 |
08 | // The single sObject format executes the for loop once per returned record |
09 | Integer i = 0 ; |
10 | for (Account tmp : [ SELECT Id FROM Account WHERE Name = 'yyy' ]) { |
11 | i++; |
12 | } |
13 | System . assert (i == 3 ); // Since there were three accounts named 'yyy' in the |
14 | // database, the loop executed three times |
15 |
16 | // The sObject list format executes the for loop once per returned batch |
17 | // of records |
18 | i = 0 ; |
19 | Integer j; |
20 | for (Account[] tmp : [ SELECT Id FROM Account WHERE Name = 'yyy' ]) { |
21 | j = tmp.size(); |
22 | i++; |
23 | } |
24 | System . assert (j == 3 ); // The list should have contained the three accounts |
25 | // named 'yyy' |
26 | System . assert (i == 1 ); // Since a single batch can hold up to 200 records and, |
27 | // only three records should have been returned, the |
28 | // loop should have executed only once |
29 |
30 | // Revert the database to the original state |
31 | Database .rollback(sp); |