Show error message in Visualforce Page

Show error message in Visualforce Page using ApexPages.addmessage

apex error message

Sometime we need to show error message on Visualforce page. 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 add 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.

Click for Demo

Error Message

Visualforce Code:

<apex:page standardController="Account" extensions="ErrorMessageInVfController">
 <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="Update" action="{!save}" style="width:90px" rerender="showmsg"/>

Apex Code:

public with sharing class ErrorMessageInVfController {
    public Account acc{get;set;}
    public ErrorMessageInVfController(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'));


For more details about difference between apex:pageMessage and apex:pageMessages, refer apex:pageMessage and apex:pageMessages – Salesforce post.

Permanent link to this article:


Skip to comment form

    • kapil p on March 14, 2014 at 12:34 pm
    • Reply

    Need to create custom sharing button with apex sharing instead of “out of box” functionality of salesforce sharing If having any supporting docs related to this please it.


    • kapil p on March 14, 2014 at 12:38 pm
    • Reply

    Working fine …Its useful.

    • Tyler O on January 30, 2015 at 11:40 pm
    • Reply

    This helped me. Thank you.

    • sreenath on April 29, 2015 at 4:42 pm
    • Reply

    Nice code it is working fine

    • bhanu prakash on August 3, 2015 at 8:56 pm
    • Reply

    great blog ,i recently came across this ,thanks 🙂

    • Piyali Chowdhury on June 20, 2018 at 8:19 pm
    • Reply

    I have a question. As you have mentioned “We can add 5 different types of message in Visualforce Page”, is that a limitation or you have just added 5 messages for this particular example.

    • Teja on January 7, 2020 at 11:58 am
    • Reply

    Iam getting the error: Please resolve this…

    Visualforce Error

    System.DmlException: Insert failed. First exception on row 0 with id a082v00002r7JQ8AAM; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
    Error is in expression ‘{!save}’ in component in page visualforcepage:

    • Chris on May 2, 2020 at 1:34 am
    • Reply

    Hi there,

    How do we test these messages in an test class? I’m having trouble with the if conditions & apexpage.messages code coverage. Any help would be greatly appreciated!


    • Neha on July 30, 2020 at 6:43 pm
    • Reply

    Hi ,

    I need your help .Can you please tell me how we will take this Apexpage.message error in file and then i have to download this file.
    i have take a list and then iterating this error but when i am adding this error into the list type variable then its give error (add string() not a write method signature).

    please help me on this. thanks in advance

    • sreenivas on December 15, 2021 at 7:05 pm
    • Reply


    • Omprasad on September 13, 2022 at 12:36 pm
    • Reply

    These page messages are not showing in lightning

Leave a Reply

Your email address will not be published.