- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2025 09:32 PM
Hi All,
How to retrieve all users in the caller field who belong to the same department as the logged in user?
I have written below script include but it's doesn't fetch correct data
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2025 09:43 PM
you can use something like this in advanced ref qualifier
no script include required
javascript: 'department=' + gs.getUser().getDepartmentID();
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
01-07-2025 12:23 AM
it should work fine.
Please share your script include and ref qualifier
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2025 01:28 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2025 10:43 PM
Hi ,
Suppose wanna retrieve all users in the caller field who belong to the same assignment group as the logged in user?
The below logic correct or not
javascript: 'getAssignment group=' + gs.getUser().getAssignment group();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2025 12:32 AM
for this you will have to use script include
1) get the logged in users group
2) then query sys_user_grmember table with these groups and store those users in array
3) then return that array of user sysIds
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
01-06-2025 10:04 PM
Your script include does not properly fetch all users in the same department as the logged-in user. It only retrieves the department of the logged-in user but doesn't query for other users in that department. Here's how you can fix and update the script:
var DepartmentNameUtil = Class.create();
DepartmentNameUtil.prototype = {
initialize: function() {},
getUsersInSameDepartment: function() {
var userList = [];
var currentUserID = gs.getUserID();
// Get the department of the logged-in user
var userGR = new GlideRecord('sys_user');
userGR.get(currentUserID);
if (userGR.isValidRecord() && userGR.department) {
var departmentID = userGR.department.toString();
// Query for users in the same department
var usersGR = new GlideRecord('sys_user');
usersGR.addQuery('department', departmentID);
usersGR.query();
while (usersGR.next()) {
userList.push(usersGR.sys_id.toString()); // Add user IDs to the list
}
}
return userList; // Return the list of user IDs
},
type: 'DepartmentNameUtil'
};