Introduction
Trigger Lib is an Apex trigger framework for Salesforce. You write one small class per concern and give it a role in a trigger context. The library loops over the records, loads the data your handlers declare and commits their DML, all in bulk.
Why Trigger Lib?
- One-Line Triggers - The trigger passes an orchestrator that lists each context's handlers in run order
- Record Filtering - Each handler acts only on the records its predicate picks
- Declared Queries - Declare parent fields and related records, with no SOQL in your handler
- Unit of Work - Writers register DML, committed once after the last handler by default
- Guard Rails - SOQL or DML in a per-record method fails the save, whether a developer or an AI agent wrote it
- Bypasses and Recursion Control - Switch handlers off in Apex or custom metadata, and log or cap recursion in the update contexts
- Testability - Test handlers with in-memory records, no trigger and no DML
Quick Example
trigger ContactTrigger on Contact(before insert, after insert, before update, after update, before delete, after delete, after undelete) {
TriggerOrchestrator.run(new ContactTriggerOrchestrator());
}public with sharing class ContactTriggerOrchestrator implements TriggerOrchestrator.BeforeInsert {
public List<BeforeInsert.Handler> beforeInsertHandlers() {
return new List<BeforeInsert.Handler>{ new ContactEmailNormalizationPopulator() };
}
}public with sharing class ContactEmailNormalizationPopulator implements BeforeInsert.Populator {
public Boolean populateOnBeforeInsertWhen(TriggerTypes.InsertRecord record) {
return record.isNotBlank(Contact.Email);
}
public void populateOnBeforeInsert(TriggerTypes.InsertRecord record) {
Contact newContact = (Contact) record.getNewSObject();
record.put(Contact.Email, newContact.Email.trim().toLowerCase());
}
}How It Works
Trigger Lib has three levels:
- Trigger - one per object, with a one-line body:
TriggerOrchestrator.run(new ContactTriggerOrchestrator()), orTriggerOrchestrator.runFromMetadata()for handlers registered in custom metadata. - Orchestrator - lists the handlers of each context in run order. Under
runFromMetadata(),TriggerHandler__mdtrecords do it instead. - Handler - a small class with one role in a context, plus the add-ons it needs.
Every role has a predicate and an action, such as populateOnBeforeInsertWhen(record) and populateOnBeforeInsert(record). The library loops over the records of the chunk, calls the predicate for each one and runs the action only when the predicate returns true: the if inside the for loop you would otherwise write yourself. A Dispatcher is the exception: its dispatch method runs once, with all the qualified records.
Around that loop, the library:
- Loads data in bulk. Handlers declare parents and related records, and the library queries them per chunk, never per record. Your own SOQL in a predicate or an action throws. See Queries.
- Commits DML in bulk. Writers register DML on a unit of work, which commits after the last handler by default. DML in a predicate or an action throws. See Unit of Work.
- Handles errors. A handler's exception goes to your Logger and fails the save, unless the handler implements ContinueOnError. See Errors & Logging.
- Skips what you switch off from Apex, custom metadata or the handler itself. See Bypassing.
- Reports recursion. In the update contexts, a record that comes back to a Populator, Writer or Dispatcher after 3 runs in the transaction logs an error. A RecursionGuard sets its own depth and also skips the record.
Every step of a run, in order: Execution Order & Cost.
Roles
| Role | What it does | Contexts |
|---|---|---|
| Populator | Sets fields on the record being saved. No DML. | BeforeInsert, BeforeUpdate |
| Validator | Attaches an error to the record being saved or deleted, which rejects it. | BeforeInsert, BeforeUpdate, BeforeDelete |
| Writer | Registers DML on a unit of work. | AfterInsert, AfterUpdate, BeforeDelete, AfterDelete, AfterUndelete |
| Dispatcher | Gets all qualified records at once, for async work, events or emails. | AfterInsert, AfterUpdate, AfterDelete, AfterUndelete |
Add-ons
Pick one role per context and as many add-ons as you need. Each add-on exists only in the contexts where it fits:
| Add-on | What it does | Contexts |
|---|---|---|
| ParentQuery | Loads fields of the records the lookups point to. | BeforeInsert, AfterInsert, BeforeUpdate, AfterUpdate, AfterUndelete |
| PriorParentQuery | Loads the records the lookups pointed to before the save. | BeforeUpdate, AfterUpdate, BeforeDelete, AfterDelete |
| RelatedQuery | Loads children, siblings or any other records with your own query. | Every context |
| OwnUnitOfWork | Gives a Writer its own unit of work and settings, committed right after it. | AfterInsert, AfterUpdate, BeforeDelete, AfterDelete, AfterUndelete |
| Bypassable | Skips the handler for a run on a condition, such as a feature flag. | Every context |
| RecursionGuard | Skips a record after the handler acted on it a set number of times. | BeforeUpdate, AfterUpdate |
| Finalizer | Runs once after the loop when records qualified, with those records. | Every context |
| ContinueOnError | Logs the handler's exception and lets the save go on. | Every context |
