Friday, December 5, 2014

Show error message in Visualforce Page

Show error message in Visualforce Page using ApexPages.addmessage

Sometime we need to show  or display error message on Visualforce page with different notations like warning, error, info etc.... We can implement this requirement by creating new instance of ApexPages.message and then adding message to Apexpages using ApexPages.addmessage. Then displaying these messages in visualforce page.

We can display 5 different types of message in Visualforce Page. In the example below, we are showing 5 input fields of account. We have added a button on visualforce page. Different type of message will be shown on visualforce page if we will keep any field blank.


Visualforce Page:

<apex:page standardController="Account" extensions="DisplayErrorMessageInVfCls">
 <apex:form >
   <apex:pageblock >
      <apex:pageMessages id="showmsg"></apex:pageMessages>
         <apex:panelGrid columns="2">
           Account Name: <apex:inputText value="{!}"/>
           Account Number: <apex:inputText value="{!acc.AccountNumber}"/>
           Account Phone: <apex:inputText value="{!}"/>
           Account Site: <apex:inputText value="{!}"/>
           Account Industry: <apex:inputText value="{!acc.industry}"/>
           <apex:commandButton value="Save Details" action="{!save}" style="width:90px" rerender="showmsg"/>

Apex Class:

public with sharing class DisplayErrorMessageInVfCls{

    public Account acc{get;set;}
    //extension of Standard controller.
    public DisplayErrorMessageInVfCls(ApexPages.StandardController controller) {
        acc = new Account();
    public void save(){
      if( == '' || == null)
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please enter Account name'));
      if(acc.AccountNumber == '' || acc.AccountNumber == null)
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter Account number'));
      if( == '' || == null)
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter Account phone'));
      if( == '' || == null)
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'Please enter Account site'));
      if(acc.industry == '' || acc.industry == null)
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Please enter Account industry'));

To Refer visualforce tags :


Post a Comment

| ,