How to retrieve all users in the caller field who belong to the same department as the logged in use

vardhan3
Tera Contributor

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

 

var DepatmentNameUtil = Class.create();
DepatmentNameUtil.prototype = {
    initialize: function() {},
    departmentName: function() {
        gs.info("department" + gs.getUserID());
        var user_dep = new GlideRecord("sys_user");
        user_dep.addQuery("sys_id", gs.getUserID());
        user_dep.query();
        if (user_dep.next()) {
            return "department" + " " + user_dep.department.toString();
        }
    },

    type: 'DepatmentNameUtil'
};
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@vardhan3 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

9 REPLIES 9

@vardhan3 

it should work fine.

Please share your script include and ref qualifier

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

var DepatmentNameUtil = Class.create();
DepatmentNameUtil.prototype = {
    initialize: function() {},
    departmentName: function() {
        gs.info("department" + gs.getUserID());
        var arr = [];
        var user_dep = new GlideRecord("sys_user");
        user_dep.addQuery("sys_id", gs.getUserID());
        user_dep.query();
        while (user_dep.next()) {
            arr.push(user_dep.getUniqueValue());
        }
        return 'sys_idIN' + arr.toString();
    },

    type: 'DepatmentNameUtil'
};
 
Ref Quali: javascript: new DepatmentNameUtil().departmentName()

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();

@vardhan3 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

yuvarajkate
Giga Guru

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'
};