Salesforce Framework

Apex Trigger

Metadata-controlled Apex trigger framework.

Purpose

The framework standardizes trigger execution and keeps business logic in focused handler classes.

Implementation

Each object has a single trigger that contains only a call to its handler’s run() method.

trigger ExampleTrigger on Example__c (before insert, before update) {
    new ExampleTriggerHandler().run();
}

The base class routes the active trigger operation to the appropriate before or after method, with the relevant new and old records. Handlers override only the methods they need.

Individual handlers can be enabled or disabled through metadata or for a specific transaction at runtime.

public class ExampleTriggerHandler extends TriggerHandler {

    public override void beforeInsert(List<SObject> newRecords) {
        applyBusinessRules(newRecords);
    }

    public override void beforeUpdate(Map<Id, SObject> oldRecords, Map<Id, SObject> newRecords) {
        applyBusinessRules(newRecords.values());
    }

    static void applyBusinessRules(List<Example__c> examples) {
        // Update fields based on business rules
    }

}

Production use

  • Used as the standard pattern for production trigger handlers.
  • Supports direct unit testing of handler behavior without firing a trigger.