To remove white spaces from a field

Poorva Bhawsar
Mega Sage

Hi Community,

 

I have a BR using which i am removing the spaces from a field. First the data is getting being loaded via scheduled job. Once its loaded an after insert/update BR will work and it will remove the spaces from that field.

 

Here is the code, i thought it was working fine in background script but now it didnt removed spaces from 2 records.

var record = new GlideRecord('xyz');
    record.query();
    while (record.next()) {
 
        record.abc = record.abc.toString().replace('ABCD', '');
        record.abc = record.abc.toString().replace(/ /g, '');
        record.setWorkflow(false); //Do not run business rules
        record.autoSysFields(false); //Do not update system fields
        record.update();
    }
 
Let me know is there any change required in the code and why it didnt removed spaces for 2 records.
 
Thanks
1 ACCEPTED SOLUTION

@Poorva Bhawsar 

okay got it

then do this and it will replace 1 or more whitespaces

record.abc = record.abc.toString().replace(/\s+/g, '');

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

@Poorva Bhawsar 

why not handle it during data load itself?

try this

var record = new GlideRecord('xyz');
    record.query();
    while (record.next()) {
        record.abc = record.abc.toString().replace('ABCD', '');
        record.abc = record.abc.toString().replaceAll(" ", '');
        record.setWorkflow(false); //Do not run business rules
        record.autoSysFields(false); //Do not update system fields
        record.update();
    }

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Space can be more. Here you have given only 1 space. Will it work if there are more spaces in the field?

@Poorva Bhawsar 

okay got it

then do this and it will replace 1 or more whitespaces

record.abc = record.abc.toString().replace(/\s+/g, '');

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@Poorva Bhawsar 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

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