How to insert a message into a journal entry/comment before it is saved?

Christopher
Kilo Explorer

For our case, we need to insert the users that are currently on the watch list at the time a comment is made to have a record of who was involved in a task when. Our end users do not access to service-now, instead they email to a general mailbox to initiate a task, since they do not access service-now they do not have visibility to the system messages that record the watch list activity. Also because there is several departments that utilize the service-now platform, this rule should only apply to our department and not be a global change. So far I have setup a before business rule that pulls the watch list and inserts into the current.comment but it appears this is still after the comment has been created because it creates two entries. My question is how do I add the watch list to what the user put in to the comments field so it is one entry?

12 REPLIES 12

Christopher
Kilo Explorer

(function executeRule(current, previous /*null when async*/)
{
// Add your code here
var list = current.watch_list.split(',');
var watchArr = [];
for (var i=0; i< list.length; i++)
{
var userElement = new GlideRecord("sys_user");
if (userElement.get('sys_id', list[i]))
{
watchArr.push(userElement.getDisplayValue());
}
else
{
watchArr.push(list[i]);
}
}

current.comments = 'Included in this conversation: ' + watchArr.join(', ') + "\n\n" + current.comments;

})(current, previous);

 

In addition to the screenshot I meant to paste the script that I am using.

Christopher
Kilo Explorer

Also we are using Istanbul.

ccajohnson
Kilo Sage

I tried your code and got the same duplicate messages within the Activity Log. What I got to work is to populate the comments field using the setJournalEntry() method and leaving out the current.comments value. Here is your script you posted including the change:

 

(function executeRule(current, previous /*null when async*/) {
    var list = current.watch_list.split(',');
    var watchArr = [];
    for (var i=0; i< list.length; i++) {
        var userElement = new GlideRecord("sys_user");
        if (userElement.get('sys_id', list[i])) {
            watchArr.push(userElement.getDisplayValue()); 
        } else {
            watchArr.push(list[i]); 
        }
    }
    var cStr = 'Included in this conversation: ' + watchArr.join(', ');
    current.comments.setJournalEntry(cStr);
})(current, previous);

Sorry, I guess I didn't give a very good illustration as to what I meant by duplicates, the script you provided does prevent the current.comment from being posted twice but it is still saving two comments instead of just one. One with the original current.comments and the second with the inserted text.

find_real_file.png

Here is an example from an outbound notification. Instead of it saving as two different journal entries I would like it save as just one.