Skip to content

Commit 7cf9aaf

Browse files
authored
Fix Script to Remove Leading and Trailing Spaces from a field in bulk records (#1542)
* fix script to remove leading and trailing spaces fix script is used for removing leading and trailing spaces from a field in bulk. * Create README.md read me file, script file is self explanatory.
1 parent 849572c commit 7cf9aaf

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## Fix script to remove leading and trailing spaces from a field in bulk
2+
3+
- we often encounter in a situation that a field has leading or trailing spaces and we want to remove it
4+
- This fix script helps to remove leading and trailing spaces from a field in bulk number.
5+
- Just for an example I have taken incident table and short description as a field.
6+
- feel free to modify and use it as per your requirement.
7+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
var page = 1;
2+
var count = 0;
3+
var infoMsg = 'Fix Script to remove leading or trailing spaces from short_description. \n';
4+
var sd = ''; //sd inshort for shortdescription
5+
6+
7+
var regExSpacecheck = /^\s+|\s+$/g; //regular expression to check if a sentence has leading and trailing spaces.
8+
9+
var grInc = new GlideRecord('incident');
10+
grInc.addEncodedQuery('short_descriptionISNOTEMPTY');
11+
grInc.query();
12+
while (grInc.next()) {
13+
14+
if (regExSpacecheck.test(grInc.short_description)) {
15+
16+
infoMsg += 'incident record found with leading or trailing space for short_description: ' + grInc.sys_id + '\n';
17+
infoMsg += 'Sd ' + grInc.short_description + 'Contains leading or trailing spaces \n';
18+
sd = grInc.short_description;
19+
sd = sd.trim(); //trimmed value of short description
20+
infoMsg += 'sd is ' + sd + '\n';
21+
22+
if (regExSpacecheck.test(sd)) {
23+
infoMsg += 'sd: ' + sd + ' still has a trailing space\n';
24+
} else {
25+
infoMsg += 'sd: ' + sd + ' no longer contains trailing space \n';
26+
}
27+
//Replace the value for u_location_svid with the trimmed version
28+
grInc.setValue('short_description', sd);
29+
grInc.setWorkflow(false);
30+
grInc.update();
31+
32+
count++;
33+
34+
if (count == 50) {
35+
gs.info(infoMsg);
36+
page++;
37+
count = 0;
38+
infoMsg = 'Fix Script to remove leading or trailing spaces from short_description (' + page + ')\n';
39+
}
40+
41+
}
42+
43+
}

0 commit comments

Comments
 (0)