diff --git a/GlideFilter/CaseSensitiveSearch/Normalize_All_Variants_of_prod.js b/GlideFilter/CaseSensitiveSearch/Normalize_All_Variants_of_prod.js new file mode 100644 index 0000000000..299fb54a0d --- /dev/null +++ b/GlideFilter/CaseSensitiveSearch/Normalize_All_Variants_of_prod.js @@ -0,0 +1,27 @@ +// Create this as fix script or run as background script as per your requirement +(function normalizeProdWithGlideFilter() { + var changeGR = new GlideRecord('change_request'); + changeGR.addNotNullQuery('u_environment'); + var normalizeProdFilter = 'u_environment=prod'; + changeGR.addEncodedQuery(normalizeProdFilter); + changeGR.query(); + + var filter = new GlideFilter(normalizeProdFilter, 'filterCondition'); + filter.setCaseSensitive(false); // Match any case variant of "prod" + filter.setEnforceSecurity(true); // Enforce ACLs + + var updated = 0; + + while (changeGR.next()) { + if (filter.match(changeGR, true)) { + var original = changeGR.u_environment.toString(); + + if (original !== 'Prod') { + changeGR.u_environment = 'Prod'; + changeGR.update(); + updated++; + } + } + } + gs.info('Environment normalization completed. Total updated: ' + updated); +})(); diff --git a/GlideFilter/CaseSensitiveSearch/README.md b/GlideFilter/CaseSensitiveSearch/README.md new file mode 100644 index 0000000000..e40859c9f5 --- /dev/null +++ b/GlideFilter/CaseSensitiveSearch/README.md @@ -0,0 +1,43 @@ +# ServiceNow Fix Script: Normalize "prod" Environment Using GlideFilter + +## Problem Statement + +In many ServiceNow environments, custom fields like `u_environment` on tables such as `change_request` often contain inconsistent variants of the same logical value, for example: `prod`, `PROD`, `Prod`,`PrOd`, `pRoD` etc. + +These inconsistencies cause: +- Bad or inconsistent reports +- Broken automation rules like BR or flow with condition +- And, Poor data hygiene or dirty values + +--- + +## Solution: Fix Script or Background Script Using GlideFilter + +We use **`GlideFilter`** with **case-sensitive matching** to securely identify inconsistent values to avoid multiple `if` conditions or regular expressions. + +--- + +## Example + +Instead of writing custom logic with if statement like this: + +```javascript +var env = gr.u_environment.toString().toLowerCase(); +if (env === 'prod' || env === 'prod ' || env === 'PROD' || env === 'PrOd') { + // Normalize +} +``` + +You can simply write: +```javascript +var filter = new GlideFilter('u_environment=prod', 'envNormalize'); +filter.setCaseSensitive(false); +if (filter.match(gr, true)) { + // Normalize +} +``` + +## **How to Use** +1. Go to **Scripts - Background** or **Fix Scripts**. +2. Define the script using above glidefilter example shared. +3. Click **Run Script**.