- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-24-2017 10:42 AM
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();
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-24-2017 11:28 AM
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();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-24-2017 11:54 AM
Thank you Michael. That worked beautifully. Any idea on why my script failed suddenly? It was working earlier in the week and starting yesterday just decided to fail. Other than just being kind of a messy script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-24-2017 12:22 PM
Did your instance get upgraded recently? Without logging into your instance its hard to tell. As ark mentions, when you call ArrayUtil you need to use the correct syntax of "new ArrayUtil()" since its a script include.
Glad my script got you going!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-24-2017 11:07 AM
try
var a=new ArrayUtil().unique(newList);