How to remove double spaces for a field in the form?

sruthig
Tera Expert

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

19 REPLIES 19

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();
}

 

AnubhavRitolia
Mega Sage
Mega Sage

Hi @sruthig 

 

Use trim() function in your script to avoid extra spacing issue.

 

https://www.w3schools.com/php/func_string_trim.asp

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

Ankur Bawiskar
Tera Patron
Tera Patron

@sruthig 

you can use a fix script.

what did you start with and where are you stuck?

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

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 

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.

 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023