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

Abhijeet_Pawar
Tera Guru

Hello @BartFrelian1337 

I tested the following code please try once.

 

 

Thank you.

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('u_quantity'));
var priceStr = g_form.getValue('u_price');  
var price = parseInt(priceStr.replace(/\D/g, ''));
alert("quan = " + quan + " price = " + 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;
alert(total);
 var curr = priceStr.split(";")[0];
 g_form.setValue("u_total", curr + ";" + total);

}
   

 

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

Many Thanks Siva. Great explanation of handling the price fields. Your solution worked!

Glad it helped.

Cheers,

Siva