2

We have a managed package install in our org. When I create an opportunity, at a certain stage, I need to create a record of that managed package. For this we define validation rule for certain stages.

In the test class, when I create an opportunity record and reach a certain stage, it throws a validation error.

The Problem is in the test class we don't know how to create a managed package object record data (it refers to an apex class which is part of a managed package).

In order to bypass the validation rule, I created a checkbox field on the User object. In the validation rule, I always check the value as false.

$User.BypassValidation__c=false

So If i manually set the BypassValidation__c to true then it bypasses the validation rule. In order to bypass the validation rule in the test class, I made a boolean variable:

public class TriggerDataFactory 
{
    global static Boolean bypassValidation {
        get {
            return bypassValidation;
        }
        set{
        system.debug('calling set');
         User currentUser = [select BypassValidation__c from User where Id = :UserInfo.getUserId()];
         currentUser.BypassValidation__c=bypassValidation;
         system.debug('Bypass Apex Value:'+bypassValidation);
        }
    }
}

In the test class, I create an Opportunity record and before updating the stage, I am setting the variable value as true so that it allows me to bypass validation rule:

 @isTest public class OpportunityTest{
@isTest static void testMethod(){
Profile p = [SELECT Id FROM profile WHERE name='System Administrator'];
        User usr = TestDataFactory.createUser('Test', 'User', 'test.user@psa.com', p.Id, true); 
        system.runAs(usr){
Opportunity opt=new Opportunity(
Name='Test Opt'
);
insert opt;
Opportunity op=[Select id from Opportunity where Id=:opt.Id];
TriggerDataFactory.bypassValidation=true; //set the value to true so that validtion rule should not enforced.
op.Stage='Stage 2';
update op;
TriggerDataFactory.bypassValidation=false; //Reset to False
}
}
}

But when I run the test class, it still enforces the validation rule. When I check the debug log, I found that Bypass Apex Value: is null, hence I cannot assign/set the value to true.

Can someone please suggest me what I am missing here?

  • DML. You need to update User if you want validation rule to apply your checkbox – kurunve Sep 6 '17 at 13:55
  • Better yet, what was the validation rule / error message? Maybe it gives enough information that you can setup your data in your test to pass the validation?? – Eric Sep 6 '17 at 14:36
  • Problem is we dont have managed package access and hence can not create the managed package object record without sufficient information. – Dhananjay Patil Sep 6 '17 at 15:10
3

You need to create a user and update BypassValidation__c checkbox in the user record.

Also, in the setter you have missed to update user record.

public class TriggerDataFactory 
{
    global static Boolean bypassValidation {
        get {
            return bypassValidation;
        }
        set{
        system.debug('calling set');
         User currentUser = [select BypassValidation__c from User where Id = :UserInfo.getUserId()];
         currentUser.BypassValidation__c = bypassValidation;
         update currentUser; //you have missed this line
         system.debug('Bypass Apex Value:'+bypassValidation);
        }
    }
}

Finally run the test script with System.runAs(user).

Update based on comments

@isTest public class OpportunityTest
{
    @isTest static void testMethod()
    {
        Profile p = [SELECT Id FROM profile WHERE name='System Administrator'];
        User usr = PSATestDataFactory.createUser('Test', 'User', 'test.user@psa.com', p.Id, false);
        User currentUser = [SELECT BypassValidation__c from User where Id = :usr.Id];
        currentUser.BypassValidation__c = true;
        update currentUser;  
        system.runAs(currentUser)
        {
            Opportunity opt=new Opportunity(
            Name='Test Opt'
            );
            insert opt;
            Opportunity op=[Select id from Opportunity where Id=:opt.Id];
            op.Stage='Stage 2';
            update op;          
        }

        User currentUser = [SELECT BypassValidation__c from User where Id = :usr.Id];
        currentUser.BypassValidation__c = false;
        update currentUser;  

    }
}
  • Is it not possible to set the Boolean variable? Because in some methods I am not creating user and use system.runAs() in test classes – Dhananjay Patil Sep 6 '17 at 13:58
  • you have missed to update the user, pls check this updated ans – Santanu Boral Sep 6 '17 at 14:00
  • Instead of creating user in test class,I am using TriggerDataFactory.bypassValidation=true; in apex test class that I mentioned in test class but it still throws an error. Must I create a user and update checkbox value in test class? – Dhananjay Patil Sep 6 '17 at 14:09
  • 1
    you should always create a specific user to run test scripts. It is not advisable to run the script with system admin – Santanu Boral Sep 6 '17 at 14:10
  • I create a user but not updating the checkbox value and I'm using system.RunAs() but it throws an error INVALID_TYPE_ON_FIELD_IN_RECORD, BypassValidation: value not of required type: : [BypassValidation__c] – Dhananjay Patil Sep 6 '17 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.