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
miko Leemiko Lee 

How to display the Apex Pages Error Message

Hi guys,

Why it keep on showing me this message (Map key error), instance I want to display the Error Message that I type myself. And, when i view in Log of Developer Console, it got show that I got hit my Error Message but why it still cant show in Visualforce Page???Map Key Error

 

NagendraNagendra (Salesforce Developers) 
Hi Miko,

Please check with below example:

Visual Force Page:
<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="{!acc.name}"/>
           Account Number: <apex:inputText value="{!acc.AccountNumber}"/>
           Account Phone: <apex:inputText value="{!acc.phone}"/>
           Account Site: <apex:inputText value="{!acc.site}"/>
           Account Industry: <apex:inputText value="{!acc.industry}"/>
           <apex:commandButton value="Update" action="{!save}" style="width:90px" rerender="showmsg"/>
         </apex:panelGrid>
    </apex:pageblock>
 </apex:form>
</apex:page>
Apex Controller:
public with sharing class ErrorMessageInVfController {
    public Account acc{get;set;}
    public ErrorMessageInVfController(ApexPages.StandardController controller) {
        acc = new Account();
    }
 
    public void save(){
      if(acc.name == '' || acc.name == 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(acc.phone == '' || acc.phone == null)
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter Account phone'));
 
      if(acc.site == '' || acc.site == 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'));
 
    }
}
Hope this helps.

Please mark this as solved if it's resolved.

Best Regards,
Nagendra.

 
Andries.NeyensAndries.Neyens
Dont know when and how this error is occuring, but you can put a try catch around the code that is causing it and let it bubble to the surface with the ApexMessage:

try{

} catch(Exception e){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING, e.getMessage() ));
}

and in your visual force:

<apex:pageMessages id="showmsg"></apex:pageMessages>