Requested for should show manager's reportees and reportee's reportees

si21
Tera Guru

Hi experts,

We have below requirement.

When ever a manager opens 'XYZ record producer' on portal, Requested for variable should display their reportees and If these reportees are managers, their reportees should also be displayed.

 

Ex. If manager 'A' has 3 reportees ( Say P,Q,R) and if P has 3 reportees (say X,Y,Z)  

A should see P,Q,R as well as X,Y,Z

 

How can we achieve this.

TIA

1 ACCEPTED SOLUTION

Hi @si21 

 

Please try with below Script Include :

 

var GetReportees = Class.create();
GetReportees.prototype = {
    initialize: function() {},

    getAssociates: function(user) {
        var reportees = [];
        var userGr = new GlideRecord('sys_user');
        userGr.addQuery('manager.sys_id', user);
        userGr.query();
        while (userGr.next()) {
            reportees.push(userGr.sys_id.toString());
            var userSysID = userGr.sys_id.toString().trim();
			var managerGr = new GlideRecord('sys_user');
			managerGr.addQuery('manager',userSysID);
			managerGr.query();
            var rowCount = managerGr.getRowCount();
            if (rowCount != '0') {
                while(managerGr.next()); {
					reportees.push(managerGr.sys_id.toString());
                }
            }
        }
		return 'sys_idIN'+reportees;
    },

    type: 'GetReportees'
};

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

View solution in original post

5 REPLIES 5

_Gaurav
Kilo Sage

Hi @si21 
This is the background script that will fulfill your requirements. You just need to make slight changes and it will work for you.

 

var userArr=[];

var grSysUser = new GlideRecord('sys_user');
grSysUser.addEncodedQuery("manager=6816f79cc0a8016401c5a33be04be441"); //pass the sysid of the logged in user
grSysUser.query();
while (grSysUser.next()) {
userArr.push(grSysUser.getValue('sys_id'));
}
var splitted_value = userArr.toString().split(',');
for(var count = 0 ; count<splitted_value.length ; count++){
var grSysUser1 = new GlideRecord('sys_user');
grSysUser1.addEncodedQuery("manager.sys_id="+splitted_value[count]);
grSysUser1.query();
while (grSysUser1.next()) {
userArr.push(grSysUser1.getValue('sys_id'));
}
}
gs.print(userArr.toString());


Please mark this as helpful and accept it as a solution if this works for you.
Thanks!

Amit Verma
Kilo Patron
Kilo Patron

Hi @si21 

 

Please follow the below approach :

 

1. Create a Script Include as below :

 

var GetReportees = Class.create();
GetReportees.prototype = {
    initialize: function() {},

    getAssociates: function(user) {
        var reportees = [];
        var userGr = new GlideRecord('sys_user');
		var managerGr = new GlideRecord('sys_user');
        userGr.addQuery('manager', user);
        userGr.query();
        while (userGr.next()) {
            reportees.push(userGr.sys_id.toString());
            var userSysID = userGr.sys_id;
            managerGr.addQuery('manager', userSysID);
			managerGr.query();
			gs.info(userSysID);
			gs.info(managerGr.getRowCount());
            var rowCount = managerGr.getRowCount();
            if (rowCount != '0') {
				gs.info(userGr.getRowCount());
                while(managerGr.next()); {
					reportees.push(managerGr.sys_id.toString());
                }
            }
        }
		//gs.info(reportees.toString());
		return 'sys_idIN'+reportees;
    },

    type: 'GetReportees'
};

 

AmitVerma_0-1709802180075.png

 

2. Make use of below Advanced Reference Qualifier in your Requested for Variable :

javascript&colon; new GetReportees().getAssociates(gs.getUserID());

 

AmitVerma_1-1709802256251.png

 

Output :

 

AmitVerma_2-1709802673150.png

 

AmitVerma_3-1709802689887.png

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

Hi @Amit Verma ,

Thanks for your response, the code you gave is working for direct reportees only.

Currently Manager is able to see their direct 4 reportees but cannot see reportee's reportees.

Ex. Currently with above code, Abraham lincoln is able to see only his direct reportees. Since Abel tuter is also manager for Alejandra, Abraham lincoln should see 5 members as his reportees including Alejandra.

Please refer below image.

 

a12325.png

 

Hi @si21 

 

Please try with below Script Include :

 

var GetReportees = Class.create();
GetReportees.prototype = {
    initialize: function() {},

    getAssociates: function(user) {
        var reportees = [];
        var userGr = new GlideRecord('sys_user');
        userGr.addQuery('manager.sys_id', user);
        userGr.query();
        while (userGr.next()) {
            reportees.push(userGr.sys_id.toString());
            var userSysID = userGr.sys_id.toString().trim();
			var managerGr = new GlideRecord('sys_user');
			managerGr.addQuery('manager',userSysID);
			managerGr.query();
            var rowCount = managerGr.getRowCount();
            if (rowCount != '0') {
                while(managerGr.next()); {
					reportees.push(managerGr.sys_id.toString());
                }
            }
        }
		return 'sys_idIN'+reportees;
    },

    type: 'GetReportees'
};

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.