The CreatorCon Call for Content is officially open! Get started here.

Dynamic Filter Script / Script Include for Child Groups that are part of Parent Group for Report

WazzaJC
Tera Expert

Dynamic Filter Script / Script Include for Child Groups that are part of Parent Group for Report

 

Hi Team - I would really appreciate any help/guidance on how to achieve this.

 

I need to set up a Dynamic Filter so that I can filter an Incident Report to only show Open Incidents for the Assignment

Groups that are children of a parent Group that I am the Manager of (this parent Group is not itself an Assignment Group, but its sub-groups are and they are linked via a child-parent mapping).

 

These are child Assignment Groups that are mapped to a parent Group - I am set as the Manager of that parent Group.

 

What would be the Script Include I need to write to dynamically filter on those Open Incidents, associated to all Assignment Groups that are part of the parent Group that I am the Manager of.

 

I need to build this as a Dynamic Filter that I can reference within a Report that points to the Incident table.

 

Many thanks for any advice/guidance.

3 ACCEPTED SOLUTIONS

Bert_c1
Kilo Patron

Hi,

 

I see that script needs to be extended, to get the child groups of the groups returned. To meet your needs

 

 

 

function getMyManagedGroups() {
    var selected = [];
    var grpManager = gs.getUserID();
    // Instead of dynamic for current user, I will use a specific manager that I know has groups
    grpManager='c092a629db66ae00580ed211ce9619a0';


    //Get the groups where grpManager is the 'manager'
    var grps = new GlideRecord('sys_user_group');
    grps.addQuery('manager', grpManager);
    grps.query();

    while (grps.next()) {
        //Add the group sys_id values to the returned array
		selected.push(grps.getValue('sys_id'));
    }

	// now get the list of child groups
	var childGroups = [];
	for (i=0; i< selected.length; i++) {
		var childGrps = new GlideRecord('sys_user_group');
		childGrps.addQuery('parent', selected[i]);
		childGrps.query();
		while (childGrps.next()) {
			childGroups.push(childGrps.getValue('sys_id'));
		}
	}

    return childGroups;
}

var grpList = getMyManagedGroups();
gs.info("Managed child groups: " + grpList);

 

 

View solution in original post

Bert_c1
Kilo Patron

Hi WazzJC,

 

I have modified the script logic to be like what was in the post I referred to, tested in scripts background.

 

function ManagerChildGroupsUsers() {
		var selected = [];
		var grpManager = gs.getUserID();
//		gs.info('getMyManagedChildGroups: Manager = ' +grpManager);
		// Instead of dynamic for current user, I will use a specific manager that I know has groups
		grpManager='c092a629db66ae00580ed211ce9619a0';		// Rosie Matthews - Demo data

		//Get the groups where grpManager is the 'manager'
		var grps = new GlideRecord('sys_user_group');
		grps.addQuery('manager', grpManager);
		grps.query();
		while (grps.next()) {
			//Add the group sys_id values to the returned array
			selected.push(grps.getValue('sys_id'));
		}

		// next get the list of child groups
		var childGroups = [];
		for (i=0; i< selected.length; i++) {
			var childGrps = new GlideRecord('sys_user_group');
			childGrps.addQuery('parent', selected[i]);
			childGrps.query();
//			gs.info("Found " + childGrps.getRowCount() + " record where parent = " + selected[i]);
			while (childGrps.next()) {
				childGroups.push(childGrps.getValue('sys_id'));
			}
		}

		// now get the users in the child groups
		var users = [];
		for (i=0; i< childGroups.length; i++) {
			var userGrps = new GlideRecord('sys_user_grmember');
			userGrps.addQuery('group', childGroups[i]);
			userGrps.query();
//			gs.info("Found " + userGrps.getRowCount() + " record where group = " + childGroups[i]);
			while (userGrps.next()) {
//				gs.info("Adding user = " + userGrps.user);
				users.push(userGrps.getValue('user'));
			}
		}
		
		return users;
}

 

And Dynamic Filter Option:

Screenshot 2023-08-01 092716.png

 

Debug shows the script include is create the 'users' array correctly, but testing I get "(empty)" when I select that for "opened by", "is (dymanic)" and the script include.  I don't have time to see what is going on today, hopefully it'll work for you or someone else here will respond on that aspect.

View solution in original post

@WazzaJC ,

 

I got my last example to work for 'opened by' on incident list, I had a mis-spelling in the dynamic filter definition Script should be "new ManagerChildGroupsUsers();". Please test. And comment-out line 6 where I have a test user.

View solution in original post

13 REPLIES 13

Bert_c1
Kilo Patron

Hi WazzJC,

 

Please see similar post from last week:

 

https://www.servicenow.com/community/developer-forum/dynamic-filter-on-group-types/td-p/2620813

 

