Skip to content

Commit 3361fce

Browse files
Merge branch 'main' into patch-1
2 parents 95831b6 + 55d8b4b commit 3361fce

File tree

4 files changed

+143
-0
lines changed

4 files changed

+143
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Count Open Incidents per Priority Using GlideAggregate
2+
3+
## Overview
4+
This script dynamically calculates the **number of open incidents** for each priority level using **server-side scripting** in ServiceNow.
5+
Priority levels typically include:
6+
+ 1 – Critical
7+
+ 2 – High
8+
+ 3 – Moderate
9+
+ 4 – Low
10+
11+
The solution leverages **GlideAggregate** to efficiently count records grouped by priority. This approach is useful for:
12+
+ Dashboards
13+
+ Automated scripts
14+
+ Business rules
15+
+ SLA monitoring and reporting
16+
17+
---
18+
19+
## Table and Fields
20+
+ **Table:** `incident`
21+
+ **Fields:** `priority`, `state`
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
(function() {
2+
// Create GlideAggregate object on 'incident' table
3+
var ga = new GlideAggregate('incident');
4+
5+
// Filter only open incidents (state != Closed (7))
6+
ga.addQuery('state', '!=', 7);
7+
8+
// Group results by priority
9+
ga.groupBy('priority');
10+
11+
// Count number of incidents per priority
12+
ga.addAggregate('COUNT');
13+
14+
ga.query();
15+
16+
gs.info('Open Incidents by Priority:');
17+
18+
while (ga.next()) {
19+
var priority = ga.priority.getDisplayValue(); // e.g., Critical, High
20+
var count = ga.getAggregate('COUNT');
21+
gs.info(priority + ': ' + count);
22+
}
23+
})();
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
## ServiceNow Fix Script: Remove Extra Spaces
2+
A generic ServiceNow fix script to clean data by removing extra whitespace from any specified field on any table.
3+
4+
### Problem It Solves
5+
This script resolves data integrity issues caused by inconsistent user input, such as:
6+
- Leading or trailing spaces (e.g., " Hello World ").
7+
- Multiple spaces between words (e.g., "Hello World").
8+
9+
### How to use
10+
1. Create and Configure the Fix Script
11+
- First, create a new Fix Script in your ServiceNow instance (**System Definition > Fix Scripts**) add past the code
12+
13+
2. Add your table name and field that you want to clean up
14+
- Before running, you must update the following variables inside the script to define your target:
15+
```js
16+
var tableName = 'incident'; // <-- CHANGE THIS to your table name
17+
var fieldName = 'short_description'; // <-- CHANGE THIS to your field name
18+
```
19+
20+
3. Change `processRecords` value and run
21+
- To see what changes will be made without actually updating records, ensure the `processRecords` variable in the script is set to `false`
22+
```js
23+
var processRecords = false;
24+
```
25+
- To actually do the update, change the `processRecords` variable to `true` and run the script
26+
```js
27+
var processRecords = true;
28+
```
29+
30+
4. Run the script
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Fix Script: Remove Extra Spaces from a Field
3+
*
4+
* Description:
5+
* This script removes leading/trailing spaces and collapses multiple
6+
* consecutive spaces into a single space for a specified field on a specified table.
7+
*
8+
* INSTRUCTIONS:
9+
* 1. Update the tableName and fieldName with the table and field you want to remove spaces from, usually fieldName will be name or short_description
10+
* 2. Set 'processRecords' to false for a preview, set to true to do the updates
11+
*/
12+
13+
(function () {
14+
// Set to true to perform the update, false to just preview the changes
15+
var processRecords = false;
16+
17+
var tableName = 'incident'; // Add your table name
18+
var fieldName = 'short_description'; // Add the field that might contain leading spaces
19+
20+
var recordsUpdated = 0;
21+
22+
if (gs.nil(tableName) || gs.nil(fieldName)) {
23+
gs.print('Please set the table and field name');
24+
return;
25+
}
26+
27+
gs.info('Starting space removal process for table: [' + tableName + '], field: [' + fieldName + ']');
28+
gs.info('Run mode: ' + (processRecords ? 'UPDATE' : 'PREVIEW'));
29+
30+
try {
31+
var gr = new GlideRecord(tableName);
32+
// Only query records where the target field is not empty
33+
gr.addNotNullQuery(fieldName);
34+
gr.query();
35+
36+
while (gr.next()) {
37+
var originalValue = gr.getValue(fieldName);
38+
39+
// Replace all occurrences of 2 or more spaces with a single space and trim leading/trailing whitespace
40+
var cleanedValue = originalValue.replace(/\s\s+/g, ' ').trim();
41+
42+
// Check if the value has actually changed
43+
if (originalValue !== cleanedValue) {
44+
gs.print('Record sys_id: ' + gr.getUniqueValue() + ' (Number: ' + gr.getDisplayValue('number') + ')\n' +
45+
'---> Original: "' + originalValue + '"\n' +
46+
'---> Cleaned: "' + cleanedValue + '"\n'
47+
);
48+
49+
if (processRecords) {
50+
gr.setValue(fieldName, cleanedValue);
51+
gr.setWorkflow(false);
52+
gr.autoSysFields(false);
53+
gr.update();
54+
recordsUpdated++;
55+
}
56+
}
57+
}
58+
59+
gs.print('Space removal process finished.');
60+
if (processRecords) {
61+
gs.info(recordsUpdated + ' records were updated.');
62+
} else {
63+
gs.print('This was a PREVIEW run. No records were updated. To apply changes, set the "processRecords" variable to true.');
64+
}
65+
66+
} catch (e) {
67+
gs.error('An error occurred during the space removal script: ' + e.message);
68+
}
69+
})();

0 commit comments

Comments
 (0)