- 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-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 12:25 AM
Thank-you very much Lars, I was unaware of the ArrayUtil - this is now working great.
Best,
D
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2016 12:59 AM
Hi again Lars, if you get 5 minutes spare would you possibly mind breaking down each line of the script for me in terms of what is happening - I have read the Wiki but would like to be sure of what exactly is occurring at each stage for my own understanding. Thank-you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2016 01:06 AM
- We create an array object by splitting the IDs in the watch list comma seperated string into array elements
- We get the sys_id of the user selected in the Contact field
- We create a new object to use the ArrayUtil function
- .
- We use the ArrayUtil object to check if the sys_id of the contact is among the sys_ids in the watch list
- If it is not in the watch list we add the sys_id to the array
- .
- We update the watch list field with the sys_ids we now have in the array. The join() funtion converts the array to a comma seperated string again