You can leave out the logic for checking group type. And change logic to:

 

function getMyManagedGroups() {
    var selected = [];
    var grpManager = gs.getUserID();
    // Instead of dynamic for current user, I will use a specific manager that I know has groups
    grpManager='c092a629db66ae00580ed211ce9619a0';


    //Get the groups where grpManager is the 'manager'
    var grps = new GlideRecord('sys_user_group');
    grps.addQuery('manager', grpManager);
    grps.query();

    while (grps.next()) {
        //Add the group sys_id values to the returned array
	selected.push(grps.getValue('sys_id'));
    }

    return selected;
}

// for testing in Scripts Background
var grpList = getMyManagedGroups();
gs.info("Managed groups: " + grpList);

 

I hope this helps.

Bert_c1
Kilo Patron

Hi,

 

I see that script needs to be extended, to get the child groups of the groups returned. To meet your needs

 

 

 

function getMyManagedGroups() {
    var selected = [];
    var grpManager = gs.getUserID();
    // Instead of dynamic for current user, I will use a specific manager that I know has groups
    grpManager='c092a629db66ae00580ed211ce9619a0';


    //Get the groups where grpManager is the 'manager'
    var grps = new GlideRecord('sys_user_group');
    grps.addQuery('manager', grpManager);
    grps.query();

    while (grps.next()) {
        //Add the group sys_id values to the returned array
		selected.push(grps.getValue('sys_id'));
    }

	// now get the list of child groups
	var childGroups = [];
	for (i=0; i< selected.length; i++) {
		var childGrps = new GlideRecord('sys_user_group');
		childGrps.addQuery('parent', selected[i]);
		childGrps.query();
		while (childGrps.next()) {
			childGroups.push(childGrps.getValue('sys_id'));
		}
	}

    return childGroups;
}

var grpList = getMyManagedGroups();
gs.info("Managed child groups: " + grpList);

 

 

Hello Bert_c1,

 

This is absolutely brilliant, thank you ever so much, I have this now up and running on my PDI and it works like a dream, really appreciate your guidance kind sir ! 🙂

 

One last question please - how can I replicate this / adjust the script, so that separately I can build a similar Dynamic Filter, which shows the Incidents 'Opened By' Group Members associated to the Parent Group, that I am a Manager of?

 

So in this case it is related to Incidents 'Opened By' not the 'Assignment Group' for the Incidents.

 

Again, just to clarify, it is Incidents 'Opened By' Group Members associated to the (Parent) Group that I am Manager of (where those Groups are Children of the Parent Group and I am Manager of the Parent Group to those Child Groups).

 

I would greatly appreciate your further help/guidance here.

 

Many thanks kind sir. 🙂

Bert_c1
Kilo Patron

Hi WazzaJC,

 

The script I provided would be used in a Script Include, as described in the other post. And those are "callable" by any other script. I believe that addresses your question on 'replicating' the functionality. I'll try testing that and indicate changes.

 

And seems you want to 'hard-code' the group manager value (that's ok).  And logic is needed to search sys_user_grmember table for the current user, to see if they are a member of any group that is a child to a group your are the manager. I'll have to think about that.

 

I'll review the Dynamic Filter in the post I referenced and try to help with that aspect, maybe respond in day or two.

 

I've tested the script include 

Name: ManagerChildGroups
Application: global
Accessible from: All applications
Client callable: true

the script is:

 

var ManagerChildGroups = Class.create();
ManagerChildGroups.prototype = {
    initialize: function() {
    },

	getMyManagedChildGroups: function () {
		var selected = [];
		var grpManager = gs.getUserID();
		// Instead of dynamic for current user, I will use a specific manager that I know has groups
		grpManager='c092a629db66ae00580ed211ce9619a0';


		//Get the groups where grpManager is the 'manager'
		var grps = new GlideRecord('sys_user_group');
		grps.addQuery('manager', grpManager);
		grps.query();

		while (grps.next()) {
			//Add the group sys_id values to the returned array
			selected.push(grps.getValue('sys_id'));
		}

		// now get the list of child groups
		var childGroups = [];
		for (i=0; i< selected.length; i++) {
			var childGrps = new GlideRecord('sys_user_group');
			childGrps.addQuery('parent', selected[i]);
			childGrps.query();
//			gs.info("Found " + childGrps.getRowcount() + " record where parent = " + selected[i]);
			while (childGrps.next()) {
				childGroups.push(childGrps.getValue('sys_id'));
			}
		}

		return childGroups;
	},

    type: 'ManagerChildGroups'
};

 

I test it in scripts background using:

 

var grpList = new ManagerChildGroups;
var list = grpList.getMyManagedChildGroups();
gs.info("Managed child groups: " + list);

 Next will be logic for the user being in one of those groups.