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

So I think I have determined I will need to use both an onSubmit client script as well as include similar script in the inbound email routing rule to insert the message into a new or reply email as it comes in. The onSubmit works with the way our task form is setup. The user has to select save or update to post new comments to the task and the script works, it just isn't pulling the display value any longer. Here is an example of how the task form is setup so the onSubmit will work to include the message into the journal entry.

find_real_file.png

Here is what I am getting in my comments though, only the sys_ids.

find_real_file.png

So any help in editing this script to get the display name for the sys_ids would be much appreciated.

function onSubmit(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || g_form.getValue('comments') == '' || g_form.getValue('watch_list') == '') {
return;
}

//Pulls the current watch list and adds it to the current comment before
//a record is saved in the journal field
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

Show us what you have for your inbound action so that we can narrow our solution. My guess is that it should behave much like your original business rule.

Christopher
Kilo Explorer

I have the inbound action handled, that works. I was pretty much able to just apply my business rule script to it because I already have it set to pull the recipients and add them to my watch list. Here is that portion of script:

 

var list = email.recipients.split(",");
gs.log("POSTMASTER: Recipients " + list);

var watchArr = [];
for (var i=0; i < list.length; i++)
{
var u = new GlideRecord('sys_user');
if (u.get('email', list[i]))
{
if (u.getValue('sys_id') != '6bc0b4371384d34073e3bda12244b053')
{
watchArr.push(u.getValue('sys_id'));
}
}
else
{ watchArr.push(list[i]); }
}

current.u_store_workforce_task.watch_list = watchArr.join(',') + ",";

gs.log("POSTMASTER: Recipients " + current.watch_list);

var wList = current.watch_list.split(',');
var wArr = [];
for (var i=0; i< wList.length; i++)
{
var userElement = new GlideRecord("sys_user");
if (userElement.get('sys_id', wList[i]))
{
wArr.push(userElement.getDisplayValue());
}
else
{
wArr.push(wList[i]);
}
}

current.u_store_workforce_task.comments = 'Included in this conversation: ' + wArr.join(', ') + "\n\n" + email.body_text;
gs.log("Comments: " + current.comments);

 

There is still some log entries that I will remove at some point but it works. The major part I am having trouble with at this point is the client script. Specifically the portion where it is comparing the sys_ids to the list and the grabbing their display values. It is no longer grabbing the display values or comparing against the list because from my understanding glide record isn't supported in client scripts and my knowledge is at a bit of a dead end on how to get the display values of the watch list instead of just the system ids while still passing any emails straight through.