script help to add user to watch list
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-15-2016 05:56 AM
I have a script which is working currently to add the user in the assigned_to field on a Change Task to the watch list on the parent Change Request. This script does not duplicate entries, and successfully adds the user when it changes. The one thing missing which I cannot figure out, if the user in the assigned_to field changes, how to remove the existing user from the watch list. Example if Employee A is assigned a CTASK, and then switches to Employee B, both will appear on the watch list. I only want the current user "Employee B" assigned.
Thinking I may need to go a different route and use a glide record?
function onBefore(current, previous)
{
var RFC = current.change_request.getRefRecord();
var wlArr = RFC.watch_list.toString().split(',');
var user = current.assigned_to.toString();
var arrUtil = new ArrayUtil();
if (!arrUtil.contains(wlArr, user))
wlArr.push(user);
RFC.watch_list = wlArr.join(',');
RFC.update();
}
Any help would be much appreciated.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-15-2016 06:00 AM
Hi Tadd,
I have a script include that can help with that. You can use methods to check if someone is in the list, add them, remove them, get the entire list, etc. It was specifically made around the watch list, but works with any other list field.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-15-2016 06:51 AM
Would I just add your script in the link to a Business Rule, or just parts of it? Would it require customization on certain lines to match tables and such?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-15-2016 06:59 AM
Hi Tadd,
The idea is that the script in my article goes in a new Script Include. These can be thought of as libraries of reusable code that are called as needed. Once in place, you can call it from your business rule (or any other server side script) something like this (untested)
var lu = new ListUtil(current.watch_list);
// If the current assigned to person is not in the list, add them
if (!lu.isInList(current.assigned_to))
lu.addToList(current.assigned_to);
// If the assigned to field changed, check if the old person was in the list and remove them
if (current.assignedTo.changes()) {
if (lu.isInList(previous.assigned_to))
lu.removeFromList(previous.assigned_to))
These may help with script includes.
Script Includes - ServiceNow Wiki
TechNow Episode 6 | Script Includes - YouTube