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

ccajohnson
Kilo Sage

What I discovered is that you seem to have two Journal entries. One with just the additional string, and one with the additional string and the comment.

Since everything you need is in that second journal entry, all we have to do is strip out the date - User (Additional Comments) line that sits between. We can actually accomplish this by using a custom Notification Email Script:

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */ event) {

    var jString = current.comments.getJournalEntry(2);
    var nString = setJournalString(jString);
    template.print(nString);
})(current, template, email, email_action, event);

/**
 * Modifies a journal entry with a timestamp in the middle
 * @param {string} jString The journal entry to be modified
 */
function setJournalString(jString) {
    var regex = /\n\n.+/g;
    var nString = jString.replace(regex, '');
    regex = /\n/g;
    var result = nString.replace(regex, '<br/>')
    return result;
}

Let me know if you have any questions about what the setJournalString function does and I can easily explain.

So your script does work, but it will only work if I want to send just those two journal entries combined into one. If I am sending all of the journal entries, then this will not fix it. Really I am looking for a way to get to the journal entry before a record is inserted into the journal table. So it only creates the one journal entry and not two separate ones.

Christopher
Kilo Explorer

So I think I have found that running a client script will do what I want to add in the text that I want to the comment, the only problem is I don't really know Ajax so I am not sure how to change the script to get what I need. This is the current script that I have, it mostly works other than it doesn't pull the Display Value of the sys_user, it only adds the sys_id. Any help converting this to Ajax would be great! Thanks!

 

function onSubmit(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}

//Pulls the current watch list
var wList = g_form.getValue('watch_list');
var list = wList.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 theComments = 'Included in this conversation: ' + watchArr.join(', ') + "\n\n" + g_form.getValue('comments');
g_form.setValue('comments', theComments);

}

ccajohnson
Kilo Sage

I thought I would try that approach of using an Ajax call to populate the comments with an onSubmit script, but I found that there are a lot of "you need to make sure that you..." scenarios in order for it to work.

  • You need to make sure that the user does not use the Post button.
  • You need to make sure that the user saves the record.
  • If there is more than one journal field on the form at the same time (i.e. work_notes and comments), you need to make sure both are visible for it to work.

In all, the best approach would be to not include the verbiage in the comments, but rather include it in the notification. We already know how to get the display values of the users in the watch list, we just need to put that in an Notification Email Script.

Christopher
Kilo Explorer

The problem with putting it in the notification directly is it won't leave the history in the journal entry. It would only show who is currently on the watch list at the time that comment is sent. It wouldn't be saved in the journal entry anywhere. That is why there is the need to add it to the journal entry itself, however, I have realized that the client script may not work because if there is an inbound email that adds to the comments, it doesn't fire the client script to add in the watch list.