Skip to content

Commit b481665

Browse files
ServiceNow Approval Matrix Generator (#2153)
* Create Catalog.js Catalog Builder API - A Scripted REST API to auto-create ServiceNow Catalog Items programmatically. * Create README.md This readme file has info about how to Automate Catalog Item Creation with a Single REST Call. * Update README.md Automate Catalog Item Creation with a Single REST Call * Delete Integration/Scripted REST Api/Create Catalog Item Dynamically/README.md * Delete Integration/Scripted REST Api/Create Catalog Item Dynamically/Catalog.js * Create Matrix.js Instead of hardcoding approvals in Flow Designer or scripting them per catalog item, you can maintain one Approval Matrix table. * Create README.md The ServiceNow Approval Matrix Generator is a configurable, data-driven engine that automatically assigns approvers based on business rules such as department, amount, or role.
1 parent 31c4639 commit b481665

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*Scenario :
2+
Whenever a record (like an HR Case, Request Item, or Change) is created, the script:
3+
Looks up the right approvers based on dynamic rules (department, amount, category, etc.)
4+
Automatically creates approvals in the sysapproval_approver table.*/
5+
6+
/* Business Rule :
7+
8+
Table: sc_request or proc_po_request or custom table
9+
When: After Insert
10+
Condition: Only when approval is required */
11+
12+
(function executeRule(current, previous /*null when async*/) {
13+
14+
try {
15+
var dept = current.u_department.name + '';
16+
var amount = parseFloat(current.u_amount + '');
17+
if (!dept || isNaN(amount)) {
18+
gs.info('Approval Matrix: Missing department or amount');
19+
return;
20+
}
21+
22+
// Query approval matrix for matching rule
23+
var matrix = new GlideRecord('u_approval_matrix');
24+
matrix.addQuery('u_department.name', dept);
25+
matrix.addQuery('u_min_amount', '<=', amount);
26+
matrix.addQuery('u_max_amount', '>=', amount);
27+
matrix.query();
28+
29+
if (!matrix.hasNext()) {
30+
gs.info('Approval Matrix: No matching rule found for ' + dept + ', amount: ' + amount);
31+
return;
32+
}
33+
34+
while (matrix.next()) {
35+
var approverUser = '';
36+
37+
// Option 1: Approver directly specified
38+
if (!gs.nil(matrix.u_approver)) {
39+
approverUser = matrix.u_approver;
40+
}
41+
// Option 2: Use role from requester’s hierarchy
42+
else if (!gs.nil(matrix.u_role)) {
43+
approverUser = getApproverByRole(current.requested_for, matrix.u_role + '');
44+
}
45+
46+
if (approverUser) {
47+
createApproval(current.sys_id, current.getTableName(), approverUser);
48+
gs.info('Approval Matrix: Created approval for ' + approverUser);
49+
}
50+
}
51+
52+
} catch (ex) {
53+
gs.error('Approval Matrix Error: ' + ex.message);
54+
}
55+
56+
// --- Helper: Find approver based on user role ---
57+
function getApproverByRole(userSysId, roleName) {
58+
var usr = new GlideRecord('sys_user');
59+
if (usr.get(userSysId)) {
60+
if (roleName == 'Manager' && usr.manager) return usr.manager;
61+
if (roleName == 'Director' && usr.u_director) return usr.u_director; // custom field
62+
if (roleName == 'VP' && usr.u_vp) return usr.u_vp;
63+
}
64+
return '';
65+
}
66+
67+
// --- Helper: Create approval record ---
68+
function createApproval(targetSysId, targetTable, approverSysId) {
69+
var appr = new GlideRecord('sysapproval_approver');
70+
appr.initialize();
71+
appr.sysapproval = targetSysId;
72+
appr.table_name = targetTable;
73+
appr.state = 'requested';
74+
appr.approver = approverSysId;
75+
appr.insert();
76+
}
77+
78+
})(current, previous);
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# ServiceNow Approval Matrix Generator
2+
**A Dynamic, Data-Driven Approval Workflow Engine for ServiceNow**
3+
4+
---
5+
6+
## Overview
7+
8+
The **Approval Matrix Generator** is a configurable engine that automates approval generation in ServiceNow based on business rules —
9+
without hard-coding approvers or creating dozens of Flow Designer flows.
10+
11+
By maintaining a simple **Approval Matrix table**, you can define which user or role should approve a request dynamically (e.g., based on department and amount).
12+
This approach provides scalability, maintainability, and full visibility across all approval logic.
13+
14+
---
15+
16+
## Key Highlights
17+
18+
✅ 100% native ServiceNow solution (no plugins)
19+
✅ Centralized approval logic in a single configuration table
20+
✅ Works for ITSM, HRSD, Finance, or Procurement workflows
21+
✅ Supports multi-level approvals (Manager → Director → CFO)
22+
✅ Can run via **Business Rule**
23+
24+
---
25+
26+
## Use Case
27+
28+
An organization wants dynamic approval routing for procurement or HR requests based on:
29+
- Department (IT, HR, Finance)
30+
- Request amount (₹0–₹10,000, etc.)
31+
- Approval role (Manager, Director, VP)
32+
33+
Instead of building multiple flows, the Approval Matrix defines all rules in a single table.
34+
When a new request is submitted, the script automatically finds and assigns the correct approvers.
35+
36+
---
37+
38+
## Step 1 — Create Table `u_approval_matrix`
39+
40+
| Field | Type | Description |
41+
|--------|------|-------------|
42+
| **u_department** | Reference (sys_user_group) | Department owning the rule |
43+
| **u_min_amount** | Decimal | Minimum amount for range |
44+
| **u_max_amount** | Decimal | Maximum amount for range |
45+
| **u_role** | Choice | Role type – Manager / Director / VP |
46+
| **u_approver** | Reference (sys_user) | Direct approver (optional) |
47+
48+
This table drives all the logic.
49+
Example data:
50+
51+
| Department | Min | Max | Role | Approver |
52+
|-------------|-----|-----|------|-----------|
53+
| IT | 0 | 5000 | Manager | *(blank)* |
54+
| IT | 5000 | 10000 | Director | *(blank)* |
55+
| Finance | 0 | 10000 | *(blank)* | John CFO |
56+
57+
---
58+
59+
## ⚙️ Step 2 — Business Rule Script
60+
61+
**Table:** `sc_request` (or your custom table)
62+
**When:** After Insert
63+
**Condition:** Approval required
64+
65+
## Example Input (Request Record)
66+
| Field | Value |
67+
| ------------- | ----------------- |
68+
| Requested For | Ravi Gaurav |
69+
| Department | IT |
70+
| Amount | 8000 |
71+
| Category | Hardware Purchase |
72+
73+
## Example Output
74+
| Field | Value |
75+
| -------------------- | ----------------------------------------- |
76+
| Matched Rule | IT, 5000–10000, Role = Director |
77+
| Approver Found | Ravi’s Director (from `u_director` field) |
78+
| Approval State | Requested |
79+
| sysapproval_approver | Created Automatically |

0 commit comments

Comments
 (0)