ApexSensei

Drills Debugging

Fix DML inside a loop

Every insert, update or delete statement counts against the limit of 150 per transaction.

Your task

Fix the handler so it saves all follow-up Tasks with one insert, however many Opportunities were created.

public class AS_37B5DFEF8A27_FollowUpTasks {
    public static void afterInsert(List<Opportunity> opportunities) {
        for (Opportunity opportunity : opportunities) {
            Task followUp = new Task(
                Subject = 'Follow up: ' + opportunity.Name,
                WhatId = opportunity.Id
            );
            insert followUp;
        }
    }
}

Requirements

  1. createsOneLinkedTask should pass
  2. createsATaskPerOpportunity should pass
  3. usesOneDmlStatementForTheTasks should pass
  4. handlesTwoHundredOpportunities should pass

Sign in to open this drill