Error Message Handling and Custom Check on Visual Force Pages : A easy to reuse approach

by - 9:35 PM

Most of time, we custom write the error code on VisualForce Pages to validate user input, there are bunch of ways you can write the error code and display on visualforce pages. You can read about message class here.

I simply developed a helper class for repetitive usage and to streamline handling of error messages. My utility class is primarily covered in two parts, the first part checks for values in fields that are 'not null' meaning if custom picklist has nothing selected or else if textbox is empty, we check for null values and throw error.

I simply designed a method here that converts an object to string and then checks if the string is null or not
Here is self-explaintory code-snippet


Now lets jump back to later part, which is to streamline the error codes and to display them on visual force pages in simple fashion. Let us design five methods to handle the type of warnings.

Having designed Error Handler class, lets use both the classes to handle error code on any visualforce pages, here is how you can call by creating another class, let name it moduleHelper (basically this class wrap up all  checks in one method).

Why do we need this class, basically this class can be reused make all the checks we designed at any point by wrapping all logic in one method. So simply,we can instantiate this class at any point of time on any visualforce controller to custom display the error code on any visualforce page, this save lot of time of repeating error code at multiple places

Below is ModuleHelper Class


Awesome, so we have designed a class that has a wrapper logic to check the field for null and handle error code. How do we use this though ?

Clearly lets move on, and use this wrapper logic on a landing page. To explain it more, here I have a landing page that deal with opportunity and this page has a  picklist, this is controller extension, the picklist on this custom page do not accept null value, and if null value is found we show error message


You May Also Like

0 comments