# How do you prevent trigger recursion properly using a Set<Id> of processed record IDs?

**Topic:** Apex Trigger · **Difficulty:** Medium · **Asked at:** Salesforce, Deloitte, Persistent · **Tags:** apex-trigger, trigger, recursion

Read it on the site: https://www.forcechallenges.com/interview-questions/how-do-you-prevent-trigger-recursion-properly-using-a-set-id-of-processed-record-ids

## Answer

Keep a static `Set<Id>` in the handler. Each time the trigger runs, filter `Trigger.new` to records whose Id is not yet in the set, process only those, and add them to the set.

```apex
public class AccountHandler {
    private static Set<Id> processedIds = new Set<Id>();
    public void afterUpdate(List<Account> news) {
        List<Account> toProcess = new List<Account>();
        for (Account a : news) {
            if (!processedIds.contains(a.Id)) {
                processedIds.add(a.Id);
                toProcess.add(a);
            }
        }
        // process toProcess
    }
}
```
This works across 200-record chunks. If a record legitimately must be reprocessed after another change, compare field values instead.

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