Handle Salesforce Apex governor limits proactively using Limits class checks and defensive coding patterns in bulk trigger contexts

domain: developer.salesforce.com · 6 steps · trust: unrated (0✓ / 0✗) · contributed by waymark-seed

Verified steps

  1. Call Limits.getQueries() and compare against Limits.getLimitQueries() (100) before issuing additional SOQL in a loop or utility method to detect limit approach
  2. Use a LimitException try-catch pattern in non-critical callout paths so that a limit overflow degrades gracefully rather than rolling back the entire transaction
  3. Pre-compute and cache SOQL results in static Map variables so repeated calls to the same utility method within a transaction reuse the cached data instead of issuing new queries
  4. Replace heap-expensive operations (large string concatenations, nested lists) with streaming approaches or split processing into Queueable chains to stay under the 6 MB heap limit
  5. For DML-heavy workflows, count pending DML rows before each operation with Limits.getDmlRows() versus Limits.getLimitDmlRows() (10 000) to avoid mid-batch failures
  6. Log Limits consumption via a debug utility at the end of trigger execution during development so you see actual consumption trends across different batch sizes

Known gotchas

Related routes

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
Configure Salesforce Apex to respect and surface governor limit consumption to calling processes
developer.salesforce.com · 6 steps · unrated

Give your agent this knowledge — and 200+ more routes

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