+ Start a Discussion
Sami ShakithSami Shakith 

Only to show custom error messages in VF page

Hi, 

I want to show only custom error messages and dont want to show the exception while saving my Record using VF page

Here is my controller
public class Sample 
{
    public String nam {get;set;}
    public Decimal age {get;set;} 
    public string email{get;set;}

    public pageReference submit()
    {
    pageReference p;
        try
        {
            Account m = new Account();
            m.Name = nam;
            m.Age__c = age;
            m.Account_email__c=email;
            insert m;
            p=new pageReference('/'+m.id);
            return p;
        }
        catch(Exception e)
        {
        If(Nam==null){
              ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Name Should not be null');
              ApexPages.addMessage(msg);
         }            
         if(Age==0){
               ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Age should not be null');
               ApexPages.addMessage(msg);
         }
         if(email==null){
               ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,'email should not be null');
               ApexPages.addMessage(msg);
         }
        return null;    

        }
    }

}
Here is my VF page
<apex:page controller="Sample" sidebar="false" >
<apex:pagemessages />
<apex:form >
    <apex:pageblock >      
        <apex:pageBlocksection >
            <apex:pageblockSectionItem ><apex:outputLabel>Name</apex:outputLabel><apex:inputtext value="{!nam}" /></apex:pageblockSectionItem>          
            <apex:pageblockSectionItem ><apex:outputLabel>Email</apex:outputLabel><apex:inputtext value="{!email}" /></apex:pageblockSectionItem>
            <apex:pageblockSectionItem ><apex:outputLabel>Age</apex:outputLabel><apex:inputtext value="{!age}" /></apex:pageblockSectionItem>         
        </apex:pageBlocksection>        
        <apex:pageblockButtons >
            <apex:commandButton value="Submit" action="{!submit}" reRender=""/>
        </apex:pageblockButtons>
    </apex:pageblock>
</apex:form>
</apex:page>

It showing error msg like as below
User-added image

I want to display like
Name Should not be null
Age Should not be null
email Should not be null

How can i achieve this? can someone help me?
 
Rajiv Penagonda 12Rajiv Penagonda 12
That is default salesforce behavior you cannot change. You however can address the issue in your code, by moving around the error conditions. Refer your updated code below:
 
public class Sample {
    public String nam {get;set;}
    public Decimal age {get;set;} 
    public string email{get;set;}

    public pageReference submit() {
		pageReference p;

		if(Nam == null){
			ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Name Should not be null');
			ApexPages.addMessage(msg);
		}
		
		if(Age == 0) {
			ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Age should not be null');
			ApexPages.addMessage(msg);
		}
		
		if(email == null) {
			ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,'email should not be null');
			ApexPages.addMessage(msg);
		}
		
        try {
			Account lAccount = new Account(Name=nam, Age__c=age, Account_email__c=email);
            insert lAccount;
            return new pageReference('/' + lAccount.id);
        }
        catch(Exception e) {
			ApexPages.addMessages(e);
			return null;    
        }
    }

}

You don't really have to wait for an exception to check if the input data is correct. Always check for input data and run all validations and only then try DML. The try-catch is for exceptional conditions and not for regular negative cases.
Siddharth ManiSiddharth Mani
This is one way of achieving your requirement, but the error message will still be system generated and not customizable:
 
<apex:page controller="Sample" sidebar="false" >
<apex:pagemessages />
<apex:form >
    <apex:pageblock >      
        <apex:pageBlocksection >
            <apex:pageblockSectionItem ><apex:outputLabel >Name</apex:outputLabel><apex:inputtext value="{!nam}" required="true" label="Name"/></apex:pageblockSectionItem>          
            <apex:pageblockSectionItem ><apex:outputLabel >Email</apex:outputLabel><apex:inputtext value="{!email}" required="true" label="Email"/></apex:pageblockSectionItem>
            <apex:pageblockSectionItem ><apex:outputLabel >Age</apex:outputLabel><apex:inputtext value="{!age}" required="true" label="Age"/></apex:pageblockSectionItem>         
        </apex:pageBlocksection>        
        <apex:pageblockButtons >
            <apex:commandButton value="Submit" action="{!submit}" reRender=""/>
        </apex:pageblockButtons>
    </apex:pageblock>
</apex:form>
</apex:page>

 
mritzimritzi
Here is a sleek code for your requirement

Apex:
public class Sample 
{
	public String errorMessage{get;set;}
    public Account m{get;set;}
    
    public Sample(){
        errorMessage='';
    }
    public pageReference submit()
    {
		errorMessage='';
		if(m.Name==''){errorMessage = 'Name Should not be null';return null;}
		else if(m.Age__c==null){errorMessage = 'Age should not be null';return null;}
		else if(m.Account_email__c==''){errorMessage = 'email should not be null';return null;}
        try
        {
            insert m;
            Pagereference p=new PageReference('/'+m.id);
            return p;
        }
        catch(Exception e){   
			errorMessage = e.getMessage();
        }
    }

}


VF:
<apex:page controller="Sample" sidebar="false" >
<div style="text-align:center;color:red;">{!errorMessage}</div>
<apex:form >
    <apex:pageblock >      
        <apex:pageBlocksection >
            <apex:pageblockSectionItem ><apex:outputLabel>Name</apex:outputLabel><apex:inputtext value="{!m.Name}" /></apex:pageblockSectionItem>          
            <apex:pageblockSectionItem ><apex:outputLabel>Email</apex:outputLabel><apex:inputtext value="{!m.Account_Email__c}" /></apex:pageblockSectionItem>
            <apex:pageblockSectionItem ><apex:outputLabel>Age</apex:outputLabel><apex:inputtext value="{!m.Age__c}" /></apex:pageblockSectionItem>         
        </apex:pageBlocksection>        
        <apex:pageblockButtons >
            <apex:commandButton value="Submit" action="{!submit}" reRender=""/>
        </apex:pageblockButtons>
    </apex:pageblock>
</apex:form>
</apex:page>



Mark it as Best Answer, if it helps.