# Trigger Lib > Apex Trigger Framework One small class per concern, run only for the records that qualify. ## Table of Contents ### Docs - [Introduction](https://trigger.beyondthecloud.dev/introduction.md): What Trigger Lib is, why to use it, a quick example, how a trigger run works, and the roles and add-ons of each Salesforce trigger context. - [Installation](https://trigger.beyondthecloud.dev/installation.md): Install Trigger Lib in a Salesforce org - deploy via button, or copy the Apex classes, custom metadata types and page layouts together with the SOQL Lib and DML Lib dependencies. - [Your First Handler](https://trigger.beyondthecloud.dev/guide/first-handler.md): Tutorial that wires a Contact trigger and orchestrator, adds a before insert Populator and Validator, tries them in anonymous Apex, and unit-tests them without DML. - [Trigger & Orchestrator](https://trigger.beyondthecloud.dev/guide/orchestrator.md): Wire a Salesforce trigger to Trigger Lib - the one-line trigger, the orchestrator and its handler lists, handlers registered in custom metadata, run order, one class in several contexts, handler instances per chunk and the before to after handoff. - [Contexts at a Glance {#method-names}](https://trigger.beyondthecloud.dev/contexts.md): Every Trigger Lib context side by side, with the exact interface and method names for before insert, after insert, before update, after update, before delete, after delete and after undelete. - [Design Principles](https://trigger.beyondthecloud.dev/introduction/design-principles.md): The rules behind Trigger Lib's API - one handler per concern, one role per context, declared queries instead of SOQL, a unit of work for DML, explicit qualification and guard rails that cannot be switched off. ### BeforeInsert - [BeforeInsert](https://trigger.beyondthecloud.dev/before-insert.md): Before insert in Trigger Lib - set fields with a Populator or reject records with a Validator before new records are saved. - [BeforeInsert.Populator](https://trigger.beyondthecloud.dev/before-insert/populator.md): Set, default or normalize fields on records before insert with record.put - no DML and no second save. - [BeforeInsert.Validator](https://trigger.beyondthecloud.dev/before-insert/validator.md): Reject records before insert with a record-level or field-level error message, like a validation rule written in Apex. - [Record API in BeforeInsert](https://trigger.beyondthecloud.dev/before-insert/record-api.md): What a before insert handler receives - InsertRecord, RejectableInsertRecord and InsertRecords - with every method. #### Add-ons - [Add-ons in BeforeInsert](https://trigger.beyondthecloud.dev/before-insert/add-ons.md): Add-ons for before insert handlers - parent fields, related records, bypass, finalizer and error handling. - [BeforeInsert.ParentQuery](https://trigger.beyondthecloud.dev/before-insert/add-ons/parent-query.md): Read parent (lookup) fields in a before insert Populator or Validator without writing SOQL. - [BeforeInsert.RelatedQuery](https://trigger.beyondthecloud.dev/before-insert/add-ons/related-query.md): Query children, siblings, duplicates or configuration once per handler per run in before insert and read them per record, without SOQL in the loop. - [BeforeInsert.Bypassable](https://trigger.beyondthecloud.dev/before-insert/add-ons/bypassable.md): Skip (bypass, disable) a before insert handler for the whole chunk when a condition holds, such as a static flag or a batch. - [BeforeInsert.Finalizer](https://trigger.beyondthecloud.dev/before-insert/add-ons/finalizer.md): Run code once per chunk after a before insert handler, with the qualified records, for checks across records. - [BeforeInsert.ContinueOnError](https://trigger.beyondthecloud.dev/before-insert/add-ons/continue-on-error.md): Let a before insert handler fail without failing the insert - its exceptions are logged and swallowed, and the save goes on. ### AfterInsert - [AfterInsert](https://trigger.beyondthecloud.dev/after-insert.md): After insert in Trigger Lib - write other records with a Writer or start async work with a Dispatcher after new records are saved. - [AfterInsert.Writer](https://trigger.beyondthecloud.dev/after-insert/writer.md): Create, update or delete other records, or publish platform events, after insert through a unit of work that commits with the save. - [AfterInsert.Dispatcher](https://trigger.beyondthecloud.dev/after-insert/dispatcher.md): Make one bulk call per chunk after insert - enqueue a Queueable, publish events or send email for the records that qualified. - [Record API in AfterInsert](https://trigger.beyondthecloud.dev/after-insert/record-api.md): What an after insert handler receives - InsertRecord and InsertRecords - with every method. #### Add-ons - [Add-ons in AfterInsert](https://trigger.beyondthecloud.dev/after-insert/add-ons.md): Add-ons for after insert handlers - parent fields, related records, own unit of work, bypass, finalizer and error handling. - [AfterInsert.ParentQuery](https://trigger.beyondthecloud.dev/after-insert/add-ons/parent-query.md): Read parent (lookup) fields in an after insert Writer or Dispatcher without writing SOQL. - [AfterInsert.RelatedQuery](https://trigger.beyondthecloud.dev/after-insert/add-ons/related-query.md): Query children, siblings or other records once per handler per run in an after insert Writer or Dispatcher, and read them by key per record. - [AfterInsert.OwnUnitOfWork](https://trigger.beyondthecloud.dev/after-insert/add-ons/own-unit-of-work.md): Give an after insert Writer its own DML Lib unit of work for user mode, sharing, partial success or your own statement order. - [AfterInsert.Bypassable](https://trigger.beyondthecloud.dev/after-insert/add-ons/bypassable.md): Skip an after insert Writer or Dispatcher for the whole chunk when a condition holds, such as a static flag or a custom permission. - [AfterInsert.Finalizer](https://trigger.beyondthecloud.dev/after-insert/add-ons/finalizer.md): Run once per chunk after an after insert Writer or Dispatcher, with the qualified records. - [AfterInsert.ContinueOnError](https://trigger.beyondthecloud.dev/after-insert/add-ons/continue-on-error.md): Log and swallow an after insert handler's exception so later handlers run and the insert goes on. ### BeforeUpdate - [BeforeUpdate](https://trigger.beyondthecloud.dev/before-update.md): Before update in Trigger Lib - set fields with a Populator or reject changes with a Validator, with the old and new values side by side. - [BeforeUpdate.Populator](https://trigger.beyondthecloud.dev/before-update/populator.md): Set, derive or clear fields on records being updated, usually when another field changed - no DML and no second save. - [BeforeUpdate.Validator](https://trigger.beyondthecloud.dev/before-update/validator.md): Reject an update with a record-level or field-level error before it is saved, such as a forbidden transition or a cleared value. - [Record API in BeforeUpdate](https://trigger.beyondthecloud.dev/before-update/record-api.md): What a before update handler receives - UpdateRecord, RejectableUpdateRecord and UpdateRecords - with every method. #### Add-ons - [Add-ons in BeforeUpdate](https://trigger.beyondthecloud.dev/before-update/add-ons.md): Add-ons for before update handlers - current and previous parents, related records, bypass, recursion limit, finalizer and error handling. - [BeforeUpdate.ParentQuery](https://trigger.beyondthecloud.dev/before-update/add-ons/parent-query.md): Read parent (lookup) fields in a before update Populator or Validator without writing SOQL, including a lookup the user just changed. - [BeforeUpdate.PriorParentQuery](https://trigger.beyondthecloud.dev/before-update/add-ons/prior-parent-query.md): Read fields of the parent a lookup pointed to before this update, to compare the old and new parent or to name the previous one. - [BeforeUpdate.RelatedQuery](https://trigger.beyondthecloud.dev/before-update/add-ons/related-query.md): Load children, siblings or other records once per handler per run in before update, keyed for fast reads, instead of a query per record. - [BeforeUpdate.Bypassable](https://trigger.beyondthecloud.dev/before-update/add-ons/bypassable.md): Skip a before update Populator or Validator when a condition holds, such as a static flag or a custom permission. - [BeforeUpdate.RecursionGuard](https://trigger.beyondthecloud.dev/before-update/add-ons/recursion-guard.md): Limit how many times a before update Populator acts on the same record in one transaction. At the limit, the record is skipped and an error is logged. - [BeforeUpdate.Finalizer](https://trigger.beyondthecloud.dev/before-update/add-ons/finalizer.md): Run once per chunk after a before update handler, with the qualified records, for checks across records. - [BeforeUpdate.ContinueOnError](https://trigger.beyondthecloud.dev/before-update/add-ons/continue-on-error.md): Let a non-critical before update handler fail without failing the update. The exception is logged and swallowed. ### AfterUpdate - [AfterUpdate](https://trigger.beyondthecloud.dev/after-update.md): After update in Trigger Lib - change other records with a Writer or hand bulk and async work to a Dispatcher, with old and new values. - [AfterUpdate.Writer](https://trigger.beyondthecloud.dev/after-update/writer.md): After update, create, update or delete other records or publish platform events through a unit of work that commits with the save. - [AfterUpdate.Dispatcher](https://trigger.beyondthecloud.dev/after-update/dispatcher.md): After update, collect the qualifying records and make one bulk call per chunk, such as enqueueing a Queueable for a callout. - [Record API in AfterUpdate](https://trigger.beyondthecloud.dev/after-update/record-api.md): What an after update handler receives - UpdateRecord and UpdateRecords - with every method, change detection included. #### Add-ons - [Add-ons in AfterUpdate](https://trigger.beyondthecloud.dev/after-update/add-ons.md): Add-ons for after update handlers - parent fields, previous parents, related records, own unit of work, bypass, recursion guard, finalizer and error handling. - [AfterUpdate.ParentQuery](https://trigger.beyondthecloud.dev/after-update/add-ons/parent-query.md): Read parent (lookup) fields, such as an opportunity's account, in an after update Writer or Dispatcher without writing SOQL. - [AfterUpdate.PriorParentQuery](https://trigger.beyondthecloud.dev/after-update/add-ons/prior-parent-query.md): Read fields of the parent a lookup pointed to before the update, such as the previous owner, in an after update Writer or Dispatcher. - [AfterUpdate.RelatedQuery](https://trigger.beyondthecloud.dev/after-update/add-ons/related-query.md): Query children, siblings or any other records once per handler per run in an after update Writer or Dispatcher, and read them per record by key. - [AfterUpdate.OwnUnitOfWork](https://trigger.beyondthecloud.dev/after-update/add-ons/own-unit-of-work.md): Give an after update Writer its own DML Lib unit of work, for user mode, sharing, partial success, a commit hook or your own statement order. - [AfterUpdate.Bypassable](https://trigger.beyondthecloud.dev/after-update/add-ons/bypassable.md): Skip an after update Writer or Dispatcher for a whole chunk when a condition holds, such as a static flag, a batch job or a custom permission. - [AfterUpdate.RecursionGuard](https://trigger.beyondthecloud.dev/after-update/add-ons/recursion-guard.md): Cap how many times an after update Writer or Dispatcher acts on the same record in one transaction. At the cap, the record is skipped and an error is logged. - [AfterUpdate.Finalizer](https://trigger.beyondthecloud.dev/after-update/add-ons/finalizer.md): Run once per chunk after an after update Writer or Dispatcher, with the qualified records, for one bulk query or registration. - [AfterUpdate.ContinueOnError](https://trigger.beyondthecloud.dev/after-update/add-ons/continue-on-error.md): Log and swallow the exceptions of an after update Writer or Dispatcher, so later handlers still run and the update is saved. ### BeforeDelete - [BeforeDelete](https://trigger.beyondthecloud.dev/before-delete.md): Before delete in Trigger Lib - block a delete with a Validator, or change related records with a Writer while the rows and their links still exist. - [BeforeDelete.Validator](https://trigger.beyondthecloud.dev/before-delete/validator.md): Block a delete with a record-level or field-level error before the records are removed, like a validation rule written in Apex. - [BeforeDelete.Writer](https://trigger.beyondthecloud.dev/before-delete/writer.md): Create, update or delete other records before a delete, while the rows and the records that point at them still exist - through a unit of work. - [Record API in BeforeDelete](https://trigger.beyondthecloud.dev/before-delete/record-api.md): What a before delete Validator or Writer receives - DeleteRecord, RejectableDeleteRecord and DeleteRecords - with every method. #### Add-ons - [Add-ons in BeforeDelete](https://trigger.beyondthecloud.dev/before-delete/add-ons.md): Add-ons for before delete handlers - former parent fields, related records, own unit of work, bypass, finalizer and error handling. - [BeforeDelete.PriorParentQuery](https://trigger.beyondthecloud.dev/before-delete/add-ons/prior-parent-query.md): Read parent (lookup) fields of the records being deleted in a before delete Validator or Writer, without SOQL in the handler. - [BeforeDelete.RelatedQuery](https://trigger.beyondthecloud.dev/before-delete/add-ons/related-query.md): Query children, siblings or other records in a before delete Validator or Writer with one query per provider, while the rows are still linked. - [BeforeDelete.OwnUnitOfWork](https://trigger.beyondthecloud.dev/before-delete/add-ons/own-unit-of-work.md): Give a before delete Writer its own DML Lib unit of work - user mode, sharing, partial success, statement order - committed right after that Writer. - [BeforeDelete.Bypassable](https://trigger.beyondthecloud.dev/before-delete/add-ons/bypassable.md): Skip a before delete Validator or Writer for the chunk when a condition holds, such as a static flag or a batch purge. - [BeforeDelete.Finalizer](https://trigger.beyondthecloud.dev/before-delete/add-ons/finalizer.md): Run once per chunk after a before delete Validator or Writer, with the qualified records - one bulk registration or one check across records. - [BeforeDelete.ContinueOnError](https://trigger.beyondthecloud.dev/before-delete/add-ons/continue-on-error.md): Let a before delete Validator or Writer fail without failing the delete - its exception is logged and swallowed. ### AfterDelete - [AfterDelete](https://trigger.beyondthecloud.dev/after-delete.md): After delete in Trigger Lib - clean up or recompute other records with a Writer, or hand work to a job once per chunk with a Dispatcher, after records are deleted. - [AfterDelete.Writer](https://trigger.beyondthecloud.dev/after-delete/writer.md): Create, update or delete other records after a delete - remove orphans, note the former parent, recompute a rollup - through a unit of work. - [AfterDelete.Dispatcher](https://trigger.beyondthecloud.dev/after-delete/dispatcher.md): Hand deleted records to a Queueable, callout, platform event or email once per chunk with an AfterDelete Dispatcher. - [Record API in AfterDelete](https://trigger.beyondthecloud.dev/after-delete/record-api.md): What an after delete handler receives - DeleteRecord and DeleteRecords - with every method. #### Add-ons - [Add-ons in AfterDelete](https://trigger.beyondthecloud.dev/after-delete/add-ons.md): Add-ons for after delete handlers - former parent fields, related records, own unit of work, bypass, finalizer and error handling. - [AfterDelete.PriorParentQuery](https://trigger.beyondthecloud.dev/after-delete/add-ons/prior-parent-query.md): Read fields of the former parent (lookup) of a deleted record in an after delete Writer or Dispatcher, without SOQL in the handler. - [AfterDelete.RelatedQuery](https://trigger.beyondthecloud.dev/after-delete/add-ons/related-query.md): Query the records that remain after a delete, or records that still look up to the deleted rows, once per handler per run in an after delete handler. - [AfterDelete.OwnUnitOfWork](https://trigger.beyondthecloud.dev/after-delete/add-ons/own-unit-of-work.md): Give an after delete Writer its own DML Lib unit of work - user mode, sharing, partial success, statement order - committed right after that Writer. - [AfterDelete.Bypassable](https://trigger.beyondthecloud.dev/after-delete/add-ons/bypassable.md): Skip an after delete Writer or Dispatcher for a chunk when a condition holds, for example during a batch purge or a data migration. - [AfterDelete.Finalizer](https://trigger.beyondthecloud.dev/after-delete/add-ons/finalizer.md): Run one step per chunk after an after delete Writer or Dispatcher - one aggregate query or one write per former parent. - [AfterDelete.ContinueOnError](https://trigger.beyondthecloud.dev/after-delete/add-ons/continue-on-error.md): Keep a delete going when an after delete Writer or Dispatcher fails - log and swallow its exception so later handlers still run. ### AfterUndelete - [AfterUndelete](https://trigger.beyondthecloud.dev/after-undelete.md): After undelete in Trigger Lib - react to records restored from the Recycle Bin with a Writer or a Dispatcher, and block a restore. - [AfterUndelete.Writer](https://trigger.beyondthecloud.dev/after-undelete/writer.md): After undelete, create, update or delete other records, or publish platform events, through a unit of work that commits with the restore. - [AfterUndelete.Dispatcher](https://trigger.beyondthecloud.dev/after-undelete/dispatcher.md): After undelete, make one bulk call per chunk with the restored records that qualify - enqueue a Queueable (async, callout), publish events or send email. - [Record API in AfterUndelete](https://trigger.beyondthecloud.dev/after-undelete/record-api.md): What an after undelete handler receives - UndeleteRecord and UndeleteRecords - with every method. #### Add-ons - [Add-ons in AfterUndelete](https://trigger.beyondthecloud.dev/after-undelete/add-ons.md): Add-ons for after undelete handlers - parent fields, related records, own unit of work, bypass, finalizer and error handling. - [AfterUndelete.ParentQuery](https://trigger.beyondthecloud.dev/after-undelete/add-ons/parent-query.md): Read parent (lookup) fields of restored records in an after undelete Writer or Dispatcher without SOQL in the handler. - [AfterUndelete.RelatedQuery](https://trigger.beyondthecloud.dev/after-undelete/add-ons/related-query.md): Query children, siblings or configuration once per handler per run for restored records in an after undelete Writer or Dispatcher, and read them by key. - [AfterUndelete.OwnUnitOfWork](https://trigger.beyondthecloud.dev/after-undelete/add-ons/own-unit-of-work.md): Give an after undelete Writer its own DML Lib unit of work - user mode, sharing, partial success, statement order - that commits at the Writer's turn. - [AfterUndelete.Bypassable](https://trigger.beyondthecloud.dev/after-undelete/add-ons/bypassable.md): Skip (bypass, disable, turn off) an after undelete Writer or Dispatcher for the whole chunk when a condition holds. - [AfterUndelete.Finalizer](https://trigger.beyondthecloud.dev/after-undelete/add-ons/finalizer.md): Run code once per chunk after an after undelete Writer or Dispatcher, with the restored records that qualified - aggregate queries, bulk registrations. - [AfterUndelete.ContinueOnError](https://trigger.beyondthecloud.dev/after-undelete/add-ons/continue-on-error.md): Log and swallow an after undelete Writer's or Dispatcher's exceptions so later handlers run and the restore goes on. ### Advanced - [Execution Order & Cost](https://trigger.beyondthecloud.dev/guide/execution-order.md): The order in which Trigger Lib runs bypasses, handlers, parent queries, providers, Finalizers and unit-of-work commits, and what one run costs in SOQL queries and DML statements. - [Bypassing](https://trigger.beyondthecloud.dev/guide/bypasses.md): Switch Trigger Lib handlers off from Apex for one transaction with TriggerOrchestrator.bypass(), org-wide with TriggerObject__mdt and TriggerHandler__mdt, or on a condition with a context's Bypassable, plus a data migration recipe. - [Queries](https://trigger.beyondthecloud.dev/guide/queries.md): How Trigger Lib handlers get data without a query per record - parent fields with ParentQuery, configuration in static variables, and children, siblings or other records with RelatedQuery, and what each one costs. - [Unit of Work](https://trigger.beyondthecloud.dev/guide/unit-of-work.md): How Writers insert, update, upsert, delete and publish platform events through TriggerTypes.UnitOfWork - registration, which unit a Writer gets, when it commits, and your own unit with DML Lib. - [Errors & Logging](https://trigger.beyondthecloud.dev/guide/error-handling.md): What happens when a Trigger Lib handler throws - the TriggerOrchestrator.Logger, ContinueOnError, what is never logged, the library exceptions, and failing one record instead of the whole save. - [RelatedQuery Recipes](https://trigger.beyondthecloud.dev/guide/related-query.md): RelatedQuery recipes for Trigger Lib - RecordsProvider classes that load children, siblings, records under the previous parent, text and composite keys, configuration, a declared parent's field and the trigger records' own formula fields, once per handler per run. - [Testing](https://trigger.beyondthecloud.dev/guide/testing.md): Unit-test Trigger Lib handlers and orchestrators without DML - mock a trigger context with TriggerOrchestrator.mock(), run it with runTestFor, and mock parents, related records, the unit of work, the metadata and the Logger. ### API - [TriggerOrchestrator](https://trigger.beyondthecloud.dev/api/trigger-orchestrator.md): TriggerOrchestrator reference: run() and runFromMetadata() from the trigger, the seven registration interfaces, the bypass() builder, the Logger and Error interfaces, and TriggerLibException. - [Record API](https://trigger.beyondthecloud.dev/api/record.md): Record API reference: InsertRecord, UpdateRecord, DeleteRecord, UndeleteRecord and the Rejectable records of Validators, with accessors, put and addError, value checks, change detection and record type checks. - [Record Collections](https://trigger.beyondthecloud.dev/api/record-collections.md): Record Collections reference: InsertRecords, UpdateRecords, DeleteRecords and UndeleteRecords, the bulk view that a RecordsProvider query, a Dispatcher and a Finalizer receive, with getIds, getIdsOf, getValuesOf, getOldIdsOf, getRecords and size. - [TriggerTypes.ParentFields](https://trigger.beyondthecloud.dev/api/parent-fields.md): TriggerTypes.ParentFields reference: choose the parent (lookup) and grandparent fields that ParentQuery and PriorParentQuery load, the lookup map key, how declarations merge, and what the parent queries cost. - [RelatedRecords & RecordsProvider](https://trigger.beyondthecloud.dev/api/related-records.md): RelatedRecords and RecordsProvider reference: the provider a RelatedQuery add-on returns to query children, siblings or unrelated records once per handler per run, its query and keyOf methods, and the RelatedRecords index a handler reads with getRelated. - [TriggerTypes.UnitOfWork](https://trigger.beyondthecloud.dev/api/unit-of-work.md): TriggerTypes.UnitOfWork reference: the seven registration methods a Writer uses to insert, update, upsert, delete and publish in before delete and the after contexts, and DML.Record lookups to records inserted in the same unit. - [Custom Metadata](https://trigger.beyondthecloud.dev/api/custom-metadata.md): TriggerObject__mdt and TriggerHandler__mdt reference: switch off every handler on an object, or one handler class in one context, org-wide without deploying code, and register the handlers of a runFromMetadata() trigger per context in Order__c order; fields, the ApexClassName__c format, when the records are read, and how to deploy them.