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

Sujatha V M
Kilo Patron
Kilo Patron

@Insider Please refer the below link for your requirement. 

 

https://www.servicenow.com/community/itsm-forum/catalog-item-variable-how-to-select-user-from-member...

 

Alternatively you can even pass the variable V1 inside the function as below on the V2 variable as reference qualifier using User table, 

 

javascript &colon; new <Script Include>.<function name>(current.variables.requested_for);

 

 

Please Note: It's not a best practice to use "GlideRecord" in client scripts directly. You must go with GlideAjax only. 

 

https://developer.servicenow.com/dev.do#!/guides/utah/now-platform/tpb-guide/client_scripting_techni...

 

Please mark this as helpful and accept it as a solution if this resolves your query.

Thanks,

Sujatha V.M.

 

Please mark this as helpful and accept it as a solution if this resolves your query.
Sujatha V.M.

Hello,

 

V1 and V2 are not dependent. 

they are independent variables.

 

V1 is a select box with choices.

V2 refers to Group table.

 

Deepak Shaerma
Kilo Sage

Hi @Insider 
As per your requirement, you need to Create New- onChange Catalog Client Script

1. Type-> onChange

2. Variable-> V1

 

Script:

 

var groupName = newValue + "Group"; // Constructs the group name based on the selection, e.g., “TYPE1Group”

    // Clear previous options
    g_form.clearOptions('V2');

    // Query to get the sys_id of the group
    var groupIdQuery = new GlideRecord('sys_user_group');
    groupIdQuery.addQuery('name', groupName);
    groupIdQuery.query();
    if (groupIdQuery.next()) {
        var groupId = groupIdQuery.sys_id;
        
        // Now, get the group members
        var memberQuery = new GlideRecord('sys_user_grmember');
        memberQuery.addQuery('group', groupId);
        memberQuery.query();
        while (memberQuery.next()) {
            var userId = memberQuery.user;
            
            // Get user details
            var userRec = new GlideRecord('sys_user');
            if (userRec.get(userId)) {
                var userName = userRec.name;
                var userSysId = userRec.sys_id.toString();
                
                // Add this user as an option to V2
                g_form.addOption('V2', userSysId, userName);
            }
        }
    }

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 Deepak,

 

V1 and V2 are not dependent. 

they are independent variables.

 

V1 is a select box with choices like Type, Change and etc...

V2 refers to Group table.