Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

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

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


I am glad you got it figured out.


Masha,



Your suggestions got me pointed in the right direction to resolve this. Thanks again