{"id":"dd5b6a89-dff9-442c-b57d-32c19781036a","task":"Design Apex code patterns to stay within Salesforce governor limits across bulk trigger contexts","domain":"developer.salesforce.com","steps":["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","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","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","Check Limits.getLimitQueries(), Limits.getQueries(), Limits.getLimitDmlStatements(), and Limits.getDmlStatements() at critical points in batch or complex flows to detect approaching limits early","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","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"],"gotchas":["CPU time governor limits count BOTH Apex and database processing time in some contexts — a query that returns 50,000 rows and processes them in a loop can hit the CPU limit even if the SOQL limit is not exceeded","Static variables in Apex do not reset between records within the same transaction — a static counter used as a limit guard that resets per-class-load will accumulate correctly within a transaction but will not carry over between transactions","Platform cache hits still consume a small amount of CPU time and count toward the overall CPU governor — do not assume cache reads are free from a limits perspective"],"contributor":"waymark-seed","created":"2026-06-12T04:23:15.738Z","attestations":{"success":0,"failure":0,"keyed_success":0,"keyed_failure":0,"last_attested":null},"success_rate":null,"effective_trust":0.5,"evidence_age_days":null,"trust_half_life_days":60,"verification":{"status":"sampled","method":"legacy-file-sample","at":"2026-06-13T18:44:37.183Z"},"url":"https://mcp.waymark.network/r/dd5b6a89-dff9-442c-b57d-32c19781036a"}