Client Script onChange - cannot calculate total price.

BartFrelian1337
Tera Contributor

Hello Folks!

 

Hopefully i can pick your mind on the issue i am running while trying to execute client script onChange.

Here is my usecase:

I have following fields:

- quantity (int)

- price_unit (price)

- total (price)

 

My goal is to run following formula to calculate total: quantity * price_unit = total

 

Here is the scirpt i am running:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
-------------------------------
// Get the values of quantity and price_unit
var quan = parseFloat(g_form.getValue('quantity'));
var price = g_form.getValue('price_unit');

alert("quan = " + quan + " price = " + price);

// Check for empty values in quan and price
if (quan === '' || price === '') {
g_form.setValue('total', 'Invalid input: empty value');
return;
}

// Check if both quan and price are valid numbers
if (isNaN(quan) || isNaN(price)) {
g_form.setValue('total', 'Invalid input'); // Or handle the error in some other way
return;
}


// Set the total value by multiplying quantity and price
var total = quan * price;
g_form.setValue('total', total);
}
---------------------
 
Actual result:
On change of the value system shows following debug alert:
BartFrelian1337_0-1741800972219.png

 

So looks like my values are not null or empty.

However one of the IF statements i built for debuging suggests that one of these values are not numbers:

 
if (isNaN(quan) || isNaN(price)) {
g_form.setValue('total', 'Invalid input'); // Or handle the error in some other way
return;
}

 

BartFrelian1337_1-1741801062343.png
 
However if parseFloat price_unit to ensure it is treated as int updating script with:
 
var price = parseFloat(g_form.getValue('price_unit'));
 
I get this:
BartFrelian1337_2-1741801254389.png

Any help?

1 ACCEPTED SOLUTION

J Siva
Tera Sage

Hi @BartFrelian1337 

PFB the modified script. Please note I've created the onChange client script on "Quantity" field, so I'm picking the newValue in the script.
Price type field is a combination of currency type and amount. So just get the numeric value before doing math and at the end convert the same into currency type;amount before storing that in the total field.

 

    // Get the values of quantity and price_unit
    var quan = newValue;
    var price = g_form.getValue('price_unit');

    alert("quan = " + quan + " price = " + price);

    // Check for empty values in quan and price
    if (quan === '' || price === '') {
        g_form.showFieldMsg('total', 'Invalid input: empty value', 'error');
        return;
    }

    // Set the total value by multiplying quantity and price

    var curr = price.split(";")[0]; // getting currency
    var amount = price.split(";")[1]; // getting numeric value

    // Check if both quan and price are valid numbers
    if (isNaN(quan) || isNaN(amount)) {
        g_form.showFieldMsg('total', 'Invalid input', 'error'); // Or handle the error in some other way
        return;
    }


    var total_amount = quan * parseFloat(amount);
    g_form.setValue("total", curr + ";" + total_amount);
    g_form.addInfoMessage(total_amount);

 

Hope this helps.
Regards,
Siva

View solution in original post

6 REPLIES 6

Simon Christens
Kilo Sage

Hi,

 

When working with currency fields you need to make sure that you are working with consistent grouping and decimal separators - as it can change depending on language in the browser etc. (not all countries using the same format)

Grouping separator is used for ex with 1.000 where decimal separator is cents or whatever currency you have.

Ex 1.000,50 where other uses 1,000.50

SimonChristens_0-1741853974908.png

I made a post years back - maybe its helpful to you

https://www.servicenow.com/community/developer-forum/how-to-calculate-rent-with-vat/m-p/1423317/high... 

 

Hopefully you'll sort it out.

BartFrelian1337
Tera Contributor

Thanks Simon this is helpful!