- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-10-2017 08:36 AM
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
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-11-2017 06:13 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-10-2017 08:38 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-10-2017 08:49 AM
Try below code
getChildDepartment: function(dep) {
var finalArr = [];
var gr = new GlideRecord('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;
},
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-10-2017 11:23 AM
Hi sanjiv,
The code you mentioned below will return only "B" and "C".
Regards
Ramandeep
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-10-2017 11:34 AM
getChildDepartment: function(dep) {
var finalArr = [];
var gr = new GlideRecord('cmn_department');
gr.addQuery('u_parent', dep);
gr.query();
while(gr.next()){
finalArr.push(gr.sys_id);
if (gr.u_parent!='')
this._getChildDepartment(gr.sys_id);
}
return finalArr;
},
Please mark this response as correct or helpful if it assisted you with your question.
