-
Notifications
You must be signed in to change notification settings - Fork 909
Prevent closure parent incident when child incident is open #1748
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
naveensnow
wants to merge
2
commits into
ServiceNowDevProgram:main
from
naveensnow:PreventClosureIncident
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
...nents/Business Rules/Abort Parent Incident Closure When Child is Open/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| 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. | ||
21 changes: 21 additions & 0 deletions
21
...de Components/Business Rules/Abort Parent Incident Closure When Child is Open/scriptBR.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
naveensnow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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))