- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-12-2025 10:42 AM
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:
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:
Any help?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2025 01:02 AM - edited 03-13-2025 05:36 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2025 12:49 AM - edited 03-13-2025 01:20 AM
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);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2025 01:02 AM - edited 03-13-2025 05:36 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2025 03:01 PM
Many Thanks Siva. Great explanation of handling the price fields. Your solution worked!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2025 05:38 PM
Glad it helped.
Cheers,
Siva