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
kdaviskdavis 

Trigger Static Recursive Variables in Test Classes

Hi,

 

I'm using a static recursive variables for my triggers and I am encountering an issue when testing. I have 2 triggers. One on a custom object called procedure and another on custom object revenue that updates a procedure when inserted.

 

So in my test class I insert a procedure record, the first trigger runs and at the end sets the recursive variable hasRun = True.

 

Now when I go an insert my Revenue record, I want it to run through the procedure trigger again, but hasRun still equals to True? Do you need to reset static variables like that in test cases because test classes are considered a single execution state?

 

Best Answer chosen by Admin (Salesforce Developers) 
JPClark3JPClark3

It will remain set within the same test method. It is considered the same context.

 

It will reset to default in between test methods.

All Answers

souvik9086souvik9086

No, I don't think it will be needed to be reset in test classes.

 

You can share if you face any issue regarding this.

kdaviskdavis

That was my first assumption as well,

I just debugged the static variable through my test method and it doesn't reset it remains the same through the entire test method.

 

I declare my static variable in a class as such:

 

public class DataUtil {

    public static Boolean HasRun = False;

}

 

 

This is the execution in the test method:

 

@isTest static void untiTest() {

insert Procedures

//Procedure Trigger fires and sets static variable HasRun = TRUE

 

insert Revenue

//Revenue trigger fires and updates procedures. HasRun blocks the trigger from running, when you would assume it would //reset because of a new DML operation

}

Not really sure what to take from this, whether I am doing something wrong in the test method or with the recursive variable.

 

 

JPClark3JPClark3

It will remain set within the same test method. It is considered the same context.

 

It will reset to default in between test methods.

This was selected as the best answer