How can a new child group inherit the roles and members of a parent group once attached?

Tee Fortune
Kilo Explorer

Hello everyone,

I was tasked with syncing parent and child groups for the child to inherit the roles and members of the parent group. The BR inserted worked however when a new child group was created and attached to the parent group, it did not inherit the members/users but only inherited the roles of the parent group? 

var childArr = [];

var thisUser = current.getValue('user');

var grp = new GlideRecord('sys_user_group');

grp.addEncodedQuery('active=true^parent=' + current.getValue('group'));

grp.query();

gs.print('count=' + grp.getRowCount());

 while (grp.next()) {
childArr.push(grp.getValue('sys_id'));
gs.print(grp.getDisplayValue());
}

 // Add user to the child groups

if (current.operation() == 'insert') {
gs.print('INSERT');
for (var i = 0; i < childArr.length; i++) {
if (userInGroup(thisUser, childArr[i])) {
gs.print('user is already in the group ' + childArr[i]);
continue;
}
addUserToGroup(thisUser, childArr[i]);
}
}

  // Remove user from child groups

if (current.operation() == 'delete') {
gs.print('DELETE');
for (var i = 0; i < childArr.length; i++) {
if (!userInGroup(thisUser, childArr[i])) {
gs.print('user is NOT in the group ' + childArr[i]);
continue;
}
deleteUserFromGroup(thisUser, childArr[i]);
}

function userInGroup(user, group) {
var ug = new GlideRecord('sys_user_grmember');
var q = 'user=' + user + '^group=' + group;
ug.addEncodedQuery(q);
ug.query();
gs.print('userInGroup(): q=' + q + ' count=' + ug.getRowCount());
return ug.hasNext();
}

 function deleteUserFromGroup(user, group) {
if (!user)
return;
if (!group)
return;
var ug = new GlideRecord('sys_user_grmember');
var q = 'user=' + user + '^group=' + group;
ug.addEncodedQuery(q);
ug.query();
gs.print('deleteUserFromGroup(): q=' + q + ' count=' + ug.getRowCount());
if (ug.next()) {
gs.print('delete');
ug.deleteRecord();
}
}

function addUserToGroup(user, group) {
if (!user)
return;
if (!group)
return;
var ug = new GlideRecord('sys_user_grmember');
ug.newRecord();
ug.user = user;
ug.group = group;
ug.insert();
gs.print('insert');
}

3 REPLIES 3

Community Alums
Not applicable

Did you tried to use gr.setForceUpdate(true)? It updates the record even if fields have not changed.

Hello Rafael,

How would I do that? Also, would it update/sync a new child group when associated with an established parent group? I am quite new to this and I appreciate the suggestions.

Parneet1
Tera Expert

Hi Tee, did you get the solution to your problem? Seems like the role and group hierarchy are opposite to each other, i.e. the child group inherits the parent's roles and the parent group inherits the child's group members.