- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2023 11:46 AM
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
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
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,
This is what the filter shows when you run it
And here is the log in the incident description
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2023 03:19 AM - edited 07-22-2023 12:16 PM
@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;
}
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 !!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2023 03:19 AM - edited 07-22-2023 12:16 PM
@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;
}
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 !!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2023 06:07 AM
Thank you very much for your assistance Sandeep!
Alain
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2026 06:52 AM
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.
