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

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
Giga Sage
Giga 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'
};