- Salesforce Development Posts of a Fashion by Matt Lacey

Back to Posts

Escaping the Page Messages - Run Away! Inconsistency Alert!

Update 26/02/2014: As of the Spring '14 this issue would appear to be fixed as per 1645's comment below.

Original Post

One of my main problems with the platform is the inconsistencies I come across while working with it, they tend to be rare but they also tend to come out of left field and suck up a few hours when you're trying to do something relatively trivial. Recently I wanted to put a link into a message displayed on a Visualforce page using our friend, <apex:pageMessages/>*. To achieve this is easy enough, you can simply include HTML markup in the message you add in code, and then set the escape attribute to false on the <apex:pageMessages/> tag:

// Code:
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'This is an info message with a <a href="">link to google</a>.'));

<!-- Page: -->
<apex:pagemessages escape="false">

I  soon noticed that sometimes the link appeared correctly, and other times the HTML was escaped and so you'd just see the markup in the message rather than a link—it took me a short while to notice a difference between the two scenarios, they were both using the same action and the same line of code to add the message to the page so it didn't appear to make any sense. After some head scratching I finally noticed that in the case where the link did work, I had one message with Severity.ERROR and then one which has Severity.INFO and the link, as per the code snippet above; in the case where the link wasn't working I had two Severity.INFO messages.

My conclusion? There's a bug within Visualforce whereby the use of two messages of the same type will cause the escape attribute to be overridden, meaning the HTML is always escaped. I could be wrong, but I wrote a quick page to test this idea and it appears to confirm my thinking, the screenshots below demonstrate the issue perfectly:

At this point I don't have a solution to the issue, but hopefully if you're encountering this problem it'll help provide some insight into what's going on.

*On a side note, if you use <apex:messages/> you'll find your messages are displayed without formatting, if you use <apex:pageMessages/> then they use the default Salesforce styling and look much nicer.

Related Posts