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
Gheorghe Sima 11Gheorghe Sima 11 

Error message on visualforce page.

Hi,
I make a visualforce page where i create a new Contact. How can I display  an error message if I can't insert the record in the Database? I want to display the message like in the picture below.
User-added image
Jitendra RawatJitendra Rawat
Hi Gheroghe,

You have to handle the exception by using try-catch statment. In catch block you can write the code to display message on the page.
You have to write bellow code.
try{
     //write your code logic for insert the records.
}catch(Exception ex) {
     ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,ex.getmessage());
     ApexPages.addMessage(myMsg);
}

And on page place this tag where you want to show the message.
<apex:pageMessages />

Please select this as best answer if you think this is helping you.

Thanks
Jitendra