3

Can we check if there are any error messages on a Visualforce Page?

My scenario is to redirect to a record after a record is inserted on button click. I have an oncomplete attribute on a commanbutton that redirects to specific record.

But if there are any error message in process of insertion, it shouldn't redirect and error message should be displayed on page.

My plan was to check for any error messages on Visualforce Page and call the redirect in oncomplete if error messages doesn't exist.

<apex:commandButton id="btnSubmit" value="Submit" action="{!submit}" oncomplete="redirect()"/>

Please let me know if this is possible or any other solutions that suits my situation.

3 Answers 3

5

You are looking for ApexPages.hasMessages (documentation):

if (ApexPages.hasMessages()) return null;
return intendedReference

If you want to use it in Javascript, you can expose a property like so:

public Boolean hasError { get { return ApexPages.hasMessages(); } }

Then in your script you can check it like:

if ({!NOT(hasError)}) // do your redirect 
7
  • i added a property, but it gives me same boolean always when any error exists or doesn't exist. public Boolean hasError { get { return ApexPages.hasMessages(); } }, javascriprt function called from oncomplete, function CloseAndRefresh(){ alert({!hasError}); }
    – Anurag
    May 24, 2016 at 20:20
  • Hmm, it works for me. Did you try wrapping it in NOT? Is it always true or always false? @AnuragA
    – Adrian Larson
    May 24, 2016 at 20:23
  • Ya I declared the property at the beginning of the class and debugged before the return statement in method. I see error in debug but in alert i always get same value, alert({!NOT(hasError)}), its always true
    – Anurag
    May 24, 2016 at 20:27
  • @AnuragA Are you actually adding them to the page? You need to call ApexPages.addMessage (or addMessages) at some point for this (or any) approach to work.
    – Adrian Larson
    May 24, 2016 at 20:29
  • Ya Adrian, Im addid the error messages to ApexPages.addMessage for displaying on the page, i debugged before return statemnets and am seeing expected results, but when i call i from javasciprt its always same value. Do we just need to declare the property at the beginig and call it or manipulate something with it in the methods?
    – Anurag
    May 24, 2016 at 20:36
3

The more common pattern, given that you are calling a server-side controller method anyway, is to make the decision in Apex:

public PageReference submit() {
    ...
    return return ApexPages.getMessages().size() == 0 ? Page.NextPageName : null;
}

where returning null leaves you on the current page. You then don't need the oncomplete logic in the Visualforce.

3
  • This is working fine, but the submit button is on a popup vf page, i have to redirect to the parent window closing the popup. This is redirecting me to parent record but in popup window itself, how can i close the current window and get to parent record.
    – Anurag
    May 24, 2016 at 19:55
  • @AnuragA Think you have to add an Apex property that you can then reference in your Visualforce - see Adrian's answer. Don't think you can get the messages through $CurrentPage.
    – Keith C
    May 24, 2016 at 20:04
  • @AnuragA Just before the return statement add System.debug(ApexPages.getMessages()) in case there are other non-error messages that are being counted or to see the messages.
    – Keith C
    May 24, 2016 at 20:21
0

I ran into the same problem implementing a js method which prevented multiple submissions by deactivating the submit buttons. I wanted this method to deactivate the submit buttons if the data submitted had no VF errors, but not if it did. Like others, I tried testing apexPages.hasMessages() but found that it always returned true, even when a user had corrected their data.

I eventually ended up giving users one chance to correct their data, and moving on or erroring out after that.

I created a global js variable: let AlreadySubmitted = false;

Then I used it in a js method I called onClick:

    function preventMultipleSubmissions(){
        elem = document.getElementById('apexErrorMessagesSection');
        if (elem.innerText.length==0 || AlreadySubmitted==true) {
            document.getElementById('SubmitAndNextButton').disabled = true;
            document.getElementById('SubmitAndExitButton').disabled = true;
        } else {
            elem.scrollIntoView(true);
        }
            AlreadySubmitted=true;
    }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .