How can a script adding users to the watcher list

colinmorgan
Kilo Contributor

Good day all,

I am looking for a bit of advice/help with a script as I am pretty clueless with scripting; here is some context for what I am after:

We are using Service Now in a multi-tenanted environment where our customers are a range of different organisation with a number of users in them who use the self service portal to log incidents and interact with us.

In this scenario, all users within an organisation are separate and can only see their own incident records. Most of our organisations include a number of users who work together and need to be able to collaborate on Incident records. Service Now has the concept of watchers which would do the trick however I cannot make that function available to customer users as they can potentially lookup all of our users across all organisations which is something that we can't have happen.

What I would like to achieve if have a boolean field in the user records that determines whether that user wants to see all tickets across the organisation or not. If they chose to see it all, I would like a business rule to add all those users to the watcher list upon the creation of the incident.

Setting this up is pretty straightforward up to the point of the script bit which would have to lookup the users assigned to the organisation   that have that flag active and then add them to the watcher list.

Has anyone done something like this or maybe has an easy alternative i have not thought about and could share their ideas?

Many thanks in Advance!

Colin

18 REPLIES 18

If you're going t Knowledge16, we can hit the cafeteria line.  




-Brian


Hey Brian, I would like to get in touch with you outside this forum, if you are comfortable with that can you email me at colin.morgan@bluecentral.com?


Was hoping to make it to Knowledge16 but will be on leave before it draws to a close unfortunately.


Cheers.


Colin


Brian, We are wanting to move the assigned_to person in sc_task to the Request level   watch list. I have been trying to adapt your script. Can you help? We are very new to ServiceNow



I know I want create a Before Insert BR on sc_task, but having trouble with how to get the assigned_to user added to the Request's Watch List. Appreciate any help!


Brian, We are wanting to move the assigned_to person in sc_task to the Request level   watch list. I have been trying to adapt your script. Can you help? We are very new to ServiceNow



I know I want create a Before Insert BR on sc_task, but having trouble with how to get the assigned_to user added to the Request's Watch List. Appreciate any help!



So far I have this script. It works, EXCEPT, it puts everyuser into the Watch List, not just the assigned_to user from sc_task...



(function executeRule(current, previous /*null when async*/) {




  // Add your code here


  var gr = new GlideRecord('sc_request');


  gr.get(current.request_item.request);


var addUsers = new GlideRecord('sys_user');


addUsers.addQuery(current.assigned_to.sys_id);


addUsers.query();



//Build the watch list as a comma separated string of sys_ids


var watchList = "";


var sep = "";


while(addUsers.next()){


        watchList += sep + addUsers.sys_id;


        sep = ",";    


}


//Populate the watch list on the current INC record


gr.watch_list = watchList;


gr.update();




})(current, previous);


Hi William,



If you are getting ALL users as a result in your query, it's usually an indication that your query condition is invalid.   In your case, I think you need to look at your addQuery() statement.   If you read here, using it with a single argument will expect an encoded query string and not just a value/sys_id:



http://wiki.servicenow.com/index.php?title=GlideRecord#addQuery




Try adjusting your script like this (in bold😞



(function executeRule(current, previous /*null when async*/) {




// Add your code here


var gr = new GlideRecord('sc_request');


gr.get(current.request_item.request);


var addUsers = new GlideRecord('sys_user');


addUsers.addQuery('sys_id', current.assigned_to.sys_id);


addUsers.query();



//Build the watch list as a comma separated string of sys_ids


var watchList = "";


var sep = "";


while(addUsers.next()){


        watchList += sep + addUsers.sys_id;


        sep = ",";    


}


//Populate the watch list on the current INC record


gr.watch_list = watchList;


gr.update();




})(current, previous);




See how that works for you. (don't be afraid to mark Helpful, or Super Helpful, if that's the right solution... )




Thanks,


-Brian