On all of my Visualforce Pages that involve user input and DML actions I include the handy <apex:pageMessages/> tag. This is great when I'm creating my own custom error messages, and in some instances if there is a validation rule failure it has it's own page message automatically created. However, this seems to occur rather erratically. Are there any rules out there for when a validation rule failure will automatically generate it's own page message and when I need to make sure to create them manually?

Please note this is not a question about specific piece of code that isn't displaying a message, but rather a question about when Salesforce automatically creates a page message for you that will be shown in the <apex:pageMessages/> tag and when Salesforce will NOT automatically create a page message for you that will be shown.

For the purpose of this question please assume that I'm either doing a full page refresh or am rerendering the page messages component.

  • Apex:pageMessages will display any unhandled exceptions or any handled exceptions which are added as ApexPages.Message. If you handle an exception but don't add it as a ApexPages.Message it will not be displayed. – techtrekker Jul 22 '13 at 22:22
  • Hmm, that hasn't been my experience. I often have two try catch blocks, one for DMLException which I do nothing (since adding anything duplicate the validation failure), and another for a general Exception, which I do have to handle. – Ralph Callaway Jul 22 '13 at 22:45
  • As a general rule it seems follow another difference in behavior. If the dml exception doesn't generate a page message not catching it takes you to a white screen of death, if the DML exception does generate a page message, not catching it shows the message in page messages if present (or nothing if it's not there). – Ralph Callaway Jul 22 '13 at 22:50

You may need to include showDetails="{!showDetails}" on apex:pageMessages.

I'm probably like you - errors can be raised by validation rules, apex class logic using .addError() and general exceptions caught by try - catch.

I tend to always use a Utility method to emit an ApexPages.Message that exploits: ApexPages.Message myMsg = new ApexPages.Message(ApexPages.severity, summary, detail);

The utility method will 'friendlify' the exception message, stripping out unnecessary SFDC verbosity from raised validation errors before outputting the ApexPage.Message

I also use a separate controller method getShowDetails() that decides whether to 'showDetails'. The method checks to see if the text in the detail portion is the same as the text in the summary portion of ApexPages.Message. This works around some VF issues when the summary and detail are essentially the same.

  • That's a pretty nice technique checking the existing page messages to avoid duplication of existing messages, never would have thought of that. That said, as you've outlined there are a number of ways errors can be raised, in particular validation rules, but why do some show up automatically in the pageMessages and some don't? – Ralph Callaway Jul 22 '13 at 23:27
  • @Ralph - good question, I think I kept trying to workaround what I observed and ended up writing a flexible Utility method that dealt with all circumstances as I needed such method anyway to friendlify SFDC validation exception messages – cropredy Jul 24 '13 at 1:37
  • Is that posted anywhere? Would love to take a peek at that – Ralph Callaway Jul 24 '13 at 14:54
  • any chance you could share a gist with your utility class? – Ralph Callaway Jul 31 '13 at 16:32

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.