How to add a email address to a watchlist via client script when priority changes?

Richard78
Giga Contributor

Hello community,

So here is the requirements;

When a user from a specific location creates a ticket or has a ticket created for them and it is priority high or critical, add a email address to the watch list.

If the priority drops below a high/and or the affected end-user changes to one not at the location remove the email from the watchlist.

The problem,

I have everything working with the exception of if a user or group is already in the watchlist, if so then the SID of the user/group shows up instead of the user/group and no email is sent to them.

Thoughts,

So I know that the watchlist is a list that pulls from the users and groups tables and reference's them, as well as taking text entry for the email add. When in the script I call the .getValue('watch_list') it is returning the string value for what it contains... when I do the .setValue('watch_list', newWatchers) I am adding a string list? single value? so I need to be able to add a value as a user/group reference? Any help would be appreciated, code is below.

NOTE: <locationSID> is where I have the actual SID for the location I'm checking the user for. Also, the findUsrLoc is a server side script include that get's the user location sid and returns it, I don't have issue with this code, if your curious about it I can share it.

CLIENT SCRIPT JS:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
          return;
    }
var loc, watchers;
var ga = new GlideAjax('FindUsrLoc');
ga.addParam('sysparm_name', 'findUsrLoc');
ga.addParam('sysparm_usrSID', g_form.getValue('caller_id'));
ga.getXML(FindUsrLocResponse);

function FindUsrLocResponse(response) {
  var answer = response.responseXML.documentElement.getAttribute("answer");
  loc=answer;
  var pri = ""+g_form.getValue('priority');
  if(loc == "<locationSID>"){
    if((pri == "2") || (pri == "1")){
      watchers = g_form.getValue('watch_list');
      newWatchers = watchers+",example@example.com";
      g_form.setValue('watch_list', newWatchers);
      alert("example@example.com added to the watchlist!");
    }else{
      dropOmniWatcher();
    }
  }else{
    dropOmniWatcher();
  }
}

function dropOmniWatcher(){
  var newWatchers;
  watchers = g_form.getValue('watch_list');
  if(watchers.indexOf("example@example.com") != -1){
    newWatchers=watchers.replace("example@example.com", "");
    g_form.setValue('watch_list', newWatchers);
  }
}
   
}

1 ACCEPTED SOLUTION

larstange
Mega Sage

Hi



You are on the right path.



The watch list simply contains a string of comma seperated values of either user sys_ids or direct email adresses.



So to add a user, you simply add that users sys_id to the string and update the watch_list field. There is nothing else to it.


You cannot add servicenow groups directly, as the watch list field is pointing to the sys_user table



find_real_file.png


<watch_list>


5137153cc611227c000bbd1bd8cd2005,a8f98bb0eb32010045e1a5115206fe3a,test@test.com


</watch_list>


View solution in original post

2 REPLIES 2

larstange
Mega Sage

Hi



You are on the right path.



The watch list simply contains a string of comma seperated values of either user sys_ids or direct email adresses.



So to add a user, you simply add that users sys_id to the string and update the watch_list field. There is nothing else to it.


You cannot add servicenow groups directly, as the watch list field is pointing to the sys_user table



find_real_file.png


<watch_list>


5137153cc611227c000bbd1bd8cd2005,a8f98bb0eb32010045e1a5115206fe3a,test@test.com


</watch_list>


Richard78
Giga Contributor

Honestly, my solutions is working now without any changes. I don't know if there was some caching going on with my session that day that caused the issue or what.


Thank Lars for throwing in!