Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Is there a way to clear the PageMessages in my controller without losing my Page's viewstate?

At the moment I'm doing a ajax call from my visualforce page, which rerenders part of my page, but this could also generate new error messages, I would like to clear older error messages without having to reload my entire page and loose the viewstate.

I've tried

ApexPages.getMessages().clear();

In my extension, without luck, and the documentation doesn't seem to be very helpful on this point either.

share|improve this question

4 Answers 4

up vote 3 down vote accepted

There are some alternative ways to display error messages, but like @Mark Pond answered you cannot clear the ApexPages.getMessages(). Also, you cannot iterate over it and replace the elements on it, or rather you can but it doesn't stick just like you observed with the clear() call.

I suspect that the List that is returned by getMessages() is defensive copy of what is actually stored.

There are alternatives, such as doing away completely with apex:pageMessages in favor of using apex:pageMessage and building up your own error message(s) to display under that tag. Obviously there are drawbacks such as maintainability with having to manually handle and report on all of the errors.

share|improve this answer

You won't need to clear any existing messages (in fact you can't modify or clear the collection), nor will you lose your viewstate. What you are describing is exactly how the system works.

Each time the page is posted, the pagemessages collection is cleared and any new errors are added and displayed for the new request/response cycle.

Sample Working Controller:

public with sharing class PageController {

    public string myStringValue { get; set; }       
    public PageController() {}

    public PageReference action1() {
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Info Message'));
        return null;
    }
    public PageReference action2() {
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Error Message'));
        return null;
    }
}

and the page

<apex:page controller="PageController" >
    <apex:form >
        <apex:pageMessages id="messages" /> 
        <apex:inputText value="{!myStringValue}" />
        <apex:commandButton action="{!action1}" value="action 1" rerender="messages" />
        <apex:commandButton action="{!action2}" value="action 2" rerender="messages" />
    </apex:form>
</apex:page>

If this doesn't describe your scenario or if you are seeing behavior that differs from this, can you provide more information and markup to support it?

share|improve this answer
    
This is all true when you aren't using actionsupport calls (ajax) to make calls to your controller. upon receiving the response from your controller, you can perfectly add new messages (in your controller) and have them added to your page dynamically, but you can't clear / delete old values in your controller and have them dynamically removed from your page –  PJC Jan 17 '13 at 9:49

Maybe try something like:

<apex:page> 
<apex:actionFunction action="..." rerender="pageMessages" />
<apex:pageMessages id="pageMessages" rendered="someBooleanAssignedControllerAction" />
...

Using the 'ActionFunction' with the partial page refresh allows you to assign a variable that will hide the <apex:pagemessage /> output. Your probably going to have to assign them using a list of string and a custom array, as others have mentioned due to limitations with them persisting in a single setate.

share|improve this answer

As Peter Knolle mentioned, we can supress the error messsage that is being displayed.

I have done as follows and its working, its displaying custom message: <apex:PageMessages rendered="{!showErrorMessages}"/>

<apex:pageMessage strength="1" severity="error" summary="Error Message2" rendered="{!NOT(showErrorMessages)}"/

controller:

 if(ApexPages.hasMessages(ApexPages.Severity.Error))
 {

  List&lt;ApexPages.Message> listOfMessage=ApexPages.getMessages();

   for(integer i=0;i&lt;listOfMessage.size();i++)

   {

   if(listOfMessage.get(i).getSummary().contains('Message 1'))

   {

      showErrorMessages=False;

   }
  }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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