Script Include for reference qualifier, for showing groups with atleast one member

AadityaSahu
Tera Contributor

Hello Community

 

The requirement is that I need to create Reference Type Variable and this variable should display Groups from the sys_user_group Table.

But it has 3 conditions :

1. It should ONLY display Active Groups

2. It should ONLY display Groups of XYZ Company

3. It should ONLY display Group members with ATLEAST one member

 

I want to create a Script Include. And want to use this Script Include as Advance Qualifier for this Reference Variable.

I created a Script but it is not Working at all.

 

Hope you guys would be able to point out what is wrong with the following Script

 

var ActiveGroupsWithMembers = Class.create();
ActiveGroupsWithMembers.prototype = Object.extendsObject(AbstractAjaxProcessor,
{
    getActiveUsersGroups: function()
    {
        function getActiveUsersGroups()
        {
        var GroupIfMember = [];
        var usr = new GlideRecord("sys_user_grmember");
        usr.addEncodedQuery("sys_domain=sysID__of__XYZ__Company^group.active=true");
        usr.query();
        while(usr.next())
        {
        GroupIfMember.push(usr.group.sys_id);
        }
        return 'sys_idIN' + GroupIfMember;
        }
    },
    type: 'ActiveGroupsWithMembers'
});
4 REPLIES 4

Uncle Rob
Kilo Patron

Did you do any logging yet?  That's what I do.
Log to see if I'm in the Script Include AT ALL
Log to see the usr query returned anything
Log to see if the array is being generated correctly...

etc.

Ankur Bawiskar
Tera Patron
Tera Patron

@AadityaSahu 

should be an easy one to handle

what does this mean? -> 2. It should ONLY display Groups of XYZ Company

are you having domain separated instance?

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

Yes

@AadityaSahu 

you are declaring function within function

try this. ensure this filter is applied correctly for domain, give correct sysId

Call it like this

javascript: new ActiveGroupsWithMembers().getActiveUsersGroups();
var ActiveGroupsWithMembers = Class.create();
ActiveGroupsWithMembers.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getActiveUsersGroups: function() {
        var groupIds = [];
        var gr = new GlideRecord('sys_user_grmember');
        gr.addEncodedQuery('group.active=true');
		gr.addEncodedQuery('sys_domain=sysID__of__XYZ__Company');
        gr.query();
        while (gr.next()) {
            if (!groupIds.includes(gr.group.sys_id.toString())) {
                groupIds.push(gr.group.sys_id.toString());
            }
        }
        return 'sys_idIN' + groupIds.join(',');
    },
    type: 'ActiveGroupsWithMembers'
});

If my response helped please mark it correct and close the thread so that it benefits future readers.

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