- 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-15-2014 09:06 AM
Ahhh that was it! I had to use the sys_id for the company value... Geez it's always the little things that will trip you up! Thank you!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-24-2014 02:11 PM
The script I'm using works and I have it as a scheduled job but I realized I keeps on adding the roles over and over again so one user will have the same role multiple times. So I need to add a if statement to it but when I came up with isn't working.. Any ideas?
var gr = new GlideRecord("sys_user");
gr.addQuery('company', '818c19b66fff55003ac289871e3ee4b1');
gr.query();
while(gr.next()) {
if (gr.accumulated_roles.toString().indexOf(",us_users,") == -1) { // IF statement added that it not working
var myrole = new GlideRecord('sys_user_has_role');
myrole.user = gr.sys_id;
myrole.role = 'b1e8e21b643521006601c2c80d6c04bf';
myrole.insert();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-28-2014 01:15 AM
I normally do a query to see if it exists already...
var myrole= new GlideRecord("sys_user_has_role");
myrole.addQuery("user", gr.sys_id);
myrole.addQuery("role", 'b1e8e21b643521006601c2c80d6c04bf');
myrole.query();
if (myrole.next()) {
gs.log(" already has the group_admin role for the group - not adding");
} else {
myrole.initialize();
myrole.user = gr.sys_id;
myrole.role = 'b1e8e21b643521006601c2c80d6c04bf;
myrole.insert();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-28-2014 07:56 AM
The code you're showing doesn't include the initial logic I need to have for the company field on the sys_user table. I don't want to add this role to everyone..
So basically I would need to have both conditions, if the users company is '818c19b66fff55003ac289871e3ee4b1' and if they do not already have the role.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-28-2014 08:58 AM
yes sorry, just wanted to show you the solution is to query for the record, if it exists don't create it again.