Apex トリガーと Apex クラスは、ケースが変更されるたびにカウンター `Edit_Count__c` を増分します。 ```java public class CaseTriggerHandler { public static void handle(List<Case> cases) { for (Case c : cases) { c.Edit_Count__c = c.Edit_Count__c + 1; } } } trigger on Case(before update) { CaseTriggerHandler.handle(Trigger.new); } ``` ケースオブジェクトに、ケースの作成または更新時に実行される保存前レコードトリガフローを本番環境に新しく追加しました。このプロセスを追加して以来、ケースの編集時に「Edit_Count__c」が複数回増加しているという報告を受けています。この問題を修正するApexコードはどれでしょうか?
Correct Answer: B
This scenario illustrates a classic trigger recursion issue triggered by the Salesforce Order of Execution. When a record is updated, Salesforce runs through a specific sequence: first, it executes "Before-Save" Record- Triggered Flows, then "Before" triggers, followed by "After" triggers, and finally "After-Save" flows and processes. If a process or flow updates the same record that initiated the transaction, the entire cycle of Apex triggers can be re-invoked within that single transaction. To prevent logic from running multiple times during these re-entrant cycles, developers implement a static boolean variable as a "recursion guard." Static variables in Apex persist for the entire duration of a single transaction. By checking the value of a static boolean at the start of the trigger, the code can determine if it has already processed the current set of records. Option B is the correct implementation of this pattern. It defines `public static Boolean firstRun = true;` in the handler class. The trigger checks if `firstRun` is true, executes the handler logic, and then immediately sets `firstRun` to false. Any subsequent execution of the Case trigger within the same transaction will find the variable set to false and skip the increment logic. Option A is incorrect because it resets `firstRun` to true every time the trigger starts, defeating the guard. Option C incorrectly uses an instance variable (non-static), which is recreated for every call. Option D uses a local variable within the trigger body, which is re-initialized to true every time the trigger fires, providing no protection against recursion.