Dynamic filter one of my group members
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
7 hours ago
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?
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'
};
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
Hi @Alon Grod ,
1.Create a Glide AJAX enabled Script include .
Name - OneOfMyGroupMembers
Glide AJAX enabled - True
Accessible from - All Application scopes
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
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(',');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
45m ago
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.
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
34m ago
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.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
14m ago
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:
Allow us to select only one option, or
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
8m ago
need to check that, did you check any OOTB dynamic filter options and see the behavior
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
