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
SFDC_2706SFDC_2706 

Standard validation rule error message on VF page

Hi All
I created one VR on a custom object called "projects__c" where user is not allowed to enter more than 100% in 5 different percent fields. This works fine on the standard page but when i created a VF page to override the button the error message doesn't pop in the VF page.

I tried using try and catch statements in controller and apex:messages in VF page but no luck. Please find below my code and help me in this :

Controller: (Not entire controller)
public class ProjectsCustomPageCtrl{
  public Projects__c proj {get;set;}

  public ProjectsCustomPageCtrl(ApexPages.StandardController myController){            
            proj = new Projects__c();
      }
      
  public PageReference SaveRec(){  
        if(proj.Name!=null && proj.Name!=''){                         
            insert proj;                  
        }                 
      return new PageReference('/' +proj.id).setRedirect(true);       
    }   
}

VF Page:
<apex:page standardController="Projects__c" extensions="ProjectsCustomPageCtrl" sidebar="false" tabStyle="Projects__c">
<apex:messages/>
 <apex:sectionHeader title="Project Details"/>
   <apex:form >        
      <apex:pageBlock id="blck"> 
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!SaveRec}"/>
                <apex:commandButton value="Cancel" action="{!DoCancel}" immediate="true"/>
            </apex:pageBlockButtons>
            
            <apex:pageBlockSection title="Project Information" columns="2">
                   <apex:inputField value=" {!proj.name}" required="true"/>
                   <apex:inputField value=" {!proj.Account__c}"/>
                   <apex:inputField value=" {!proj.Job_Number__c}"/>
                   <apex:inputField value=" {!proj.Status__c}">
                      <apex:actionSupport event="onchange" action="{!ChkProb}" rerender="blck"/>
                   </apex:inputField>   
                   <apex:inputField value=" {!proj.Line_Owner__c}"/>
                   <apex:inputField value=" {!proj.Probability__c}"/>
                   <apex:inputField value=" {!proj.Gross_Revenue__c}"/>
                   <apex:inputField value=" {!proj.Project_Type__c}"/>
                   <apex:inputField value=" {!proj.X3rd_Party_Cost__c}"/>
                   <apex:inputField value=" {!proj.Start_Date__c}"/>
                   <apex:outputText ></apex:outputText>
                   <apex:inputField value=" {!proj.Duration_In_months__c}"/>                                      
            </apex:pageBlockSection>
         
            <apex:pageBlockSection title="Work Spread Details" columns="2">                  
                   <apex:inputField value=" {!proj.January__c}"/>
                   <apex:inputField value=" {!proj.February__c}"/>
                   <apex:inputField value=" {!proj.March__c}"/>
                   <apex:inputField value=" {!proj.April__c}"/>
                   <apex:inputField value=" {!proj.May__c}"/>
                   <apex:inputField value=" {!proj.June__c}"/>
                   <apex:inputField value=" {!proj.July__c}"/>
                   <apex:inputField value=" {!proj.August__c}"/>
                   <apex:inputField value=" {!proj.September__c}"/>
                   <apex:inputField value=" {!proj.October__c}"/>
                   <apex:inputField value=" {!proj.November__c}"/>
                   <apex:inputField value=" {!proj.December__c}"/>
            </apex:pageBlockSection>       
     </apex:pageBlock> 
     
  </apex:form> 
</apex:page>

Please help me soon. Thanks in advance..!!
Kalpesh Vyas 14Kalpesh Vyas 14
Please refere below link:
https://developer.salesforce.com/forums/?id=906F000000099ZVIAY

Put your insert into try catch block and throw exception. ALso change <apex:message> with <apex:pagemessages/>
Soundar Rajan PonpandiSoundar Rajan Ponpandi
Hi Ronu,

Please refer my following code .
 
public class ProjectsCustomPageCtrl{
public Projects__c proj {get;set;}

public ProjectsCustomPageCtrl(ApexPages.StandardController myController){            
            proj = new Projects__c();
      }

public pagereference SaveRec(){

Try{
if(proj.Name!=null && proj.Name!=''){                         
            insert proj;                  
}
}catch(Exception ex){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please enter The Project Name'));
}
return null;

}

}

 

Regards,

Soundar.
SFDC_2706SFDC_2706

@kaplesh : I have already tried this but no luck.

@Soundar : I tried the same thing you have written but facing one error i.e When I enter more than 100 in 5 different fields it given me error on the top but when i enter les than 100 value and hit save the page remains on the same edit mode but in the backend it does create record.

Below is the Save() method :
 
public PageReference SaveRec(){  
  
        Try{
        if(proj.Name!=null && proj.Name!=''){                                     
            insert proj;                  
        }              
    }     
     catch(Exception ex){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please enter The Project Name'));
            }
      //return null;                   
      return new PageReference('/' +proj.id).setRedirect(true);         
    }    
   
    public PageReference doCancel(){        
       return new PageReference('/a01/o').setRedirect(true);
   }    


This is what i am using because i want my page to return to projects ID & should not return null thaty's why commented it. And with this code i get following error :

Update failed. First exception on row 0 with id a012600000FWE90AAH; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Work spread cannot be &gt; 100%: []
Error is in expression '{!SaveRec}' in component <apex:commandButton> in page projectscustomeditpage: Class.ProjectsCustomEditPageCtrl.SaveRec: line 25, column 1


Let me know if you can help here..!!