ApexSensei

Drills DML operations

Bulk update once

Bulk-safe Apex changes records inside a loop and saves them all with one DML after the loop.

Your task

Set Description = 'Reviewed' on the three starter Accounts in a loop, save them with one update, and print the four labelled lines.

String tag = 'AS-BULK-' + UserInfo.getUserId() + '-' +
    String.valueOf(Datetime.now().getTime());
List<Account> accounts = new List<Account>{
    new Account(Name = tag + '-A', Description = 'Pending'),
    new Account(Name = tag + '-B', Description = 'Pending'),
    new Account(Name = tag + '-C', Description = 'Pending')
};
insert accounts;
Integer dmlBefore = Limits.getDmlStatements();
Set<Id> createdIds = new Set<Id>();

Requirements

  1. Limits.getDmlStatements() should be present
  2. DML inside the for loop should be absent
  3. update accounts; should be present after the loop
  4. One DML statement should be printed as Bulk DML used: 1
  5. The full list should be printed as Saved count: 3
  6. Every reviewed row should be printed as Reviewed count: 3
  7. The tag check should be printed as All tagged: true

Sign in to open this drill