How to remove double spaces for a field in the form?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā05-18-2023 05:58 AM
Hi Team,
I have 2 string field in my form. Fund Name and Fund Number. How can I clean up the the existing this 2 field with leading double spaces?
Please suggest the way and a example code
Regards,
Sruthi

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā05-31-2023 06:25 AM
Hello,
I was just providing that as an example of how to remove the double space. To do it in a loop of records in a fix script it would be something like this:
var gr = new GlideRecord('table_name');
gr.query();
while (gr.next()){
gr.<field_name1> = gr.<field_name1>.trim();
gr.<field_name1> = gr.<field_name1>.replace(' ', ' ');
gr.update();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā05-18-2023 06:04 AM
Hi @sruthig
Use trim() function in your script to avoid extra spacing issue.
https://www.w3schools.com/php/func_string_trim.asp
Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā05-18-2023 06:14 AM
you can use a fix script.
what did you start with and where are you stuck?
Ankur
⨠Certified Technical Architect || ⨠9x ServiceNow MVP || ⨠ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā05-18-2023 06:22 AM - edited ā05-18-2023 06:24 AM
Hi Ankur,
I didn't started yet. So before start I need a clear understanding about this. Which script I can use if I'm clean up the trailing and spaces for existing 2 fields.
Back ground or fix script.
Does the below code (fix script)works if I want to remove trailing and spaces before and after the string field ?
var gr = new GlideRecord('table_name'); gr.query(); while (gr.next()){ var clean1 = gr.field_name1.trim(); var clean2 = gr.field_name2.trim(); gr.field_name1 = clean1; gr.field_name2 = clean2; gr.update(); }
Please suggest
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā05-18-2023 06:25 AM
Hi @sruthig
Fix Script is always recommended than Background script as you can reuse it and also captured in Update Set.
Go for Fix Script.
And as mentioned, above code will work for any extra spacing before or after.
Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023