Skip to content

Commit 7c55c08

Browse files
Auto close incident if all related change requests are closed (#2542)
* Create Readme.md Auto close incident if all related changes are closed * ServiceNow After business rule script Auto close incident if all related change requests are closed
1 parent 4cc0540 commit 7c55c08

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Business Rule: Auto-Close Incident When All Related Changes Are Closed
2+
Table : change_request
3+
When to Run: After update
4+
Condition: state changes to Closed (or your equivalent "Closed" state number, e.g. state == 3)
5+
6+
Detailed Working
7+
1. Trigger Point
8+
This After Business Rule runs after a Change Request record is updated.
9+
Specifically, it checks when the state changes to “Closed”.
10+
11+
2. Check for Related Incident
12+
The script retrieves the incident reference field (incident) from the current change request.
13+
If there’s no linked incident, it skips execution.
14+
15+
3. Check for Any Remaining Open Change Requests
16+
A new GlideRecord query checks for other Change Requests linked to the same incident where:
17+
If any such records exist, it means not all change requests are closed — so the incident remains open.
18+
19+
4. Close the Incident Automatically
20+
If no open Change Requests remain, the script:
21+
Fetches the linked incident.
22+
Sets: state = 7 (Closed)
23+
close_code = Auto Closed
24+
close_notes = Auto closure as all changes are closed.
25+
Updates the record.
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)