Skip to content

Commit 12b0a2e

Browse files
authored
Update set scope validation to prevent completion (#2441)
* Adding the Validation for the scopes for the update sets * Clarify script section in README Updated the README to clarify the script section.
1 parent af03b70 commit 12b0a2e

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
# Business Rule: Validate Update Set Scope
3+
4+
5+
## Challenge
6+
7+
When working with update sets, mismatched scopes can lead to issues during the preview process in the target instance. This often results in errors that require manual intervention to resolve. Such errors can be time-consuming, especially for minor mismatches that could have been identified and prevented earlier. This challenge highlights the need for a mechanism to validate update set scopes before completion, saving time and effort during deployment.
8+
9+
10+
## Overview
11+
This business rule ensures that all customer updates within an update set match the application scope of the update set. If any updates have mismatched scopes, the business rule restricts the completion of the update set and displays a message listing the files with mismatched scopes.
12+
13+
## Features
14+
- Validates the scope of all updates in an update set.
15+
- Prevents the completion of an update set if scope mismatches are detected.
16+
- Displays a detailed message listing the files with mismatched scopes.
17+
18+
## Use Case
19+
This business rule is useful for ensuring that updates in an update set adhere to the correct application scope, preventing potential issues caused by scope mismatches.
20+
21+
## Implementation
22+
23+
### 1. Create the Business Rule
24+
1. Navigate to **System Definition > Business Rules** in your ServiceNow instance.
25+
2. Click **New** to create a new business rule.
26+
3. Configure the business rule as follows:
27+
- **Name**: Validate Update Set Scope
28+
- **Table**: `sys_update_set`
29+
- **When**: Before
30+
- **Insert**: False
31+
- **Update**: True
32+
- **Delete**: False
33+
34+
### 2. Add the Script
35+
Use the following script in the **Script** field:
36+
37+
```javascript
38+
(function executeRule(current, previous /*null when async*/) {
39+
40+
// Add attached code here
41+
42+
})(current, previous);
43+
44+
```
45+
46+
### 3. Test the Business Rule
47+
1. Create an update set and add updates with different scopes.
48+
2. Attempt to complete the update set.
49+
3. Verify that the business rule prevents completion and displays the appropriate error message.
50+
51+
52+
## Screenshots
53+
![Output Result](error.png)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
(function executeRule(current, previous /*null when async*/) {
2+
3+
// Add your code here
4+
5+
var updateSetSysId = current.sys_id; // Get the sys_id of the current update set
6+
var updateSetScope = current.application; // Get the scope of the current update set
7+
var gr = new GlideRecord('sys_update_xml');
8+
gr.addQuery('update_set', updateSetSysId); // Query for records in the current update set
9+
gr.query();
10+
11+
var misMatchedUpdates = [];
12+
while (gr.next()) {
13+
if (gr.application != updateSetScope) { // Check if the scope matches the update set scope
14+
misMatchedUpdates.push( gr.target_name.toString() + ' (' + gr.type.toString() + ')'); // Collect the file names with mismatched scope
15+
}
16+
}
17+
18+
if (misMatchedUpdates.length > 0) {
19+
gs.addErrorMessage('The following files have a different scope than the update set scope: \n' + misMatchedUpdates.join(', '));
20+
current.setAbortAction(true); // Prevent the update set from being completed
21+
}
22+
23+
24+
25+
})(current, previous);
153 KB
Loading

0 commit comments

Comments
 (0)