+ Start a Discussion
jucuzoglujucuzoglu 

How do I properly catch and display error messages in VisualForce

I have a visualforce pag where I override the Save command.

 

Since I am overriding the Save command. In the script I utilize the Update DML statement, however I have validation that used to work fine and display the error in my VisualForce page when I used the standard controller.

 

Now I believe I need to re-validate in my code, catch the same error condition and throw the error message to the visualforce page so it shows up where the validation error message normally would.

 

How do I do this?

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Chamil MadusankaChamil Madusanka

You code must be followed following structure

 

try
{


}
catch(Exception e)
{
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'YOUR MESSAGE HERE'));
}

 In VF page you have to use <apex:pageMessages> tag where you want to display your error message

 

For more detail refer :

http://www.salesforce.com/us/developer/docs/pages/Content/apex_methods_system_apexpages.htm?SearchType=Stem

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_pages_message.htm

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

 

All Answers

Chamil MadusankaChamil Madusanka

You code must be followed following structure

 

try
{


}
catch(Exception e)
{
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'YOUR MESSAGE HERE'));
}

 In VF page you have to use <apex:pageMessages> tag where you want to display your error message

 

For more detail refer :

http://www.salesforce.com/us/developer/docs/pages/Content/apex_methods_system_apexpages.htm?SearchType=Stem

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_pages_message.htm

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

 

This was selected as the best answer
PrashanthNeoPrashanthNeo

This answer helped alot thankyou but  how can we display same message in two lines?
I have a long string of message to be displayed. So need to display in two different lines. and also how to display the character backslash( \ ) in a message?

 

Thanks
Prashanth