- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2015 02:05 PM
I'm trying to create a dynamic filter that will return the entire reporting hierarchy of the current user by looking at the manager field on sys_user. So far, I've gotten it to pull the direct reports of the current user but I've not been able to get the direct report's of the current user's direct reports. I would like to go six levels deep using something like this:
Current User -> Current User's Direct Reports -> Current User's Direct Report's Direct Reports
I thnk I need to use a while loop but I'm not quite sure how to do that. Any thoughts? Thanks!
Here's the script I have so far:
var getWithinMyReportingHierarchy = Class.create();
getWithinMyReportingHierarchy.prototype = {
initialize: function() {
},
getWithinMyReportingHierarchy:function(){
var u = gs.getUserID();
var gp = [];
var g = new GlideRecord("sys_user");
g.addQuery("manager", u);
g.query();
while( g.next()) {
gp.push(g.sys_id.toString());
}
return gp;
//
},
type: 'getWithinMyReportingHierarchy'
};
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2015 06:09 PM
Hi Andrew,
Try this one, I left the other function in there in case you want to run it the other way around:
var getWithinMyReportingHierarchy = Class.create();
getWithinMyReportingHierarchy.prototype = {
initialize: function () {
},
hierarchy: [],
getWithinMyReportingHierarchy: function () {
var sid = gs.getUserID();
this.getManager(sid);
return this.hierarchy;
},
getMyDirectReportHierarchy: function () {
var sid = gs.getUserID();
this.getDirectReports(sid);
return this.hierarchy;
},
getManager: function (sid) {
var gr = new GlideRecord('sys_user');
if (gr.get(sid)) {
if (!gr.manager.isNil()) {
if (this.hierarchy.toString().indexOf(gr.manager.sys_id.toString()) == -1) {
this.hierarchy.push(String(gr.manager.sys_id));
this.getManager(String(gr.manager.sys_id));
}
}
}
},
getDirectReports: function (sid) {
var gr = new GlideRecord('sys_user');
gr.addQuery('manager', sid);
gr.query();
while (gr.next()) {
if (this.hierarchy.toString().indexOf(gr.sys_id.toString()) == -1) {
this.hierarchy.push(String(gr.sys_id));
this.getDirectReports(String(gr.sys_id));
}
}
},
type: 'getWithinMyReportingHierarchy'
};
var reportHierarchy = new getWithinMyReportingHierarchy();
var result = reportHierarchy.getMyDirectReportHierarchy();
gs.log(result, 'hie');
Regards,
Mike Moody
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2015 02:11 PM
A recursive function will be the best way to do this.
Thanks,
Berny
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2015 02:40 PM
Hi Andrew,
Try something like this:
var getWithinMyReportingHierarchy = Class.create();
getWithinMyReportingHierarchy.prototype = {
initialize: function () {
},
hierarchy: [],
getWithinMyReportingHierarchy: function () {
var sid = gs.getUserID();
this.getManager(sid);
return this.hierarchy;
},
getManager: function (sid) {
var gr = new GlideRecord('sys_user');
if (gr.get(sid)) {
if (!gr.manager.isNil()) {
if (this.hierarchy.toString().indexOf(gr.manager.sys_id.toString()) == -1) {
this.hierarchy.push(String(gr.manager.sys_id));
this.getManager(String(gr.manager.sys_id));
}
}
}
},
type: 'getWithinMyReportingHierarchy'
};
I hope that helps.
Mike
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2015 08:41 PM
Thanks Michael! Looks like it should work but I'm not getting any results when I use the dynamic filter with this script. I'm impersonating users that have a reporting hierarchy. I'm not even sure how to troubleshoot at this point.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2015 11:00 AM
Unfortunately you can't gs.log when you are calling the code from a filter. I'd recommend temporarily adding a call to this object's function from within an on-demand Scheduled Job that you can trigger by hand and observe gs.log results. Then you could add gs.log statements to display the contents of the heirarchy array.