- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-18-2016 09:07 AM
Hi guys,
I have looked through some prior similar posts but still require help with this please. Basically I would like to add whichever user is in a reference field called 'Contact' (separate from caller_id on our INC form) to the Watch list. If this value changes I would like this new value/person added too but I do not want it to keep adding the same value/person on update of the record i.e. do not add if already on watch list and do not overwrite existing values on the watch list.
Any help with this business rule much appreciated. Many thanks,
D
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-18-2016 10:12 AM
Hi dasi
The watch list is just a comma seperated list of sys_ids of users.
So you convert the watch list to an array, and user ArrayUtil - ServiceNow Wiki to check if the sys_id of the user in the Contact field is already in the array.
It would look like this:
var arr = current.watch_list.split(',');
var contact = current.u_contact;
var arrayUtil = new ArrayUtil();
if (!arrayUtil.contains(arr, contact)) {
arr.push(contact);
}
current.watch_list = arr.join();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-19-2016 02:36 AM
Really appreciate that Lars, thank-you.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-09-2022 05:56 AM
Hi Lars
I did find an issue with your solution. As coded it will not add people to the watch list if it is empty.
I modified you code for Incident when the Assigned to field changes to test for an undefined watch list which means it is empty. In that case, you just do a direct assignment.
If it's not empty, then it's basically your code with the changes needed to handle the fields I was working with.
This may be caused by the fact that I am working in a San Diego image and I don't know what you were working in when you wrote your original solution.
I also attached the code so anyone can get it without retyping it since the Insert/Edit code sample makes it readable but not copyable and the Insert source code completely messes up the formatting.
Thank you for your help, it got me going in the right direction.
(function executeRule(current, previous /*null when async*/ ) {
// Adds the New Assigned to, to the Watch list if they are not already on it.
/*
The watch list is a single string of sys_id's of the users in it. To add to it, we need to turn it into an array of strings so we can
add the new Assigned to, to the array. We then have to turn the array back into a single, comma separated string.
*/
var arr = current.watch_list.split(','); // Turn current watch list into an array.
var contact = current.assigned_to; //New assigned to
var arrayUtil = new ArrayUtil(); //Create an ArrayUtil object to use it's functions.
var contains = arrayUtil.contains(arr, contact); //Find out if the watchlist already has this Assigned to in it.
//gs.addInfoMessage("Contact type of contact is: " + typeof(contact));
if (arr == undefined) { //if arr == undefined, then the Watch List is empty so we need to add the first person differently.
//gs.addInfoMessage("arr == undefined");
current.watch_list = contact;
} else if (!contains) { //The watch list has at least 1 entry so, if the new Assigned to is not on it, the we can add them.
//gs.addInfoMessage("arr has data");
arr.push(contact);
current.watch_list = arr.join();
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-25-2018 05:13 PM
I have the same requirement but I need to add 3 reference fields to my Watch list. Can somebody please assist?