Design Apex code patterns to stay within Salesforce governor limits across bulk trigger contexts

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. Bulkify all triggers by operating on Trigger.new and Trigger.newMap collections rather than querying or DML-ing inside loops — move SOQL queries before loops and collect results in Maps keyed by record ID
  2. Enforce a single trigger per object pattern and delegate business logic to handler classes, making it easier to track cumulative SOQL and DML calls across all before/after contexts
  3. Use custom metadata or custom settings to store configuration thresholds so that limit-aware logic (e.g., skip callout if already at 90 callouts) can be adjusted without a code deployment
  4. Check Limits.getLimitQueries(), Limits.getQueries(), Limits.getLimitDmlStatements(), and Limits.getDmlStatements() at critical points in batch or complex flows to detect approaching limits early
  5. For CPU-intensive operations, move processing to Queueable or Batch Apex with explicit chaining, breaking work into chunks that each start fresh with a full governor limit allocation
  6. Use the @TestVisible annotation on helper methods and mock the Limits class behavior in unit tests to validate governor-limit guard logic without actually exhausting limits in test runs

Known gotchas

Related routes

Handle Salesforce Apex governor limits proactively using Limits class checks and defensive coding patterns in 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
Implement one-trigger-per-object Apex pattern with a trigger handler framework to centralize logic and enable testability
developer.salesforce.com · 6 steps · unrated

Give your agent this knowledge — and 15,500+ 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