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

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Jerry,



I see error in line 3. Please replace current.webreq_techcontact with current.variables.webreq_techcontact and check the result.


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


}  


 


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


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


}else{  


  sc_req_item = user_array.toString();  


}  




Hi Pradeep,



Thank you for your quick response! I made the changes as suggested and still no luck. I found another error that I fixed too. Here is what the updated code is:



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?


In line 8 try to not convert sys_id to string and see if it works that way



And if it does dot help - try to change check condition for if starement on something like .... watch_list != ''


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