function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Dippan PatelDippan Patel 

Workflow executing Apex trigger twice

I have After Update Trigger with two dynamic filter criteria and they are in "OR". 
Example: If Lead Source Data Value Changes ---> process record 
If Email Opt Out Data Value Changes --> process record. 

Now, I also have a workflow with condition on any update on record. 
Example: If Email Opt out is FALSE, then change it to TRUE. 

Steps of Execution: 
1. If you change Lead Source manually, trigger fires. 
2. Workflow fires to change Email Opt Out to TRUE. 
3. And Trigger fires again. 

I only want my Trigger to fire once. How do I prevent trigger executing twice without changing workflows and filter criteria?

Thank you.  
Best Answer chosen by Dippan Patel
David Zhu 🔥David Zhu 🔥
You may need to add a static boolean variable in your trigger handler.

Static boolean runOnce = false;

Put the following code at the very beginning of the trigger handler.

if (runOnce)
 runOnce = true;