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
MVJMVJ 

How to Clear apex:pageMessages?

I have a VF page that allows a team member to do a search.  The page has an error message tag that shows any errors that we validate for when the user clicks on the Search button.

 

What I want to do is if the user clicks the button I want to clear any messages that may currently being displayed and no longer relavante.

 

How can I do that?  Any help is aprriciated.

 

Here is the VF Page:

 

 

<apex:page Controller="AllAccountSearchController" tabstyle="Account" sidebar="false"> <!-- Used to include sorttable static resource for applying client sorting functionality --> <script type="text/javascript" src="{!URLFOR($Resource.sorttable, 'sorttable.js')}"/> <!-- Filter criteria --> <apex:form id="Filter"> <apex:pageBlock title="Filter"> <apex:pageMessages ></apex:pageMessages> <apex:pageBlockSection title="Criteria" columns="6" collapsible="false"> <apex:panelGrid columns="6" id="theGrid"> <apex:outputLabel style="font-weight:bold;" value="Duns Number" for="DunsNumber"/> <apex:inputText id="searchDunsNumber" value="{!searchDunsNumber}" size="10"/> <apex:outputLabel style="font-weight:bold;" value="" for=""/> <apex:outputLabel style="font-weight:bold;" value="" for=""/> <apex:commandButton action="{!search}" value="Search" id="searchBtn"/> </apex:panelGrid> </apex:pageBlockSection> </apex:pageBlock> </apex:form> <!-- Displays the Search Results section --> <apex:pageBlock title="Account(s)" > <apex:pageBlockSection title="Search Results" columns="1" collapsible="false"> <apex:dataTable value="{!AllAccounts}" var="each" cellpadding="10" border="0" width="100%" columnsWidth="1000,200,200,200,200,200,200,200" style="vertical-align:top;word-wrap: break-word;" styleClass="sortable list"> <apex:column headerValue="Account Name"><a href="/{!each.Id}">{!each.Name}</a>&nbsp;</apex:column> <apex:column headerValue="Account Duns">{!each.Site}&nbsp;</apex:column> <apex:column headerValue="Address">{!each.BillingStreet}&nbsp;</apex:column> <apex:column headerValue="City">{!each.BillingCity}&nbsp;</apex:column> <apex:column headerValue="State">{!each.BillingState}&nbsp;</apex:column> <apex:column headerValue="Zip">{!each.BillingPostalCode}&nbsp;</apex:column> <apex:column headerValue="Account Owner">{!each.Owner.Name}&nbsp;</apex:column> <apex:column headerValue="Owner email">{!each.Owner.email}&nbsp;</apex:column> <apex:column headerValue="Created Date">{!each.createddate}&nbsp;</apex:column> <apex:column headerValue="Modified Date">{!each.LastModifiedDate}&nbsp;</apex:column> </apex:dataTable> </apex:pageBlockSection> </apex:PageBlock> </apex:page>

 

Here is the Controller:

 

 

