開発者は、商談のステージが何回変更されたかを追跡する既存の機能を使用しています。Opportunity のステージが変更されると、フィールドの値を 1 つ増やすワークフロー ルールが起動されます。開発者は、フィールドが 4 から 5 に変更されたときに子レコードを作成するための after トリガーを作成しました。
ユーザーが商談のステージを変更し、カウント フィールドを手動で 4 に設定します。カウント フィールドは 5 に更新されますが、子レコードは作成されません。
このようなことが起こっている理由は何ですか?
正解:A
The reason this is happening is that Trigger.old does not contain the updated value of the count field. Trigger.old is a list of the old versions of the sObject records, before any changes were made in the trigger. Trigger.new is a list of the new versions of the sObject records, after any changes were made in the trigger. In this case, the workflow rule is fired after the trigger, and updates the count field from 4 to 5. However, the trigger does not see this change, as it only compares the values in Trigger.old and Trigger.new, which are both 4. To fix this, the developer should use an after update trigger, and re-query the updated records to get the latest values of the count field. After triggers fire before workflow rules is incorrect, as workflow rules fire after triggers. Trigger.new does not change after a field update is incorrect, as Trigger.new does change after a field update, but only within the same trigger context. After triggers are not fired after field updates is incorrect, as after triggers are fired after field updates, unless the field updates are made by a workflow rule or a process. Reference: [Triggers and Order of Execution], [Trigger Context Variables], [Apex Developer Guide]