How to Show error messages in Visualforce

Visualforce is Salesforce’s MVC style pages. In this blog post, we’ll cover how to display error messages in a Visualforce page and how to add error messages.

Showing error messages is a really import part of the user experience. A good error message should tell the user exactly what the problem is and how to potentially resolve it. For example, a missing Account Name error should say something like:

“The Account Name is required, please enter one.”

Instead of:

“Account Name is null.”

To display messages from the controller or an extension, we need to use the apex:pageMessage or apex:pageMessages visualforce tage. Like so:

An error message being displayed would look something like this in a Visualforce page.

We can add five different types of error messages to a Visualforce page ranging from Info to Fatal. It’s a really good practice to show custom error messages instead of allowing the page to just error when there’s a problem.

We can add add a custom error to our Visualforce page by using “ApexPages.addMessage()” which accepts a ApexPages.message object. To instantiate a ApexPages.message object we need to pass in an severity ENUM and a message.

In this blog article, we went over why and how to show error messages in Visualforce. We also briefly covered how to show custom errors in Apex too!

Originally published at Brian Cline.

--

--

Dad, Husband, #Christian Enterprise Architect, #Salesforce, #Automation #JavaScript

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Brian R. Cline

Dad, Husband, #Christian Enterprise Architect, #Salesforce, #Automation #JavaScript