Add email to Watch List via Script

Sharee1
Kilo Contributor

Within an Incident if the the Priority is 1 or 2 I need to add the Service Desk's email to a Watch List. I can't seem to get it to work. Here is the script I am trying:



function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (!isLoading) {
if(newValue != '') {

var pr = g_form.getValue('priority');
var wList = current.u_itil_watch_list;
var eMail = "it.servicedesk@myemail.com";

if ((pr == '1') || (pr == '2')){
wList = wList + "," + eMail;
current.u_itil_watch_list = wList;
current.update();
}
}
}
}
2 REPLIES 2

gaidem
ServiceNow Employee
ServiceNow Employee

I see your doing this via client script, you may want to make a business rule instead.

Also try this:

var wList = current.u_itil_watch_list.toString();


Yes, as the previous comment suggested, you would be better doing this in a business rule - you don't have access to "current" in a client script. Set the Condition on the business rule to current.priority.changesTo(1)||current.priority.changesTo(2)

Also, you may like to check that you don't add the servicedesk email to the watch list each time the priority changes from 1 to 2 or vice versa, or you may end up with the same address several times. I've done something similar to this using a script similar to the following:



var list = current.watch_list.toString();
var array = list.split(",");
var len = array.length;

/* Get the sys_id of the User you want to add */

var usr = new GlideRecord('sys_user');
usr.addQuery('email', "the email address you want to add");
usr.query();
usr.next();

/* See if the User is already on the list */

var onList = false;
for (var i=0; i<len; i++) {
if (array<i> == usr.sys_id) {
onList = true;
}
}

if (!onList) {
array[len] = usr.sys_id;
}

current.watch_list.setValue(array.join());


Hope this helps.

Brian Broadhurst (TeamUltra Ltd.)