custom dynamic filter to show only user's groups

Alon Grod
Tera Expert

Im trying to create a cusom dynamic filter to show only the incidents that Assignment group is one of user's groups (without parent group).
For some reason it only shows me one group among the groups that i belong to.

Screenshot 2023-08-31 at 15.34.49.png

Screenshot 2023-08-31 at 15.35.20.png

Screenshot 2023-08-31 at 15.36.11.png

Screenshot 2023-08-31 at 15.36.27.png

 

I can only see the incidents of one of my groups instead of the two of them:

Screenshot 2023-08-31 at 15.36.55.png

2 ACCEPTED SOLUTIONS

Hi @Alon Grod,

 

Then it probably is OOTB in some plugin installed in my instance.

This is the underlying code, which you can use:

 

/**
    	Default filter "One of My Groups - d6435e965f510100a9ad2572f2b47744" includes parent groups too
    	As per defect "DEF0173629" we want only groups the user is part of not parent groups
    */

    getGroupsIAmMemberOfExcludeParents: function(userId) {
        var groups = [];
        var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery('user', userId);
        gr.addQuery('group.active', true);
        gr.query();
        while (gr.next()) {
            var groupId = gr.getValue('group');
            if (!gs.nil((groupId)))
                groups.push(groupId);
        }
        return groups;
    },

Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

View solution in original post

Hi @Alon Grod,

 

No need to use javascript: int the script field here just use (assuming you are not changing the script include name you already have, and will use this script I sent you)

new getGroups().getGroupsIAmMemberOfExcludeParents(gs.getUserID());


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

View solution in original post

14 REPLIES 14

Claude DAmico
Kilo Sage

According to documentation, there should already be a "One of My Groups" dynamic filter available. I looked at that and see the script doesn't call a script include at all. It just uses gs.getUser().getMyGroups() in the script field.

Claude E. D'Amico, III - CSA

Bert_c1
Kilo Patron

Hi @Alon Grod,

 

I have the following dynamic filter and script include working in my instance.

 

Screenshot 2023-08-31 094430.png

 

Screenshot 2023-08-31 094448.png

 

The script from above is:

	function getMyGroups() {
		// Get list of groups the logged in user is a member of
		var uID = gs.userID();
		gs.info("getMyGroups: User = " + uID);

		// get the user groups
		var userGroups = [];
		var sgm = new GlideRecord("sys_user_grmember");
		sgm.addQuery('user', uID);
		sgm.query();
		while (sgm.next()) {
			userGroups.push(sgm.group.getDisplayValue());
		}
		gs.info("getMyGroups: userGroups = " + userGroups + ".");

		// Now query group table for the groups
		var grpr = new GlideRecord('sys_user_group');
		// Build query to include the groups
		var tQuery = '';
		for (i=0; i<userGroups.length; i++) {
			tQuery += "name" + userGroups[i] + "^";
		}
		grpr.addEncodedQuery(tQuery);
		grpr.query();
		gs.info("getMyGroups: processing " + grpr.getRowCount() + " user groups.");

		// Get list of groups to use in filter
		var queryGrps = '';
		while (grpr.next()) {
			queryGrps += grpr.sys_id.toString() + ",";
		}

		gs.info("getMyGroups: queryGrps = " + queryGrps + "." );
		return queryGrps;
	}

What is returned is a comma separated string with the sys_id values of the group records.

@Bert_c1 its returning the Parent's assignemt group as well which its not good

@Alon Grod 

There should be an existing OOTB dynamic filter which does exactly that:

'Groups I am member of' : /nav_to.do?uri=sys_filter_option_dynamic.do?sys_id=4fef30720f032010717cc562ff767e90


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.