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
pankaj tiwari 13pankaj tiwari 13 

I am trying to bypass validation rule through vf need help

Hi, I was trying to bypass validation rule (min pay rule) if I am entering the record from visualforce page.

I have taken the following steps;
1. Create a checkbox formula looking like;

AND(NOT( 
ISBLANK( Min_Pay__c ) || 
ISBLANK( Max_Pay__c ) || 
(Min_Pay__c > Max_Pay__c) 

)

2. Then I have created a workflow rule if above formula is true then make it false.

3. Now I was trying to create a class with a constructor where I can define boolean type true always but it is not working;
Please check the class code;

public class positionvf{
Position__c p=new Position__c();
isRequestFromVF_c__c cc();
  public Position__c getp(){
  public boolean cc{get;set;}
  return p;
  }
  
  public positionvf()
  {
  cc = true;
  }
  
  public void change()
  {
 if(cc == false)
 cc=true;
 else
 cc=true;
 // system.debug('%%%%%%%%%%%%%%' +cc);
 }
  
  public PageReference save(){
  insert p;
  return null;
  }
}

 

Any help will be appreciated.

Maharajan CMaharajan C
Hi Pankaj,

Did you added this custom field in the Validation rule condition as like (isRequestFromVF_c__c = false) and set the Default value as False in the Field.

One more suggestion i can give as you can try with the Immediate Attribute in the Save Command button  to bypass the Validation rule.

As like below:
<apex:CommandButton action="{!Save}" value="Save" immediate="true">

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Raj
pankaj tiwari 13pankaj tiwari 13

Hi Maharajan,

Thanks for your reply.

I have used immediate true is not giving validation errors but its also not saving the actual data inserted through VF.

My minimum pay rule for error condition is 

"Min pay > max pay"

If I have to add 'isRequestFromVF_c__c = false' in my validation rule so I have to use AND operator or what?

 

pankaj tiwari 13pankaj tiwari 13

Also I would like to know how can I add my checkbox formula to the constructor. 

In below code I wanted to call the checkbox as an object so that I can proceed further.

public class positionvf{
Position__c p=new Position__c(); //this is ok
isRequestFromVF_c__c cc(); // problem start here, I am not sure how to call my custom formula check box here
  public Position__c getp(){
  public boolean cc{get;set;}
  return p;

VamsiVamsi
Hi Pankaj,

Please go through the following Link 

http://bobbuzzard.blogspot.com/2011/11/bypass-validation-rules-from-apex.html

Please mark as best answer if the above helps...!!!
Abdul KhatriAbdul Khatri
First please can you revisit your forumla. I think that is not correct, I think it should be like this
AND(
    NOT(ISBLANK( Min_Pay__c )), 
	NOT(ISBLANK( Max_Pay__c )), 
	(Min_Pay__c > Max_Pay__c) 	 
)

You said you want to bypass the validation rule but haven't shared how it looks.

Can you please also share the VF page in order to see how you are making use of the above class. 
pankaj tiwari 13pankaj tiwari 13

Hi @Abdul,

I will correct the formula.

Here is my apex class:

public class testcontroller
{
   
   public class pos 
   {
   Position__c p = new Position__c();
   }

   
   public Position__c p{get;set;}
   {
   return p;
   public Boolean cbTest{get;set;}
   }

      public testcontroller()
      {
      cbTest=true;
      }
   
       public void change()
       {
          if(cbTest == false)
          cbTest=true;
        else
          cbTest=true;
          // system.debug('%%%%%%%%%%%%%%' +cbTest);
       }
   
  public PageReference save(){
  insert p;
  return null;
  }
}

At the time I am struggling with my apex code then I will call the controller again to my vf page.

My existing VF page is:

 

<apex:page controller="positionvf" sidebar="false">
<apex:form >
<apex:pageBlock title="bypass validation rule, record insertion in position object">
<apex:pageblockSection id="a">
<apex:inputField value="{!p.Name}"/>
<apex:inputField value="{!p.Functional_Area__c}"/>
<apex:inputField value="{!p.Type__c}"/>
<apex:inputField value="{!p.Satus__c}"/>
<apex:inputField value="{!p.skills_Required__c}"/>
<apex:inputField value="{!p.Country__c}"/>
<apex:inputField value="{!p.State__c}"/>
<apex:inputField value="{!p.Max_Pay__c}"/>
<apex:inputField value="{!p.Min_Pay__c}"/>
<apex:inputField value="{!p.Name__c}"/>
<apex:inputField value="{!p.Job_Description__c}"/>
<apex:inputField value="{!p.Open_Date__c}"/>
<apex:inputField value="{!p.Close_date__c}"/>
</apex:pageblockSection>
<apex:pageBlockButtons >
<apex:commandButton style="float:centre" value="Save" action="{!save}" reRender="a" immediate="true"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>

 

pankaj tiwari 13pankaj tiwari 13

Now I have changed my apex code and there is no errors. Please guide me if I will post any issues.

 

public with sharing class bypassvalidation {

   public boolean cc{get;set;}
   
   Position__c p = new Position__c();
   public position__c getp(){
   return p;
   }
   
   
   public bypassvalidation (){
   cc = true;
   }
   
   public void change(){
   if(cc=false)
   cc=true;
   else
   cc=true;
   }

   public PageReference save(){
   insert p;
   return null;
   }

}

Abdul KhatriAbdul Khatri
So it means formula correction helped you fix your errors and for now you don't need any help.
Abdul KhatriAbdul Khatri
Hey was I helpful?