2

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 ?

5

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 Apr 4 '17 at 7: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.