Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

Dynamic filter one of my group members

Alon Grod
Tera Expert

Hi,

Im trying to create a dynamic filter that will return all users from my groups but i had no luch. What am I doing wrong?

AlonGrod_0-1790146742824.png

 

AlonGrod_1-1790146787923.png



var OneOfMyGroupMembers = Class.create();

OneOfMyGroupMembers.prototype = {
    initialize: function() {},

    getMyGroupMembers: function() {
        var groups = gs.getUser().getMyGroups();
        var groupQuery = groups && groups.join ? groups.join(',') : String(groups || '');
        if (!groupQuery)
            return '';

        var users = [];
        var seen = {};

        var member = new GlideRecord('sys_user_grmember');
        member.addQuery('group', 'IN', groupQuery);
        member.addQuery('user.active', true);
        member.query();

        while (member.next()) {
            var userId = member.getValue('user');
            if (userId && !seen[userId]) {
                seen[userId] = true;
                users.push(userId);
            }
        }

        return users.join(',');
    },

    type: 'OneOfMyGroupMembers'
};
10 REPLIES 10

Rakesh_M
Mega Sage

Hi @Alon Grod ,
1.Create a Glide AJAX enabled Script include .

Name - OneOfMyGroupMembers
Glide AJAX enabled - True
Accessible from - All Application scopes

Rakesh_M_0-1790159988291.png

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

    getMyGroupMembers: function() {
        
		var my_groups = gs.getUser().getMyGroups();
        var arr = new ArrayUtil().convertArray(my_groups);
        var groupQuery = arr.join(',');

        if (!groupQuery)
            return '';

        var users = [];
        var seen = {};

        var member = new GlideRecord('sys_user_grmember');
        member.addQuery('group', 'IN', groupQuery);
        member.addQuery('user.active', true);
        member.query();

        while (member.next()) {
            var userId = member.getValue('user');
            if (userId && !seen[userId]) {
                seen[userId] = true;
                users.push(userId);
            }
        }

        return "sys_idIN "+users.join(',');
    },

    type: 'OneOfMyGroupMembers'
});


2.Create a dynamic filter option.

Label - OneOfMyGroupMembers
Script - new global.OneOfMyGroupMembers().getMyGroupMembers();
Field Type - reference
Referenced table - User [sys_user]
Available for ref qual - True

Rakesh_M_1-1790160287737.png

 

I noticed these issues in your script:


1.The Script Include needs to be Client callable for it to work with a dynamic filter.

2.var groupQuery = groups && groups.join ? groups.join(',') : String(groups || '');

Here, groupQuery is a string representation of an array


3.You are currently returning: return users.join(',');

For a reference qualifier, you need to return a valid encoded query. For example:
return "sys_idIN" + users.join(',');

 

 




Hi @Ankur Bawiskar , @Tanushree Maiti 

I implemented the above-mentioned configuration, and it is working as expected for reference fields.

However, when I applied the same dynamic filter in a list view(shown below), it did not work.

 

Rakesh_M_0-1790165862984.png


What I noticed is:
1. return "sys_idIN" + users.join(','); — works for reference fields only.
2. return users; — works for list filters only.

what return statement should we use so the same dynamic filter works in both places?

Hi @Rakesh_M 

Its totally depends on requirement.

Instead of a script include ,we can write two script include - one for reference field ,another for list filter.

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

HI @Tanushree Maiti ,

 

I understand that it depends on the requirement and that we could create two Script Includes—one for reference fields and another for list filters.

 

If we need two separate Script Includes, wouldn't we also need two separate Dynamic Filter Options? In that case, what is the purpose of having both checkboxes.

 

If the same Script Include cannot support both types, I would expect the UI to either:

  1. Allow us to select only one option, or

  2. Hide/disable the other checkbox once one type is selected.

Otherwise, selecting both options may give the impression that the same Dynamic Filter Option can be used for both reference fields and list filters.

@Rakesh_M 

need to check that, did you check any OOTB dynamic filter options and see the behavior

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader