
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-22-2020 06:13 PM
I need to create a report for all incidents where the caller falls under a specific department, but the department we want to look at could have several layers of child departments under it.
I started writing a report that had a similar structure to:
caller_id.department = 'DeptA' OR
caller_id.department.parent = 'DeptA' OR
caller_id.department.parent.parent = 'DeptA' OR
...
...
...
but this seems really inefficient and prone to breaking if a new department layer is added in beyond what has been coded for.
Is there an better way to get a list for all the possible sub-departments and the parent department than what I outlined above?
Solved! Go to Solution.
- Labels:
-
Reporting

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-23-2020 11:50 AM
Hi
Yes we can iterate through the child departments so that we can roll up every level. Amend the above script include (Client Callable TRUE) as below:
var getChildDepartment = Class.create();
getChildDepartment.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getChildDepartmentMethod: function(parentDept){
var arr = [parentDept];
var deptArr = this.getAllDepartments(parentDept, arr);
return deptArr;
},
getAllDepartments : function(parentDept, returnArr){
var dept = new GlideRecord('cmn_department');
dept.addEncodedQuery('parent='+parentDept);
dept.query();
while(dept.next()){
returnArr.push(dept.sys_id.toString());
this.getAllDepartments(dept.sys_id.toString(), returnArr);
}
return returnArr;
},
type: 'getChildDepartment'
});
The query on the report changes as below where I have input the 'sys_id' of the department. Since we could be dealing with 'n' number of departments as we roll down the parent-child hierarchy, its best we input the 'sys_id' of the department.
So your query will be 'Caller.Department.SysID' 'is one of' 'javascript: (new getChildDepartment()).getChildDepartmentMethod('sysIDofDepartment')'
I have created a hierarchy as below:-
And my report where I input the sys_id of "Department 1" (which is grouped by caller.department) looks like below:-
Hope it helps! 🙂
Kindly mark my comment as correct/helpful based on the impact. Thank You!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-28-2022 02:08 AM
Hello,
How can I make the starting department dynamic and use the department id of the logged in user?