Skip to content

Commit 9f876c5

Browse files
Create Matrix.js
Instead of hardcoding approvals in Flow Designer or scripting them per catalog item, you can maintain one Approval Matrix table.
1 parent c0877b3 commit 9f876c5

File tree

1 file changed

+78
-0
lines changed
  • Server-Side Components/Business Rules/Approval Matrix

1 file changed

+78
-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);

0 commit comments

Comments
 (0)