Email script line break

Katie Boal
Tera Contributor

We have a older script which doesn't seem to be adding the line breaks back into comments anymore from our incidents. It is still taking the last comment, removing author and date and attaching the KB article correctly, but the body of the remaining comments has no line breaks. I'm unsure what the issue is looking at the code.

 

(function runMailScript(current, template, email, email_action, event) {
    /* the following code has been amended after it was agreed that the best we could do to satisfy this story is to set the 'Newlines to HTML' on this email script, remove the first line of the latest journal entry (which contains date and author) then output the remaining message which retains and line breaks and multiple line spacing as required.
    Unfortunately this option doesn't allow us to style the 'comments' as per the rest of the email but this was acceptable to the customer
    */
    /*
     Updated to include KB details in the email if it exists in the last 2 comments
    */

    var comments = current.comments.getJournalEntry(1);

    var lastcommentlength = 0;
    lastcommentlength = comments.length;

    //gs.print("Comments: " + comments);

    var regex = new RegExp("\n");
    var index = comments.search(regex);
    if (index > 0) {
        comments = comments.substring(index + 1, comments.length);

        //check if KB exists in the last comment. If not check the previous comment
        //Based on the settings KB might have been embedded as a link or it can come as a full text
        //The code handles both the link and full text
        if ((comments.indexOf("href") > 0 && comments.indexOf("kb_view") > 0) || comments.indexOf("Knowledge article KB") > 0) {
            comments = comments.replace("[code]", "").replace(" [/code]", "");
            //gs.print("KB exists in the last Journal: " + comments);
            template.print('<p>');
            template.print(comments);
            template.print('<p>');
        } else {
            var twoComments = current.comments.getJournalEntry(2);
            //gs.print("twoComments: " + twoComments);

            if ((twoComments.indexOf("href") > 0 && twoComments.indexOf("kb_view") > 0) || twoComments.indexOf("Knowledge article KB") > 0) {
                var prevcomments = twoComments.substring(lastcommentlength, twoComments.length);
                //gs.print("prevComments: " + prevcomments);

                //Remove the date and author from the 2nd comment
                var index2 = prevcomments.search(regex);
                if (index2 > 0) {
                    prevcomments = prevcomments.substring(index + 1, prevcomments.length);
                    //gs.print("KB exists in the prev Journal: " + comments + "\n\n" + prevcomments);
                    var prevcommentArr = prevcomments.split("code]");
                    if (prevcommentArr.length > 1) {
                        prevcommentArr[1] = prevcommentArr[1].replaceAll("</div><div>", "");
                    }
                    prevcomments = prevcommentArr.join("code]").replace("[code]", "").replace("[/code]", "");
                    comments = newLineHTML(comments);

                    var fullcomments = comments + "\n" + prevcomments;
                    template.print('<p>');
                    template.print(fullcomments);
                    template.print('<p>');
                }
            } else {
                comments = newLineHTML(comments);
                template.print('<p>');
                template.print(comments);
                template.print('<p>');
            }
        }
    }
})(current, template, email, email_action, event);

//Need to convert newlines to HTML on just some of the comments - cannot use Newlines to HTML option
function newLineHTML(noBreaks) {

    var re3 = /XiLBXZ/gi;
    var re4 = /XiLBXZXiLBXZ/gi;

    noBreaks = noBreaks.replace(/\r\n/g, "XiLBXZ");
    noBreaks = noBreaks.replace(/\n/g, "XiLBXZ");
    noBreaks = noBreaks.replace(/\r/g, "XiLBXZ");

    noBreaks = noBreaks.replace(re4, "</p><p>");
    noBreaks = noBreaks.replace(re3, "<br />");
    noBreaks = "<p>" + noBreaks + "</p>";
    return noBreaks;
}

 

Wondering if someone could advise what the issue might be? 

2 REPLIES 2

Tushar
Kilo Sage
Kilo Sage

Hi @Katie Boal 

 

I see the issue. The newLineHTML function is not replacing the line breaks in the comments with HTML entities.

The re3 and re4 regular expressions are looking for the string XiLBXZ, which is not a valid line break character.

 

You need to change the regular expressions to look for the actual line break characters:

var re3 = /(\r\n|\n|\r)/gi;
var re4 = /(\r\n|\n|\r)\s*(\r\n|\n|\r)/gi;

This will replace all line breaks with the HTML entity <br />. You also need to change the noBreaks variable to be a string, not an array. 

 

The updated code -

function newLineHTML(noBreaks) {

    var re3 = /(\r\n|\n|\r)/gi;
    var re4 = /(\r\n|\n|\r)\s*(\r\n|\n|\r)/gi;

    noBreaks = noBreaks.replace(re3, "<br />");
    noBreaks = noBreaks.replace(re4, "</p><p>");
    noBreaks = "<p>" + noBreaks + "</p>";
    return noBreaks;
}

This should fix the issue and the line breaks will be added back into the comments in the email.

 

 

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Regards,
Tushar

Hi @Tushar ,

 

With your answer I replaced the last function on the script but unfortunately the email script still isn't providing the line break from the comment.

 

Regards,

Katie