Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Understanding comments.getJournalEntry()

JPing
Tera Expert

Hello Developers, 

 

When using the <task>.comments.getJournalEntry(1) function this would return the last comment entry in a record. It appears that what is returned is a string in the format "<date> - <users> (Comments) <comment>". Because this is just a string and I somewhat know the format, I can use some sting functions to pull out the section I want. My question is there a built in function already where I can just get the <comment> portion.

 

I ask this because the activity.xml formatter is able to separate these sections and display them in the nice activity logs that you see on a record. With the user (with picture) in the top left, the date and type in the right and the comments below. I don't know if any one has any insight on the activity.xml file, but I feel like understanding that file will help me parse comment better. 

 

Thanks in advance. 

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

Conveniently, there is a newline character embedded in the string, so if you split on that or do some voodoo like this, (example from a Business Rule, but you can do a GlideRecord or whatever to retrieve them also...) you'll end up with just the comment

var cmt = current.comments.getJournalEntry(-1);
var regex= new RegExp('\n');
var a = cmt.search(regex);
cmt = cmt.substring(a+1, cmt.length);

 

View solution in original post

2 REPLIES 2

Brad Bowman
Kilo Patron
Kilo Patron

Conveniently, there is a newline character embedded in the string, so if you split on that or do some voodoo like this, (example from a Business Rule, but you can do a GlideRecord or whatever to retrieve them also...) you'll end up with just the comment

var cmt = current.comments.getJournalEntry(-1);
var regex= new RegExp('\n');
var a = cmt.search(regex);
cmt = cmt.substring(a+1, cmt.length);

 

RikV
Tera Expert

Hi JPing,

 

I am not sure what exactly is the use case, but to have more, and easier, control over comment(s) within a record i would suggest querying the sys_journal_field or sys_audit table. 

 

If you just want the comments and/or work_notes attached to a record, the sys_journal_field table would be sufficient. If you are planning to create some kind of activity timeline, i would suggest the sys_audit table as it also keeps track of non-journal field updates and works almost in a similair fashion. 

 

Example using sys_journal_field table:

(function () {
    var recordSysId = 'YOUR_RECORD_SYS_ID_HERE';

    var commentGr = new GlideRecord('sys_journal_field');
    commentGr.addQuery('element_id', recordSysId); 
    commentGr.addQuery('element', 'comments'); // Only fetch comments (can also do work_notes)
    commentGr.query();

    var allComments = []
    while (commentGr.next()) {
        var comment = {
            author: commentGr.sys_created_by.getDisplayValue(),
            comment: commentGr.value,
            created: commentGr.sys_created_on.getDisplayValue(),
        }
        allComments.push(comment);
    }
})();