Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
This business rule is designed for ServiceNow to prevent a parent incident from being closed or resolved while it still has active child incidents.
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.
If open children are found, the update will be aborted, and an error message will be displayed to the user.

Navigate to System Definition > Business Rules in the ServiceNow filter navigator.
Click New.
Fill out the form with the following details:
Name: Prevent Parent Closure with Open Children
Table: Incident [incident]
Advanced: true
When: before
Update: Check this box.
In the When to run tab, set the Condition field:
current.state.changesTo(7) || current.state.changesTo(6) || current.state.changesTo(8)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just my preference have them count up so 6, 7, 8, and add what 6, 7, 8 are (closed, resolved, canceled (like in line 6 of your code))

Note: The state values (6, 7, 8) may vary based on your instance configuration.
In the Advanced tab, paste the provided script into the Script field.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
(function executeRule(current, previous /*null when async*/ ) {

// Query for any child incidents related to the current parent incident.
var childIncidents = new GlideRecord('incident');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using a GlideAggregate, it is more efficient (getting the aggregate instead of full records) and getting the count of child incidents is enough to allow or prevent the closure of the parent.

childIncidents.addQuery('parent_incident', current.sys_id);
childIncidents.addEncodedQuery('state!=6^ORstate!=7^ORstate!=8'); // state is not closed,Resoved,cancelled
childIncidents.addActiveQuery();
childIncidents.query();
// Check if any open child incidents were found.
if (childIncidents.getRowCount() > 0) {
var childNumbers = [];
while (childIncidents.next()) {
childNumbers.push(childIncidents.number.toString());
}
// Display an error message with the open child incident numbers.
gs.addErrorMessage('Cannot close this incident. Please close the following child incidents first: ' + childNumbers.join(', '));
//prevent saving the record
current.setAbortAction(true);
}

})(current, previous);
Loading