public class AllAccountSearchController { private Boolean searched = False; private String searchDunsNumber = removedashes(ApexPages.currentPage().getParameters().get('duns')); String strQuery = 'Select id, name, site, BillingStreet, BillingCity, BillingState, BillingPostalCode, createddate, LastModifiedDate, Owner.Name, Owner.email from Account where site like ' +'\'%'+searchDunsNumber +'%\'' + 'order by Name asc'; public string removedashes(string str){ return str.replaceall( '-', '' ); } public void search() { searchDunsNumber = removedashes(searchDunsNumber); if (this.searchDunsNumber.length() > 3 && this.searchDunsNumber.length() < 10){ //this.searched = True; strQuery = 'Select id, name, site, BillingStreet, BillingCity, BillingState, BillingPostalCode, createddate, LastModifiedDate, Owner.Name, Owner.email from Account where site like ' +'\'%'+searchDunsNumber +'%\'' + 'order by Name asc'; } else{ ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Please enter a DUNS Number with at least 4 characters and less then 10.'); ApexPages.addMessage(myMsg); } } public List<Account> getAllAccounts(){ List<Account> listAccounts = new List<Account>(); //listAccounts = [Select id, name, site, BillingStreet, BillingCity, BillingState, BillingPostalCode, createddate, LastModifiedDate, Owner.Name, Owner.email from Account where site like'%123456789%' order by Name desc]; //if (searched) listAccounts = Database.query(strQuery ); if (this.searchDunsNumber.length() > 3 && this.searchDunsNumber.length() < 10){ listAccounts = Database.query(strQuery ); } else{ ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Please enter a DUNS Number with at least 4 characters and less then 10.'); ApexPages.addMessage(myMsg); } return listAccounts ; } public String getsearchDunsNumber() { return removedashes(searchDunsNumber); } public void setsearchDunsNumber(String searchDunsNumber){ this.searchDunsNumber = removedashes(searchDunsNumber.trim()); } }

 

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
WesNolte__cWesNolte__c

Hey

 

Your Page is holding onto a reference of your controller so the messages aren't being cleared. If you modify your search() method the Page and it's controller will be created from scratch i.e. your controller will be re-instantiated. To do this we'll need to make these changes,

 

 

public PageReference search() { searchDunsNumber = removedashes(searchDunsNumber); if (this.searchDunsNumber.length() > 3 && this.searchDunsNumber.length() < 10){ //this.searched = True; strQuery = 'Select id, name, site, BillingStreet, BillingCity, BillingState, BillingPostalCode, createddate, LastModifiedDate, Owner.Name, Owner.email from Account where site like ' +'\'%'+searchDunsNumber +'%\'' + 'order by Name asc'; } else{ ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Please enter a DUNS Number with at least 4 characters and less then 10.'); ApexPages.addMessage(myMsg); }

 

PageRerence pr = System.Page.TheNameOfThePageTHatYouPosted;

pr.setRedirect(true);

return pr; }

 

 Give that a bash and tell me if it helps.

Wes 

 

 

 

All Answers

WesNolte__cWesNolte__c

Hey

 

Your Page is holding onto a reference of your controller so the messages aren't being cleared. If you modify your search() method the Page and it's controller will be created from scratch i.e. your controller will be re-instantiated. To do this we'll need to make these changes,

 

 

public PageReference search() { searchDunsNumber = removedashes(searchDunsNumber); if (this.searchDunsNumber.length() > 3 && this.searchDunsNumber.length() < 10){ //this.searched = True; strQuery = 'Select id, name, site, BillingStreet, BillingCity, BillingState, BillingPostalCode, createddate, LastModifiedDate, Owner.Name, Owner.email from Account where site like ' +'\'%'+searchDunsNumber +'%\'' + 'order by Name asc'; } else{ ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Please enter a DUNS Number with at least 4 characters and less then 10.'); ApexPages.addMessage(myMsg); }

 

PageRerence pr = System.Page.TheNameOfThePageTHatYouPosted;

pr.setRedirect(true);

return pr; }

 

 Give that a bash and tell me if it helps.

Wes 

 

 

 

This was selected as the best answer
MVJMVJ

Wes, Thanks that worked well.

 

 

All here is the updated Search routine that does what I want it to:

 

 

public PageReference search() { searchDunsNumber = removedashes(searchDunsNumber); if (this.searchDunsNumber.length() > 3 && this.searchDunsNumber.length() < 10){ //this.searched = True; strQuery = 'Select id, name, site, BillingStreet, BillingCity, BillingState, BillingPostalCode, createddate, LastModifiedDate, Owner.Name, Owner.email from Account where site like ' +'\'%'+searchDunsNumber +'%\'' + 'order by Name asc'; } else{ ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Please enter a DUNS Number with at least 4 characters and less then 10.'); ApexPages.addMessage(myMsg); } PageReference pr = New PageReference('/apex/accountownersearch?DUNS='+removedashes(searchDunsNumber)); pr.setRedirect(true); return pr; }