Advanced 40 min read

Build the Trigger Handlers

By the end of this lesson, you'll be able to:

  • Complete the trigger handler for every object in this project's data model
  • Verify the layers built so far (Lessons 3-5) compose correctly end to end
  • Recognize the full request path from a DML statement to a business decision

Prerequisites: "Build the Service Layer"

The remaining handlers, same shape throughout

public with sharing class SupportCaseTriggerHandler {
    public void afterInsert(List<Support_Case__c> newCases) {
        ProjectSupportBurdenService.recordCaseOpened(newCases);
    }
}

Every handler in this project — ProjectTriggerHandler, TimeEntryTriggerHandler, SupportCaseTriggerHandler, alongside Lesson 3\'s MilestoneTriggerHandler — follows the exact same shape: a thin dispatcher, delegating immediately to a service. Four objects, one consistent pattern.

Tracing one full request end to end

A Milestone's Status__c changes to "Complete" via a user update.
  → MilestoneTrigger (domain layer) fires, before update.
  → MilestoneTriggerHandler.beforeUpdate routes to...
  → MilestoneStatusService.validateStatusTransition (service layer),
    which allows the transition.
  → after update fires: MilestoneTriggerHandler.afterUpdate routes to...
  → MilestoneStatusService.recalculateProjectHealth (service layer),
    which calls...
  → ProjectHealthService.calculateHealth (service layer), which
    queries via...
  → MilestoneSelector.selectByProjectIds (selector layer)
  → The Project's Health__c field is updated based on the result.

Every layer from Lessons 3-5 participates in this one user action — this is what "the architecture composes" actually means in practice, not just each layer working in isolation.

Why this composition matters more here than in Module 42

Module 42\'s banking system had two triggers on one object. This project has four objects each with their own handler, all ultimately updating a shared Project__c record — the layered architecture is what keeps this genuinely manageable, since each handler, service, and selector can be understood, tested, and changed independently, exactly the payoff Module 43\'s "Why Architecture Matters at Scale" lesson promised.

Exercise

As a comment, trace what happens, layer by layer, when a new Support_Case__c is inserted against a Project.

Show hint

Follow the same layer-by-layer format as this lesson's traced example.

APEX

Build the Trigger Handlers Quiz

1. What does "the architecture composes end to end" mean, as demonstrated by this lesson's traced Milestone status-change example?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

This lesson completes the remaining trigger handlers — ProjectTriggerHandler, TimeEntryTriggerHandler, SupportCaseTriggerHandler — and traces one full request through every layer, confirming the architecture actually composes end to end.