How to add a user to watch list from existing field on INC form

jas101
Tera Expert

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

1 ACCEPTED SOLUTION

larstange
Mega Sage

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();


View solution in original post

7 REPLIES 7

larstange
Mega Sage

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();


Thank-you very much Lars, I was unaware of the ArrayUtil - this is now working great.


Best,


D


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.


larstange
Mega Sage
  1. We create an array object by splitting the IDs in the watch list comma seperated string into array elements
  2. We get the sys_id of the user selected in the Contact field
  3. We create a new object to use the ArrayUtil function
  4. .
  5. We use the ArrayUtil object to check if the sys_id of the contact is among the sys_ids in the watch list
  6. If it is not in the watch list we add the sys_id to the array
  7. .
  8. 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