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
Limits.getDmlStatements()should be present- DML inside the
forloop should be absent update accounts;should be present after the loop- One DML statement should be printed as
Bulk DML used: 1 - The full list should be printed as
Saved count: 3 - Every reviewed row should be printed as
Reviewed count: 3 - The tag check should be printed as
All tagged: true