Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Dynamic Filter reporting hierarchy script

andrewgamble
Tera Contributor

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'

};

1 ACCEPTED SOLUTION

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


View solution in original post

17 REPLIES 17

When I run the following code from a on-demand Scheduled Job with my admin account, which is currently configured with a management hierarchy, I am getting successful results in my log output. I did notice that my previous code snippet had some typos, I have fixed those below. If you were to call this code from a script include via a filter that call should look something like this:


javascript: (new getWithinMyReportingHierarchy()).getWithinMyReportingHierarchy();



Here is the code I put in a on-demand scheduled job, which worked successfully.


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'


};




var reportHierarchy = new getWithinMyReportingHierarchy();


var result = reportHierarchy.getWithinMyReportingHierarchy();


gs.log(result, 'hie');



regards,



Mike


andrewgamble
Tera Contributor

Mike,



I was able to get that one to work.   The results are going up the hierarchy instead of going down so I'm getting the current user's manager and the manager's manager.   I'm wanting it to return the current user's subordinates and then the subordinates of those subordinates going down 6 levels or until it reaches a non-manager user.   Is there an easy way to reverse this script to make it go the other way?



Thanks again for all your help!   I'm still new to most of this.



-Andrew


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


Mike,



It's working beautifully!   If you're ever in Southwest Florida, I totally owe you a drink!!



Thanks again,


Andrew


You're welcome Andrew. I'm glad I was able to help. If I'm ever in the neighborhood, I might take you up on that. Have a great 4th!