0

I have requirement where i need to disable validation rule to avoid deployment errors.

I have added a custom setting and my rule is below

AND($Setup.cust_setting__c.ValidationsOff__c=FALSE, (ISBLANK( phone ) ))

i have added the profile in custom setting too disable validationn to run but still my below test class is failing due to validation.How can i handle it

@isTest
   public class ContactPhoneTest {
   @isTest
    public static void  TestPhone(){
        contact con=new contact();
        con.lastname='Test';
        con.FirstName='Con';
        insert con;
      }
   }

2 Answers 2

1

Since tests cannot see existing data, You have to add a record for the custom setting, and set it to the correct value

Do it this way:

cust_setting__c sett = cust_setting__c.getInstance();

    if (sett.Name == null) { // A default value doesn't exist. Create one.
        sett = new cust_setting__c();
        sett.ValidationsOff__c = false;

        insert sett;
    }

Note, this snippet is also good if you want to in regular code, in case you have to turn off validations programatically via apex.

5
  • do i need to add this code to every class or how it is
    – aditi
    Nov 1, 2018 at 15:21
  • 1
    Not at all, you can add this to your test (maybe in @TestSetup?). Then, you can add this anywhere you thing it's needed (or abstract it in a utility class and call it) Nov 1, 2018 at 15:21
  • it will be very helpful if you can provide some sample
    – aditi
    Nov 1, 2018 at 15:23
  • There are many classes which are written by others ,is This possible without touching to other classes avoid validations to run
    – aditi
    Nov 1, 2018 at 15:26
  • @aditi there is a sample in the answer. Just post it above your current code, or anywhere where you want to stop validations. Nov 1, 2018 at 15:33
1

Based on your comment on the other answer

There are many classes which are written by others ,is This possible without touching to other classes avoid validations to run

If you don't want to create the custom setting in respective test classes, one of the ways is to change your validation rule.

You can instead utilize a Profile Name or Custom Permission assigned to a User's profile who's deploying the component.

So your validation rule looks something as below:

AND($Profile.Name <> 'your profile name', (ISBLANK(phone)))

OR, if you use Custom Permission, you can have it as:

AND(NOT($Permission.Skip_Validation_Rule), (ISBLANK(phone)))

and that it will be only triggered on other profiles but not for the respective profile which is performing the deployment.

2
  • I disagree with this answer. If her profile name changes, or others need to be added, maintenance of all of these VRs can very quickly prove impossible. Nov 1, 2018 at 15:36
  • Sure you can. But this VR is not a maintenance nightmare for sure. You don't change the name of the profile that frequently. Then if it's specific for deployments, the usual way is that you will have a specific profile/or system admin profile doing that for you. And then having Custom Permissions let's you quickly enable/disable based on which User is the actor here.
    – Jayant Das
    Nov 1, 2018 at 15:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .