Add users to watch_list based on caller's department and location

mathew_kimber
Giga Contributor

Hello all,

I am rather new to ServiceNow development so please excuse my lack of understanding, I am learning as I go. I am having trouble with a business rule I've created for a requirement we have whereby we need to add all users to an incident watchlist that matche the current caller's location and department. I need this to run whenever a new incident or request is created, or if the location/contact field of either is updated. The script works fine when I create or update incidents as my user (admin role), but fails to add anyone to the watch list when an ESS user creates a new INC or REQ. Is a business rule the best way to go about this? I can't figure out a trigger that will execute this when an ESS user creates a ticket. Any help would be much appreciated.

Here's my current business rule script:

Runs: before Insert/Update, when contact or location changes. (also tested "after" with no success)

//This custom business rule runs anytime the 'location' or 'contact' fields on an 'incident' record are updated or inserted.

(function executeIncWatchlistRule(current, previous) {

var watchList = current.watch_list;

var wlUser       = new GlideRecord('sys_user');

var calLoc       = current.caller_id.location;

var calDep       = current.caller_id.department;

//regex of dept sys_ids removed for this post

if (calDep.match(/^(myhiddenregex)$/)) {

  wlUser.addQuery('location', calLoc);

  wlUser.addQuery('department', calDep);

  wlUser.query();

  while (wlUser.next()) {

      var userToAdd = wlUser.sys_id;

      var presence   = watchList.search(userToAdd);

      if (presence == -1) {

          watchList = (watchList + "," + userToAdd);

      }

  }

  current.watch_list = watchList;

}

})(current, previous);

1 ACCEPTED SOLUTION

Hey Masha,



I tested by mapping a variable in the record producer to the watch_list in order to have that present on incident creation and that solved my issue! I very much appreciate your suggestion here.



Best,


Mat


View solution in original post

7 REPLIES 7

Masha
Kilo Guru

Can your ESS user 'read' Location and Department fields on the sys_user table? Try logging out: gs.log("User location: "+ current.caller_id.location); on the first line of your BR and see if you get anything. If not you will have to adjust your sys_user table ACLs in order for your BR to run as expected.


Hey Masha,



Thank you very much for your quick response. I placed the gs.log statement you mentioned as well as a gs.addInfoMessage on my script to test. I then opened a new ticket impersonating the ESS user and I received the location sys_id message as well as a syslog entry for User location ("source: *** Script"). Unfortunately the watch_list users were not added to the new incident. I was really hoping you had the right answer for me, been scratching my head over this for a few hours now. Could it possibly be some other permission issue (i.e. public vs private function)?


Can ESS user write to watch_list?


Hey Masha,



Do I check that by going to the System Definition > Tables> incident table and looking at access controls? I see an 2 ACs named incident.watch_list for "write", one for 2 roles (itil and engagement_member) and the other with no restrictions. Sorry if I'm not looking in the correct location.



Is it possible that because there is no "watch_list" on the ESS user's custom incident creation form (record producer?), I can't assign value to that variable until after the incident has been created? When I create an incident as impersonating any user (i.e. not using the ess portal), the watch_list is already present on the creation form before submitting and the script functions as intended.



Best,


Mathew