The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Best way to create a report for all departments that roll up to a specific department?

Brian Bouchard
Mega Sage

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?

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi @Brian Bouchard ,

 

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')'

find_real_file.png

 

 

I have created a hierarchy as below:-

 

find_real_file.png

 

And my report where I input the sys_id of "Department 1" (which is grouped by caller.department) looks like below:-

 

find_real_file.png

 

Hope it helps! 🙂

 

Kindly mark my comment as correct/helpful based on the impact. Thank You!

View solution in original post

5 REPLIES 5

Hello,
How can I make the starting department dynamic and use the department id of the logged in user?