We have a trigger on Opportunity that updates the OpportunityLineItem when the Opportunity is won. We also have workflow rules on the OpportunityLineItem that fire and this causes some recursion to occur.
At the moment this doesn't cause any real problems but while looking at one of the approaches (static boolean class in combination with the trigger setting it once it's fired) - this works fine within the UI but we found the test methods no longer passed. It looks as though the triggers have already fired and the boolean has already been set to prevent further running.
The testmethod is too large to share on SFSE but basically inserts a number of Opportunities then updates the Stage so they are won. Without the CheckRecursive class they assert as expected with CheckRecursive the asserts fail as the triggers have not fired.
Is this a limitation of testmethods or something else I am missing?
Not sure if relevant but I am using test.StartTest() and test.StopTest() either side of the stage update in my test method:
Test.startTest();
update closeOptyList;
Test.stopTest();
Example Trigger:
trigger OptyDemo on Opportunity (before insert, before update)
{
if(CheckRecursive.RunMe)
{
Opty_Utils.someMethod(Trigger.New);
CheckRecursive.RunMe=false; //Prevent recursion
}
}
Example Recursive class:
public class CheckRecursive
{
public static boolean runME = true; //General use
}