You need to sign in to do that
Don't have an account?
Sami 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
It showing error msg like as below
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?
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
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?
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.
Apex:
VF:
Mark it as Best Answer, if it helps.