Display users NOT belonging to specific group in a catalog variable

Somujit1
Tera Contributor

Hello,

 

For a catalog item, I have a requirement to display members not belonging to a specific group on a catalog variable referencing to sys_user table.

I have written the below global not client callable script include as -

 

var getNonItilUsers = Class.create();
getNonItilUsers.prototype = {
        initialize: function() {},
            getUser: function() {
                var userName = [];
                var users = new GlideRecord('sys_user');
                users.addActiveQuery();
                users.query();
                while (users.next()) {
                    var userGroup = new GlideRecord('sys_user_grmember');
                    userGroup.addQuery('user', users.sys_id);
                    userGroup.addQuery('group','a671c95edb875910cb183632f39619bf');
                    userGroup.query();
                    if (!userGroup.hasNext()) {
                        userName.push(users.getDisplayValue());
                    }
                    return userName
                }
            },

            type: 'getNonItilUsers'
        };
 
On the variable advanced reference qualifier, i have defined as -
javascript:new getNonItilUsers().getUser();
 
However, the variable displays users belonging to the group which i sys_id'ed when testing the catalog item.
 
Note: To test the script include, I ran it in a background script using gs.print(users.getDisplayValue()), and it works fine.
 
Can you please let me know what i am doing wrong to achieve this scenario?
 
Thanks,
Somujit
5 REPLIES 5

Daniel Borkowi1
Mega Sage

Hi @Somujit1, if I understood correct, you want to use this function as reference qualifier. If so, then you need to return a query and not an array of user display values. Something like sys_idIN<sys_id_user_1>,<sys_id_user2>,<...>.

Check this article: https://www.servicenow.com/community/developer-articles/reference-qualifiers-in-servicenow/ta-p/2765...

 

This should work (not tested):

 

 

 

 

 

 

var GetNonItilUsers = Class.create();
GetNonItilUsers.prototype = Object.extendsObject(AbstractAjaxProcessor, {
        initialize: function() {},
            getUser: function() {
                var userArray = [];
                var users = new GlideRecord('sys_user');
                users.addActiveQuery();
                users.query();
                while (users.next()) {
                    var userGroup = new GlideRecord('sys_user_grmember');
                    userGroup.addQuery('user', users.sys_id);
                    userGroup.addQuery('group','a671c95edb875910cb183632f39619bf');
                    userGroup.query();
                    if (!userGroup.hasNext()) {
                        userArray .push(users.getUniqueValue());
                    }
                    return "sys_idIN" + userArray .join(",");
                }
            },

            type: 'GetNonItilUsers '
        };
});

 

 

 

 

 

Additional I would optimize your code:

 

 

 

 

 

var GetNonItilUsers = Class.create();
GetNonItilUsers.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getUser: function() {
        var userArray = [];
        var userGroup = new GlideRecord('sys_user_grmember');
        userGroup.addQuery('group', 'a671c95edb875910cb183632f39619bf'); //recommend to define this sys_id in a sys_properties
        userGroup.query();
        while (userGroup.next()) {
            userArray.push(userGroup.getValue('user'));
        }
        if (userArray.length > 0) {
            return "sys_idNOT IN" + userArray.join(",");
        }
        return "";

    },
    type: 'GetNonItilUsers'
});

 

 

 

 

 

Greets
Daniel

Please mark reply as Helpful/Correct, if applicable. Thanks!

 

Hi @Daniel Borkowi1 ,

 

Thankyou for your reply. I have updated the script include to return a query as suggested by you and as below, however the variable dropdown is still showing all the users belonging to the sys id group 😞

var getNonItilUsers = Class.create();
getNonItilUsers.prototype = {
    initialize: function() {},
    getUser: function() {
        var userName = [];
        var users = new GlideRecord('sys_user');
        users.addActiveQuery();
        users.query();
        while (users.next()) {
            var userGroup = new GlideRecord('sys_user_grmember');
            userGroup.addQuery('user', users.sys_id);
            userGroup.addQuery('group','a671c95edb875910cb183632f39619bf');
            userGroup.query();
            if (!userGroup.hasNext()) {
                userName.push(users.getDisplayValue());
            }
return 'sys_id IN' + userName;       
}
    },

    type: 'getNonItilUsers'
};

Hi @Somujit1 , the issue is your selected function getDisplayValue, it doesn't return a sys_id. Use getUniqueValue instead.

I really recommend to use this algorithm:

 

 

    getUser: function() {
        var userArray = [];
        var userGroup = new GlideRecord('sys_user_grmember');
        userGroup.addQuery('group', 'a671c95edb875910cb183632f39619bf'); //recommend to define this sys_id in a sys_properties
        userGroup.query();
        while (userGroup.next()) {
            userArray.push(userGroup.getValue('user'));
        }
        if (userArray.length > 0) {
            return "sys_idNOT IN" + userArray.join(",");
        }
        return "";

    },

 

 

 

 

Greets
Daniel

Please mark reply as Helpful/Correct, if applicable. Thanks!

Daniel Borkowi1
Mega Sage

@Somujit1 is your Script Include Client Callable?

DanielBorkowi1_0-1726817703586.png

 

DanielBorkowi1_0-1726818689306.png

 

This works for me. In my group there are 2 user, the reference shows all other users.Greets
Daniel

Please mark reply as Helpful/Correct, if applicable. Thanks!