Dynamic filter on group types

AlainT1
Tera Guru

Hello,

I’m trying to create a Dynamic Filter where I will be able to list only groups of a certain type (let’s assume ‘catalog’ in our PDI), where I am assigned. 

I, as system admin, have added myself to the following groups:

  • Capacity Mgmt
  • Field Services

AlainT1_0-1689964921339.png

 

Here is my script include to list the active groups of type catalog where I am present

Name: getMyAssignmentGroups
Application: global
Accessible from: All applications
Client callable: true
Role: itil

function getMyAssignmentGroups() {

    var msg = [];

    var selected = [];

 

    //Get the sys_id of the current user

    var uID = gs.getUserID(); 

    var type = '74af88c6c611227d0066386e74dc853d'; // the sys_user_group_type catalog sys_id

 

    // Get all active groups where user is a member

    var grmember = new GlideRecord('sys_user_grmember');

 

    // this returns the proper groups in the log but nothing in the Dynamic filter

    var query = 'user='+uID+'^group.active=true^group.typeCONTAINS'+type; 

    grmember.addEncodedQuery(query);

    grmember.query();

 

    msg.push('Date ' + new Date());

    msg.push('Type = ' + type);

    msg.push(grmember.getEncodedQuery()); // to see what was queried

 

    while (grmember.next()) {

        //Add the group sys_id values to the returned array

        selected.push(grmember.group);

        msg.push(grmember.group.getDisplayValue());

    }

    // Since we do not seem to be able to log with gs.log we use the description field of 

    // an incident to get the results we are looking for

    var inc = new global.GlideRecord('incident');

    inc.get('e329de99731423002728660c4cf6a73c'); // INC0009004 incident OOTB

    inc.description = JSON.stringify(msg, null, 4);

    inc.update();

 

    return selected;

}

 

Here is the Dynamic Filter

AlainT1_1-1689964921343.png

 

Note that I have tried with and without a Reference script and that there was no difference to the behaviour

 

When I use the Dynamic Filter, from the incident list of records,

AlainT1_2-1689964921353.png

 

This is what the filter shows when you run it

AlainT1_3-1689964921355.png

 

And here is the log in the incident description

AlainT1_4-1689964921357.png

 

As you can see, the Dynamic Filter does not seem to be able to deal with a “CONTAINS” clause or something else altogether different is happening.

 

Does anyone have any idea how to work around this?  I have inserted the update set containing the code, you will only need to add yourself to a catalog group of your choosing.

 

#dynamic_filter

1 ACCEPTED SOLUTION

SANDEEP28
Mega Sage

@AlainT1  Add below code in script include. Your logic was correct. I just changed the one line of code  "selected.push(grmember.group)" to   "selected.push(grmember.getValue('group'))".

 

Its best practice to use getValue() method while retrieving any value from object. 

 

 

 

function getMyAssignmentGroups() {
    var selected = [];

    //Get the sys_id of the current user
    var uID = gs.getUserID(); 
    var type = '74af88c6c611227d0066386e74dc853d'; // the sys_user_group_type catalog sys_id

    // Get all active groups where user is a member
    var grmember = new GlideRecord('sys_user_grmember');

	// this returns the proper groups in the log but nothing in the Dynamic filter
    var query = 'user='+uID+'^group.active=true^group.typeCONTAINS'+type; 
    grmember.addEncodedQuery(query);
    grmember.query();

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

    return selected;
}

SANDEEP28_0-1690020982646.png

 

SANDEEP28_1-1690021017076.png

Also make sure you have incident assigned to those catalog type of groups. 

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !! 

 

View solution in original post

3 REPLIES 3

SANDEEP28
Mega Sage

@AlainT1  Add below code in script include. Your logic was correct. I just changed the one line of code  "selected.push(grmember.group)" to   "selected.push(grmember.getValue('group'))".

 

Its best practice to use getValue() method while retrieving any value from object. 

 

 

 

function getMyAssignmentGroups() {
    var selected = [];

    //Get the sys_id of the current user
    var uID = gs.getUserID(); 
    var type = '74af88c6c611227d0066386e74dc853d'; // the sys_user_group_type catalog sys_id

    // Get all active groups where user is a member
    var grmember = new GlideRecord('sys_user_grmember');

	// this returns the proper groups in the log but nothing in the Dynamic filter
    var query = 'user='+uID+'^group.active=true^group.typeCONTAINS'+type; 
    grmember.addEncodedQuery(query);
    grmember.query();

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

    return selected;
}

SANDEEP28_0-1690020982646.png

 

SANDEEP28_1-1690021017076.png

Also make sure you have incident assigned to those catalog type of groups. 

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !! 

 

Thank you very much for your assistance Sandeep!

 

Alain

Not sure when or how things changed, but I had to make two or three adjustments to get this working.

1. The query line should use LIKE instead of CONTAINS (doesn't matter if there a multiple Group Types associated to a group)

2. Obviously the function name line needs reformatting for a script include to something like "getMyAssignmentGroups: function() {

3. Not sure whether this was necessary, but the call in the dynamic filter needs to be "new global.<scriptincludename>().<functionname>().

4. I DID include the Script include in the reference script space ... but again, not 100% sure that was required.

5. In the "push" line I added the standard + '' after the push value.