ApexSensei

Drills Debugging

Stop a trigger calling itself

maximum trigger depth exceeded means a trigger caused DML that ran the same trigger, sixteen times.

Your task

Fix the handler so each Case closed in a transaction is marked exactly once, and the follow-up update does not run the handler's work again.

public class AS_AFE55B6263D0_CaseCloser {
    public static void afterUpdate(List<Case> cases, Map<Id, Case> oldMap) {
        List<Case> followUps = new List<Case>();
        for (Case caseRecord : cases) {
            if (caseRecord.Status == 'Closed') {
                followUps.add(new Case(
                    Id = caseRecord.Id,
                    Description = 'Closed by the handler'
                ));
            }
        }
        if (!followUps.isEmpty()) {
            update followUps;
        }
    }
}

Requirements

  1. marksAClosedCaseOnce should pass
  2. leavesAnOpenCaseAlone should pass
  3. marksTwoCasesClosedSeparately should pass
  4. handlesTwoHundredClosedCases should pass

Sign in to open this drill