Automatically add role to group via script

ejaram
Kilo Expert

Hello,

I am working on a script to create a group, add users to the group, set the manager, and add a role to the group.

I have the group creation, user add, and manager set working fine however I cannot get the role portion to work. I am not sure how to manipulate the 'sys_user_group' table to add the role to the group (since the 'Roles' is a related list on the table).

Anyone have any experience with this or how to go about it?

Thanks in advance,
Evan

1 ACCEPTED SOLUTION

TJW2
Mega Guru

The 'sys_group_has_role' table is where this information is stored. You will need to store the reference to the Group and the reference to the Role in this table.


View solution in original post

5 REPLIES 5

TJW2
Mega Guru

The 'sys_group_has_role' table is where this information is stored. You will need to store the reference to the Group and the reference to the Role in this table.


Well you just made everything 10000x easier. Thanks a ton!


Aaron40
Kilo Guru

Hi Evan,

You'll need to be looking at the sys_group_has_role table. All you need to do is create a new record on here with the group sysid and the role sysid.


yjaques
Tera Contributor

Just in the interest of getting more scripts posted on this wiki, here's some code I was using to find a certain group, add it if it doesn't exist and otherwise remove all its members and then add one or more new members. Someone might find it useful:



//get the system id of the group and if it doesn"t exist create it


var group = "Nexams";


var groupExists = false;



var groupRecord = new GlideRecord("sys_user_group");


groupRecord.query();


while (groupRecord.next()) {


      if (groupRecord.name == group) {


              groupExists = true;


              var group_sys_id = groupRecord.sys_id;


              logFields(groupRecord);


              break;


      }


}


//doesn"t exist so create it


if (!groupExists) {


      gs.addInfoMessage("No existing group found!");


      groupRecord.initialize();


      groupRecord.name = group;


      var group_sys_id = groupRecord.insert();


      if (group_sys_id != null) {


              gs.addInfoMessage("Created new group: " + group_sys_id);


      } else {


              gs.addInfoMessage("Failed to create new group!");


      }


} else {


      //wipe the group clean of existing members


      var groupUserRecord = new GlideRecord("sys_user_grmember");


      groupUserRecord.query();


      while (groupUserRecord.next()) {


              if (groupUserRecord.group.sys_id == group_sys_id) {


                      logFields(groupUserRecord);


                      var result = "Deleted user record.";


                      if (!groupUserRecord.deleteRecord()) {


                              result = "Failed to delete user record!";


                      }


                      gs.addInfoMessage(result);


              }


      }


}



//in this example we will now add user "jaques" as a member to the group. In essence you can just call this function as many times as you want with an array of user ids.


var uid = "jaques";


addUserToGroup(uid);



function addUserToGroup(uid) {


      var userRecord = new GlideRecord("sys_user");


      userRecord.query();


      while (userRecord.next()) {


              if (userRecord.user_name == uid) {


                      // we found the user so create the relationship


                      groupUserRecord.initialize();


                      groupUserRecord.group = group_sys_id;


                      groupUserRecord.user = userRecord.sys_id;


                      if (groupUserRecord.insert() != null) {


                              gs.addInfoMessage("Successfully added user to group: " + userRecord.getDisplayValue());


                      } else {


                              gs.addInfoMessage("Failed to add user to group!");


                      }


              }


      }


}



function logFields(ci) {


      gs.addInfoMessage("==========================================");


      gs.addInfoMessage("Configuration Item: " + ci.getDisplayValue());


      gs.addInfoMessage("Sys ID: " + ci.sys_id);


      //get all the fields and print them


      var fields = ci.getFields();


      gs.addInfoMessage("Field values:");


      for (var i = 0; i < fields.size(); i++) {


              var glideElement = fields.get(i);


              if (glideElement.hasValue()) {


                      gs.addInfoMessage(glideElement.getName() + ":               " + glideElement);


              }


      }


}