Parse Journal Entries to separate values

Katie A
Mega Guru

Hello,

Would it be possible to parse a journal entry to retrieve the timestamp, username who made the update, and the text?

I was able to parse out the timestamp by just using string length and sub string. However since the username is not a fixed length I don't know if it is possible to parse that out.

I need to parse this information out of each journal entry in the comments and work notes in order to meet the requirements described here.

https://community.servicenow.com/message/888965

var notes = current.work_notes.getJournalEntry(-1); //gets all journal entries as a string where each entry is delimited by '\n\n'

var na = notes.split("\n\n");                                             //stores each entry into an array of strings

  for (var i = 0; i < na.length; i++) {

  var tmstmp = na[i].substring(0, 19);

  var entrylength = na[i].length;

  var textonly = na[i].substring(20, entrylength);

  gs.log("Comment: " + na[i] + "Textonly: " + textonly + "Timestamp: " + tmstmp);

  gs.log(na[i]);

  }

1 ACCEPTED SOLUTION

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

5 REPLIES 5

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! 🙂