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
Linda 98Linda 98 

URGENT!!! Display multiple error messages on VF page.

I am having 6 fields on a VF page.All those fields are required and cant be left blank if name field is filled.

I am using <apex:pagemessages/ > in my Vf page and below is my code.
Problem which i am not able to solve is,when i give name value and left other fields blank,i am getting only one error message.
Like Phone is required.
When i enter phone and save it,i get billing street is required.

But i want to display all the error messages at once.(fields which are nit filled ..error messages related to those fields)

Please help.
Public pagereference save(){

    if(accountname!= '' && accountname!= null){
    
        account.name=accountname;
        if(phonenumber!= '' && phonenumber!=null)
        {
            account.phone=phonenumber;        
        }
        else{
        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Phone is required'));
        return null;
        }
        
        if(Billingstreet != '' && Billingstreet !=null)
        {
            account.billingstreet=Billingstreet;        
        }
        else{
        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Billing Street is required'));
        return null;
        }
        
        if(Billingcity != '' && Billingcity !=null)
        {
            account.billingcity=billingcity;
        }
        else{
        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Billing City is required'));
        return null;
        }
        
        if(billingzipcode != '' && billingzipcode !=null)
        {
            account.billingpostalcode=billingzipcode;
        }
        else{
        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Billing Zip is required'));
        return null;        
        }
        
        upsert account;
        
        contact c=new contact();
        c.accountid=account.id;
        
        if(contactname!= '' && contactname !=null){
            c.lastName=contactname;
        }else{

        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Contact Name is required'));
        return null;
        }
        if(contactemail!= '' && contactemail !=null){
            c.Email=contactemail;
        }else{
        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Contact Email is required'));
         return null;
        }
       
        insert c;
        
    }
    
    return new PageReference('/'+pr.Id+'/e?retURL=%2F'+pr.Id);   
    }

 
Linda 98Linda 98
Below is my VF page:
<apex:page standardController="object__c" extensions="controller1" docType="html-5.0" >
   
<apex:form >
<apex:pageMessages id="showmsg" />
        <apex:pageBlock">
            <apex:pageBlockbuttons >
                <apex:commandButton value="Save" action="{!save}" reRender="showmsg"/>
                <apex:commandButton value="Cancel" action="{!Cancel}"/>
            </apex:pageBlockbuttons>

<apex:pageBlockSection title="1" columns="2">
            
           
           <apex:pageBlockSectionItem >
               <apex:outputLabel value="Account Name"/>
               <apex:outputPanel layout="block" styleClass="requiredInput">
               <apex:outputPanel layout="block" styleClass="requiredBlock"/>
               <apex:inputtext label="New Vendor Name" value="{!accountname}"/>
               </apex:outputPanel>
            </apex:pageBlockSectionItem>
            
            
           <apex:pageBlockSectionItem >
               <apex:outputLabel value="Phone"/>
               <apex:outputPanel layout="block" styleClass="requiredInput">
               <apex:outputPanel layout="block" styleClass="requiredBlock"/>
               <apex:inputtext label="Phone" value="{!phonenumber}"/>
               </apex:outputPanel>
            </apex:pageBlockSectionItem>
            
           <apex:pageBlockSectionItem >
               <apex:outputLabel value="Billing Street"/>
               <apex:outputPanel layout="block" styleClass="requiredInput">
               <apex:outputPanel layout="block" styleClass="requiredBlock"/>
               <apex:inputtext label="Billing Street" value="{!Billingstreet}"/>
               </apex:outputPanel>
            </apex:pageBlockSectionItem>
            
           <apex:pageBlockSectionItem >
               <apex:outputLabel value="Billing City"/>
               <apex:outputPanel layout="block" styleClass="requiredInput">
               <apex:outputPanel layout="block" styleClass="requiredBlock"/>
               <apex:inputtext label="Billing City" value="{!Billingcity}"/>
               </apex:outputPanel>
            </apex:pageBlockSectionItem>
            
            
           <apex:pageBlockSectionItem >
               <apex:outputLabel value="Billing Zip/Postal Code"/>
               <apex:outputPanel layout="block" styleClass="requiredInput">
               <apex:outputPanel layout="block" styleClass="requiredBlock"/>
               <apex:inputtext label="Billing Zip/Postal Code" value="{!Billingzipcode}"/>
               </apex:outputPanel>
            </apex:pageBlockSectionItem>
           
            <apex:pageBlockSectionItem >
               <apex:outputLabel value="Contact Name"/>
               <apex:outputPanel layout="block" styleClass="requiredInput">
               <apex:outputPanel layout="block" styleClass="requiredBlock"/>
               <apex:inputtext label="Contact Name" value="{!contactname}" />
               </apex:outputPanel>
            </apex:pageBlockSectionItem>
            
            <apex:pageBlockSectionItem >
               <apex:outputLabel value="Contact Email"/>
               <apex:outputPanel layout="block" styleClass="requiredInput">
               <apex:outputPanel layout="block" styleClass="requiredBlock"/>
               <apex:inputtext label="Contact Email" value="{!contactemail}"/>
               </apex:outputPanel>
            </apex:pageBlockSectionItem>


    </apex:pageblockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

 
