The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Insert BODY only of additional comments in email notifications

nickde
Tera Contributor

For on-prem Xanadu:

 

I have a requirement to include the latest public comment (comments:1 or current.comments) into the email notifications that get sent out. However, no matter what variation I use of the getJournalEntry(), I am getting the date, name of the commenter, (Additional Comments), <comment>. There was a line-break hint (\n) but SN doesn't like any variation of JS, HTML, or UNICODE I have found for the line break.  I believe this line break tip was to extract the Comment from the entire journal entry.

 

I'm getting better at scripting in SN but any help would be much appreciated.

2 REPLIES 2

GlideFather
Tera Patron

Hi @nickde,

 

what do you mean "latest public comment"? 

 

If somebody adds comment it's the latest one, but if they add another one after 2 minutes then the latest is this one.. if the latest one has a typo and they add comment to correct typo so it's the latest.... how do you define the latest - per hour, per day, week, month, ... ?

 

And with the above, there's OOTB notification for additional comments - you can either change the conditions, recipients or contents.

 

Also, what table are we talking about - ITSM, other module or custom app?

———
/* If my response wasn’t a total disaster ↙️ drop a Kudos or Accept as Solution ↘️ Cheers! */


Rafael Batistot
Kilo Patron

Hi @nickde 

 

In ServiceNow on-prem (Xanadu), the comments field is a journal field, so by default methods like getJournalEntry() return the full formatted entry:

 

2025-09-20 10:12:34 - John Doe (Additional comments)
This is the actual comment

 

That’s why you’re always seeing timestamp, user, and label bundled with the comment itself.

 

The problem

You only want the latest comment text itself (not the metadata), and when you try to split it, you run into issues with line breaks.

 

Working approach

You can pull only the latest raw comment like this:



(function execute(current) {
var latestComment = "";

var gr = new GlideRecord('sys_journal_field');
gr.addQuery('element_id', current.sys_id); // record you’re on
gr.addQuery('element', 'comments'); // journal field
gr.orderByDesc('sys_created_on'); // newest first
gr.setLimit(1);
gr.query();
if (gr.next()) {
latestComment = gr.value.toString();
}

// make it safe for email (convert newlines to <br/>)
latestComment = latestComment.replace(/\r\n|\n/g, '<br/>');

template.print(latestComment);

})(current);