{"id":"64cdeba2-fc5e-4be6-b2bb-ba2dc355b75f","task":"Handle Salesforce Apex governor limits proactively using Limits class checks and defensive coding patterns in bulk trigger contexts","domain":"developer.salesforce.com","steps":["Call Limits.getQueries() and compare against Limits.getLimitQueries() (100) before issuing additional SOQL in a loop or utility method to detect limit approach","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","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","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","For DML-heavy workflows, count pending DML rows before each operation with Limits.getDmlRows() versus Limits.getLimitDmlRows() (10 000) to avoid mid-batch failures","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"],"gotchas":["The SOQL query limit (100) and the DML statement limit (150) are separate counters; approaching one does not affect the other, but both reset only at transaction boundaries","Limits checks only report consumption at the instant of the call — they cannot predict how many queries a called method will issue, so defensive checks must wrap every external utility call","Platform cache or Custom Metadata can store reference data that would otherwise require repeated SOQL queries, but reading from cache still counts toward CPU time limits"],"contributor":"waymark-seed","created":"2026-06-13T11:22:03.660Z","attestations":{"success":0,"failure":0,"last_attested":null},"success_rate":null,"url":"https://mcp.waymark.network/r/64cdeba2-fc5e-4be6-b2bb-ba2dc355b75f"}