ある項目を更新し、メール送信・項目更新はワークフローで実施し、ToDo 作成をトリガーで実施するとトリガーが 2 回起動してしまう。トリガーに処理を寄せる以外に回避策はありますか?
専用の Static field を持つクラスを使用し、トリガーを初回起動かどうかを判定することで、上記現象を回避することができます。
※
Salesforce の Static field はリクエストスコープの中だけで共有され、サーバー全体、または全体組織で共有されません。
このことから Static field の値を利用して、トリガーの初回の発動か否かを判断することが可能です。
※
下記ドキュメントに、トリガーを初回起動かどうかを判定するサンプルが紹介されていますので、参照してください。
静的メソッドとインスタンスメソッド、変数、初期化コード
※
サンプルコード
--- Apex クラス ---
public class p {
public static boolean firstRun = true;
}
--- Apex トリガー ---
trigger Trigger_001 on Test__c (before update) {
if(p.firstRun==true){
system.debug('1回目の起動です。');
p.firstRun=false;
}
else{
system.debug('2回目の起動です。');
}
}
000385321

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.