- 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-28-2014 12:42 PM
So would basically have to add both of these scripts into one some how?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-29-2014 09:34 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-30-2014 01:18 AM
I think that should work to be honest, the other thing I might try is just doing the role check in a separate function so you can just return if you find the role...
var gr = new GlideRecord('sys_user');
gr.addQuery('company','company_sys_id');
gr.query();
while (gr. next()){
gs.log('checking for role against user ' + gr.user_name);
checkuserhasrole();
}
function checkuserhasrole(){
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");
return;
} else {
gs.log('cannot find user with role so adding ');
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
‎03-17-2016 05:42 AM
Hi mguy,
can u please tellme what is the role of accumulated_roles field? where does it get its value populated from?
thanks,
ramya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-30-2014 09:38 AM
The final script I had posted an image of was correct like you said, I just had to confirm the Sys_IDs... talk about tedious stuff!! But I'm getting there