Katie A
Mega Guru

I think I can just query the Journal table and match element ID with the sys ID of the current record.



I located a helpful article.



ServiceNow KB: Deleting or Editing a Bad Comment or Work Note from a Record (KB0520375)


Rahul Priyadars
Tera Sage

THis will split into two lines.

 

var workinfo=current.work_notes.getJournalEntry(1).toString();
var notesarry = workinfo.split("\n");
var wnote=notesarry[1];
gs.log('wnote work Info processed after='+wnote);

 

now from notesarray[0] you can fetch time stamp and user info based on length .

 

Regards

RP

Chuck Tomasi
Tera Patron

I tend to use Regular Expressions to get patterns of text extracted from strings like this. I encourage you to watch episodes 31 and 32 of TechNow for more information how Regular Expression can be used in ServiceNow.

TechNow Episode List

Katie A
Mega Guru

I created a custom script include to get the latest journal entry.

var TextFormatUtil = Class.create();
TextFormatUtil.prototype = {
  initialize: function () {},

  /* Extract only the text value from journal entry, leaving out timestamp and sender */
  getJournalText: function (current_sysid, element_type) {

    var text_value = '';
    // Get current sysid
    var sys_id = current_sysid.toString();
    // Get element type
    var element = element_type.toString();

    // Get required journal entry
    var journalEntry = new GlideRecord('sys_journal_field');
    // Name of desired journal field such as 'comments' or 'work_notes'
    journalEntry.addQuery('element', element);
    // Match sys_id of the record on which the journal field is present
    journalEntry.addQuery('element_id', sys_id);
    // Order by created on date
    journalEntry.orderByDesc('sys_created_on');
    // Return only latest comment
    journalEntry.setLimit(1);
    journalEntry.query();
    while (journalEntry.next()) {
      // Get the text value only
      text_value = journalEntry.value.toString();
    }

    return text_value;
  },

  type: 'TextFormatUtil'
};

View solution in original post

Tim Woodruff
Mega Guru

Hey there! 

I wrote an article (and free tool) that might be helpful. It's about how to get Journal entries from a given record and (optionally, depending on how you call the function in the article) parse and convert/sanitize the contents of each entry for HTML. 

For example, by calling the function in the article, in the following way, in a Notification Mail Script, you can get all journal entries formatted as HTML: 

var arrCommentsAndWorknotes = getJournalEntries(
current,
'comments_and_work_notes',
false,
false,
-1 //Get ALL journal entries
);

There's also some code near the bottom of the article that'll convert the result into an HTML table for you as well, if you like. 🙂 

The article is at https://go.snc.guru/journal
(Note: You can now create your own short-URLs using https://go.snc.guru/short!)

And don't forget - If you found this helpful, please click to mark it as helpful. It really helps out! 🙂