Page 1 of 1
Calculate Total of related item column
Posted: 03 Jul 2017
by enillrae
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
Re: Calculate Total of related item column
Posted: 03 Jul 2017
by Nikita Kurguzov
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.
Re: Calculate Total of related item column
Posted: 04 Jul 2017
by enillrae
Thank you, it worked!
Re: Calculate Total of related item column
Posted: 05 Jul 2017
by Nikita Kurguzov
Glad to have helped, you are welcome!
Re: Calculate Total of related item column
Posted: 21 Jul 2017
by enillrae
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?
Re: Calculate Total of related item column
Posted: 24 Jul 2017
by Nikita Kurguzov
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.
Re: Calculate Total of related item column
Posted: 24 Jul 2017
by enillrae
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.

- results after running fd.relatedItems(0).data('ctx').ListData.Row.length
- relateditemsconsole.PNG (19.01 KiB) Viewed 3289 times
.

- results of console.log(subTotal);
- relateditemsconsole2.PNG (1.26 KiB) Viewed 3289 times
Please advise. Thank you.
Re: Calculate Total of related item column
Posted: 24 Jul 2017
by Nikita Kurguzov
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.']);
});