cancel
Showing results for
Did you mean:
Highlighted
Nintex Newbie

## Sum Single Field in a Repeating Section

I'm using a slightly older version of SharePoint on prem, and I'm trying to adapt a previous solution I found on this site to suit a business requirement.

I have a simple text field in a repeating section that I need to total. I've tried the solution using a calculated field and the sum(fieldname) formula, but unfortunately I've found on testing that if a row is deleted or updated in the repeating section, the total shows an incorrect calculation (see below example, where I added/deleted rows.)

As this solution doesn't appear to be working reliably, I researched and found the below solution on this site and I'm trying to adapt the code from it to suit my much more basic table. I'm having difficulty with the javascript as my coding skills aren't that strong (I'm a newbie.) If someone could please help me simplify the code, I'd appreciate it.

Here is the Javascript.

```(function(accountNumbers, countQuantities) {
var currentStateObject = {}; /* Number.isNumber polyfill */
Number.isNaN = Number.isNaN || function(value) {
return value !== value;
};
accountNumbers.forEach(function(accountNumber, accountIndex) {
if (!Number.isNaN(accountNumber) && accountNumber) {
var countQuantity = parseFloat(countQuantities[accountIndex])*100;
if (Number.isNaN(countQuantity) || countQuantity < 1) {
countQuantity = 0;
}
if (currentStateObject.hasOwnProperty(accountNumber)) {
currentStateObject[accountNumber].totalQty = currentStateObject[accountNumber].totalQty + countQuantity;
currentStateObject[accountNumber].totalInstances = currentStateObject[accountNumber].totalInstances += 1;
} else {
currentStateObject[accountNumber] = {
totalQty: countQuantity,
totalInstances: 1
};
}
}

});
var tallyTable = NWF\$("#tallyTable");
tallyTable.find("tr[class]").remove();
Object.keys(currentStateObject).forEach(function(accountNumber) {
var newTableRow = NWF\$("<tr><td></td><td></td><td></td></tr>");
NWF\$(newTableRow.children()[0]).text(accountNumber);
NWF\$(newTableRow.children()[1]).text(currentStateObject[accountNumber].totalInstances);
NWF\$(newTableRow.children()[2]).text(currentStateObject[accountNumber].totalQty/100);
tallyTable.find("tbody").append(newTableRow);
});
return true;
}(parseLookup(choice1), txt1))‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍```

This is the source code for the rich text control

```<table id="tallyTable" style="width:100%">
<tbody>
<tr>
<th>Account Number</th>
<th>Instances</th>
<th>Total Qty</th>
</tr>
</tbody>
</table>‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍```
Labels: (1)

10 Replies
Highlighted
Nintex Newbie

## Re: Sum Single Field in a Repeating Section

I've been experimenting with the solution as-is. For some reason it's not rendering the total as I'd expected. I'm wondering if the solution solves my original problem at all, but instead uses the basic 'sum(fieldname)' formula recommended elsewhere which doesn't work reliably.

Highlighted
Automation Master

## Re: Sum Single Field in a Repeating Section

'sum(fieldname)' should normaly work. I do not recall there would ever be any problems with such a formula.

let's try to focus on this first.

first of all check developer console that there are no errors reported.

if there're no errors, add one another calculated value control on form and set its formula to simply 'fieldname'.

populate your repeating section and grab a screenshot.

delete row, update values or perform whatever you think causes problems with calculation.

grab another screenshot.

post them both to see what's going on there.

post as well configuration/formula of your 'total' calculation control.

do you perfom/apply any changes to repeating section out of form GUI (eg. from javascript, CSS,

workflow,...)? do you delete row or update value such a way?

do you apply any rules or conditional appearance settings on repeating section and/or its controls?

could you clarify as well what exact form version are you on?

Highlighted
Nintex Newbie

## Re: Sum Single Field in a Repeating Section

Thanks for your offer to help me out.

I have gone through the steps as you have suggested with the below results.

1. No developer errors are reported in the console
2. Added a second calculated field with the formula {fieldname}. It can be seen next to the first row in the below screenshot.

3. I deleted the row with the value '5'

4. Added row with the value '30'

From this point, the calculations only get more jumbled. The Nintex form version we are using is 17.0. The only other coding related to/effecting the repeating field is a hide/show rule on the background panel.

Highlighted
Automation Master

## Re: Sum Single Field in a Repeating Section

version '17.0' doesn't seem to be correct form version, can you go in forms designer to Help >> About and check the version there?

that really looks weird...

just to confirm, have you deleted it by clicking on a cross icon at the very right?

I wonder wheter it couldn't happen due to that empty value in 4th row. does this heppen when all values are populated? could you maybe set default value for Cost to zero?

what's datatype of Cost control? - looks like a text? have you tried with a number?

could you try one more thing?

add a calculated value control into repeating section and set its formula to 'Cost'. give it a name, eg calcCost

add a calculated value control out of repeating section and set its formula to 'calcCost'

how does this evaluate?

Highlighted
Nintex Newbie

## Re: Sum Single Field in a Repeating Section

Hi,

Yes, sorry, I accidentally gave the form version rather than the Nintex version. The Nintex version is 2.9.3.0

1. No. I don't delete rows by any means aside from clicking on the default delete 'cross' on the right

2. The empty row was only there because I was using it to refresh the calculating field. Clicking outside the repeating field refreshed the calculation just as well. Not having the row made no difference to the problem.

3. I have tried setting the default value to zero, and changing the datatype (it was currency) to number. This made no difference either.

4. The below screenshot is what happened following your instructions. (I tried to be more helpful this time by naming each item in sequence. The missing number is a row I deleted.)

One thing that strikes me as interesting is that when a row is deleted, it appears that multiple values are taken out of the array. The calculation works flawlessly as long as I'm just adding rows and do not update or delete any previous ones. After conferring with a colleague it appears this issue is common to our environment which is why I thought it might have something to do with the Nintex version.

This was the reason I was looking for a javascript solution to somehow store and update the needed calculation without relying on the formula of the calculated value field. I realise though that the solution I was originally looking at actually has nothing to do with what I'm attempting to accomplish.

Highlighted
Automation Master

## Re: Sum Single Field in a Repeating Section

you seem to hit a bug that was fixed immediatelly once it was 'published'

ok, until you upgrade following might help

```function sumCosts(costCtrlClass){
var totCost = 0;
NWF\$('.' + costCtrlClass + ':not(.nf-repeater-row-hidden)').each(function(idx,itm){
if (!isNaN(itm.value)) totCost += 1*itm.value;
})
}```

usage

Tags (5)
Highlighted
Nintex Newbie

## Re: Sum Single Field in a Repeating Section

Wow! That seems to have solved the problem. Thank you so much!!

The only thing I needed to change was the title case in the javascript code.

(I will also be looking into what it takes to get our Nintex updated. I'll need to evaluate the impact on our environment obviously.)

Highlighted
Automation Master

## Re: Sum Single Field in a Repeating Section

great it works for you!

what exactly you needed to change? it should have worked as it is posted....

Highlighted
Nintex Newbie