開発者は、Expense _c オブジェクトで次のトリガーを識別します。

トリガーは、それぞれ削除前、挿入前、更新イベントの前に処理されます。
トリガーのベスト プラクティスに従うために、開発者が実装する必要がある 2 つの手法はどれですか。
2つの回答を選択してください
正解:A,C
A: Unify all three triggers in a single trigger on the Expense__c object that includes all events:
Salesforce best practices recommend having only one trigger per object to avoid redundancy and conflicts.
By combining all the events (before delete, before insert, and before update) into a single trigger, the developer can manage the logic in an organized and maintainable manner.
This also simplifies debugging and ensures that the trigger logic executes in a predictable order.
C: Create helper classes to execute the appropriate logic when a record is saved:
Using helper classes allows for a clean separation of concerns. The trigger becomes a dispatcher that delegates logic to dedicated classes.
For example, you can create methods like applyDefaultsToExpense(), validateExpenseUpdate(), and deleteExpense() in a helper class and invoke them from the trigger.
This improves reusability, readability, and testability of the code.
Why not the other options?
B: Unify the before insert and before update triggers and use Flow for the delete action:
While Flow is a powerful tool, it is not ideal for deleting records or replacing Apex trigger functionality, especially when triggers already exist for other events.
D: Maintain all three triggers on the Expense__c object but move the Apex logic out of the trigger definition:
Maintaining multiple triggers on the same object can lead to conflicts and execution order issues, even if the logic is moved to helper classes.
References:
Trigger Best Practices
Trigger Design Patterns
最新のコメント (最新のコメントはトップにあります。)
正解は A と C です。
この問題は、Salesforce開発における「Apexトリガーのベストプラクティス(設計原則)」に関する非常に有名なトピックです。
領域の確認
領域: 2. プロセスの自動化とロジック (Process Automation and Logic)
トピック: Apexトリガーのベストプラクティス、デザインパターン
正解の解説
Salesforceの開発では、以下の 2つの鉄則(ベストプラクティス) があります。この問題は、まさにその2つを選ばせるものです。
A. Unity の 3 つのトリガーすべてが、すべてのイベントを含む Expense__c オブジェクトの単一のトリガーになります。 (※「Unity」は原文の "Unify" や "Combine" などの誤訳と思われますが、「統合する」という意味です)
原則: 「1オブジェクトにつき1トリガー (One Trigger Per Object)」
理由: 1つのオブジェクト(Expense__c)に対して複数のトリガー(削除用、挿入用、更新用...)を作ってしまうと、Salesforceはどのトリガーを先に実行するかを保証しません。実行順序がランダムになるため、予期せぬバグの原因になります。
これを防ぐために、すべてのイベント(insert, update, deleteなど)を扱う 1つのトリガーファイル に統合する必要があります。
C. レコードが保存されるときに適切なロジックを実行するためのヘルパー クラスを作成します。
原則: 「ロジックレス・トリガー (Logic-less Triggers)」
理由: トリガーファイル(.trigger)の中に直接 if 文や for ループなどの複雑なロジックを書くと、コードが読みづらくなり、テストや再利用が難しくなります。
トリガーファイルは「イベントの振り分け(交通整理)」だけを行い、実際の処理は Apexクラス(ヘルパークラス/ハンドラクラス) に記述して呼び出すのが正しい設計です。
不正解の解説
B. 挿入前トリガーと更新前トリガーを統合し、削除アクションにフローを使用します。 これは 不正解 です。 フローを使用すること自体は間違いではありませんが、「トリガーのベストプラクティス」を問われている文脈では不適切です。また、削除アクションだけをフローに分けると、ロジックが分散して管理が複雑になる可能性があります。まずはApexトリガーの整理整頓(AとC)が優先です。
D. Expense__c オブジェクトの 3 つのトリガーをすべて維持しますが、Apex ロジックをトリガー定義から移動します。 これは 不正解 です。 「Apex...