Expand my Community achievements bar.

LiveCycle Conditional Sum in Expendable Table

Avatar

Level 1

Hello experts, help please!

I am creating a form that needs to do some conditional sum but after so many searches I still found no ways of making it work. Please see code below:

var rCount = tblAcctEnt._Row1.count;

var CRtotal = 0;

for(var i=0; i<rCount; i++){

if (tblAcctEnt.Row1[i].Cell2.rawValue == "CR"){

CRtotal =+ tblAcctEnt.Row1[i].Cell8.rawValue;

}

}

this.rawValue = CRtotal;

LC form.PNG

All I wanted to do is just making a sum of all the "LCY" values ("Cell8" - Decimal Field) that has "CR" ("Cell2" - Text Field) in front and put the sum value in the "Total CR" (Decimal Field). but looks like the code doesn't work for me. (Code is located in Calculate as JavaScript).

I tried debugging the code and for some reason I get "CR" as return value when I do "this.rawValue = Row1.Cell2.rawValue;" but getting nothing when I do "this.raValue = Row1[0].Cell2.rawValue". Can someone explain why I get 2 different results when the condition should be exactly the same please and how can I make the conditional sum work?

Many Many Thanks.

2 Replies

Avatar

Level 10

Hi,

you cannot use the accessor [n] in JavaScript to resolve other instances of a repeatable object than the first.

To do this you'll have to use the resoveNode() or resoveNodes() methods instead.

var oRows = xfa.resolveNodes("tblAcctEnt.Row1[*]"),

  iTotal = 0;

for (var i = 0; i < oRows.length; i += 1) {

    iTotal += oRows.item(i).Cell8.rawValue;

}

this.rawValue = iTotal;

Or you use FormCalc instead of JavaScript

$ = Sum( tblAcctEnt.Row1[*].Cell8)

Avatar

Level 1

Hi Radzmar

Thanks for your prompt advice. Using resolvenodes does make the difference, but when I apply the if statement for checking whether Cell2 is equal to "CR" or not, the iTotal (from your example) kept on resetting it self, so I am not able to get the result I want.

For example, if my table has 5 rows, 3 with "CR" and LCY value is 100 per cell, 2 with "DR" and LCY value is also 100 per cell. I want to populate the result, 300 to the iTotal field. is the following code doing the job please?

Within "iTotal" cell, under calculate, program as JavaScript:

var oRows = xfa.resolveNodes("tblAcctEnt.Row1[*]");

var iTotal;

for(var i=0; i<oRows.length; i++){

if (oRows.item(i).Cell2.rawValue == "CR"){

iTotal =+ oRows.item(i).Cell8.rawValue;

}

}

this.rawValue = iTotal;