From 387498cf2f33419c24402a7b0e9c2cdbe883d80c Mon Sep 17 00:00:00 2001 From: Lucifer <108731648+shivamvish160@users.noreply.github.com> Date: Wed, 22 Oct 2025 10:31:51 +0530 Subject: [PATCH 1/2] Create README.md --- .../README.md | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Server-Side Components/Script Includes/Dynamic Approval Routing Based on Organizational Hierarchy/README.md diff --git a/Server-Side Components/Script Includes/Dynamic Approval Routing Based on Organizational Hierarchy/README.md b/Server-Side Components/Script Includes/Dynamic Approval Routing Based on Organizational Hierarchy/README.md new file mode 100644 index 0000000000..bcc501f214 --- /dev/null +++ b/Server-Side Components/Script Includes/Dynamic Approval Routing Based on Organizational Hierarchy/README.md @@ -0,0 +1,24 @@ +# Dynamic Approval Routing Based on Organizational Hierarchy + +## Overview + +This Script Include provides a dynamic way to determine approval routing based on a user's organizational hierarchy, including their manager, department head, and role-based approvers. It is designed to be used in workflows, business rules, or Flow Designer actions to automate complex approval chains. + +## Features + +- Retrieves a user's manager as an approver. +- Adds department head if available. +- Includes role-based approvers (e.g., Finance Head for users with `finance_approver` role). +- Easily extendable for other roles or organizational logic. + +## Usage + +### Script Include: `ApprovalRouter` + +Call the `getApprovers(userId)` method to retrieve a list of approver `sys_id`s. + +```javascript +var router = new ApprovalRouter(); +var approvers = router.getApprovers(current.requested_for.toString()); + +gs.info('Approvers: ' + approvers.join(', ')); From 8ed3d88e48a157a0f8d9556d52b00ea6d9935e48 Mon Sep 17 00:00:00 2001 From: Lucifer <108731648+shivamvish160@users.noreply.github.com> Date: Wed, 22 Oct 2025 10:32:18 +0530 Subject: [PATCH 2/2] Create code.js --- .../code.js | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Server-Side Components/Script Includes/Dynamic Approval Routing Based on Organizational Hierarchy/code.js diff --git a/Server-Side Components/Script Includes/Dynamic Approval Routing Based on Organizational Hierarchy/code.js b/Server-Side Components/Script Includes/Dynamic Approval Routing Based on Organizational Hierarchy/code.js new file mode 100644 index 0000000000..42495c370f --- /dev/null +++ b/Server-Side Components/Script Includes/Dynamic Approval Routing Based on Organizational Hierarchy/code.js @@ -0,0 +1,57 @@ +var ApprovalRouter = Class.create(); +ApprovalRouter.prototype = { + initialize: function() {}, + + /** + * Returns an array of approver sys_ids based on user's department and role. + * @param {String} userId - Sys ID of the user for whom to find approvers. + * @returns {Array} approverSysIds - List of approver sys_ids. + */ + getApprovers: function(userId) { + var approverSysIds = []; + + // Get user record + var userGR = new GlideRecord('sys_user'); + if (!userGR.get(userId)) { + gs.error('User not found: ' + userId); + return approverSysIds; + } + + // Add manager if exists + if (userGR.manager) { + approverSysIds.push(userGR.manager.toString()); + } + + // Add department head if available + if (userGR.department) { + var deptGR = new GlideRecord('cmn_department'); + if (deptGR.get(userGR.department)) { + if (deptGR.u_department_head) { + approverSysIds.push(deptGR.u_department_head.toString()); + } + } + } + + // Add role-based approvers (e.g., Finance Approver) + var roleGR = new GlideRecord('sys_user_has_role'); + roleGR.addQuery('user', userId); + roleGR.query(); + while (roleGR.next()) { + var roleName = roleGR.role.name; + if (roleName == 'finance_approver') { + // Add finance head + var financeGR = new GlideRecord('sys_user'); + financeGR.addQuery('title', 'Finance Head'); + financeGR.setLimit(1); + financeGR.query(); + if (financeGR.next()) { + approverSysIds.push(financeGR.sys_id.toString()); + } + } + } + + return approverSysIds; + }, + + type: 'ApprovalRouter' +};