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

Error message not appear in VF page

Good day, 


I would like to create a VF page as a error page receiver mainly for exception, with the simple code below, i do not know why the error come from controller doesn't appear in following Vf page



<apex:form >
        <apex:pageBlock >
       	<apex:pageMessages />




 My controller have something like below 



ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'this is error');

 URL = '/apex/myErrorPAge';

PageReference pageRef = new PageReference(URL);
return pageRef;

 But i can't figure out why the error message doesn't show in VF page ?


Question :

1. in VF page, i do not put controller, so that it can be used as general exception page, can i do that ?

2. Even i have put the controller in VF page, but it still not able to show me message, not sure what i have miss out


Thanks in advance !


Afzal MohammadAfzal Mohammad

I would suggest you to do the following.


  1. pass your error message as a query string parameter to your error vf page.
  2. your error vf page should have controller class
  3. read the error message from query string
  4. call addmessages method in our controller's constructor and pass your error message to it.

Hope that helps.




    Thanks   !


    So we expecting error show in URL ? do we have chance to avoid the error msg show in URL string ?


    If you want to pass information from one page to another they must either share the same controller or pass through query parameters.

    And if you want to pass via the controller state you must NOT use setDirect(true), which will realod and hence discard any view state.  



    I can't pass in something like below ? I get null value :( ..any chance to declare session like java ?


     ApexPages.currentPage().getParameters().put('errorStr', 'this is error msg');

    Afzal MohammadAfzal Mohammad

    You are trying to add a parameter to current page, which is not going to work.


    Suppose you are redirecting user from page1 to page2 (error page). Then in your page1's controller you will have something like this.



    PageReference p = Page.page2;
    p.getParameters().put('error', 'this is error msg');
    return p;

    Hope that helps.