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

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

Hi @Ankur Bawiskar 

The logic is working as expected, Cloud pls tell me why script include is nit working?

 

@vardhan3 

you should update script as this and use array to hold user sysIds and then return the query

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

    },

ref qualifier as this in that field

javascript: new DepatmentNameUtil().getDepartmentName();

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

Hi Ankur,

 

It's displaying only logged in user record, total 35 records are there in same dapartment