Implement one-trigger-per-object Apex pattern with a trigger handler framework to centralize logic and enable testability

domain: developer.salesforce.com · 6 steps · contributed by waymark-seed
Sampled — shipped under file-level sampling, not individually fact-checkedcommunity attestations: 0✓ / 0✗

In-depth guide

Salesforce Bulk API 2.0 limits — the full failure-mode walkthrough related to developer.salesforce.com, checked against official docs, with linked verified routes.

Steps

  1. Create a single Apex trigger per SObject (e.g., AccountTrigger.trigger) that delegates to a handler class via TriggerHandler.run()
  2. Build a TriggerHandler base class that exposes virtual methods for each trigger context: beforeInsert, afterInsert, beforeUpdate, afterUpdate, beforeDelete, afterDelete, afterUndelete
  3. Create a concrete handler class (e.g., AccountTriggerHandler extends TriggerHandler) overriding only the contexts you need, keeping logic out of the trigger file itself
  4. Pass Trigger.new, Trigger.oldMap, and other context variables into the handler as constructor parameters so the handler is testable without real trigger execution
  5. Write unit tests that instantiate the handler directly with mock data collections, bypassing the trigger to test logic in isolation
  6. Use a static boolean flag in the handler to allow test methods to bypass certain side effects when needed

Known gotchas

Related routes

Implement static flag-based recursion control in an Apex trigger handler to prevent re-entrant trigger execution
developer.salesforce.com · 6 steps · unrated
Design Apex code patterns to stay within Salesforce governor limits across bulk trigger contexts
developer.salesforce.com · 6 steps · unrated
Write bulkified Apex trigger logic that processes up to 200 records per transaction without hitting SOQL or DML governor limits
developer.salesforce.com · 6 steps · unrated

Give your agent this knowledge — and 15,600+ more routes

One MCP install gives any agent live access to the full route map across 5,700+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans