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
Teach_me_howTeach_me_how 

How to implement Test.isRunningTest() in Validation Rule?

I dont want to execute validation rule upon running test class?

SurekaSureka
Hi,

I have followed the following method for test methods, data loading etc.

1. Create a boolean field in User object - IsValidationRuleAllowed?
2. Set the field value appropriately whether you want the validation rule to be executed for the corresponding user
3. Use this field in all the validation rules
4. When creating users in test method, set this field appropriately.
5. Validation rules will not run for the corresponding test method user.


Thanks
Bhuvana
Ankit AroraAnkit Arora

You can do this using custom settings, here is the process:

 

1) Create a custom setting (for e.g "TurnOffValidationRule" and a boolean "TurnOff" field in it)

2) Now you can access the custom setting in validation rule like this:

$Setup.TurnOffValidationRule__c.TurnOff__c , put this in IF condition before anything

3) From test class you can make this "TurnOff__c" to true

 

Now when you execute your test class your validations will not be executed.

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

 

Teach_me_howTeach_me_how

I like the idea of custom setting but i dont like to modify existing test class to consider the custom setting. too  many existing test class

Ankit AroraAnkit Arora

AM sorry there is no other straight forward option.

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

Mark Hartnady 13Mark Hartnady 13
Using a custom setting like $Setup.TurnOffValidationRule__c.TurnOff__c as suggested by Ankit Arora won't work when your unit tests run. This is because by default, unit tests run in a SeeAllData=False content, meaning your custom setting will be invisible in a Test Execution context. This is why Validation Rules need something like a $System.TestIsRunning global variable much like the Apex system-wide static method Test.isRunningTest() 
docbilldocbill
Here is a very nasty hack.   

First add a custom Hierachy setting of "Testing".   Add a field called "SeeAllData".   Set the default value to "false".   Add an organizational default to set the default to true.

You can now test if your custom setting are visible in your validation rule...   Here is an example.   Lets say you have a validation rule that only applies when a particular custom setting is set:

$Setup.AdminByPass__c.IsIT__c

If you did not want this validation rule to block testing, you would extend this with:
 
OR($Setup.AdminByPass__c.IsIT__c,NOT($Setup.Testing__c.SeeAllData__c)) 

The way this works is simply when running without SeeAllData, even the organizational default is not visible.   So the value of "false" is seen.   But if you are not running a test class, or you are running a test class where SeeAllData is set to true, then the organizational default is seen.

No modifications to your test classes are needed...
 
docbilldocbill
The See All Data hack the allows you to add other checkboxes and such for testing, that are assigned to specific users.   So for example you could add a IsTestUser flag, do the same tricks as the See All Data, except reverse checked and unchecked and add an entry for the test user.   Then if running without SeeAllData, the IsTestUser will always be true.   If running with SeeAllData, then the flag will only be true for the user you use as a test user.