I have an After Insert Trigger on Task. This trigger passes data to a handler class that then does some logic and sends an email to the Lead related to the task.

Ideally, I want to save the email as an activity on the Lead record. However, by doing so, this will fire the Trigger again as the activity related to the email is a Task (which will fire another email etc, etc).

I know that it is possible to prevent this with a static variable tracking if its the first or second run and with conditional statements in my Trigger, so it will not send another email. The trigger is still fired for at least 2 times though.

Is it possible to somehow prevent the trigger from firing on the activity insert caused by the sent email? I've looked into trigger context variables and those don't seem to meet my requirements.


Actually, your trigger should be able to look for the Trigger.isExecuting context variable which your class could pass to it, particularly if your trigger runs using something along the lines of Dan Appleman's Trigger Handler Architecture. Or, as you say, the class could also set a boolean flag on a Global Static Variable the first time through the instance of the trigger. The issue would be that you'd likely have a new instance of the trigger and would need to pass the variables through to the new instance that get's triggered unless you use a trigger dispatcher class.

Regardless of what you do, the trigger will initiate/fire the 2nd time around. The important thing is that you can prevent it from executing it's logic so that it won't create another email and attachment on the lead object. You can add logic to your trigger that checks to see what was done; including determining whether the email was sent or attached to the lead object. If that criteria is true, and the task was on lead, you can remove the task Id from the set of Ids for processing. That would be another means of preventing the trigger from continuing to execute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.