Skip to content

Commit 5d284c5

Browse files
authored
Prevent closure parent incident when child incidents open (#1750)
* Create scriptBR.js * Create README.md * Update scriptBR.js * Update README.md * Update README.md
1 parent ce798cc commit 5d284c5

File tree

2 files changed

+42
-0
lines changed
  • Server-Side Components/Business Rules/Abort Parent Incident Closure When Child is Open

2 files changed

+42
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
This business rule is designed for ServiceNow to prevent a parent incident from being closed or resolved while it still has active child incidents.
2+
If a user attempts to set the parent incident's state to "Resolved," "Closed," or "Cancelled," the rule will query for any related child incidents that are still open.
3+
If open children are found, the update will be aborted, and an error message will be displayed to the user.
4+
5+
Navigate to System Definition > Business Rules in the ServiceNow filter navigator.
6+
Click New.
7+
Fill out the form with the following details:
8+
Name: Prevent Parent Closure with Open Children
9+
Table: Incident [incident]
10+
Advanced: true
11+
When: before
12+
Update: Check this box.
13+
In the When to run tab, set the Condition field:
14+
current.state.changesTo(7) || current.state.changesTo(6) || current.state.changesTo(8) //The state values are: 6 (Resolved), 7 (Closed), 8 (Cancelled).
15+
Note: The state values (6, 7, 8) may vary based on your instance configuration.
16+
In the Advanced tab, paste the provided script into the Script field.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
(function executeRule(current, previous /*null when async*/) {
2+
3+
4+
//When the Incident state values are set to: 6 (Resolved), 7 (Closed), 8 (Cancelled).
5+
// The `previous.state` check prevents the script from running when a closed ticket is re-closed.
6+
if ((current.state == '6' || current.state == '7' || current.state == '8') && current.state != previous.state) {
7+
8+
// Use GlideAggregate to efficiently count child incidents that are not yet closed.
9+
var ga = new GlideAggregate('incident');
10+
ga.addQuery('parent_incident', current.sys_id);
11+
ga.addActiveQuery();
12+
ga.addAggregate('COUNT');
13+
ga.query();
14+
var childCount = 0;
15+
if (ga.next()) {
16+
// Retrieve the aggregated count.
17+
childCount = ga.getAggregate('COUNT');
18+
}
19+
// If open child incidents are found, abort the parent's closure and display an error.
20+
if (childCount > 0) {
21+
gs.addErrorMessage('Cannot close this incident. ' + childCount + ' child incidents are still open.');
22+
current.setAbortAction(true);
23+
}
24+
}
25+
26+
})(current, previous);

0 commit comments

Comments
 (0)