Create Approval Records
Automatically create approval records and request approvals when certain conditions are met (e.g., high-cost purchases, major changes).
Table: change_request When: after
#approval #workflow #after #insert #update #sysapproval_approver #automation
Script Code
JavaScript
1(function executeRule(current, previous /*null when async*/) {
2
3 // Configuration: Define approval requirements
4 var requiresApproval = false;
5 var approvers = [];
6 var approvalLevel = 'sequential'; // 'sequential' or 'parallel'
7
8 // Example 1: Require approval for high-risk changes
9 if (current.risk.toString() === '1' || current.risk.toString() === '2') { // High or Moderate risk
10 requiresApproval = true;
11
12 // Add CAB manager as approver
13 var cabManager = gs.getProperty('change.cab_manager'); // Customize property name
14 if (cabManager) {
15 approvers.push(cabManager);
16 }
17
18 // Add Change Manager
19 var changeManager = gs.getProperty('change.manager');
20 if (changeManager) {
21 approvers.push(changeManager);
22 }
23 }
24
25 // Example 2: Require approval for emergency changes
26 if (current.type.toString() === 'emergency') {
27 requiresApproval = true;
28
29 // Add VP of IT
30 var vpIT = gs.getProperty('it.vp_user_id');
31 if (vpIT) {
32 approvers.push(vpIT);
33 }
34 }
35
36 // Example 3: Require approval based on affected CIs
37 if (!current.cmdb_ci.nil()) {
38 var ci = current.cmdb_ci.getRefRecord();
39 if (ci.u_criticality.toString() === 'mission_critical') {
40 requiresApproval = true;
41
42 // Add CI owner as approver
43 if (!ci.owned_by.nil()) {
44 approvers.push(ci.owned_by.toString());
45 }
46 }
47 }
48
49 // Only process on insert or when approval is not yet requested
50 if (requiresApproval && (current.isNewRecord() || current.approval.toString() === 'not requested')) {
51
52 // Set approval state to requested
53 current.approval = 'requested';
54 current.update();
55
56 // Create approval records
57 var order = 1;
58 approvers.forEach(function(approverId) {
59 // Check if approval already exists
60 var grExisting = new GlideRecord('sysapproval_approver');
61 grExisting.addQuery('source_table', current.getTableName());
62 grExisting.addQuery('sysapproval', current.sys_id);
63 grExisting.addQuery('approver', approverId);
64 grExisting.setLimit(1);
65 grExisting.query();
66
67 if (!grExisting.hasNext()) {
68 // Create new approval record
69 var grApproval = new GlideRecord('sysapproval_approver');
70 grApproval.initialize();
71 grApproval.source_table = current.getTableName();
72 grApproval.sysapproval = current.sys_id;
73 grApproval.approver = approverId;
74 grApproval.state = 'requested';
75
76 // Set order for sequential approvals
77 if (approvalLevel === 'sequential') {
78 grApproval.order = order;
79 order++;
80 }
81
82 // Optional: Set due date (e.g., 24 hours from now)
83 var dueDate = new GlideDateTime();
84 dueDate.addDaysLocalTime(1);
85 grApproval.due_date = dueDate;
86
87 var approvalId = grApproval.insert();
88
89 if (approvalId) {
90 gs.info('Created approval for ' + current.number + ', approver: ' +
91 grApproval.approver.getDisplayValue());
92 }
93 }
94 });
95
96 // Add work note
97 if (approvers.length > 0) {
98 current.work_notes = 'Approval requested from ' + approvers.length + ' approver(s)';
99 gs.eventQueue('approval.requested', current, current.sys_id, approvers.join(','));
100 }
101 }
102
103 // Check if all approvals are complete
104 if (current.approval.toString() === 'requested') {
105 var grApprovals = new GlideRecord('sysapproval_approver');
106 grApprovals.addQuery('source_table', current.getTableName());
107 grApprovals.addQuery('sysapproval', current.sys_id);
108 grApprovals.query();
109
110 var allApproved = true;
111 var anyRejected = false;
112
113 while (grApprovals.next()) {
114 if (grApprovals.state.toString() === 'requested') {
115 allApproved = false;
116 }
117 if (grApprovals.state.toString() === 'rejected') {
118 anyRejected = true;
119 }
120 }
121
122 // Update parent record approval status
123 if (anyRejected) {
124 current.approval = 'rejected';
125 current.work_notes = 'Change request rejected by approver';
126 } else if (allApproved) {
127 current.approval = 'approved';
128 current.work_notes = 'All approvals completed - change approved';
129 }
130 }
131
132})(current, previous);
How to Use
1. Create an after Business Rule on your table
2. Check "Insert" and "Update" checkboxes
3. Customize the approval requirements and logic
4. Update approver sys_ids or use system properties
5. Test with various scenarios to ensure proper approval routing
6. Consider using ServiceNow's approval engine for complex workflows
Related Scripts
Auto-assign Based on Category
Automatically assign tickets to the appropriate assignment group based on category.
Send Email Notifications
Send customized email notifications when specific conditions are met.
Populate Fields on Insert
Automatically populate fields with default values or calculated values when a record is created.