Show users who report to me

v-paulp
Tera Contributor

Hi All, 

There is one user table reference type variable in a catalog item and we want on that variable on those users records should show who report to the requestor. 

For example I am raising a request from the catalog item in that case on that variable drop down list only those user name should show whose manager is me or their managers manager is me and so on....

 

I know we can achieve this using reference qualifier but I need help to built the logic.

 

 

1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron
Tera Patron

@v-paulp Create a dynamic reference qualifier and put the following function inside it. Call this script include function from your reference qualifier.

 

function getMyReportees() {
    var userList = [];
    var currentUserId = gs.getUserID(); // Get the logged-in user's ID
    
    // Function to recursively find users reporting to a manager
    function getUsersReportingTo(managerId) {
        var gr = new GlideRecord('sys_user');
        gr.addQuery('manager', managerId);
        gr.query();
        while (gr.next()) {
            if (!userList.includes(gr.sys_id.toString())) {
                userList.push(gr.sys_id.toString());
                getUsersReportingTo(gr.sys_id); // Recursively find users under this manager
            }
        }
    }
    
    // Add the current user's direct reports
    getUsersReportingTo(currentUserId);
    
    // Return query for the user table
    return 'sys_idIN' + userList.join(',');
}

View solution in original post

1 REPLY 1

Sandeep Rajput
Tera Patron
Tera Patron

@v-paulp Create a dynamic reference qualifier and put the following function inside it. Call this script include function from your reference qualifier.

 

function getMyReportees() {
    var userList = [];
    var currentUserId = gs.getUserID(); // Get the logged-in user's ID
    
    // Function to recursively find users reporting to a manager
    function getUsersReportingTo(managerId) {
        var gr = new GlideRecord('sys_user');
        gr.addQuery('manager', managerId);
        gr.query();
        while (gr.next()) {
            if (!userList.includes(gr.sys_id.toString())) {
                userList.push(gr.sys_id.toString());
                getUsersReportingTo(gr.sys_id); // Recursively find users under this manager
            }
        }
    }
    
    // Add the current user's direct reports
    getUsersReportingTo(currentUserId);
    
    // Return query for the user table
    return 'sys_idIN' + userList.join(',');
}