- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-23-2014 12:13 PM
Does anyone have an example script I can use to add a role to a group. I need to add this as a clone script and I am having a hard time finding any examples to get started with. Basically just need to add the 'admin' role to a group called 'CS-ServiceNow Administrators'. Any help would be greatly appreciated.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-23-2014 12:20 PM
var role = new GlideRecord('sys_group_has_role');
role.initialize();
role.group = '<sys_id of group>';
role.role = '<sys_id of admin role>';
role..inherits = true;
role.insert();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-01-2014 10:06 PM
Use an if condition with gs.getProperty("instance_name")
if (gs.getProperty("instance_name") == 'mycompanydev') {
// execute this
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-02-2014 01:17 AM
I would normally query to make sure the group doesn't already have the role as well, just makes it easier to stop groups getting duplicate role records and getting messy:
function addGroupRole(rl) { | ||
var rec = new GlideRecord('sys_group_has_role'); | ||
rec.addQuery('role', 'sys_id'); | ||
rec.addQuery('group', 'sys_id'); | ||
rec.query(); | ||
if (!rec.next()) { | ||
rec.initialize(); | ||
rec.role= 'sys_id'; | ||
rec.group = 'sys_id'; | ||
rec.insert(); | ||
} |
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-02-2014 08:25 AM
Thanks Geoffrey! That did it! My script is complete and works like a charm
And mguy, Since that group will never have the admin role in production, when cloning they will never have the admin role until the script grantsit to them. That information will however come in handy for future scripts. Thank you!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-01-2016 02:26 AM
you would have to do another quick query to check the table for an entry for that group/user first
- var user = new GlideRecord('sys_user');
- if(user){
- user.addQuery('location','57fd7b993790200044e0bfc8bcbe5d19');
- user.query();
- //var currentUser = user.getDisplayValue();
- while(user.next()) {
- var grmem = new GlideRecord('sys_user_grmember');
- grmem.addQuery('group','b85d44954a3623120004689b2d5dd60a');
- grmem.addQuery('user',user.sys_id);
- grmem.query();
- if (!grmem.hasNext()){
- var member = new GlideRecord('sys_user_grmember');
- member.initialize();
- member.user = user.name.getDisplayValue();
- member.group = 'b85d44954a3623120004689b2d5dd60a';
- member.insert();
- }
- }

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-23-2021 08:58 AM
I used below query to add role to a group.
var groupQuery = // Add your encoded query for the groups you want to update;
var grp = new GlideRecord('sys_user_group');
grp.addEncodedQuery(groupQuery);
grp.query();
while(grp.next()){
gs.info(grp.name);
var role = new GlideRecord('sys_group_has_role');
role.initialize();
role.group = grp.sys_id;
role.role = // Sys_id of the role you want to add;
role.inherits = true;
role.insert();
}
Please mark my answer helpful, if it helped you.
Regards
Pawan K Singh