Trigger Frameworks in Salesforce

Jun 30, 2024

Lecture on Trigger Frameworks by Adam

Introduction

  • Presenter: Adam, Salesforce Developer at YouTube
  • Background:
    • 500+ Trailhead badges
    • 14 Salesforce certifications
    • Volunteer with Rad Women
    • Frequent presenter at Dreamforce
  • Contact:
    • Twitter: @adam17amo
    • Blog: AdamToArchitect.com
    • Course: Pluralsight courses on Lightning Migration

Content Overview

  • Introduction to Triggers
  • Trigger Handlers
  • Trigger Frameworks
    • Three different types of frameworks
    • Comparisons and considerations
  • Summary and Wrap-Up

Apex Triggers

  • Definition: Automated responses to database events in Salesforce
    • Events: insert, update, delete, undelete
    • Timing: before or after record save operation
  • Execution: Triggers fire before initial validation or after post-processing (e.g., workflow, email)
  • Flexibility: Triggers can operate on any object, with few exceptions
  • Order of Operations: Focus on steps 5 (before save) and 19 (after save)
  • Best Practice: One trigger per object to avoid race conditions and make debugging easier

Basic Trigger Example

  • Scenario: Trigger on Lead object that checks for existing accounts and creates a case if matches found
  • Problem: Single trigger can become too long and complex, making it hard to maintain and debug

Trigger Handlers

  • Concept: Use an Apex class to handle trigger logic
  • **Benefits: **
    • Keeps triggers simple
    • Easier to reuse and test code
    • Simplifies debugging

Trigger Frameworks

  1. Virtual Class Framework

    • Use a virtual class with methods to override
    • Constructor to initialize and set the trigger context
    • Routes trigger contexts using a run method
    • Example: Simplify triggers to a single line
  2. Interface Framework

    • Use an interface that requires implementation of all trigger methods
    • Trigger Dispatcher handles routing
    • Adds an isDisabled method for recursion check and disabling triggers
    • Example: One line trigger, all methods enforced
  3. Full Object-Oriented Architecture Framework

    • Combines virtual and interface approaches
    • Consists of multiple classes: dispatcher interface, dispatcher base class, handler interface, and handler base class
    • Allows for differentiation between main entry and in-progress entry
    • Example: Very clean, modular triggers with specific handlers for each event type

Comparing Frameworks

  • Commonalities:
    • Simplified triggers
    • Handled routing
    • Recursion checks and potential to disable triggers
  • Differences:
    • Virtual and architecture allow picking specific events
    • Interface enforces consistency
    • Implementation complexity varies

Summary and Best Practices

  • Mix and Match: Choose concepts that fit your org’s needs
  • Recursion Checks: Implement checks to avoid repeated trigger execution
  • Complexity vs. Benefits: Weigh the overhead against the maintainability benefits

Helpful Resources