Skip to content

Commit 8ed3d88

Browse files
Create code.js
1 parent 387498c commit 8ed3d88

File tree

1 file changed

+57
-0
lines changed
  • Server-Side Components/Script Includes/Dynamic Approval Routing Based on Organizational Hierarchy

1 file changed

+57
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
var ApprovalRouter = Class.create();
2+
ApprovalRouter.prototype = {
3+
initialize: function() {},
4+
5+
/**
6+
* Returns an array of approver sys_ids based on user's department and role.
7+
* @param {String} userId - Sys ID of the user for whom to find approvers.
8+
* @returns {Array} approverSysIds - List of approver sys_ids.
9+
*/
10+
getApprovers: function(userId) {
11+
var approverSysIds = [];
12+
13+
// Get user record
14+
var userGR = new GlideRecord('sys_user');
15+
if (!userGR.get(userId)) {
16+
gs.error('User not found: ' + userId);
17+
return approverSysIds;
18+
}
19+
20+
// Add manager if exists
21+
if (userGR.manager) {
22+
approverSysIds.push(userGR.manager.toString());
23+
}
24+
25+
// Add department head if available
26+
if (userGR.department) {
27+
var deptGR = new GlideRecord('cmn_department');
28+
if (deptGR.get(userGR.department)) {
29+
if (deptGR.u_department_head) {
30+
approverSysIds.push(deptGR.u_department_head.toString());
31+
}
32+
}
33+
}
34+
35+
// Add role-based approvers (e.g., Finance Approver)
36+
var roleGR = new GlideRecord('sys_user_has_role');
37+
roleGR.addQuery('user', userId);
38+
roleGR.query();
39+
while (roleGR.next()) {
40+
var roleName = roleGR.role.name;
41+
if (roleName == 'finance_approver') {
42+
// Add finance head
43+
var financeGR = new GlideRecord('sys_user');
44+
financeGR.addQuery('title', 'Finance Head');
45+
financeGR.setLimit(1);
46+
financeGR.query();
47+
if (financeGR.next()) {
48+
approverSysIds.push(financeGR.sys_id.toString());
49+
}
50+
}
51+
}
52+
53+
return approverSysIds;
54+
},
55+
56+
type: 'ApprovalRouter'
57+
};

0 commit comments

Comments
 (0)