Catalog Variable that displays a users Direct and Indirect Reports

Sam Giles
Tera Contributor

Hi,

I have a request for a catalog item that requires a field that references the sys_user table to display a managers Direct and also Indirect reports (No other users should appear). I've had no issues getting it to display direct reports with a reference qualifier but I can't figure out how to make it also display any Indirect reports.

I've tried using a script includes along with a client script but i'm not having much luck and all users are being returned.

This is the Script Includes which is Client callable and accessible from all scopes:

var GetUserReports = Class.create();
GetUserReports.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getReportUserIDs: function() {
        var currentUser = gs.getUserID();
        var reports = [];

        function getReports(managerId) {
            var gr = new GlideRecord('sys_user');
            gr.addQuery('manager', managerId);
            gr.addQuery('active', true);
            gr.query();
            while (gr.next()) {
                var id = gr.getUniqueValue();
                if (reports.indexOf(id) === -1) {
                    reports.push(id);
                    getReports(id);
                }
            }
        }

        getReports(currentUser);

        return reports.join(',');
    }
});


This is the Client Script which runs on load for the catalog item in question:

function onLoad() {
    var ga = new GlideAjax('GetUserReports');
    ga.addParam('sysparm_name', 'getReportUserIDs');
    ga.getXMLAnswer(function(response) {
        if (response) {
            var filter = 'sys_idIN' + response;
            g_form.setReferenceQual('direct_report', filter);
        } else {
            g_form.setReferenceQual('direct_report', 'sys_id=NO_MATCH');
        }
    });
}



Any advice would be appreciated.

Thanks!

2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@Sam Giles 

so you want the direct manager rolling up to the next level and get all those users?

if yes then use this in advanced ref qualifier, no client script required

javascript: 'sys_idIN' + new GetUserReports().getReportUserIDs();

Script Include:

var GetUserReports = Class.create();
GetUserReports.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getReportUserIDs: function() {
        var currentUser = gs.getUserID();
        var reports = [];

        function getReports(managerId) {
            var gr = new GlideRecord('sys_user');
            gr.addQuery('manager', managerId);
            gr.addQuery('active', true);
            gr.query();
            while (gr.next()) {
                var id = gr.getUniqueValue();
                if (reports.indexOf(id) === -1) {
                    reports.push(id);
                    getReports(id); // Recursive for indirect reports
                }
            }
        }

        getReports(currentUser);

        // Always include the current user too if needed
        // reports.push(currentUser);

        // Return as a comma-separated string
        return reports.join(',');
    },

    // This is needed for GlideAjax
    type: 'GetUserReports'
});

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

@Sam Giles 

the script I shared and the reference qualifier I shared should cover that scenario

Did you apply that and test it?

Example Scenario
Suppose:

You (manager) have 5 direct reports: A, B, C, D, E.

A manages 5 people: F, G, H, I, J.

B, C, D, E have no reportees.

Result:
The script will return sys_ids for:
A, B, C, D, E (your direct reports)
F, G, H, I, J (A's direct reports; your indirect reports)

Total: 10 users.

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

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

@Sam Giles 

so you want the direct manager rolling up to the next level and get all those users?

if yes then use this in advanced ref qualifier, no client script required

javascript: 'sys_idIN' + new GetUserReports().getReportUserIDs();

Script Include:

var GetUserReports = Class.create();
GetUserReports.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getReportUserIDs: function() {
        var currentUser = gs.getUserID();
        var reports = [];

        function getReports(managerId) {
            var gr = new GlideRecord('sys_user');
            gr.addQuery('manager', managerId);
            gr.addQuery('active', true);
            gr.query();
            while (gr.next()) {
                var id = gr.getUniqueValue();
                if (reports.indexOf(id) === -1) {
                    reports.push(id);
                    getReports(id); // Recursive for indirect reports
                }
            }
        }

        getReports(currentUser);

        // Always include the current user too if needed
        // reports.push(currentUser);

        // Return as a comma-separated string
        return reports.join(',');
    },

    // This is needed for GlideAjax
    type: 'GetUserReports'
});

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,

Thanks for the response.

I'm looking for it to roll down. E.g: I manage 5 people, 1 of these people manages 5 other people. I should see 10 people in the list. Both my direct reports and the people who report into my direct reports.

Thanks,
Sam

@Sam Giles 

the script I shared and the reference qualifier I shared should cover that scenario

Did you apply that and test it?

Example Scenario
Suppose:

You (manager) have 5 direct reports: A, B, C, D, E.

A manages 5 people: F, G, H, I, J.

B, C, D, E have no reportees.

Result:
The script will return sys_ids for:
A, B, C, D, E (your direct reports)
F, G, H, I, J (A's direct reports; your indirect reports)

Total: 10 users.

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

Thanks Ankur, I added this as a reference qualifier which solved the issue: 
javascript: new GetUserReports().getReportUserIDs();