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

Hi,

I tried the below fix script but it is not removing the extra spaces after Run Fix Script. Please sugges

sruthig_2-1684485151227.png

I have added spaces before Testing as below. But not removing the extra spaces after Run Fix Script

sruthig_4-1684485188328.png

 

 

 

 

Hi @sruthig 

 

Try below Script once:

 

var gr = new GlideRecord('sn_customerservice_case');
gr.query();

while (gr.next()){
   gs.addInfoMessage(gr,number); // Info Msg just to verify that code is going inside the While loop correctly or not
     var clean1 = gr.<field_name>.toString().trim();
     gr.<field_name> = clean1;
     gr.setWorkflow(false); 
     gr.update();
}

 

Verify that you are getting Info Message. 

 

You can also try with some filter on addQuery() and setLimit() to validate few records first than go for the whole table.

 

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

Hi Anubhav,

Thanks for the response. It's working when I add string().

Can you please help me how to remove extra space in between 2 strings(Words)
Example - Testing Test (If in between Testing and Test, 2 spaces then how to remove the one space)

Please suggest.

Regards,

Sruthi

You can use the replace() method.

var string = 'Testing  Test';

var newString = string.replace('  ', ' '); //replace the double space '  ' with a single space ' '

 

https://www.w3schools.com/jsref/jsref_replace.asp

Hi,

You are just using 'Testing Test'. There are many record with these double space. How can I add condition to remove double space or more spaces in fix script. Not for single record.

Please suggest.