The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Watch List Script Help

jasonprice
Tera Expert

Hello,

The following script has suddenly stopped performing as it should. The script excludes certain email addresses from the list, and filters out duplicates using ArrayUtil. It was working until recently, now it just simply does not add anyone to the watch list that is CCd on an email. Not only does it not add them, it removes anyone else that is on the watch list. If I remove the ArrayUtil it works except I get duplicate entries in the watch list. Any help would be appreciated.

var cwList = current.watch_list;

gs.log("starting watchlist: " + cwList);

var rarray = email.recipients_array;

var instanceEmail = gs.getProperty('glide.email.user');

for (var i = 0; i < rarray.length; i++) {

var recipient = rarray[i];

var gr = new GlideRecord('sys_user');

gr.addQuery('email', recipient);

gr.query();

//Look for a user to add to a watchlist, but only if it isn't support or traininghelp

if (gr.next()) {

// It's a user

if(cwList != "") {

if (recipient != instanceEmail && recipient != 'example@example.com' && recipient != 'example2@example.com'){

//gs.log("1st watchlist: " + cwList);

cwList = ( cwList + "," + gr.sys_id);

//gs.log("2nd watchlist: " + cwList);

}

} else {

if (recipient != instanceEmail && recipient != 'example@example.com' && recipient != 'example2@example.com'){

//gs.log("3rd watchlist: " + cwList);

cwList = gr.sys_id;

//gs.log("4th watchlist: " + cwList);

}

}

} else {

//It's not a user either...so just add the address to the list...except instance email address

if (recipient != instanceEmail && recipient != 'example@example.com' && recipient != 'example2@example.com') {

if(cwList != "") {

//gs.log("5th watchlist: " + cwList);

cwList = (cwList + "," + recipient);

//gs.log("6th watchlist: " + cwList);

} else {

if (recipient != instanceEmail && recipient != 'example@example.com' && recipient != 'example2@example.com')

//gs.log("7th watchlist: " + cwList);

cwList = recipient;

//gs.log("8th watchlist: " + cwList);

}

}}}

//create a temporary watchlist, convert it to a string and split it on commas. Once converted to a available, ArrayUtil can be used to filter out duplicates. The watchlist field expects a string.

var newList = cwList;

newList = newList.toString().split(',');

cwList = ArrayUtil.unique(newList);

gs.log('new watchlist: ' + newList);

gs.log('ending watchlist: ' + cwList);

current.watch_list = cwList.join(',');

current.update();

1 ACCEPTED SOLUTION

OK the following untested code should work for you.   Its doing the same thing, just more streamlined since you had many If statements checking for the same thing over and over again.   I also took the liberty to change your variable names to make it easier to follow, following best practice.



gs.log("starting watchlist: " + current.watch_list.toString());


var watchListArray = current.watch_list.toString().split(",");


var recipientArray = email.recipients_array;


var instanceEmail = gs.getProperty('glide.email.user');



for (var i = 0; i < recipientArray.length; i++) {


      var recipient = recipientArray[i];


     


      // Check to make sure the recipient is not the instance email, support, or training help


      if (recipient != instanceEmail && recipient != 'example@example.com' && recipient != 'example2@example.com') {


             


              //Check to see if the recipient is already on the watch list, this will take care of group/non-user emails


              if (watchListArray.toString().indexOf(recipient) == -1) {


                      // Check to see if the recipient is a user


                      var userRec = new GlideRecord('sys_user');


                      userRec.addQuery('email', recipient);


                      userRec.query();


                      // Its a user, add the user's sys_id to the watch list


                      if (userRec.next()) {


                              // Check to see if the recipient is already on the watch list


                              if (watchListArray.toString().indexOf(userRec.sys_id) == -1) {


                                      watchListArray.push(userRec.sys_id.toString());


                              }


                      // Its an email address, add it to the watch list


                      } else {


                              watchListArray.push(recipient.toString());


                      }


              }


      }


}




gs.log('ending watchlist: ' + watchListArray.toString());


current.watch_list = watchListArray.join(',');


current.update();


View solution in original post

7 REPLIES 7

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

Jason where is this script running?   I couldn't determine by looking at it because I see reference to email.recipients_array yet you are updating a "current" record.   Is it a business rule running on task, mail script, or what?



Edit: Is it an inbound email action?


This is part of an inbound action to update a record on the incident table upon reply from a customer.


OK thanks.   I am rewriting your logic, give me a few and I should have something that should work for you.   You don't need to even use ArrayUtil.


OK the following untested code should work for you.   Its doing the same thing, just more streamlined since you had many If statements checking for the same thing over and over again.   I also took the liberty to change your variable names to make it easier to follow, following best practice.



gs.log("starting watchlist: " + current.watch_list.toString());


var watchListArray = current.watch_list.toString().split(",");


var recipientArray = email.recipients_array;


var instanceEmail = gs.getProperty('glide.email.user');



for (var i = 0; i < recipientArray.length; i++) {


      var recipient = recipientArray[i];


     


      // Check to make sure the recipient is not the instance email, support, or training help


      if (recipient != instanceEmail && recipient != 'example@example.com' && recipient != 'example2@example.com') {


             


              //Check to see if the recipient is already on the watch list, this will take care of group/non-user emails


              if (watchListArray.toString().indexOf(recipient) == -1) {


                      // Check to see if the recipient is a user


                      var userRec = new GlideRecord('sys_user');


                      userRec.addQuery('email', recipient);


                      userRec.query();


                      // Its a user, add the user's sys_id to the watch list


                      if (userRec.next()) {


                              // Check to see if the recipient is already on the watch list


                              if (watchListArray.toString().indexOf(userRec.sys_id) == -1) {


                                      watchListArray.push(userRec.sys_id.toString());


                              }


                      // Its an email address, add it to the watch list


                      } else {


                              watchListArray.push(recipient.toString());


                      }


              }


      }


}




gs.log('ending watchlist: ' + watchListArray.toString());


current.watch_list = watchListArray.join(',');


current.update();