PavanKPavanK
Hi Linda,

Can you remove 'Return null' from every else loop.

And add below 'If' condition before 'insert c;'
 
if((phonenumber!= '' && phonenumber!=null) || (Billingstreet != '' && Billingstreet !=null)|| ..............|| (contactemail!= '' && contactemail !=null))
{
return null;
}
 insert c;

Please let me know, if its still not working.

Please mark this answer as best if reply helpeed anyway.
 
PavanKPavanK
Sample code is

Public pagereference save(){

    if(accountname!= '' && accountname!= null){
    
        account.name=accountname;
        if(phonenumber!= '' && phonenumber!=null)
        {
            account.phone=phonenumber;        
        }
        else{
        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Phone is required'));
        }
        
        if(Billingstreet != '' && Billingstreet !=null)
        {
            account.billingstreet=Billingstreet;        
        }
        else{
        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Billing Street is required'));
        }
        
        if(Billingcity != '' && Billingcity !=null)
        {
            account.billingcity=billingcity;
        }
        else{
        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Billing City is required'));
        }
        
        if(billingzipcode != '' && billingzipcode !=null)
        {
            account.billingpostalcode=billingzipcode;
        }
        else{
        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Billing Zip is required'));
        }
        if((phonenumber!= '' && phonenumber!=null)|| (Billingstreet != '' && Billingstreet !=null)......|| (billingzipcode != '' && billingzipcode !=null))
        {
        	return null;
        }
        upsert account;
        
        contact c=new contact();
        c.accountid=account.id;
        
        if(contactname!= '' && contactname !=null){
            c.lastName=contactname;
        }else{

        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Contact Name is required'));
        }
        if(contactemail!= '' && contactemail !=null){
            c.Email=contactemail;
        }else{
        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Contact Email is required'));
        }
       if((contactname!= '' && contactname !=null)|| (contactemail!= '' && contactemail !=null))
       return null;
       
        insert c;
        
    }
    
    return new PageReference('/'+pr.Id+'/e?retURL=%2F'+pr.Id);   
    }

 
Linda 98Linda 98
But i want to display individual error messages.Like..If phone is missing,then 'phone is required'
If the value is given then check other conditions.

 
PavanKPavanK
Another good method is remove all 'ApexPages.addMessage' related logic from class and add 'Required' tab in visualforce tag. for example

 <apex:inputtext label="Contact Email" value="{!contactemail}" required="true"/>
Linda 98Linda 98
Thanks for that..I cant use required=true as i am having other sections in the VF page.
I just made chnages to code as you suggested.However,all other fields displays error message except contact name.
When i fill account name,and leave contact name(which will be required to create a contact) i am getting standard error( Rather than error on VF page).

