Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Reference Qualifier not working

Prithvi Ramesh1
Mega Sage
  • User Selection: The form allows selection of multiple users.
  • Group Filtering: Based on the selected users, the Group field should dynamically filter and show only the groups that are common to all selected users.
  • Validation Logic: This ensures that users are not removed from groups they are not part of, avoiding errors.

Script Include:

 

var getCommonGroups = Class.create();
getCommonGroups.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getCommonGroups: function(userList) {
        gs.info("PRITHVI: Starting getCommonGroups with userList = " + userList);

        var users = userList.split(',');
        var groupCount = {};
        var commonGroups = [];

        for (var i = 0; i < users.length; i++) {
            gs.info("PRITHVI: Checking groups for user = " + users[i]);

            var gr = new GlideRecord('sys_user_grmember');
            gr.addQuery('user', users[i]);
            gr.query();
            while (gr.next()) {
                var groupId = gr.group.toString();
                gs.info("PRITHVI: Found group = " + groupId + " for user = " + users[i]);

                groupCount[groupId] = (groupCount[groupId] || 0) + 1;
            }
        }

        for (groupId in groupCount) {
            gs.info("PRITHVI: Group " + groupId + " has count = " + groupCount[groupId]);

            if (groupCount[groupId] == users.length) {
                commonGroups.push(groupId);
                gs.info("PRITHVI: Group " + groupId + " is common to all users");
            }
        }

        var result = commonGroups.join(',');
        gs.info("PRITHVI: Returning common groups = " + result);
        return result;
    },

    type: 'getCommonGroups'
});

 

Reference Qualifier: 

 

javascript&colon; 'sys_idIN' + getCommonGroups(current.variables.requestor);

 

Issue: Wfter selecting the user even though is part of the group the list does not show any groups and even the logs is empty.

 

PrithviRamesh1_0-1751904668038.png

 

 

1 ACCEPTED SOLUTION

Chaitanya ILCR
Mega Patron

Hi @Prithvi Ramesh1 

javascript&colon; 'sys_idIN' + new getCommonGroups().getCommonGroups(current.variables.requestor);

,

update the ref qual as this

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

View solution in original post

3 REPLIES 3

Chaitanya ILCR
Mega Patron

Hi @Prithvi Ramesh1 

javascript&colon; 'sys_idIN' + new getCommonGroups().getCommonGroups(current.variables.requestor);

,

update the ref qual as this

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

Hello @Chaitanya ILCR ,

I tried updating the reference qualifier using:

 
 
javascript&colon; 'sys_idIN' + new getCommonGroups().getCommonGroups(current.variables.requestor);

 

Unfortunately, it didn’t work as expected. The field didn’t filter correctly based on the common groups. Let me know if there’s a different approach we should try or if any adjustments are needed in the script.

Thanks!

Working after i made the changes as per the below line - 

 

javascript&colon; 'sys_idIN' + new getCommonGroups().getCommonGroups(current.variables.requestor);