Skip to content

Commit e3a3a32

Browse files
authored
Merge pull request #2042 from mandeepkaran/mandeepkaran-patch-GF1
GlideFilter Case-Insensitive Search
2 parents d29ced4 + db5636c commit e3a3a32

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Create this as fix script or run as background script as per your requirement
2+
(function normalizeProdWithGlideFilter() {
3+
var changeGR = new GlideRecord('change_request');
4+
changeGR.addNotNullQuery('u_environment');
5+
var normalizeProdFilter = 'u_environment=prod';
6+
changeGR.addEncodedQuery(normalizeProdFilter);
7+
changeGR.query();
8+
9+
var filter = new GlideFilter(normalizeProdFilter, 'filterCondition');
10+
filter.setCaseSensitive(false); // Match any case variant of "prod"
11+
filter.setEnforceSecurity(true); // Enforce ACLs
12+
13+
var updated = 0;
14+
15+
while (changeGR.next()) {
16+
if (filter.match(changeGR, true)) {
17+
var original = changeGR.u_environment.toString();
18+
19+
if (original !== 'Prod') {
20+
changeGR.u_environment = 'Prod';
21+
changeGR.update();
22+
updated++;
23+
}
24+
}
25+
}
26+
gs.info('Environment normalization completed. Total updated: ' + updated);
27+
})();
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# ServiceNow Fix Script: Normalize "prod" Environment Using GlideFilter
2+
3+
## Problem Statement
4+
5+
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.
6+
7+
These inconsistencies cause:
8+
- Bad or inconsistent reports
9+
- Broken automation rules like BR or flow with condition
10+
- And, Poor data hygiene or dirty values
11+
12+
---
13+
14+
## Solution: Fix Script or Background Script Using GlideFilter
15+
16+
We use **`GlideFilter`** with **case-sensitive matching** to securely identify inconsistent values to avoid multiple `if` conditions or regular expressions.
17+
18+
---
19+
20+
## Example
21+
22+
Instead of writing custom logic with if statement like this:
23+
24+
```javascript
25+
var env = gr.u_environment.toString().toLowerCase();
26+
if (env === 'prod' || env === 'prod ' || env === 'PROD' || env === 'PrOd') {
27+
// Normalize
28+
}
29+
```
30+
31+
You can simply write:
32+
```javascript
33+
var filter = new GlideFilter('u_environment=prod', 'envNormalize');
34+
filter.setCaseSensitive(false);
35+
if (filter.match(gr, true)) {
36+
// Normalize
37+
}
38+
```
39+
40+
## **How to Use**
41+
1. Go to **Scripts - Background** or **Fix Scripts**.
42+
2. Define the script using above glidefilter example shared.
43+
3. Click **Run Script**.

0 commit comments

Comments
 (0)