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

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


That's exactly what I needed, thank you so much!


Mike, I used this below script to add users to a group if they are from a particular location.



var user = new GlideRecord('sys_user');


if(user){


user.addQuery('location','57fd7b993790200044e0bfc8bcbe5d19');


user.query();


//var currentUser = user.getDisplayValue();


while(user.next()) {


  var member = new GlideRecord('sys_user_grmember');


  member.initialize();


  member.user = user.name.getDisplayValue();


  member.group = 'b85d44954a3623120004689b2d5dd60a';


  member.insert();


}


Is there a way to ignore the users that are already added to that group & make sure only new users are added ?


jared_wentzel
Kilo Contributor

So I have this running on a Clone - Cleanup Script. Does anyone know how to make it only run when the clone is to a certain environment? So basically have it running after a clone if the clone is to our dev instance.