# Why is using a simple static Boolean flag to prevent recursion dangerous when processing more than 200 records?

**Topic:** Apex Trigger · **Difficulty:** Hard · **Asked at:** Salesforce, Deloitte, Slalom, Infosys · **Tags:** apex-trigger, trigger, recursion, bulk

Read it on the site: https://www.forcechallenges.com/interview-questions/why-is-using-a-simple-static-boolean-flag-to-prevent-recursion-dangerous-when-processing-more-than-200-records

## Answer

Triggers process records in chunks of 200. A static variable lives for the whole transaction, so after the first chunk sets `hasRun = true`, **chunks 2, 3...** in the same transaction see the flag and skip all logic, leaving records 201+ unprocessed (silently).

Fix: track processed record Ids in a static `Set<Id>` and process only Ids not yet in the set. For updates, you can also reprocess a record only if relevant fields changed.

```apex
public static Set<Id> processed = new Set<Id>();
for (Account a : Trigger.new) {
    if (processed.add(a.Id)) { /* process */ }
}
```

---
Source: ForceChallenges · last updated 2026-10-21
