1

I have four scenarios with catch exceptions in controller and used "apex:pageMessages" only once in Visualforce page.But that is not working for all the scenarios? Is it mandatory to use "apex:pageMessages" in all the places where we call those scenarios?

Visualforce Page:

This is a part of the code where the method for the catch exception is called. I have one more method say "catch method2" with a different catch exception.So for each scenario should I use apex:pageMessages? Using it outside form doesn't catch all my exceptions.I need to manually give it inside form for each method as I have specified.

     <apex:form id="myForm">
      <div id="container" style="float:left;width:80%">
      <apex:commandButton action="{!ViewCatch method}" value="catch" id="catchbutton" style="{!(If(displayCatch,'color:red;','color:black;'))}" status="loadStatus" rerender="myForm"/>
**<apex:pageMessages />**
    <apex:pageBlockTable value="{!Catch method}" var="oq"  styleClass="dataTable">

Class:

catch(Exception e){
            Apexpages.Message mymsg = new Apexpages.Message(Apexpages.Severity.FATAL,e.getStackTraceString());
            Apexpages.addMessage(mymsg);

I need a solution on how to catch all the exceptions with a single apex:pageMessages?Any Idea?

  • 2
    Hi Welcome to SFSE. if you can share your code, it will help the community members to answer you. – Vamsi Krishna Jan 21 '16 at 12:23
  • typically, apex:pageMessages appears only once - just below the apex:form - any controller use of apexpages.currentPage().addMessage(...) will append to the current page's list of messages – cropredy Jan 21 '16 at 15:18
  • @crop1645 Yeah.But I have five buttons with a different catch statement in a single page.When I click the second button first button's catch is also getting displayed.How can I overcome it? – Pril Jan 22 '16 at 12:48
1

It sounds like you have several commandButtons tied to several action methods where each action method is not doing a page redirect. As such, the apex:pageMessages will fetch all messages in the current page context.

One way around this issue is to use the (singular) component <apex:pageMessage summary="{!myMsg}" severity="error" strength="2" rendered="{NOT(ISBLANK(myMsg))}"/> where myMsg is a getter property in the controller.

Your action methods invoked by the commandButtons set the error message into myMsg rather than using ApexPages.currrentPage().addMessage(...). Thus, only the most recent caught error is displayed.

UPDATE

public String myMsg {get; private set;}

try {..}
catch(Exception e){
     this.mymsg = e.getMessage() + ' ' + e.getStackTraceString();    
}
| improve this answer | |
-1

@pril, when you call action method and rerender some block then rerender apex:pagemessage tag too. like this -

**<apex:pageMessages id='errorMsg' />**
     <apex:form id="myForm">
      <div id="container" style="float:left;width:80%">
      <apex:commandButton action="{!ViewCatch method}" value="catch" id="catchbutton" style="{!(If(displayCatch,'color:red;','color:black;'))}" status="loadStatus" rerender="myForm, errorMsg"/>
    <apex:pageBlockTable value="{!Catch method}" var="oq"  styleClass="dataTable">

In this case, you will need to write only on apex:pageMessages tag that will show all excetions.

| improve this answer | |
  • 2
    Sandeep -- Forum etiquette is to not solicit OP to mark your answer as the solution. One exception to this etiquette rule is when you've engaged via comment thread with OP and OP has acknowledged that your answer solved his/her issue – cropredy Jan 21 '16 at 21:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.