- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-18-2024 03:38 AM
Hi Team,
I have a variable that needs to reference data from the user table.
In this case, I need to implement a reference qualifier. If the currently logged-in user is a manager, the variable should populate with the users managed by that person. Additionally, the variable should include the users managed by the logged-in manager's direct reports (ex:manger(3users)>> manger of manger(4users) = 7 Usrs need to populate)
For example:
- If a manager (X) has 3 direct reports, the variable should return 3 users.
- If another manager (Y) has 3 direct reports, and one of those users has a manager (Z) who oversees 4 users, the variable should return a total of 7 users (3 users from Y + 4 users from Z).
Thank you in advance
Sivananda reddy
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-18-2024 05:51 AM - edited ‎12-18-2024 05:53 AM
use this and pass the logged in user sysId
var UserUtils = Class.create();
UserUtils.prototype = {
initialize: function() {},
getAllReportees: function(managerSysId) {
var allReports = [];
// Start collecting from the given manager
this.collectReports(managerSysId, allReports);
return allReports.toString();
},
// Recursive function to collect reports
collectReports: function(managerId, allReports) {
var directReports = this.getDirectReports(managerId);
while (directReports.next()) {
allReports.push(directReports.sys_id.toString());
this.collectReports(directReports.sys_id, allReports); // Recursively collect indirect reports
}
},
// Function to get direct reports
getDirectReports: function(managerId) {
var gr = new GlideRecord('sys_user');
gr.addQuery('manager', managerId);
gr.query();
return gr;
},
type: 'UserUtils'
};
use this in ref qualifier
javascript: 'sys_idIN' + new UserUtils().getAllReportees(gs.getUserID());
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-18-2024 05:37 AM
you should have advanced ref qualifier on that variable and bring in the users
what script did you start with?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-18-2024 05:44 AM
Thank you for your response,
I have user below scrpit inulde and advanced ref qualifier, But no luck
Script inculde
var UserFilterForCatalogItem = Class.create();
UserFilterForCatalogItem.prototype = {
initialize: function() {},
getFilteredUsers: function() {
var currentUserId = gs.getUserID(); // Get the currently logged-in user
var userGr = new GlideRecord('sys_user');
// userGr.addActiveQuery(); // Only active users
userGr.addQuery('manager', currentUserId); // Users whose manager is the current user
var users = [];
userGr.query();
while (userGr.next()) {
// users.push(userGr.name.toString());
var userman = new GlideRecord('sys_user');
userman.addQuery('manager', users);
users.push(userman.name.toString()+userGr.name.toString());
}
return users.join(',');
},
type: 'UserFilterForCatalogItem'
};
advanced ref qualifier
javascript: 'sys_idIN' + UserFilterForCatalogItem.getFilteredUsers();
Thank you
Siva
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-18-2024 05:46 AM
Hi @Uncle Rob
Thank you for your response,
I have user below scrpit inulde and advanced ref qualifier, But no luck
Script inculde
var UserFilterForCatalogItem = Class.create();
UserFilterForCatalogItem.prototype = {
initialize: function() {},
getFilteredUsers: function() {
var currentUserId = gs.getUserID(); // Get the currently logged-in user
var userGr = new GlideRecord('sys_user');
// userGr.addActiveQuery(); // Only active users
userGr.addQuery('manager', currentUserId); // Users whose manager is the current user
var users = [];
userGr.query();
while (userGr.next()) {
// users.push(userGr.name.toString());
var userman = new GlideRecord('sys_user');
userman.addQuery('manager', users);
users.push(userman.name.toString()+userGr.name.toString());
}
return users.join(',');
},
type: 'UserFilterForCatalogItem'
};
advanced ref qualifier
javascript: 'sys_idIN' + UserFilterForCatalogItem.getFilteredUsers();
Thank you
Siva
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-18-2024 05:55 AM
check the script I shared below
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-18-2024 06:48 AM
Oh, ok... I get it now. You want a reference qualifier to select only those users.
So when troubleshooting scripts its important that you do at least some logging.
Have you verified that the script is firing AT ALL?
Have you verified the value of the string field at any point in the first or second loops?
I'd start there.