Apr 07

apex:pageMessage and apex:pageMessages – Salesforce

apex:pageMessage and apex:pageMessages – Salesforce

Most of the times we are confused between these two visualforce components i.e. apex:pageMessage and apex:pageMessages. A small different of ‘s’. Let’s see the difference between apex:pageMessage and apex:pageMessages – Salesforce.

We are confused that which one should we use, and in which case. There is a small difference between these two. Let me explain the same:

We should use apex:pageMessage when we want to have individual message on the visualforce page and you provide the message to be displayed in the attributes of this tag. This is a kind of static message which you will write in visualforce itself, and can put some conditions in rendered attribute to display the message. The strength attribute of the tag controls the size of the message box displayed on the page. No need to have any controller code for adding the message on the page, because message has been written in visualforce page itself.


<apex:page controller="theController">
<apex:pageMessage severity="Error" summary="First Error message on page" strength="1"/>
<apex:pageMessage severity="Error" detail="Second Error message on page" strength="2"/>
</apex:page>

apex:pageMessage and apex:pageMessages - Salesforce

apex:pageMessage

This can be used to display any custom message that you always want to appear on the screen. There may be a case that whenever a user is on a form you may want a warning to appear to display important information to them.

You can also use this to conditionally display a message, based on some condition that you put in the rendered attribute of this component.

Now Let’s talk about apex:pageMessages:

apex:pageMessages is used to display more than one message on a visualforce page. It will display Salesforce generated messages(probably some exception or error) as well as custom messages that you have added to the ApexPages class in your controller code.

Below is the sample code. All the messages added in apex code using ApexPages class are shown in one box only unlike the previous tag apex:pageMessage where all the messages are shown in different boxes as they are put on page. Below is one example:

<apex:page controller="theController">
<apex:pageMessage severity="Error" summary="First Error message on page" strength="1"></apex:pageMessage>
<apex:pageMessage severity="Error" detail="Second Error message on page" strength="2"></apex:pageMessage>
<apex:pageMessages ></apex:pageMessages>

</apex:page>

The controller code for the same below:


public class theController {

public theController(){

ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, 'First Error Message added from apex'));
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, 'Second Error Message added from apex'));

}
}

apex:pageMessage and apex:pageMessages - Salesforce

apex:pageMessages

Noticed that, how a single instance of the apex:pageMessages handles all of the errors on the page. The use case for this object is to capture all errors on the page including the error or exception messages generated by salesforce, and the messages added in ApexClass as well. It should be used on most custom Visualforce page as a catch all to let users know what errors occured (if no other messaging is used).

Best practice is to always keep apex:pageMessages on your page, because sometimes Salesforce generates some exception or error (may be because of some trigger or workflow), but because of non-availablitiy of this tag on your page, those messages are not displayed and you are confused why my page or functionality is not processing and not going further.

So I would suggest always keep apex:pageMessages tag on your visualforce page.

Good Luck !!

Permanent link to this article: http://www.sfdcpoint.com/salesforce/apexpagemessage-and-apexpagemessages-salesforce/

  • Sakshi Pandey

    Perfect !! I used to get confused everytime.. really helpfull:) Thanks!!