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 DevlSFDC Devl 

Show Standard Validation rule error message on the VF page via custom controller

Hi,

 

I want to show the standard error message on the same visualforce page without going away from the page. Code is working fine because it is not saving the record nor going further but the error is not displaying on the VF page. Can someone help me out? Thanks in advance.

 

Part of code from VF Page

<apex:form >
<apex:pageBlock title="Request">

<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!save}" reRender="errMsg"/>
<apex:commandButton value="Cancel" action="{!cancel}"/>
</apex:pageBlockButtons>

<apex:pageMessages id="errMsg" />

</apex:form>

 

Part of Code from the Controller extension 

 

public pageReference save()
    { 

Database.SaveResult insertResult = Database.insert(newFeasibility,false);
        system.debug('Result.......'+insertResult.isSuccess());
          if (!insertResult.isSuccess())
             {
            errorMessage = '';             
              for(Database.Error err : insertResult.getErrors())
              {
                errorMessage += (err.getMessage() + '  ');
              }
              system.debug(errorMessage);
              ApexPages.Message errMsg = new ApexPages.Message(ApexPages.Severity.INFO, errorMessage);
              ApexPages.addMessage(errMsg);  
              return null;     
             }

     else 
        {
        if (OpportunityType == 'xyz')
        {
        PageReference redirecturl = new PageReference('/a0R/e?...have page url here');
           return redirecturl; 
         }   

else return null; 

}    

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi,

 

Its just simple, you need to use <apex:pageMessages/> component in your page and try catch exceptions in your class method.

 

For your understanding, I did small changes in your code. Please take a look at the highlighted lines. 

 

VF page:

 

<apex:page ...>

<apex:pageMessages/>   //Here you don't want to specify any of the rendering function. If you need to add you can

...............

<apex:form >
<apex:pageBlock title="PBS Request">

<apex:pageBlockButtons >
<apex:commandButton value="Save & Next" action="{!save}" reRender="errMsg"/>
<apex:commandButton value="Cancel" action="{!cancel}"/>
</apex:pageBlockButtons>

<apex:pageMessages id="errMsg" />

</apex:form>

.........

</apex:page>

 

In Controller,

public pageReference save()
 { 
    

      try{

                    // Write your functionality code

            }

            catch(Exception e){

                        Apexpages.addMessages(e);

            }

       return null;

}    

 

 

Hope so this helps you...!

Please mark this answer a Solution and please give kudos by clicking on the star icon, if you found this answer as helpful.

SFDC DevlSFDC Devl

Hi KamatchiDeviR

 

All my code worked fine without using try catch blocks. The only thing I missed was I placed this <apex:pageMessages id="errMsg" /> after the Pageblock tag but now I changed and included it after the <apex:form> and it is working fine. Thank you.

 

Part of code from VF Page

<apex:form >

<apex:pageMessages id="errMsg" />
<apex:pageBlock title="Request">

<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!save}" reRender="errMsg"/>
<apex:commandButton value="Cancel" action="{!cancel}"/>
</apex:pageBlockButtons>

 

</apex:form>

 

Part of Code from the Controller extension 

 

public pageReference save()
    { 

Database.SaveResult insertResult = Database.insert(newFeasibility,false);
        system.debug('Result.......'+insertResult.isSuccess());
          if (!insertResult.isSuccess())
             {
            errorMessage = '';             
              for(Database.Error err : insertResult.getErrors())
              {
                errorMessage += (err.getMessage() + '  ');
              }
              system.debug(errorMessage);
              ApexPages.Message errMsg = new ApexPages.Message(ApexPages.Severity.INFO, errorMessage);
              ApexPages.addMessage(errMsg);  
              return null;     
             }

     else 
        {
        if (OpportunityType == 'xyz')
        {
        PageReference redirecturl = new PageReference('/a0R/e?...have page url here');
           return redirecturl; 
         }   

else return null; 

}    

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Oh wow, that's great.