Adding Error Message in the Visualforce page through Apex controller

Syntax : <apex:pageMessages ></apex:pageMessages>

Apex Controller:

public class ErrorMsgController {
public void DisplayError(){
Apexpages.addMessage( new ApexPages.Message (ApexPages.Severity.ERROR, ‘Required fields are missing. ‘)); //FATAL, WARNING, INFO, CONFIRM

VF Page:

<apex:page sidebar=”false” showHeader=”false” controller=”ErrorMsgController”>
<apex:form >
<apex:pageMessages ></apex:pageMessages>
<apex:commandButton value=”Click” action=”{!DisplayError}”/>


Written by 

Salesforce MVP | Lightning Champion | 17x Salesforce Certified | Application Architect | 7x Trailhead Ranger | Techforce Services | Australia

Leave a Reply

Your email address will not be published. Required fields are marked *