Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

How to Append TimeStamp

Charles Fredie
Tera Contributor

When we post comments on SCTASK, they get copied to the RITM Additional comments as,
2024-08-07 10:15:00 - Author Name (Additional Comments) fix is in progress

Is there a way to do this?

1 ACCEPTED SOLUTION

Just wanted to make sure you weren't already doing something extra that I was about to undo.  This should do that:

(function executeRule(current, previous /*null when async*/ ) {
    var gr = new GlideRecord('sc_task');
    gr.addQuery('request_item', current.request_item);
    gr.query();
    while (gr.next()) {
        var comment = current.comments.getJournalEntry(-1);
        var usrstrt = comment.indexOf(" - ");
        var usrend = comment.indexOf(" (Additional");
        var usrname = comment.substring(usrstrt+3, usrend); //isolate the user Name
        var usrGr = new GlideRecord('sys_user');
        if (usrGr.get('name', usrname)) {
		    var tz = '"' + usrGr.time_zone + '"';
		    comment = comment.replace(' - ', ' ' + tz + ' - '); //update the comment with "author's timezone"
	    }
        gr.comments = comment;
        gr.update();
    }
})(current, previous);

 

View solution in original post

11 REPLIES 11

Brad Bowman
Mega Patron

Hi Charles,

I'm having a think on something that might just work.  Can you post the script using the insert code icon </> of the Business Rule that is copying the comment to the RITM?

Hi @Brad Bowman
Here is the script,

(function executeRule(current, previous /*null when async*/ ) {

var gr = new GlideRecord('sc_task');
gr.addQuery('request_item', current.request_item);
gr.query();

while (gr.next()) {

var comment = current.comments.getJournalEntry(-1);

gr.comments = comment;
gr.update();
}

})(current, previous);

Just wanted to make sure you weren't already doing something extra that I was about to undo.  This should do that:

(function executeRule(current, previous /*null when async*/ ) {
    var gr = new GlideRecord('sc_task');
    gr.addQuery('request_item', current.request_item);
    gr.query();
    while (gr.next()) {
        var comment = current.comments.getJournalEntry(-1);
        var usrstrt = comment.indexOf(" - ");
        var usrend = comment.indexOf(" (Additional");
        var usrname = comment.substring(usrstrt+3, usrend); //isolate the user Name
        var usrGr = new GlideRecord('sys_user');
        if (usrGr.get('name', usrname)) {
		    var tz = '"' + usrGr.time_zone + '"';
		    comment = comment.replace(' - ', ' ' + tz + ' - '); //update the comment with "author's timezone"
	    }
        gr.comments = comment;
        gr.update();
    }
})(current, previous);

 

Thanks @Brad Bowman  - It worked