Inbound email actions to add users to watch list

Eric K3
Kilo Guru

I found the script below at http://wiki.servicenow.com/index.php?title=Useful_Inbound_Email_Scripts#gsc.tab=0http://

It adds the cc'd users to the watch list, but it also created another incident with the same incident number. Not sure what I am missing.

find_real_file.png

// Add All Email Recipients to Watch List

var wList = current.watch_list;

var gList = current.u_group_watch_list;

var rarray = email.recipients_array;

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

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

  var recipient = rarray[i];

  //Check to see if this is a group address...and if so add to the group watch list

  var grp = new GlideRecord('sys_user_group');

  grp.addQuery('email', recipient);

  grp.query();

  if (grp.next()) {

  if(gList != "") {

  gList = (gList + "," + grp.sys_id);

  } else {

  gList = grp.sys_id;

  }

  } else {

  //It's not a group address...so check to see if it's a user

  var gr = new GlideRecord('sys_user');

  gr.addQuery('email', recipient);

  gr.query();

  if (gr.next()) {

  // It's a user

  if(wList != "") {

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

  } else {

  wList = gr.sys_id;

  }

  } else {

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

  if (recipient != instanceEmail) {

  if(wList != "") {

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

  } else {

  wList = recipient;

  }

  }

  }

  }

}

current.watch_list = wList;

current.u_group_watch_list = gList;

current.insert();

1 ACCEPTED SOLUTION

manikorada
ServiceNow Employee
ServiceNow Employee

Eric,



My apologies replace current.insert() with current.update()


View solution in original post

7 REPLIES 7

Eric K3
Kilo Guru

I found another issue with this script. This script will add the user to the watch list, and a group to the group watch list, even when they are already listed. So the watch lists end up having two, three, four listings of the same user/group, basically every time an incoming email posts an update to the incident.



What method could I use to iterate through the recipients array to see if the recipient is already listed in the watch list or group watch list?


Hi Eric,



Could you please use below code for dedupe the user/group.



current.watch_list = dedupe(wList);  


current.u_group_watch_list = dedupe(gList);  


current.update();



function dedupe(p_value) {


          var split_list = p_value.split(',');


          split_list.sort();


          var last = '';


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


                if (last == split_list[i]) {


                      split_list.splice(i, 1);


                      i--;


                } else {


                      last = split_list[i];


                }


          }


          return split_list.join(',');


    }


Hi Senthil,

Could you please let me know if the above script needs to be added to existing script or replace it? Even i am facing the same issue as Eric. Please advice.

 

Regards,

Syed