We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

How to show group member by selecting choice variable

jui
Tera Contributor

Hi All,

I have below requirement kidly let me know how to resolve it

I have one catalog item in witch there are 2 variables available 

1 has 2 choices called A & B and another one is reference

If A is selected then menber of software group will be shown in reference field means in another variable and if B choice will be selected then all users from user table will be shown.

jui_0-1766493139185.png

 

3 REPLIES 3

Ankur Bawiskar
Tera Patron

@jui 

add this in advanced ref qualifier

Note: Ensure you give correct choice variable name, give correct choice value to compare

I assume your reference variable refers to sys_user_grmember

javascript: var choiceValue = current.variables.choiceVariableName;
var query = '';
if(choiceValue == 'A'){
query = 'group.nameINSoftware'; // give the group name here
}
query;

If your reference variable refers to sys_user then use this

javascript: var choiceValue = current.variables.choiceVariableName;
var query = '';
if (choiceValue == 'A') {
    var groupSysId = 'your_group_sys_id_here'; // Replace with actual sys_id
    var members = [];
    var gr = new GlideRecord('sys_user_grmember');
    gr.addQuery('group', groupSysId);
    gr.query();
    while (gr.next()) {
        members.push(gr.getValue('user'));
    }
    query = 'sys_idIN' + members.toString();
}
query;

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@jui 

Hope you are doing good.

Did my reply answer your question?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

anandmahaja
Kilo Sage

Hi @jui,

 

You can use Advanced Reference Qualifier on Reference field to achieve this requirement.

You also need to create a script include to get the group members of "Software" group.

Advanced Reference Qualifier:

javascript:
var query;
var groupName = <group_sys_id>;
var membershipUtils = new global.GroupMemberUtils();

if (current.variables.choice_variable == "A") {
query = 'active=true^sys_idIN' + membershipUtils.getMembersByGroupId(groupName);
} else {
query = 'active=true';
}
query;

 

Sample Script Include:

var GroupMemberUtils = Class.create();

GroupMemberUtils.prototype = {
    initialize: function () {},
    getMembersByGroupId: function (groupSysId) {
        var members = [];
        if (!groupSysId) {
            return members;
        }
        var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery('group', groupSysId);
        gr.query();
        while (gr.next()) {
            members.push(gr.user.toString());
        }
        return members;
    },

    type: 'GroupMemberUtils'

};

 

 

Thanks

Anand