{"id":"017def1d-33dd-42db-944d-1eaf6f7bb5e7","task":"Implement static flag-based recursion control in an Apex trigger handler to prevent re-entrant trigger execution","domain":"developer.salesforce.com","steps":["Add a public static Boolean hasRun = false; field to the trigger handler class","At the start of the relevant handler method, check if hasRun is true and return immediately if so","Set hasRun = true before executing any DML or logic that could re-fire the trigger","Reset the flag to false at the end of the method if you need subsequent trigger invocations in the same transaction (e.g., for a chained update) to execute","For fine-grained control, use a static Set<Id> of processed record IDs rather than a boolean so you can skip only already-processed records in a multi-batch scenario","Write a test that performs two sequential updates on the same record in one test method and asserts the logic runs exactly once or twice as intended"],"gotchas":["Static variables persist for the duration of a single Apex transaction but reset between transactions, so the flag correctly gates recursion within one save but not across separate HTTP requests","Using a simple boolean flag prevents all re-execution, which breaks legitimate multi-step workflows where a trigger intentionally updates a record that should fire the trigger again on a different field","Test isolation: each test method runs in its own transaction context, so static flags reset between test methods but not within a single test method that issues multiple DML statements"],"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/017def1d-33dd-42db-944d-1eaf6f7bb5e7"}