Calculation is wrong on Click event

Discussions about Forms Designer for SharePoint 2013 / 2016 and Office 365.
Locked
TWendt
Posts: 109
Joined: Mon Sep 08, 2014

06 Jan 2021

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

User avatar
mnikitina
Posts: 264
Joined: Wed Jun 05, 2019

11 Jan 2021

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)));

TWendt
Posts: 109
Joined: Mon Sep 08, 2014

12 Jan 2021

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
Calculate-Field-doubled.jpg (18.69 KiB) Viewed 7742 times
Calculate-Field-not-doubled.jpg
Calculate-Field-not-doubled.jpg (17.85 KiB) Viewed 7742 times
Many thanks for you assistance
Tom

User avatar
mnikitina
Posts: 264
Joined: Wed Jun 05, 2019

13 Jan 2021

Hello Tom,

What do you mean by OnClick? How do you call the function exactly? Do you have a custom button?

TWendt
Posts: 109
Joined: Mon Sep 08, 2014

13 Jan 2021

Hi,

yes I use a custom button.

Best wishes
Tom

User avatar
mnikitina
Posts: 264
Joined: Wed Jun 05, 2019

14 Jan 2021

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.

Locked
  • Information
  • Who is online

    Users browsing this forum: No registered users and 17 guests