how can i overcome that?
Hargobind_SinghHargobind_Singh
Hi Linda, The example from PavanK should show individual messages, all messages are added to pageMessages and are shown in separate lines. 
PavanKPavanK
Can you post revised code?
Linda 98Linda 98
Public pagereference save(){
    

    if(accountname!= '' && accountname!= null){
    
        account.name=accountname;
        if(phonenumber!= '' && phonenumber!=null)
        {
            account.phone=phonenumber;        
        }
        else{
        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Phone is required'));
        }
        
        if(Billingstreet != '' && Billingstreet !=null)
        {
            account.billingstreet=Billingstreet;        
        }
        else{
        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Billing Street is required'));
        }
        
        if(Billingcity != '' && Billingcity !=null)
        {
            account.billingcity=billingcity;
        }
        else{
        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Billing City is required'));
        }
        
        if(billingzipcode != '' && billingzipcode !=null)
        {
            account.billingpostalcode=billingzipcode;
        }
        else{
        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Billing Zip is required'));
        }
                
        if((phonenumber!= '' && phonenumber!=null) || (Billingstreet != '' && Billingstreet !=null) ||
           (billingcity != '' && billingcity !=null) || (Billingzipcode != '' && billingzipcode !=null))
          {
             return null;
          }
          
        upsert account;
        
        contact c=new contact();
        c.accountid=account.id;
        
        if(contactname!= '' && contactname !=null){
        c.lastName=contactname;
        }else{
        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Contact Name is required'));
        }
        if(contactemail!= '' && contactemail !=null){
            c.Email=contactemail;
        }else{
        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Contact Email is required'));
        }
        
        if((contactname!= '' && contactname !=null)|| (contactemail!= '' && contactemail !=null)){
        return null;
        }
        insert c;
       }
    
      return new PageReference('/'+pr.Id+'/e?retURL=%2F'+pr.Id);   
    }

 
PavanKPavanK
Public pagereference save(){
    

    if(accountname!= '' && accountname!= null){
    
        account.name=accountname;
        if(phonenumber!= '' && phonenumber!=null)
        {
            account.phone=phonenumber;        
        }
        else{
        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Phone is required'));
        }
        
        if(Billingstreet != '' && Billingstreet !=null)
        {
            account.billingstreet=Billingstreet;        
        }
        else{
        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Billing Street is required'));
        }
        
        if(Billingcity != '' && Billingcity !=null)
        {
            account.billingcity=billingcity;
        }
        else{
        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Billing City is required'));
        }
        
        if(billingzipcode != '' && billingzipcode !=null)
        {
            account.billingpostalcode=billingzipcode;
        }
        else{
        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Billing Zip is required'));
        }
                
       
        
        contact c=new contact();
        
        
        if(contactname!= '' && contactname !=null){
        c.lastName=contactname;
        }else{
        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Contact Name is required'));
        }
        if(contactemail!= '' && contactemail !=null){
            c.Email=contactemail;
        }else{
        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Contact Email is required'));
        }
        
        if((phonenumber!= '' && phonenumber!=null) || (Billingstreet != '' && Billingstreet !=null) ||
           (billingcity != '' && billingcity !=null) || (Billingzipcode != '' && billingzipcode !=null) || (contactname!= '' && contactname !=null)|| (contactemail!= '' && contactemail !=null))
           {
        	return null;
        }
        
        
        upsert account;
        c.accountid=account.id;
        insert c;
       }
    
      return new PageReference('/'+pr.Id+'/e?retURL=%2F'+pr.Id);   
    }

 
Linda 98Linda 98
I still get the same error Pavank.When i just fill account name and click on save,i get Apex developer script error for contact lastname.
If i fill lastname and click on save i get error on VF page saying i am missing other fields.
Why is it not working with lastname.(i understand lastname is required field to create a contact but still i need that to display error message on VF page).
 
