How to get the users under a perticular logged in Manager
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-21-2024 07:13 AM - edited 06-23-2024 08:45 PM
Hey guys, hope you all are doing good
I just had one requirement where I needed to filter the Requested For variable of the catalog item to show only those users who report directly or indirectly to the current logged-in manager, and I believe you also may face this requirement before or maybe in the future, thus sharing the solution
Note: The catalog item is only visible to the managers, thus eliminating the condition of being exploited to other users
{to achieve this you can create a user criteria and add the following code to the same, and add it in available for and make sure not available for is empty:
(function() {
var userSysId = gs.getUserID();
var gr = new GlideRecord('sys_user');
gr.addQuery('manager',userSysId);
gr.query();
return gr.hasNext();
})();
}
requirement:
suppose there is manager A and user B, C reports to A.
Now when B or C is using the form, then he should be getting only those users who report them
also when A is using the form, not only the users B and C but also all the users under B and C should be available in Requested For
Solution:
1. navigate to script include under system definition and create a script include, give it a name such as getHierarchial_users
2. make the SI client callable
3. add the following code in the script include
getusers: function() {
var currentUser = gs.getUserID();
var users = [];
//define the function
function getUsersUnderManager(managerId) {
// gliding to user table to get the users
var userGr = new GlideRecord('sys_user');
userGr.addQuery('manager', managerId);
userGr.addQuery('active', 'true');
userGr.query();
while (userGr.next()) {
var userId = userGr.getValue('sys_id');
//push the funded users'
users.push(userId);
// Recursively call the function for this user
getUsersUnderManager(userId);
}
}
// Start the recursion with the current user
getUsersUnderManager(currentUser);
// return the sys_id's
return 'sys_idIN' + users.toString();
},
4. now open your variable where you want to get these filtered users
5. under type specifications, select Use reference qualifier as advance
6. in the Reference qualifier add the following code:
javascript: new API_name(). getusers();
//for example my API name is global.getHierarchial_users thus my reference qualifier will be like
//javascript: new global.getHierarchial_users(). getusers();
7. try the catalog item and impersonate any manager, verify only those users are available in Requested For who report to your impersonated Manager
Let me know in case of any doubt or any question
Happy to Help😊
Please mark it as helpful if this helps you, this will make it visible to other needful users
Gaurav Vaze
SN Developer
- 300 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-24-2024 04:16 AM
Really helpful