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