Creating Dynamic Filter

Aaron Duncan
Mega Sage

I am creating a dynamic filter so that a user can pull a list of all records created by any member of a group that they are a member of. 

ie: If John Smith is a member of the Service Desk group and the On-Call group, then the list would return records created by John Smith and any other users in those two groups.

I created the Script Include below that retrieves the user's groups, and then queries for the members of those groups. From there, the list is supposed to query the Opened By field in the example I'm testing (Requested Items table), but nothing is returning. It's not returning an error either, so I'm not sure what's going on. 

 

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


getRecordsCreatedByGroupMembers : function(user) {
	var myGroups = gs.getUser().getMyGroups();
	var userSet = new Set(); // To ensure uniqueness

	while (myGroups.hasNext()) {
		var group = myGroups.next();
		var groupId = group.getValue('sys_id');

		// Query sys_user_grmember table for users in the group
		var grMember = new GlideRecord('sys_user_grmember');
		grMember.addQuery('group', groupId);
		grMember.query();

		while (grMember.next()) {
			var userId = grMember.getValue('user');
			if (!userSet.has(userId)) {
				userSet.add(userId);
			}
		}
	}

	return userSet;

},
    type: 'myTeamsRequests'
});

 

Here is my Dynamic Filter options setup as well.

 

AaronDuncan_0-1752003041720.png

 

8 REPLIES 8

@Aaron Duncan Could you share the dynamic filter configuration?? 

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Aaron Duncan 

try this

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

    getRecordsCreatedByGroupMembers: function() {
        var myGroups = new global.ArrayUtil().convertArray(gs.getUser().getMyGroups());
        var userArray = [];
        var grMember = new GlideRecord('sys_user_grmember');
        grMember.addQuery('group', 'IN', myGroups.toString());
        grMember.query();
        while (grMember.next()) {
            var userId = grMember.getValue('user');
            userArray.add(userId);
        }
        return userArray.toString();
    },

    type: 'myTeamsRequests'
});

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

Aaron Duncan
Mega Sage

Sure, here's my Dynamic Filter Options.

 

AaronDuncan_0-1752494601273.png

 

@Aaron Duncan As per the product doc, Reference script is optional if you are using javascript in the script field.

So, try removing the reference script and check. Also, in your script include, you shouldn't extend the AbstractAjaxProcessor class.

Screenshot_20250714-182511.png