0

I am trying to convert the Long Area Text in to the RTF Field, the long text area sometime has the the <> in between the texts which is treated as a markup. I tried below to escape the characters like below

        newArticle.Article_Body_Long_T__c = result.SolutionNote;
        String rep = newArticle.Article_Body_Long_T__c.replaceAll('<', '\<');
        rep = newArticle.Article_Body_Long_T__c.replaceAll('>', '\>');
        newArticle.SOP_Article_Body__c = rep.replaceAll('\n', '<br/>');

But it throws error like Illegal string literal: Invalid string literal '\<'. Illegal character sequence \<' in string literal. I want the <> braces retained in the RTF and cannot replace them with the '' empty string. Please let me how I can I handle this

3
  • Can you add an example value for result.SolutionNote to the question that replicates the problem? Jun 18, 2020 at 23:38
  • Also, by "RTF Field", I assume you mean "Rich Text Area Field"? Jun 18, 2020 at 23:40
  • why not just replace < with &lt;?
    – cropredy
    Jun 19, 2020 at 0:17

1 Answer 1

1

The \ character is an escape character in Apex. If you wanted it to be a literal backslash in the resulting string, you'd have to double up on it: \\. However, this won't work the way you expect, as the characters need to be escaped in HTML. Use escapeHtml4() to do this:

    newArticle.Article_Body_Long_T__c = result.SolutionNote.escapeHtml4();

Or, to remove them entirely:

    newArticle.Article_Body_Long_T__c = result.SolutionNote.stripHtmlTags();

Note that if <br/> is used, you'll lose the line breaks in the resulting text, so you may need to do additional replacements.

You must log in to answer this question.

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