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
Marketing Marketing 27Marketing Marketing 27 

Validation Rule Messages Not Showing Up On Visualforce Page

I have a custom VF page that helps users make a certain type of opportunity. It is triggered by a button on the opportunity record page.

I have the <apex:pagemessages /> tag on the custom page and the below try catch block in my controller extension. However, when I intentionally leave fields out to try and trip the validation rule, it doesn't show it. it just redirects me to the original opportunity when I click save.

can someone please tell me what I'm doing wrong?
 
public PageReference Save(){
        
        try{
            insert TD2nd;
            
        }
        catch(System.DmlException e){
            String error = e.getMessage();
            Integer numDML = e.getNumDml();
            system.debug('num dml exceptions: '+numDML);
            ApexPages.addMessages(new ApexPages.message(ApexPages.Severity.ERROR, error));
        }
        
        TD1st.X504_Related_Opportunity__c = TD2nd.Id;
        TD1st.X504_Combined_Amount__c = getCombinedAmount();
        update TD1st;
        PageReference originalOpp = new ApexPages.StandardController(TD2nd).view();
        Return originalOpp;
    }

 
Best Answer chosen by Marketing Marketing 27
Marketing Marketing 27Marketing Marketing 27
I was able to fix this with the help of this article.
https://www.biswajeetsamal.com/blog/display-of-validation-rule-error-on-visualforce-page/
 

All Answers

PriyaPriya (Salesforce Developers) 
Hey ,

Try replacing from
<apex:Messages />
To
<apex:pageMessages id="showmsg" />

And then in each command button or action where there is a possibility of displaying an error you have to add to your reRender attribute the apex:pageMessages id:
<apex:commandButton value="savedupe(Ignore)" action="{!Test3}" styleclass="Disab2" rendered="{!save_dup_ignore}" id="sdi" reRender="showmsg" />

For more reference refer this :- 

https://salesforce.stackexchange.com/questions/104650/validation-rules-error-not-seen-in-custom-vf-page

https://developer.salesforce.com/forums/?id=906F0000000990kIAA

Hope to have helped!

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
 
Marketing Marketing 27Marketing Marketing 27
That didn't work unfortunately.

It still just redirects to the original opportunity without any error message when I hit save.
Marketing Marketing 27Marketing Marketing 27
so I commented out the return statement and made it a void method and the error messages showed up. It was getting past the try catch block and returning the pageReference before the error messages could come up.

So how do I get it so that the pagereference gets returned only if the try catch block passes?
Marketing Marketing 27Marketing Marketing 27
I was able to fix this with the help of this article.
https://www.biswajeetsamal.com/blog/display-of-validation-rule-error-on-visualforce-page/
 
This was selected as the best answer