To auto populate group members from security group to a new group

kumaar
Tera Contributor

Hi all i have a requirement
There is a security group named as Sg-grp and a new group is created where the group members of security group is auto populated in new group.
It should work taht way when i new member is added to security group it also added to new group and vice versa.

 

3 REPLIES 3

Appanna M
Tera Guru

Hello @kumaar ,

 

Try with business rules.

Create two Business Rules, one for each group, to handle the group members are in sync. For example, you could create a Business Rule for the "Sg-grp" group to trigger when a new member is added or removed. Similarly, create another Business Rule for the new group to handle membership changes. 

Inside your business rule you can call the same script include fun for addition and removal of the group members. 

 

Please Mark My Answer as Helpful and Accept as Solution, if you find this article helpful or resolves your issue.

kumaar
Tera Contributor

HiAppanna

Can you help me with the script...

I tried but failed

kumaar
Tera Contributor

(function executeRule(current, previous /*null when async*/) {
// Check if the group is Sg-grp or New_Group
if (current.name == 'Sg-grp' || current.name == 'New_Group') {
// Get members of the current group
var members = [];
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('group', current.sys_id);
gr.query();
while (gr.next()) {
members.push(gr.user.toString());
}

// Update members of the other group
var otherGroupName = (current.name == 'Sg-grp') ? 'New_Group' : 'Sg-grp';
updateGroupMembers(otherGroupName, members);
}

function updateGroupMembers(groupName, members) {
var gr = new GlideRecord('sys_user_group');
gr.addQuery('name', groupName);
gr.query();
if (gr.next()) {
gr.setValue('members', members.join(','));
gr.update();
}
}
})(current, previous);