Having an issue with Number.toLocaleString() with optional parameters.

Michael96
Tera Expert

I have a script include that I am calling from a client script. In the script include I am trying to call Number.toLocaleString() with the locale and option parameters, i.e. Number.toLocaleString("en-GB", {style:"currency", currency:"EUR"}). If i use the function with no parameters (tempNum.toLocaleString()), I get my original number back. However, if I use the locale and option parameters (tempNum.toLocaleString("en-GB", {style:"currency", currency:"EUR"})), I get back null. Does the version of Javascript that Servicenow uses not include Number.toLocaleString with these optional parameters?

1 ACCEPTED SOLUTION

Kartik Sethi
Tera Guru
Tera Guru

Hi @Michael

 

As ServiceNow is running on ECMAScript5 so you are getting the null value. To accomplish the requirement the best way as of now is to use .toLocaleString()  in the Client Script. As Client Script will use Browser JavaScript version so this is possible on Client-side scripting.

find_real_file.png

 

Below is the dummy code:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var num = new Number(g_form.getValue('<your_field>')),
        sd = g_form.getValue('short_description'),
        result = num.toLocaleString("en-US", {
            style: "currency",
            currency: "USD"
        });
    g_form.setValue('short_description', sd + ' ## ' + result);

}

From the Script Include you are going to receive error/undefined results.

 


Please mark my answer as correct if this solves your issues!

If it helped you in any way then please mark helpful!

 

Thanks and regards,

Kartik

View solution in original post

3 REPLIES 3

Kartik Sethi
Tera Guru
Tera Guru

Hi @Michael

 

As ServiceNow is running on ECMAScript5 so you are getting the null value. To accomplish the requirement the best way as of now is to use .toLocaleString()  in the Client Script. As Client Script will use Browser JavaScript version so this is possible on Client-side scripting.

find_real_file.png

 

Below is the dummy code:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var num = new Number(g_form.getValue('<your_field>')),
        sd = g_form.getValue('short_description'),
        result = num.toLocaleString("en-US", {
            style: "currency",
            currency: "USD"
        });
    g_form.setValue('short_description', sd + ' ## ' + result);

}

From the Script Include you are going to receive error/undefined results.

 


Please mark my answer as correct if this solves your issues!

If it helped you in any way then please mark helpful!

 

Thanks and regards,

Kartik

Thank you. This is my exact problem. I think I'll use a UI script and see where that gets me. 

Hi @Michael

 

UI Script can also be a solution but I believe you will ensure that it will not affect performance!

🙂

 

Thank you so much for marking my answer as correct.

 

Thanks and regards,

Kartik