Business Rule to Auto Populate Group Members

RWHeals
Tera Contributor

Been trying and failing at scripting this business rule, so I'm here for help. 

 

I have a customer field on the group table  "u_supervisors" that is a list field referencing the user table.  On a group record, whenever a user is added to this list field, I want them automatically added to the Group Member related list ("sys_user_grmember") of this group.  So far I had the business rule triggering when Supervisors changes, after update. 

 

My script doesnt seem to do anything, any help appreciated:  

 

RWHeals_0-1690579807988.png

 

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi @RWHeals ,

 

As I understand you will want to adjust (add or remove) members from the group whenever Supervisors field get changes, correct?

 

How about if the member get removed or added manually not through Supervisors field? Will that field reflect the change also? If not it will be a mess with Group members table.

 

Anyway, I will give you the BR script that will help to adjust members base on the Supervisors field. Any manual changes will not reflect the field value.

Steps:

1. Check if the field is empty => remove all members from Group.

2. If not, get all existing group members (in sys_user_grmember).

3. Compare with current field value to decide which member to add/remove.

 

Business Rule (Insert and Update) - on After - Condition: [Supervisors changes]

(function executeRule(current, previous /*null when async*/ ) {
	
	//  === If the field is empty then remove all group members ===
    if (current.u_supervisors.nil()) {
        var member = new GlideRecord('sys_user_grmember');
        member.addQuery('group', current.sys_id.toString());
        member.query();
        while (member.next()) {
            member.deleteRecord();
        }
        return;
    }
	// === END ===
	
    var existingMem = [];
    var addItem = [];
    var removeItem = [];
    var currentSup = current.u_supervisors.toString().split(',');
	
	//  === Get all existing group members ===
    var grmember = new GlideRecord("sys_user_grmember");
    grmember.addquery("group", current.sys_id.toString());
    grmember.query();
    while (grmember.next()) {
        existingMem.push(grmember.user.sys_id.toString());
    }
	// === END ===
	
    var arrayToLoop =
        existingMem.length > currentSup.length ? existingMem : currentSup;

    for (var i = 0; i < arrayToLoop.length; i++) {
        if (existingMem.indexOf(currentSup[i]) == -1) {
            if (currentSup[i]) {
                addItem.push(currentSup[i]);
            }
        }
        if (currentSup.indexOf(existingMem[i]) == -1) {
            if (existingMem[i]) {
                removeItem.push(existingMem[i]);
            }
        }
    }

    if (removeItem.length) { // REMOVE members
        removeItem.forEach(function(itemOut) {
            var member = new GlideRecord('sys_user_grmember');
            member.addQuery('user', itemOut);
            member.addQuery('group', current.sys_id);
            member.setLimit(1);
            member.query();
            if (member.next()) {
                member.deleteRecord();
            }
        });
    }
    if (addItem.length) { // ADD members
        addItem.forEach(function(itemAdd) {
            if (itemAdd) {
                var member = new GlideRecord('sys_user_grmember');
                member.addQuery('user', itemAdd);
                member.addQuery('group', current.sys_id);
                member.query();
                if (!member.next()) {
                    member.initialize();
                    member.group = current.sys_id;
                    member.user = itemAdd;
                    member.insert();
                }
            }
        });
    }


})(current, previous);

 

Tell me if you need to adjust any logic.

View solution in original post

5 REPLIES 5

Bert_c1
Kilo Patron

Hi your script has a reference to 'current.sys_user_group', 'sys_user_group' is not a field on the 'sys_user_group' table, seems a problem in a business rule that runs on sys_user_group table.  Start there and update your script as needed.

DanielCordick
Mega Patron
Mega Patron

all that related list is pointing to is an m2m table that has 2 reference fields, Group and User, In this case you just need to add the Supervisor to the group. when the supervisor is added to the group they will show up in the sys_user_grmember list. 

 

can you copy and past your script in into the thread?

 

line 3 wont get you the existing group member, you will need to do a GlideRecord Look up on the sys_user_grmember table, the query would be something like ('group', 'sys_id') -- // group is a reference field on sys_user_grmember

from here you should be able to get the list of existing users in the group. maybe push them into an array and then check the array for the supervisor . 

with all that said, why not just manually add the supervisor to group when your adding the user to the supervisor field? 

secondly, this will break if your groups are maintained VIA LDAP. It would be removing users not contained in the AD group. It would lead to a bad user experience and more tickets to help desk with lost access to so and so 

 

 

 

 

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response helped in any way 

 

Community Alums
Not applicable

Hi @RWHeals ,

 

As I understand you will want to adjust (add or remove) members from the group whenever Supervisors field get changes, correct?

 

How about if the member get removed or added manually not through Supervisors field? Will that field reflect the change also? If not it will be a mess with Group members table.

 

Anyway, I will give you the BR script that will help to adjust members base on the Supervisors field. Any manual changes will not reflect the field value.

Steps:

1. Check if the field is empty => remove all members from Group.

2. If not, get all existing group members (in sys_user_grmember).

3. Compare with current field value to decide which member to add/remove.

 

Business Rule (Insert and Update) - on After - Condition: [Supervisors changes]

(function executeRule(current, previous /*null when async*/ ) {
	
	//  === If the field is empty then remove all group members ===
    if (current.u_supervisors.nil()) {
        var member = new GlideRecord('sys_user_grmember');
        member.addQuery('group', current.sys_id.toString());
        member.query();
        while (member.next()) {
            member.deleteRecord();
        }
        return;
    }
	// === END ===
	
    var existingMem = [];
    var addItem = [];
    var removeItem = [];
    var currentSup = current.u_supervisors.toString().split(',');
	
	//  === Get all existing group members ===
    var grmember = new GlideRecord("sys_user_grmember");
    grmember.addquery("group", current.sys_id.toString());
    grmember.query();
    while (grmember.next()) {
        existingMem.push(grmember.user.sys_id.toString());
    }
	// === END ===
	
    var arrayToLoop =
        existingMem.length > currentSup.length ? existingMem : currentSup;

    for (var i = 0; i < arrayToLoop.length; i++) {
        if (existingMem.indexOf(currentSup[i]) == -1) {
            if (currentSup[i]) {
                addItem.push(currentSup[i]);
            }
        }
        if (currentSup.indexOf(existingMem[i]) == -1) {
            if (existingMem[i]) {
                removeItem.push(existingMem[i]);
            }
        }
    }

    if (removeItem.length) { // REMOVE members
        removeItem.forEach(function(itemOut) {
            var member = new GlideRecord('sys_user_grmember');
            member.addQuery('user', itemOut);
            member.addQuery('group', current.sys_id);
            member.setLimit(1);
            member.query();
            if (member.next()) {
                member.deleteRecord();
            }
        });
    }
    if (addItem.length) { // ADD members
        addItem.forEach(function(itemAdd) {
            if (itemAdd) {
                var member = new GlideRecord('sys_user_grmember');
                member.addQuery('user', itemAdd);
                member.addQuery('group', current.sys_id);
                member.query();
                if (!member.next()) {
                    member.initialize();
                    member.group = current.sys_id;
                    member.user = itemAdd;
                    member.insert();
                }
            }
        });
    }


})(current, previous);

 

Tell me if you need to adjust any logic.

This worked well, thank you much! However, oddly enough whenever I tested with user "Abraham Lincoln", he would not get populated. I can't think of why it wouldn't work for this one user.