Page 1 of 1

Related item count - Quick Edit

Posted: 03 Jul 2019
by Office365Guy
Related items in quick edit:

Using the script below, before adding a new row to the related items, it loops the correct number of times. Once I add a new related item it loops 1 extra time returning the last item twice. Please tell me I'm crazy.

var rows = fd.relatedItems(5).data('ctx').ListData.Row;
var myCost = ''
var myTotal = 0.00
var myCount = 0.00
if (rows.length > 0) {
for (var i = 0; i < rows.length; i++) {
myCost = rows.Cost;
myCost = myCost.replace('$', '')
myCost = myCost.replace(',', '')
myCount = rows.Count;
myCost = parseFloat(myCost) * parseFloat(myCount);
myTotal = parseFloat(myTotal) + parseFloat(myCost);
}
fd.field('TotalCostM').value(myTotal);
} else {
fd.field('TotalCostM').value('');
}

Re: Related item count - Quick Edit

Posted: 04 Jul 2019
by Nikita Kurguzov
Dear Tony,
Yeah, you are not seeing it... :D Unfortunately, this is a known bug for newly added items and one of the most common questions on the forum (although still quite rare). We've not managed to find the reason for it, but we've found a relatively simple solution - you can filter out duplicates with the following code:

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

//do the calculations for singles variable, instead of rows
This should help, but let me know how it goes!

Re: Related item count - Quick Edit

Posted: 08 Jul 2019
by Office365Guy
Got it. Back on track!

Thank you.