3

Given the code below, and assuming there are 500 accounts in the system, how many DML statements will be executed?

for (List<Account> accounts : [SELECT Id, Name]) 
{ 
    update Accounts; 
}
6

0 - Your code will error as the SOQL is not valid, when fixed:

Only 3 DML statement(s) will be used and 500 dml rows will be used

each batch of 200 will count as 1 DML statement

for(Account[] a : [Select ID From Account Limit 500])
    update a;

14:05:24.588 (11588787877)|LIMIT_USAGE_FOR_NS|(default)|
Number of SOQL queries: 1 out of 100
Number of query rows: 500 out of 50000
Number of SOSL queries: 0 out of 20 Number of DML statements: 3 out of 150

From the Documentation:

For Apex saved using Salesforce.com API version 20.0 or earlier, if an API call causes a trigger to fire, the chunk of 200 records to process is further split into chunks of 100 records. For Apex saved using Salesforce.com API version 21.0 and later, no further splits of API chunks occur. Note that static variable values are reset between API batches, but governor limits are not. Do not use static variables to track state information between API batches.

A more syntactically correct way to do this would be:

database.update([Select ID, Name From Account]);

although its only purpose would be to execute triggers and code associated with an update.

Conversely, this will use 500 DML statements (obviously it will error at the limit) and should not be used in this way (only for illustration)

for (Account a : [SELECT Id, Name]) 
{ 
    update a; 
}
  • 1
    i know that its only one DML statement will be used but i was taking a mock-up dev 501 exam and the correct answer is 3 DML statement and I have no idea why its say 3 so I thought to post the question.... here – Nick Kahn Nov 14 '15 at 21:59
  • Because the trigger executes in groups of 200 so it would be 3...Answer updated – Eric Nov 14 '15 at 22:03
  • oh so each will have 200 rows at a time? – Nick Kahn Nov 14 '15 at 22:04
  • @AbuHamzah - Yes. Answer update. Give me a minute and I will post logs to show it – Eric Nov 14 '15 at 22:06
  • okay got it, i was thinking that it does in one batch (whatever the records it contains) but it only does 200 rows in a group... – Nick Kahn Nov 14 '15 at 22:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.