Formatting string fields containing numbers

sondrefaane
Tera Contributor

We have a string field called ‘sum’ in ServiceNow that sometimes contains numeric data (e.g. ‘10000’) and sometimes contains non-numeric text (like ‘No limit’).

We want to permanently store numeric values with thousands separators so that ‘10000’ becomes ‘10 000’ in all ServiceNow views. However, we want to skip records that are ‘No limit’ or other non-numeric text.

How can I write a run a Business Rule to parse, format, and update only the numeric records while leaving the special text unchanged, and ensure a space is always used as the thousands separator?

1 ACCEPTED SOLUTION

@sondrefaane 

if the value is not from above then use parseInt() and store the integer value

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

4 REPLIES 4

Vishwa Pandya19
Mega Sage

Hi @sondrefaane 

 

Try executing below script in BR.

Condition of the BR should be whenever you want the script to run.

 

function numberWithCommas(x) {
	var num_regex = '^[0-9]*$';

	if(x.match(num_regex)){
		var num_parts = x;
		num_parts = num_parts.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
		return num_parts;
	}
	else{
		return false
	}
}

var separated_value = numberWithCommas(current.<your field name>);
if(separated_value != false){
current.<your field name> = separated_value;  
//current.update()   --- uncomment this if you are using an after update BR
} 


If my answer has helped you in any way please mark it correct or helpful.

Ankur Bawiskar
Tera Patron
Tera Patron

@sondrefaane 

why not make that as integer field?

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

Because it also stores values like "No limit" or "Per Contract". It's a field describing purchasing power for a commitment connected to a role. Found a solution, but thanks.

@sondrefaane 

if the value is not from above then use parseInt() and store the integer value

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