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.

Require function in script include to pull users from both groups when used in reference qualifier

Chiranjeevi K
Tera Expert

I am looking a function in script include script which call pull user in 2 groups and return when used in reference qualifier, can some one please help with the leads.

Below I written will pull only users in one group since i given poperty value as string and only one group.

getSVMAdminG: function() {
var userSids = [];
var group = gs.getProperty('SVM.admin.group');

var gr = new GlideRecord('sys_user_grmember');
gr.addEncodedQuery('user.active=true');
gr.addQuery('group.name', group);
gr.query();
while (gr.next()) {
userSids.push(gr.getValue('user'));
}
return 'sys_idIN' + userSids.toString();

1 REPLY 1

Maddysunil
Kilo Sage

@Chiranjeevi K 

To pull users who are members of multiple groups, you need to modify your function to accept an array of group names instead of just a single group name. Then, you can loop through each group name in the array and fetch users who are members of those groups.

 

 getUsersInGroups: function(groupNames) {
        var userSids = [];
        var encodedQuery = '';

        // Build the encoded query to fetch users in the specified groups
        for (var i = 0; i < groupNames.length; i++) {
            if (i > 0) {
                encodedQuery += '^OR';
            }
            encodedQuery += 'group.name=' + groupNames[i];
        }

        var gr = new GlideRecord('sys_user_grmember');
        gr.addEncodedQuery('user.active=true^' + encodedQuery);
        gr.query();
        while (gr.next()) {
            userSids.push(gr.getValue('user'));
        }
        
        // Construct the reference qualifier query
        var referenceQualifierQuery = 'sys_idIN' + userSids.toString();
        return referenceQualifierQuery;
    },

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks