- 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 11:33 AM
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'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-10-2017 08:59 AM
To get all child CI's please use CIUtils2_flatten script include. for more details you can visit the below link
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-10-2017 11:18 PM
Hi Sriyad,
That is a lot complex. Can you please help in modifying my code to work it properly?
Regards
Ramandeep
- 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-16-2017 01:50 AM
Hi sanjiv,
Your answer is correct. Just small addition "finalArr.push(gr.sys_id);" needs to be replaced with "finalArr.push(gr.sys_id+");"
