9

I'm trying to crystallize out some 'best practises' for Visualforce Page exception handling.

I think, at a minimum, a page should look something like this:

<apex:page controller="PageController">
    <apex:pageMessages />
    <apex:pageBlock rendered="{!NOT(HasFatalMessages)}" />
</apex:page>
  • always present is apex:pageMessages
  • when a fundamental misconfiguration is detected, the UI is hidden to prevent data entry

Then the controller class might look something like this:

public with sharing class PageController {

    public Boolean getHasFatalMessages() {
        return ApexPages.hasMessages(ApexPages.Severity.FATAL);
    }

    public PageController() {
        try {
            //check org configuration required
            //for this page (for example, custom
            //settings, field set members, etc)
        } catch (SomeDomainLayerException e) {
            ApexPages.addMessages(e);
        }
    }
}

I can't for the life of me work out how ApexPages.addMessages(e) determines severity. It always seems to be ApexPages.Severity.ERROR no matter what. How can I change that?

Or should I throw an uncaught exception to blow up the whole page, indicating bigger problems?

  • could you share your "best practices" for Visualforce page exception handling ? – apn Oct 1 '14 at 16:45
  • @apn feel free to ask as a new question - it may warrant discussion (and therefore not be a good format for SFSE) but will definitely get the attention it deserves. – bigassforce Oct 2 '14 at 17:49
7

Try catching errors as below :

catch(Exception e)
        {
            String error = e.getMessage();
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info,error));
        }

Instead of just doing ApexPages.addMessages(e);

Using this in conjunction with Apexpages.hasMessages()

if(some_condition){
                        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info,'Info not an error'));
        }
        if(ApexPages.hasMessages(ApexPages.Severity.Info)){
        // do something
        }

To set severity dynamically I would use getTypeName() and create a map/ custom setting with exception key and severity value

http://www.salesforce.com/us/developer/docs/dbcom_apex230/Content/apex_classes_exception_methods.htm

  • Cool. I figured it's a little more wordy, but one really does have to specify the severity and throw domain-specific exceptions in the domain layer. Exceptions do not carry severity. – bigassforce Sep 30 '14 at 23:11
  • As far as I know exception class only carry the exception info and not the severity level, you can make a map/ custom setting that carry severity levels based on exception that might occur. – Rao Sep 30 '14 at 23:19
  • 1
    I have a comment in some code (that uses ApexPages.addMessages(e)) that says: Using this signature method appears to both avoid duplication for errors added to fields in triggers and get declarative errors such as DUPLICATE_VALUE reported albeit in rather basic form e.g. "duplicate value found: cve__Temp__c duplicates value on record with id: 0035000001epEMl". This was in a context where DmlExceptions could be thrown. So ApexPages.addMessages probably contains some useful processing logic that you will lose if you use other patterns. – Keith C Sep 30 '14 at 23:20
  • 1
    plus be aware of this delightful undocumented behavior - salesforce.stackexchange.com/questions/50180/… – cropredy Oct 1 '14 at 0:11
3

Here are some examples of how to add messages with different severities and how they are outputted:

Confirm/Success:

ApexPages.addMessage(
     new ApexPages.Message(ApexPages.Severity.Confirm,'Sample Success Message'));

enter image description here


Warning:

ApexPages.addMessage(
    new ApexPages.Message(ApexPages.Severity.Warning,'Sample Warning Message'));

enter image description here


Informational:

ApexPages.addMessage(
    new ApexPages.Message(ApexPages.Severity.Info,'Sample Informational Message'));

enter image description here


Error:

ApexPages.addMessage(
    new ApexPages.Message(ApexPages.Severity.Error,'Sample Error Message'));

enter image description here


There's a great answer in this question that has lots more detail: Difference between the multiple messaging options in Visualforce?

-2

Severity for error message is defined when we add message in to page using our code. See code below for different severity:

if(acc.name == '' || acc.name == null)
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please enter Account name'));

      if(acc.AccountNumber == '' || acc.AccountNumber == null)
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter Account number'));

      if(acc.phone == '' || acc.phone == null)
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter Account phone'));

      if(acc.site == '' || acc.site == null)
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'Please enter Account site'));

      if(acc.industry == '' || acc.industry == null)
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Please enter Account industry'));

Error Message with different severity

For more details see below link:

http://www.sfdcpoint.com/salesforce/show-error-message-visualforce-page/

  • Ankush, how does this provide more information than the already given answer by BarCotter ? – Samuel De Rycke Nov 3 '14 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.