We have some EmailMessage records where the htmlbody field has its value from the textbody of email records taken from Microsoft Outlook using the Graph API implying there should be no newline/line break characters.

So I query EmailMessage records and run the following script in apex:

for (EmailMessage em : [select id, htmlbody from emailmessage]) {
    if (
        em.htmlbody == null 
        || em.htmlbody.contains('<br>') 
        || em.htmlbody.contains('<br/>')
        || em.htmlbody.contains('<br />')
        || em.htmlbody.contains('\\n')
        || em.htmlbody.contains('\\r')
    ) continue;

The problem I'm having upon logging to the console is that on the console, I see the emails with line breaks, as if they really have line breaks of some kind. Please see the very redacted screenshot below: screenshot of issue

What am I missing here? I expect to see one long string of text whereas I see a string with line breaks.

1 Answer 1


To find literal line breaks, you need to use \r or \n, not \\r or \\n, which represent two characters (\r and \n, respectively).

    || em.htmlbody.contains('\n')
    || em.htmlbody.contains('\r')

You must log in to answer this question.

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