- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-14-2014 12:37 PM
I'm trying to add a role to all users who have a certain value for company. So I found the following wiki page script and I am trying to modify it but first of all I'm not sure where to put it for a one time on demand run. I trying putting in in a scheduled job, and a client script with no dice...
http://wiki.servicenow.com/index.php?title=Useful_User_Scripts
And here is my script...
var gr = new GlideRecord("sys_user");
gr.addQuery('company', 'Fulbright & Jaworski L.L.P.');
gr.query();
while(gr.next()) {
if (gr.accumulated_roles.toString().indexOf(",us_users,") == -1) {
gr.roles = gr.roles + ",us_users";
gr.update();
}
Solved! Go to Solution.
- Labels:
-
Integrations
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-15-2014 02:14 AM
company is a related record on the user form so you will need to use the sys_id of the company
change
gr.addQuery('company', 'Fulbright & Jaworski L.L.P.');
to
gr.addQuery('company', 'sys_id of the company');

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-14-2014 12:43 PM
Hi Jared,
You could put that code in an On Demand scheduled job and run it once.
However, if it is going to be run only once, you could do so by elevating the privileges and running the background script.
Thanks,
Mandar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-14-2014 12:48 PM
I already attempted the On Demand scheduled job approach, but that had not worked, What is the second thing you are mentioning? How do I run a background script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-14-2014 06:46 PM
try something like this in the scheduled job instead
var gr = new GlideRecord("sys_user");
gr.addQuery('company', 'Fulbright & Jaworski L.L.P.');
gr.query();
while(gr.next()) {
var myrole = new GlideRecord('sys_user_has_role');
myrole.user = gr.sys_id;
myrole.role = '0d161bdd745155000928dcfea8562f5a'; // change this to be the sys_id of your role
myrole.insert();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-14-2014 09:53 PM
How do I get the sysid of the role