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'
};
6 REPLIES 6

Ankur Bawiskar
Tera Patron

@Alon Grod 

the script include should be client callable

your script include has initialize() method so it will break

check this

Create your own Dynamic Filters - ServiceNow Community

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

@Ankur Bawiskar can you pls help me to adjust it to be client callable?

@Alon Grod 

just follow the article which I shared.

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Tanushree Maiti
Tera Patron

Hi @Alon Grod 

 

You can try with following script. It will return the sys_id of the group members where logged in user is assigned. 

 

Also you can refer this article : 2 Steps to Create a Dynamic Filter that Returns Groups Logged in Users Belong to

 

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

getMyGroupMembers: function() {
var userGroupIds = [];
var UserIds = [];

// 1. Get all groups where the logged-in user is a member
var loggedInUser = gs.getUserID();
var userGroupGR = new GlideRecord('sys_user_grmember');
userGroupGR.addQuery('user', loggedInUser);
userGroupGR.query();

while (userGroupGR.next()) {
userGroupIds.push(userGroupGR.getValue('group'));
}

// If the user belongs to no groups, return an empty filter or handle accordingly
if (userGroupIds.length === 0) {
return loggedInUser;

// 2. Get all users who are members of those specific groups
var peerGR = new GlideRecord('sys_user_grmember');
peerGR.addQuery('group', 'IN', userGroupIds.join(','));
peerGR.query();

while (peerGR.next()) {
var memberId = peerGR.getValue('user');
if (UserIds.indexOf(memberId) === -1) {
UserIds.push(memberId);
}
}

return UserIds;
},

type: 'OneOfMyGroupMembers'
};

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