Adding users to watch list during a workflow based on variables entered by user.

jhahn
Mega Expert

Hi Everyone.

This is my first time posting, so apologies if I don't include all the proper information.

We have a service catalog option that I created for requesting a website to be set up. Throughout the workflow 4 different tasks get created (linearly). The catch here is that each task needs to have a different watch list based on what the submitter enters. For example, we request to know the programmers name, projector director, etc. These variables are set up as reference fields to the user table. I tried to search through the site before submitting and I found a few useful things, but I am still unable to get this to work correctly.

To try to accomplish this I have set up a script on the task:

var user_array = [];

var users = new GlideRecord('sys_user');

users.addQuery('name',current.webreq_techcontact);

users.query();

while(users.next()){

  user_array.push(users.sys_id.toSting());

}

if(JSUtil.notNil(current.watch_list)){

  sc_req_item = current.watch_list + ',' + user_array.toString();

}else{

  sc_req_item = user_array.toString();

}

Just a couple more notes, the webreq_techcontact is my variable on the form. We are also trying to set the request item watch list instead of the task one because our users cannot view tasks and we're trying to avoid confusion. Any help would be appreciated!

1 ACCEPTED SOLUTION

jhahn
Mega Expert

I solved this after a suggestion from Michael that is posted below.



I actually found out I did not need to run any queries, as the user was already giving me the value that I needed. I just added the variable to an array and then added the watch_list field from the form onto the array, after checking to see if it was empty. Here is the final code I ended up using:



var user_array = [];




user_array.push(current.variables.webreq_techcontact);




if(JSUtil.notNil(current.variables.watch_list)){


  user_array.push(current.variables.watch_list);


  current.watch_list = user_array.toString();


}else{


  current.watch_list = user_array.toString();


}



Thank you to everyone who responded and provided me with suggestions! I need to remember not to over complicate things


View solution in original post

8 REPLIES 8

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

Variables are accessed by current.variables.VARIABLE-NAME so line 3 should be:


users.addQuery('name',current.variables.webreq_techcontact);


Thank you for the quick response, Michael!



I made the change you suggested, along with another mistake I found but still no luck. Here is my updated code:



var user_array = [];


var users = new GlideRecord('sys_user');


users.addQuery('name',current.variables.webreq_techcontact);


users.query();




while(users.next()){


  user_array.push(users.sys_id.toString());


}



if(JSUtil.notNil(current.variables.watch_list)){


  sc_req_item.watch_list = current.variables.watch_list + ',' + user_array.toString();


}else{


  sc_req_item.watch_list = user_array.toString();


}



Any other suggestions?


How is the webreq_techcontact variable defined?   Is it getting populated with a list of names or user SysIds?   You can always put its value into the log via gs.log("tech contact list: " + current.webreq_techcontact);



Depending on the answer line 3 may need to be edited.   At the very least it likely needs to include "in" since it's a list:


users.addQuery("name", "IN", current.variables.webreq_techcontact);


Thank you for the suggestion, Michael! My reference fields on the form were actually giving me all the information I needed so I did not need to run the query on the glide record. I just pushed the variable directly into the array and then the watch list and set it equal to the current.watch_list and it worked!