Skip to content
Merged
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
30 changes: 30 additions & 0 deletions Specialized Areas/Fix scripts/Remove extra spaces/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## ServiceNow Fix Script: Remove Extra Spaces
A generic ServiceNow fix script to clean data by removing extra whitespace from any specified field on any table.

### Problem It Solves
This script resolves data integrity issues caused by inconsistent user input, such as:
- Leading or trailing spaces (e.g., " Hello World ").
- Multiple spaces between words (e.g., "Hello World").

### How to use
1. Create and Configure the Fix Script
- First, create a new Fix Script in your ServiceNow instance (**System Definition > Fix Scripts**) add past the code

2. Add your table name and field that you want to clean up
- Before running, you must update the following variables inside the script to define your target:
```js
var tableName = 'incident'; // <-- CHANGE THIS to your table name
var fieldName = 'short_description'; // <-- CHANGE THIS to your field name
```

3. Change `processRecords` value and run
- To see what changes will be made without actually updating records, ensure the `processRecords` variable in the script is set to `false`
```js
var processRecords = false;
```
- To actually do the update, change the `processRecords` variable to `true` and run the script
```js
var processRecords = true;
```

4. Run the script
69 changes: 69 additions & 0 deletions Specialized Areas/Fix scripts/Remove extra spaces/removeSpaces.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Fix Script: Remove Extra Spaces from a Field
*
* Description:
* This script removes leading/trailing spaces and collapses multiple
* consecutive spaces into a single space for a specified field on a specified table.
*
* INSTRUCTIONS:
* 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
* 2. Set 'processRecords' to false for a preview, set to true to do the updates
*/

(function () {
// Set to true to perform the update, false to just preview the changes
var processRecords = false;

var tableName = 'incident'; // Add your table name
var fieldName = 'short_description'; // Add the field that might contain leading spaces

var recordsUpdated = 0;

if (gs.nil(tableName) || gs.nil(fieldName)) {
gs.print('Please set the table and field name');
return;
}

gs.info('Starting space removal process for table: [' + tableName + '], field: [' + fieldName + ']');
gs.info('Run mode: ' + (processRecords ? 'UPDATE' : 'PREVIEW'));

try {
var gr = new GlideRecord(tableName);
// Only query records where the target field is not empty
gr.addNotNullQuery(fieldName);
gr.query();

while (gr.next()) {
var originalValue = gr.getValue(fieldName);

// Replace all occurrences of 2 or more spaces with a single space and trim leading/trailing whitespace
var cleanedValue = originalValue.replace(/\s\s+/g, ' ').trim();

// Check if the value has actually changed
if (originalValue !== cleanedValue) {
gs.print('Record sys_id: ' + gr.getUniqueValue() + ' (Number: ' + gr.getDisplayValue('number') + ')\n' +
'---> Original: "' + originalValue + '"\n' +
'---> Cleaned: "' + cleanedValue + '"\n'
);

if (processRecords) {
gr.setValue(fieldName, cleanedValue);
gr.setWorkflow(false);
gr.autoSysFields(false);
gr.update();
recordsUpdated++;
}
}
}

gs.print('Space removal process finished.');
if (processRecords) {
gs.info(recordsUpdated + ' records were updated.');
} else {
gs.print('This was a PREVIEW run. No records were updated. To apply changes, set the "processRecords" variable to true.');
}

} catch (e) {
gs.error('An error occurred during the space removal script: ' + e.message);
}
})();
Loading