Page 1 of 1
Calculation is wrong on Click event
Posted: 06 Jan 2021
by TWendt
Hi all,
when I use this script the result is wrong. The source field is a calculated field, for example 1,000.00 € and the result in the target field is 1.00 €
//Calculate Kosten interner IT Aufwand
var total = 0;
var rows = fd.relatedItems(6).data('ctx').ListData.Row;
rows.forEach(function(item) {
var currencyIT = item['Kosten'];
var numberIT = parseFloat(currencyIT.replace(/[^0-9,-]+/g,"").replace(",","."));
total += numberIT;
total_overall += numberIT;
});
fd.field('Kosten_x0020_IT').value(parseFloat(total.toFixed(2)));
Please help.
Best wishes
Tom
Re: Calculation is wrong on Click event
Posted: 11 Jan 2021
by mnikitina
Hello Tom,
Try to remove ',' from the string. I've updated the code:
Code: Select all
//Calculate Kosten interner IT Aufwand
var total = 0;
var rows = fd.relatedItems(6).data('ctx').ListData.Row;
rows.forEach(function(item) {
var currencyIT = item['Kosten'];
//updated line
var numberIT = parseFloat(currencyIT.replace(/[^0-9,-]+/g,"").replace(",",""));
total += numberIT;
total_overall += numberIT;
});
fd.field('Kosten_x0020_IT').value(parseFloat(total.toFixed(2)));
Re: Calculation is wrong on Click event
Posted: 12 Jan 2021
by TWendt
Hello Mnikitina,
many thanks. So i can solve the problem. But I have an second error. When I add a new item in the related items and I use the OnClick function the value is doubled. But when I save first and open the form again and the use the OnClick function the result is correct.
var total_overall = 0;
//Calculate Kosten interner IT Aufwand
var total = 0;
var rows = fd.relatedItems(6).data('ctx').ListData.Row;
rows.forEach(function(item) {
var currencyIT = item['Kosten'];
var numberIT = parseFloat(currencyIT.replace(/[^0-9,-]+/g,"").replace(",",""));
total += numberIT;
total_overall += numberIT;
});
fd.field('Kosten_x0020_IT').value(parseFloat(total.toFixed(2)).toLocaleString());
- Calculate-Field-doubled.jpg (18.69 KiB) Viewed 11546 times
- Calculate-Field-not-doubled.jpg (17.85 KiB) Viewed 11546 times
Many thanks for you assistance
Tom
Re: Calculation is wrong on Click event
Posted: 13 Jan 2021
by mnikitina
Hello Tom,
What do you mean by OnClick? How do you call the function exactly? Do you have a custom button?
Re: Calculation is wrong on Click event
Posted: 13 Jan 2021
by TWendt
Hi,
yes I use a custom button.
Best wishes
Tom
Re: Calculation is wrong on Click event
Posted: 14 Jan 2021
by mnikitina
Hello Tom,
I couldn't reproduce the case on my side. Please make sure that the total variable is unique on the form and try to debug the code.