In a record I create a new Note (ContentNote object) with the text contained into a custom field. So for that I have to parse the text area of the custom field (string) into a blob (the type of the Content field of the ContentNote object).

Here is what I've done (simplified):

    ContentNote cn = new ContentNote();
    String body = customeField__c;
    cn.Content = Blob.valueOf(body.escapeHTML4());
    insert cn;

So this code works fine, I add the escapeHTML4() because I need the escape the characters. My problem is that the line break are not re-rendered in the ContentNote, when I put a line break in the text area, in the ContentNote I got nothing, and event no space between words.

How can I display the line break into the blob ?


1 Answer 1


First of all, thanks for a nice question.

I have checked that, Content Note puts line break into <p></p> instead of <br/>.

For the sake of experiment, I have created a Case description like this.

case description

Execute from Developer Console

String strdesc = [SELECT Id, Description From Case WHERE Id = '50090000001N25E' LIMIT 1].Description ;
String returnStr = strdesc;
if (strdesc.contains('\n'))
    returnStr = '<p>' + strdesc.replaceAll('\n','</p><p>') + '</p>';
System.debug('strdesc=' + returnStr);
ContentNote cn = new ContentNote();
cn.title = 'test16';
String body = returnStr;
cn.Content = Blob.valueOf(body);
insert cn;
System.debug('createdRecord=' + cn.id);

Debug Log

Debug log

Content Note

Content Note

Following things to be noted:

  1. To create line break, find \n characters and replace with <\p><p>.
  2. You need to put extra <p> to both start and end position of the string.
  3. DO NOT use escapeHTML3() or escapeHTML4(), as it is already formatted as HTML, so it does not need to be escaped.

You can prepare a reusable method for this:

public String prepareContent(String input)
    String returnStr = input;
    if (input.contains('\n'))
        returnStr = '<p>' + input.replaceAll('\n','</p><p>') + '</p>';
    return returnStr;
  • thanks for the very complete answer! Is there a risk using ` escapeHTML4()` ?
    – dou
    Commented Apr 4, 2017 at 7:32
  • Points 1 and 2 are a good solution, however escapeHTML4() MUST be used (prior to step 1 and 2). Emojis (e.g. <3) will break everything coming after if they are not escaped Commented Nov 27, 2023 at 17:52

You must log in to answer this question.

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