Calculate Total of related item column

Discussions about Forms Designer for SharePoint 2013 / 2016 and Office 365.
Locked
enillrae
Posts: 17
Joined: Mon May 29, 2017

03 Jul 2017

Hi,

I have a purchase order form in which there is a related item table detailing the cost of goods/services per item. I would like to SUM the total of the COST column in the related item table and display it in a field on the form.

Thanks

User avatar
Nikita Kurguzov
Posts: 889
Joined: Mon Jul 03, 2017

03 Jul 2017

Hello,
I think you can find the necessary code in this topic - viewtopic.php?f=1&t=592&p=4049&hilit=fd ... Data#p4049
Tell me if this works for you or not.
Cheers

enillrae
Posts: 17
Joined: Mon May 29, 2017

04 Jul 2017

Thank you, it worked!

User avatar
Nikita Kurguzov
Posts: 889
Joined: Mon Jul 03, 2017

05 Jul 2017

Glad to have helped, you are welcome!
Cheers

enillrae
Posts: 17
Joined: Mon May 29, 2017

21 Jul 2017

Hi Nikita,

For some reason the list items are being counted twice. Here is my code
var rows = fd.relatedItems(0).data('ctx').ListData.Row;
rows.forEach(function(item) {
subTotal += parseFloat(item['QtyRequested.'] * item['UnitCost.']);
});

When I try fd.relatedItems().length in the console it displays '1' although it is empty.

Can you help?

User avatar
Nikita Kurguzov
Posts: 889
Joined: Mon Jul 03, 2017

24 Jul 2017

Okay, let's try to figure out why this might happen. The code you have works fine for me. Try using this code from the console and let us know the results:

Code: Select all

fd.relatedItems(0).data('ctx').ListData.Row.length
This will show you how many rows there are in the table. Then you can add this to your code, to see the calculations happening:

Code: Select all

rows.forEach(function(item) {
	subTotal += parseFloat(item['QtyRequested.'] * item['UnitCost.']);
	console.log(subTotal);
});
fd.relatedItems().length simply shows that you have one Related items field on your form, even if it is empty.
Cheers

enillrae
Posts: 17
Joined: Mon May 29, 2017

24 Jul 2017

Hi Nikita,

After entering 1 entry, I entered fd.relatedItems(0).data('ctx').ListData.Row.length into the console and got the attached results. It seems that the subTotal is being counted twice.
relateditemsconsole.PNG
results after running fd.relatedItems(0).data('ctx').ListData.Row.length
relateditemsconsole.PNG (19.01 KiB) Viewed 3282 times
.
relateditemsconsole2.PNG
results of console.log(subTotal);
relateditemsconsole2.PNG (1.26 KiB) Viewed 3282 times
Please advise. Thank you.

User avatar
Nikita Kurguzov
Posts: 889
Joined: Mon Jul 03, 2017

24 Jul 2017

Okay, it seems like there is something weird going on with the list, but we haven't managed to recreate this bug.
So, for now, I can recommend changing your code to this, it will filter out all items with repeating IDs:

Code: Select all

var rows = fd.relatedItems(0).data('ctx').ListData.Row;
var dupes = {};
var singles = [];

$.each(rows, function(i, el) {
    if (!dupes[el.ID]) {
        dupes[el.ID] = true;
        singles.push(el);
    }
});

singles.forEach(function(item) {
	subTotal += parseFloat(item['QtyRequested.'] * item['UnitCost.']);
});
Cheers

Locked
  • Information
  • Who is online

    Users browsing this forum: No registered users and 3 guests