The independent daily for Salesforce admins, developers and architects

Curated by Abhinav (opens in a new tab)

A small owned trigger framework built around an explicit handler contract (opens in a new tab)

You will learn how to structure an Apex trigger framework with a single dispatcher, a recursion guard that tracks record IDs, layered bypass controls via Custom Metadata and a permission, and user-mode selectors with a savepoint-based unit of work.

Why read this

The author also explains why they chose an owned baseline over a third-party framework.

  1. Check bypass layers
  2. Confirm trigger context
  3. Check recursion guard
  4. Mark records as handled
  5. Call context hook
The trigger lifecycle checks run in a fixed order before the context hook is called

The author argues that Salesforce trigger frameworks usually fail on their contract rather than their code: when a handler runs, when it is skipped, what happens on re-entry, and who may bypass it. Their baseline is deliberately small, with a one-line trigger that delegates to a dispatcher. The lifecycle checks bypass settings first, confirms the code is running inside a trigger, resolves the context, verifies the SObject type, checks the recursion guard, marks records as handled, and then calls the context hook. Handlers only override the contexts they need and delegate rules to domain classes.

The recursion guard tracks which record IDs a named handler has already processed in the transaction, instead of using a static boolean. This avoids both blocking legitimate second passes for different records and re-entry from after-triggers. Bypass works in three layers: a runtime bypass for tests and controlled operations, an Is_Active__c flag on Custom Metadata to disable a handler without deployment, and a Custom Permission for integration users during data loads. Metadata is read once per transaction. The surrounding layers keep business logic out of the trigger, with selectors running in user mode and a unit of work that commits DML in a fixed order inside a savepoint.

The author reports that static checks and a check-only compile against an org caught configuration issues, and that the test suite exposed a null dereference outside trigger context and a wrong assumption about scheduled job execution in tests. The article closes by listing next steps, including removing defaults from test factories and enforcing the Permission Set in the deploy pipeline.

Read the original at dev.to (opens in a new tab)

Worth reading?

Recommended

Trigger Lib: a role-based Apex trigger framework built around governor limits (opens in a new tab)

You will learn how Trigger Lib splits trigger logic into populators, validators, writers and dispatchers, how orchestrators and Custom Metadata Types register handlers, and how its add-on interfaces bulkify queries and batch DML.

Trigger Lib handler roles

  • Populator
  • Validator
  • Writer
  • Dispatcher
Trigger Lib has four handler roles, each with one job in a trigger context

The article introduces Trigger Lib, an open-source Apex trigger framework from the Apex Fluently collection, written by an author who argues that Flows are impractical and that Apex triggers are the best automation option, provided they are organized well. The author’s main complaint is that existing trigger frameworks rarely help teams manage governor limits, which become a serious risk when AI-generated code and other automations share a transaction. Trigger Lib is deliberately not metadata-driven. It can be started with an orchestrator class that lists handlers, or with runFromMetadata() for handlers registered in Custom Metadata Types, and Custom Metadata is then used only to switch handlers off.

Handlers are split into four roles: Populators set field values in before insert and update, Validators reject invalid records in before contexts, Writers queue DML on a shared Unit of Work in after contexts and before delete, and Dispatchers start Queueable jobs or publish events in after contexts. Each role has a qualifying method and an action method, and SOQL and DML are blocked in the per-record methods. Add-on interfaces such as ParentQuery, PriorParentQuery, RelatedQuery, OwnUnitOfWork, Bypassable, RecursionGuard, Finalizer and ContinueOnError extend these roles, for example to bulk-load parents and related records or to let a failing handler be logged without failing the save.

The author analyzed 919 SOQL queries from three projects and found that parent and config queries made up most of the total, which is why the framework bulk-loads parents once per chunk and recommends static variables for configuration data. Writers commit through DML Lib, which orders inserts by dependency and performs one DML per operation and object type at the end of the run. The article is cut off during its discussion of the Logger interface, so the logging details are not covered.

Read the original at Beyond the Cloud (opens in a new tab)

Worth reading?