15

I am programmatically building a HTML string in Apex and then saving it to a rich text field. The problem is that SalesForce is doing some HTML encoding on the string before it gets persisted which is making the string longer and causing a STRING_TOO_LONG error. What is the best way to do HTML encoding and decoding in Apex? See below for my attempt:

public String htmlEncode (String s) {  

    Map<String, String> htmlEncodingMap = new Map<String, String>();
    htmlEncodingMap.put('<', '&lt;');
    htmlEncodingMap.put('>', '&gt;');
    htmlEncodingMap.put('"', '&amp;quot;');
    htmlEncodingMap.put('&', '&amp;');

    for (String token : htmlEncodingMap.keySet()) {
        s = s.replace(token, htmlEncodingMap.get(token));
    }

    return s;
}

public String htmlDecode (String s) {  

    Map<String, String> htmlDecodingMap = new Map<String, String>();
    htmlDecodingMap.put('&lt;', '<');
    htmlDecodingMap.put('&gt;', '>');
    htmlDecodingMap.put('&amp;quot;', '"');
    htmlDecodingMap.put('&amp;', '&');

    for (String token : htmlDecodingMap.keySet()) {
        s = s.replace(token, htmlDecodingMap.get(token));
    }
    return s;
}
7
  • (1) it's &quot; not &amp;quot;, and (2) are you trying to encode the text from a rich text input? If so, you shouldn't be attempting to escape it yourself.
    – sfdcfox
    Dec 12, 2013 at 22:35
  • Is the character length on a rich text field (eg 32,000 characters) the raw string or is it the length after SF does the HTML encoding it?
    – BarCotter
    Dec 12, 2013 at 22:40
  • As far as I'm aware, it's the limitation after any encoding is applied, but I don't remember where I saw that in the documentation.
    – sfdcfox
    Dec 12, 2013 at 22:44
  • I think thats what this question is asking then. They want to encode the text to verify (or truncate) its not over the character limit
    – BarCotter
    Dec 12, 2013 at 22:52
  • Yes that is correct. I am looking to encode the text to verify its not over the character limit, so i can truncate the string to avoid the STRING_TOO_LONG error if the encoded text is too long.
    – LMcDonald
    Dec 12, 2013 at 23:30

1 Answer 1

19

The Apex String class has HTML escape and unescape methods on it, escapteHtml4 and unescapeHtml4. Here are some examples from these topics in the documentation...

String s1 = 
   '"<Black&White>"';
String s2 = 
   s1.escapeHtml4();
System.debug(s2);
// Output:
// &quot;&lt;Black&amp;
// White&gt;&quot;

String s1 = 
   '&quot;&lt;Black&amp;White&gt;&quot;';
String s2 = 
   s1.unescapeHtml4();
System.assertEquals(
   '"<Black&White>"',
   s2);

2022.11.18: Updated broken links to SF documentation

8
  • 1
    I looked at this yesterday, and I forgot that the functions were in String. Why couldn't they have put it with, for example, urlencode? Or put it in both places, just for kicks (e.g. System.Now() and DateTime.Now()).
    – sfdcfox
    Dec 13, 2013 at 20:35
  • LOL yeah, I've never seen so many methods on a String class tbh! :) Dec 14, 2013 at 21:30
  • Whats the difference between escapeHtml3 and escapeHtml4? The docs don't show any difference
    – BarCotter
    Dec 14, 2013 at 22:23
  • Anything prefixed with a & (ampersand) in HTML is known as an "entity" for a special character, HTML 3.0 and HTML 4.0 versions had different supported entities. f2.org/links/webauth/entities.html#html3 Dec 15, 2013 at 21:29
  • 2
    Andrew to the rescue again... :) Apr 9, 2015 at 21:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .