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

J Siva
Tera Sage

Hi @Aaron Duncan 
I just modified your script and it worked. PFB.

var myTeamsRequests = Class.create();
myTeamsRequests.prototype = {

    initialize: function() {},

    getRecordsCreatedByGroupMembers: function() {

        var userID = gs.getUserID();
        var groupArray = [];
        var user = new GlideRecord('sys_user');
        if (user.get(userID)) {
            var grMember = new GlideRecord('sys_user_grmember');
            grMember.addQuery('user', userID);
            grMember.query();
            while (grMember.next()) {
                var group = grMember.getValue('group');
                groupArray.push(group);
            }
        }

        var userArray = [];
        for (var i = 0; i < groupArray.length; i++) {
            var grMember1 = new GlideRecord('sys_user_grmember');
            grMember1.addQuery('group', groupArray[i]);
            grMember1.query();
            while (grMember1.next()) {
                var userId = grMember1.getValue('user');
                if (!userArray.includes(userId)) {
                    userArray.push(userId);
                }
            }
        }

        return userArray;

    },
    type: 'myTeamsRequests'
};

Dynamic filter:

JSiva_0-1752033476121.png

Hope this helps.
Regards,
Siva

@J Siva How did you get it to work with the Reference Script in your Dynamic Filter being empty?

@Aaron Duncan I've followed the oob dynamic filters.

I believe the script in the script field should be enough.

I even set it up in my PDI with your script exactly, but I see the following when I try to use it.

 

AaronDuncan_0-1752265216052.png

I copy the query and i get this data.

request.requested_forDYNAMIC2c71c4fb83e622108db3a730ceaad398^active=true

 

and then I run it as a background script and I get this.

 

*** Script: Current user: 6816f79cc0a8016401c5a33be04be441
*** Script: 6816f79cc0a8016401c5a33be04be441,645e03b0eb32010045e1a5115206feb5,8c17c7f0eb32010045e1a5115206fe17,67484bb0eb32010045e1a5115206fee6,2428c7f0eb32010045e1a5115206feb8,a8f98bb0eb32010045e1a5115206fe3a,717ec7f0eb32010045e1a5115206fe21,46d44a23a9fe19810012d100cca80666,5137153cc611227c000bbd1bd8cd2007,681b365ec0a80164000fb0b05854a0cd

 

Any thoughts as to why it's not returning anything?