- 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 01:32 AM
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
I made a post years back - maybe its helpful to you
Hopefully you'll sort it out.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2025 03:00 PM
Thanks Simon this is helpful!