Skip to content

Commit c93a7aa

Browse files
ServiceNow After business rule script
Auto close incident if all related change requests are closed
1 parent 886cbb9 commit c93a7aa

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
(function executeRule(current, previous /*null when async*/) {
2+
// Run only when change_request moves to Closed
3+
if (current.state != previous.state && current.state == 3) { // 3 = Closed
4+
var incidentSysId = current.incident; // assuming there is a reference field to Incident
5+
6+
if (!incidentSysId) {
7+
gs.info("No related incident found for this change request: " + current.number);
8+
return;
9+
}
10+
11+
// Query for other open change requests linked to the same incident
12+
var otherCR = new GlideRecord('change_request');
13+
otherCR.addQuery('incident', incidentSysId);
14+
otherCR.addQuery('sys_id', '!=', current.sys_id);
15+
otherCR.addQuery('state', '!=', 3); // not closed
16+
otherCR.query();
17+
18+
if (otherCR.hasNext()) {
19+
gs.info("Incident " + incidentSysId + " still has open change requests. Not closing incident.");
20+
return;
21+
}
22+
23+
// If no open change requests remain, close the incident
24+
var inc = new GlideRecord('incident');
25+
if (inc.get(incidentSysId)) {
26+
inc.state = 7; // 7 = Closed (modify as per your instance)
27+
inc.close_code = 'Auto Closed';
28+
inc.close_notes = 'Incident auto-closed as all associated change requests are closed.';
29+
inc.update();
30+
gs.info("Incident " + inc.number + " auto-closed as all related change requests are closed.");
31+
}
32+
33+
}
34+
})(current, previous);
35+

0 commit comments

Comments
 (0)