Print this page

addError() when used in apex classes escapes HTML

Knowledge Article Number 000213462
Description Use case:

Suppose you have a VF page like,
<apex:page controller="TestErrorController">
    <apex:messages />
    <apex:form >
      <apex:outputLabel value="Test String"/><apex:inputField value="{!test.Name}" id="test"/>
      <br/><apex:message for="test"/>

Controller like,
public class TestErrorController{
    public Account test{get;set;}
    public TestErrorController(){
        test = new Account();
        test.Name.addError('im an html error with a <a href="">link</a>', FALSE);

Resolution addError() escapes HTML when displaying error even if escaped optional var = FALSE. 

addError() displays the HTML characters correctly when used in apex triggers but when used in apex class it escapes the HTML characters and displays as not text.
In the above case the error message would be like 
Error: im an html error with a <a href="">link</a>

This issue was fixed in apex triggers in our Winter 15 release where as the issue still persists when used in apex classes. 
Here is the known issue which was fixed,

There is a plan of fixing this in apex classes in our Summer '15 release. Until then the suggested workaround is to use addError() method in triggers to avoid escaping of HTML characters.

promote demote