Show Group members based on On Change of choices from other variable.

Insider
Giga Guru

Hello Everyone,

 

I am trying to auto populate group members in a variable based on other variable choices.

EX: We have 2 variable V1 and V2

 

V1 has 3 choices

Choices: TYPE1, TYPE2, TYPE3

 

When V1 is selected as = TYPE1

the Variable V2 should show list group members who are part of "TYPE1Group"

 

When V1 is selected as = TYPE2

the Variable V2 should show list group members who are part of "TYPE2Group"

 

When V1 is selected as = TYPE3

the Variable V2 should show list group members who are part of "TYPE3Group"

 

Please help me with this functionality.

8 REPLIES 8

Hello @Insider 
got your point, you have to go with client script and some server side scripting as well, because of the independent nature of your V1 and V2 fields, 
1. Create onChange client script for V1

 

var groupName = newValue + 'Group'; // Construct the group name based on selection
    // Call a function to populate V2 with group members
    getGroupMembers(groupName);
}

function getGroupMembers(groupName) {
    // Use GlideAjax to call the server-side function
    var ga = new GlideAjax('GetGroupMembersAjax');
    ga.addParam('sysparm_name', 'getMembers');
    ga.addParam('sysparm_group_name', groupName);
    ga.getXMLAnswer(function(response) {
        populateV2(response);
    });
}

function populateV2(membersStr) {
    var members = membersStr.split(',');
    var selectBox = g_form.getGlideUIElement('V2');
    selectBox.clearChoices();
    
    members.forEach(function(member) {
        var parts = member.split(':');
        var userId = parts[0];
        var userName = parts[1];
        if(userId && userName) {
            selectBox.addOption(userId, userName);
        }
    });

 


2. Script Include
Name : GetGroupMembersAjax (as it is previously used in client script- give same name)

Accessible from “All application scopes”.

 

 

var GetGroupMembersAjax = Class.create();
GetGroupMembersAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getMembers: function() {
        var groupName = this.getParameter('sysparm_group_name');
        var groupGr = new GlideRecord('sys_user_group');
        if (groupGr.get('name', groupName)) {
            var groupMemberGr = new GlideRecord('sys_user_grmember');
            groupMemberGr.addQuery('group', groupGr.getUniqueValue());
            groupMemberGr.query();
            var members = [];
            while (groupMemberGr.next()) {
                var userGr = new GlideRecord('sys_user');
                if (userGr.get(groupMemberGr.getValue('user'))) {
                    members.push(userGr.getUniqueValue() + ':' + userGr.getValue('name'));
                }
            }
            return members.join(',');
        }
        return '';
    }
});

 

Modify the script and names according to your requirement.

Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards 
Deepak Sharma

hello,

 

So, can't we just g_form.setValue? for the V2? to showcase the group members.

But this is different set condition for different V1 choices.

Hello Deepak,

 

I managed to create a sync between the V3 and V2.

 

V3 holds the group names and V2 should showcase the group members.

 

What i did is when V1 = Type 1, i setValue inside V3 with a group name.

Now based on V3 can i populate the V2 choices? as V3 is holding the Group name?

I tried this and i see all users in the V2 variable. But i need specific group members which i select from V1.