Add person to watch list on Incident

todd_verrastro
Giga Contributor

Hi,

I have a requirement to add a specific person to the Incident watch list when the "Caller" field is populated with certain users. Is this best done with a business rule or client script?

Also, how would you configure it?

Thanks in advance!

Todd

1 ACCEPTED SOLUTION

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

From a performance perspective unless you need it client side as the user is filling out the incident form to populate it, it is best to run it server side via a business rule.



Is this a hard coded person or is this person an attribute of these certain callers?



Create an advanced business rule that runs before insert/update and set the appropriate conditions via condition builder, and then a script like the following should work.   This code will work even if the watch list is already populated.



var userToAdd = "sys_id_of_the_user"; //This is the SysID of the user to add to the watch list



var watch_list = current.watch_list.toString().split(",");



if (watch_list.toString().indexOf(userToAdd) == -1) {


      watch_list.push(userToAdd);


      current.watch_list = watch_list.join(",").toString();


}


View solution in original post

9 REPLIES 9

This did the trick, since it is a specific user to add to the watch list for callers with a specific job title. Just created the BR and it works great.



Interesting point, when I selected both "Insert" and "Update" on the BR form, it didn't "stick" to the BR. I had to add those columns to the BR list view and make both Insert and Update "True" (see pic below).



Thanks Michael!!



find_real_file.png


Awesome glad that worked for you. The only other use case with this is could the caller ever change on the incident and you need to remove that person from the watch list?



Please mark this post with the correct answer so other can benefit in knowing the correct answer to your question.


Sachin65
Tera Expert

can any one explain this code



  1. var userToAdd = "sys_id_of_the_user"; //This is the SysID of the user to add to the watch list  
  2.  
  3. var watch_list = current.watch_list.toString().split(",");  
  4.  
  5. if (watch_list.toString().indexOf(userToAdd) == -1) {  
  6.       watch_list.push(userToAdd);  
  7.       current.watch_list = watch_list.join(",").toString();  
  8. }  

It looks at the current watch list, and if the person to add is not on it, it adds that person.


This is adding someone to a watchlist.


Line by line:


Line 1: var userToAdd = "sys_id_of_the_user";


          You are setting the variable "user to add to a sys_id you eaither already have or are getting earlier in a script.



Line 3: var watch_list = current.watch_list.toString().split(",");


You are getting the values that are currently in the watchlist. Watchlist is a "special" field type in ServiceNow you may not be used to working with. It is a glide_list field. It is like a reference field but stores values in a comma delimited String. So here you are splitting that string and returning an array of substrings.




Line 5: if (watch_list.toString().indexOf(userToAdd) == -1) {


This is saying "If the sys_id is not already in the comma delimited String I referenced above. .indexOf() will return the location of your search value as the index of an array which begins at 0. If it does not find what you are searching for in the String, it will return -1




Line 6: watch_list.push(userToAdd);


Add the user's sys_id to the array we just checked if they are not already in it.




Line 7: current.watch_list = watch_list.join(",").toString();


Here you are rejoining the array you made in line 3 back to the format of the glide_list and updating the glide_list.



So basically, you are removing the data in the list., parsing it out, checking if the data you are trying to insert is already there. If not, we add that data to the array, then convert the array back to a format that ServiceNow expects.