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

Good day Brian,



Thank you so very much again for your input. Your script is spot on!


If I may, can I throw one last question at you and I know I am really starting to push the boundaries of help and should just hire you...


The script you have shared leverages the current user which is perfect in the context of a customer end user submitting an incident.


There are however circumstances where I would for example raise an incident on behalf of a customer and in that scenario my whole company would get added to the list which is not the outcome I want.


I have tried to tweak your script but my knowledge of scripting is just too minimal to tie it together.


What I would like is to retrieve the user value from the u_requestor field in the incident request.



Again, thank you so very much for your help.



Regards,


Colin


Hi Colin,



No problem, and where are you located...



It should be easy enough to adapt to use a user reference field instead of the current user.   It would vary depending on whether you are talking about Incidents (caller_id) or Requests (requested_for).   The simplest would be to setup a BR on each table ([incident], [sc_request], etc.)... You could setup the BR on [task] instead, however you would have to check the task type to account for the differences in the user reference field names.



Using Incidents as an example, the only change needs to occur in which user to use when drilling down to a company ID.   I've bolded the change in this script (for Requests, change this to current.requested_for.company.sys_id😞


Table: [incident]


When: Before (Insert)



//Get all company users who have opted-in to receive such notices


var addUsers = new GlideRecord('sys_user');


addUsers.addQuery('u_opt_in_notify=true^company=' + current.caller_id.company.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


current.watch_list = watchList;





Good luck,



-Brian


Hi Brian,



Awesome, thank you very much, that works perfectly.


We are located in the Sydney CBD. How about you?



With that being in place though what i know realise and what I kind of had a feeling would happen is that Service Now has a default ACL for unlicensed users so that they can only see their own incident records. In my case would i would need is for that ACL to be tied to a company rather than the user but that is just the way it is I guess. Will probably have to think of another work around.



Thank you so much for your help, you have been fantastic.  



Regards,


Colin


Sydney?   Wow, THAT'd be a drive from Los Angeles.  



You can still get to your goal, however. Just add a new ACL granting access to users with the same company as the incident caller's company (or on request, requested_for's company).



Create a new ACL on the table and select "Advanced" to use a script, enter one of the following:


   


                  For Indidents:         answer = (gs.getUser().getCompanyID() == current.caller_id.company.sys_id);



                  For Requests:         answer = (gs.getUser().getCompanyID() == current.requested_for.company.sys_id);



You'd definitely need to add a Read ACL for the table, and possibly a Write ACL on both the table and the watch_list field (depending on how restricted it is) for the user to be able to add their name to the watch lists.




See if that works for you, I haven't had time to test it out specifically but it should work based on what I know of ACLs.




Thanks,


-Brian




Note:   You don't need to alter the existing ACL granting users access to their own created tasks.


Thanks Very much Brian, yes a bit of an ocean between us. A pitty because i owe you a lunch at the very least.


ACL wise i had a bit of a play around but couldn't get it to work. Changing the ACLs didn't still didn't give visibility so there must be something limiting that.


Will have to do a bit more digging but your code works.



Thank you again very much for your help.



Colin