Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Previous Comments not posting in email notification

Community Alums
Not applicable

I have an email notification for additional comments to customer that points to a mail script that is supposed to post the last journal entry. It is not posting anything.  Below is the code for the mail script I am calling in the notification.

${mail_script:previousComment} (Called script)

 

Script:

 

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {

var notes = current.comments.getJournalEntry(-1);
var noteArr = notes.split("\n\n");
if (noteArr[3].includes('*This communication was generated by compose email functionality.*')) {
template.print(noteArr[1] + '\n\n' + noteArr[2]);
} else {
template.print(noteArr[1]);
}


})(current, template, email, email_action, event);

 

Please advise why the previous comment is not sending anything.

2 REPLIES 2

SanjivMeher
Mega Patron
Mega Patron

Can you try below script

 

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {

var notes = current.comments.getJournalEntry(-1);
var noteArr = notes.split("\n\n");
if (noteArr.length>=3 && noteArr[3].indexOf('This communication was generated by compose email functionality')>-1) {
template.print(noteArr[1] + '\n\n' + noteArr[2]);
} else {
template.print(noteArr[1]);
}


})(current, template, email, email_action, event);

Please mark this response as correct or helpful if it assisted you with your question.

Community Alums
Not applicable

Hi @Community Alums ,

The issue for posting the last journal entry might be due to indexing errors or the way the journal entries are being split.

 

Can you please try the below script-

 

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */ event) {
    var notes = current.comments.getJournalEntry(-1);
    if (notes) {
        var noteArr = notes.split("\n\n");
        // Check if noteArr has the expected structure
        if (noteArr.length >= 2) {
            if (noteArr.length > 3 && noteArr[3].indexOf('This communication was generated by compose email functionality') > -1) {
                template.print(noteArr[1] + '\n\n' + noteArr[2]);
            } else {
                template.print(noteArr[1]);
            }
        } 
    } 

})(current, template, email, email_action, event);

 

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!


Thanks & Regards,

Sanjay Kumar