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.

1 ACCEPTED SOLUTION

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);

If you found this response helpful, please mark it as Helpful. If it fully answered your question, consider marking it as Correct. Doing so helps other users find accurate and useful information more easily.

View solution in original post

6 REPLIES 6

Gotcha. Ok, I'll keep digging. Thanks!

kaushal_snow
Giga Sage

Hi @nickde ,

 

The current.comments.getJournalEntry method will return the latest Additional Comment journal entry. That includes metadata (date, user name, label) plus the comment body. There's a system property glide.email.journal.lines that controls how many journal entries appear in email notifications (e.g. 1, 2, all or -1 for all)....

 

You need to tweak a bit of string parsing using mail script..

 

(function(/*GlideRecord*/ current, /*TemplatePrinter*/ template){
  var fullEntry = current.comments.getJournalEntry(1);
  if (!fullEntry) {
    return;
  }
  var parts = fullEntry.split('\n');
  var bodyText = '';
  if (parts.length > 1) {
    bodyText = parts.slice(1).join('\n').trim();
  } else {
    bodyText = fullEntry;
  }
  bodyText = bodyText.replace(/\[code\]|\[\/code\]/g, "");
  template.print(bodyText);
})(current, template);

 

add this mail script in your notification..

 

${mail_script:name_of_email_script}

 

If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/