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.

Function required to find all childs of the parent on same table

ramandeepgarg6
Kilo Guru

Hi Friends,

I have a "Department" table which contains a "Parent Department" field. See below screenshot. If I select department as 'A' then it should get all children as [A, B, C, BB1, BB2, CC1, CC2].

I have written below code but it is not working properly and it is not able to find all the departments. Could you please help?

getChildDepartment: function(dep, finalArr) {
var gr = new GlideRecord('u_cmn_department');
gr.addQuery('u_parent', dep);
gr.query();
while(gr.next()){
finalArr.push(gr.sys_id);
this._getChildDepartment(gr.sys_id, finalArr);
}
return finalArr;
},

Regards

Ramandeep

1 ACCEPTED SOLUTION

This is all you need.


I hope the table name i have put is correct 'cmn_department'


Try running the below script in Background Script



getChildDepartment('enter your department name here');


function getChildDepartment(dep)


{


var finalArr = [];


var gr = new GlideRecord('cmn_department');


gr.addQuery('u_parent.name', dep);


gr.query();


while(gr.next()){


finalArr.push(gr.sys_id);


while (gr.u_parent!='')


gs.addInfoMessage(gr.name);


        this.getChildDepartment(gr.name);


}



return finalArr;


}



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

View solution in original post

14 REPLIES 14

Hi Alikutty,



This is the filter: "javascript: new issuesByDepartment().issuesByDep();"



This is the complete script include:



var issuesByDepartment = Class.create();


issuesByDepartment.prototype = Object.extendsObject(AbstractAjaxProcessor, {



issuesByDep: function()


{


gs.log('testing');


var user = this._getCurrentUser();


gs.log('user is ' + user.user_name);


var dep = user.u_cmn_department;


var arr = [];


var arr1 = [];




if (dep != ''){


arr1.push(dep);


arr = this._getChildDepartment(dep, arr1);


gs.log('RRRarr is '+ arr);


                          } return arr;},



_getChildDepartment: function(dep, finalArr) {


  //var finalArr = [];


  var tempArr = [];


var gr = new GlideRecord('u_cmn_department');


gr.addQuery('u_parent', dep);


gr.query();


while(gr.next()){


  if(finalArr.join().indexOf(gr.sys_id) > -1){




                            continue;}


//This is test


  finalArr.push(gr.sys_id);


//this is test


this._getChildDepartment(gr.sys_id, finalArr);


}



gs.log('RRRfinalArr is '+ finalArr);


return finalArr;


},




_getCurrentUser: function()


  {


    var gr = new GlideRecord('sys_user');


    gr.addQuery('sys_id', gs.getUserID());


    gr.query();


    gr.next();


    gs.log('RRRfinalArr is '+ gr.user_name);


    return gr;


  },


      type: 'issuesByDepartment'


});


sriyad
Giga Contributor

To get all child CI's please use CIUtils2_flatten script include. for more details you can visit the below link



http://www.servicenowguru.com/scripting/script-includes-scripting/walking-servicenowcom-cmdb-relatio...


ramandeep
Kilo Contributor

Hi Sriyad,



That is a lot complex. Can you please help in modifying my code to work it properly?



Regards


Ramandeep


This is all you need.


I hope the table name i have put is correct 'cmn_department'


Try running the below script in Background Script



getChildDepartment('enter your department name here');


function getChildDepartment(dep)


{


var finalArr = [];


var gr = new GlideRecord('cmn_department');


gr.addQuery('u_parent.name', dep);


gr.query();


while(gr.next()){


finalArr.push(gr.sys_id);


while (gr.u_parent!='')


gs.addInfoMessage(gr.name);


        this.getChildDepartment(gr.name);


}



return finalArr;


}



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

Hi sanjiv,



Your answer is correct. Just small addition "finalArr.push(gr.sys_id);" needs to be replaced with "finalArr.push(gr.sys_id+");"