Linda 98Linda 98
Also i am facing a weird problem.
When i fill all the fields and click on save,NOTHING IS happening.JUst screen sits there.No account or contact is being created .
No redirect to next page.

God!!I messed up something which i am not sure what is that.

 
sandeep reddy 37sandeep reddy 37
use exception classs like 
try{
}catch(exception e){
 ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, e.getmessage()));
}
Linda 98Linda 98
No it didnt help me Sandeep reddy 37

 
sandeep reddy 37sandeep reddy 37
apex class it will works

public class displayserrors  {
    public string accountname{set;get;}
    public string phonenumber{set;get;}
    public string Billingstreet{set;get;}
    public string  billingcity {set;get;}
    public string  billingzipcode{set;get;}
    public string  contactname{set;get;}
    public string    contactemail{set;get;}
    public displayserrors(apexpages.standardcontroller count){
        account a=(account)count.getrecord();
    }
    Public void check(){
        account a=new account();
       a.Name=accountname;
         a.Phone=phonenumber;
        a.BillingState=Billingstreet;
        a.BillingCity=billingcity;
        a.BillingPostalCode=billingzipcode;
        try{
            if(accountname.isNumeric()){
              ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Phone is required'));
                
            }else{
            
                insert a;
            }
            }catch(exception e){
                 apexpages.addMessages(e);
            }
         
              //apexpages.adderror((new apexpages.message(ApexPages.Severity.ERROR,String.valueOf(new testexception('invalid account number')));
       // ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'account  is required'));
        
        
         if(phonenumber== '' || phonenumber==null)
        {
        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Phone is required'));
  
        }
        
        if(Billingstreet == ''|| Billingstreet.equals(null))
        {
        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Billing Street is required'));
 
        }
        
        if(Billingcity == '' || Billingcity.equals(null))
       {
        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Billing City is required'));
     
        }
        
        if(billingzipcode == '' || billingzipcode.equals(null))
       {
        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Billing Zip is required'));
         
        }
        contact c=new contact();
        c.accountid=a.id;
        c.LastName=contactname;
        c.Email=contactemail;
        try{
        if(contactname.isNumeric()){
        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Contact Name is must be in string'));
        }
            else{
                insert c;
            }
        }catch(exception e){
            ApexPages.addMessages(e); 
        }
        
        if(contactemail== '' ||contactemail ==null){
             ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Contact Email is required'));
        }
       
      
        
    }
    
   
    }



vf page 
<apex:page standardController="account" extensions="displayserrors" >
   
<apex:form >
<apex:pageMessages />
        <apex:pageBlock >
       
                <apex:pageBlockSection title="account information">
                    <apex:outputLabel value="name"/>
                <apex:inputText value="{!accountname}" />
                    
                    <apex:outputLabel value="phone no"/>
                <apex:inputText value="{!phonenumber}" />
                    

                    <apex:outputLabel value="street"/>
                <apex:inputText value="{!Billingstreet}" />
                    
                    <apex:outputLabel value="city"/>
                <apex:inputText value="{!billingcity}" />
                    
                    <apex:outputLabel value="zipcode"/>
                <apex:inputText value="{!billingzipcode}" />
                    
                </apex:pageBlockSection>
         
           </apex:pageBlock>
    <apex:pageBlock >
     <apex:pageBlockSection columns="2" title="contact information" >
         <apex:outputLabel value="name"/>
                <apex:inputText value="{!contactname}" />
                    
         <apex:outputLabel value="mail"/>
                <apex:inputText value="{!contactemail}" />
                  
            </apex:pageBlockSection>
    </apex:pageBlock>
    <apex:commandButton value="check" action="{!check}" />
</apex:form>
</apex:page>
i hope it is help full
Linda 98Linda 98
Thanks sandeep.But Can you please look at below question where i explained what my exact issue is.

https://developer.salesforce.com/forums?state=id#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Developer_Forums&criteria=OPENQUESTIONS&id=906F0000000kJybIAE
Please help!!