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
prasad1.3969574005596787E12prasad1.3969574005596787E12 

How to display message in apex

Hi All,

As I am trying to Display message in the apex code:
for if condition is true

ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,cw.cat.name));

Punam AgrawallaPunam Agrawalla
Hi,

You can use like this:

ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'ERROR MESSAGE HERE'));

Thanks,
SFDC_DevloperSFDC_Devloper
Hi Prasad,


  Vf Page:
<apex:page standardController="Account" extensions="errorMessgaes">
    <apex:form >
        <apex:pageblock >
            <apex:pageMessages id="shwmsg"></apex:pageMessages>
                <apex:panelgrid columns="2">
                    Account Name <apex:inputtext value="{!accountname}"/>
                    Account Number <apex:inputtext value="{!accountnumber}"/>
                        <apex:commandButton value="Update" action="{!updateAccounts}" style="width:70px" reRender="shwmsg"/>
                </apex:panelgrid>
        </apex:pageblock>
    </apex:form>
</apex:page>

Controller:

public with sharing class errorMessgaes {

public string accountName{get; set;}
public string accountNumber{get; set;}

public errorMessgaesCLs(ApexPages.StandardController controller) {
   }
  

public void updateAccounts()
{
  if(accountName==''||accountName==null)
  ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please Enter Account Name'));
 
  if(accountNumber=='' ||accountNumber==null)
  ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Please Enter Account Number'));
}

}

OutPut:

User-added image


Thanks,
Rockzz

SFDC_DevloperSFDC_Devloper
Hi Prasad,

      If this solves your problem, kindly mark it as the best answer.


Thanks,
Rockzz