Script to add role to group

jared_wentzel
Kilo Contributor

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.

1 ACCEPTED SOLUTION

Mike Allen
Mega Sage

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();


View solution in original post

9 REPLIES 9

Use an if condition with gs.getProperty("instance_name")



if (gs.getProperty("instance_name") == 'mycompanydev') {


        // execute this


}


marcguy
ServiceNow Employee
ServiceNow Employee

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();
}

jared_wentzel
Kilo Contributor

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!!


marcguy
ServiceNow Employee
ServiceNow Employee

you would have to do another quick query to check the table for an entry for that group/user first



  1. var user = new GlideRecord('sys_user');  
  2. if(user){  
  3. user.addQuery('location','57fd7b993790200044e0bfc8bcbe5d19');  
  4. user.query();  
  5. //var currentUser = user.getDisplayValue();  
  6. while(user.next()) {  
  7. var grmem = new GlideRecord('sys_user_grmember');
  8. grmem.addQuery('group','b85d44954a3623120004689b2d5dd60a');
  9. grmem.addQuery('user',user.sys_id);
  10. grmem.query();
  11. if (!grmem.hasNext()){
  12.   var member = new GlideRecord('sys_user_grmember');  
  13.   member.initialize();  
  14.   member.user = user.name.getDisplayValue();  
  15.   member.group = 'b85d44954a3623120004689b2d5dd60a';  
  16.   member.insert();  
  17. }  
  18. }

pawan k singh
Tera Guru

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