Expand my Community achievements bar.

July 31st AEM Gems Webinar: Elevate your AEM development to master the integration of private GitHub repositories within AEM Cloud Manager.

## Conditional calculation not working with repeating table

Level 3

Hi all,

I have a repeating table that can grow/shrink depending on how many entries a person needs. Each entry gets a value inputed in a "score" field. If all the scores are numbers, a simple sum of all the fields is calculated to get a number for total debit points. However, there are cases where an entry can be a "C", meaning that the score is Critical. In this case, the total debits sum should be blank or null.

The problem I run into is that the calculation will only work if the C "score" is on the first line of the repeating table. If it is on a second or subsequent line, then the debit score becomes 0. This is a problem because the total report score either reads as C if there is a critical debit or else 100 minus whatever the sum of the debits were. With my current set-up, a C entered on a second line of the table or later, may actually show up as a 100 for total report score because the total debit field was never set to blank or nullified.

Here is my code:

I'm using FormCalc now, but would be happy to go with JavaScript if it worked better.

if (ViolationsTableSubform.ViolationsTable.ViolCorrSection.ViolationsText[*].DebitVal[*] == "C") then

\$ = "";

else

sum(ViolationsTableSubform.ViolationsTable.ViolationsText[*].DebitVal[*]);

endif

What am I doing wrong?

7 Replies

Level 10

Hi,

I think that you would need to loop through the rows first to find a "C" and then process the if statement. Something along these lines...

var vScore = new Array();

var vCritical = 0;

var nRows = ViolationsTableSubform.ViolationsTable.ViolCorrSection.ViolationsText.instanceManager.count;

for (var i = 0; i < nRows; i++)
{
var oFld = xfa.resolveNode("ViolationsTableSubform.ViolationsTable.ViolCorrSection.ViolationsText[" + i + "]");
vScore = oFld.Debit Val.rawValue;

if (vScore == "C")
{
vCritical = 1;
}
}

if (vCritical == 1)

{

this.rawValue = "";

}

else

{

// your calculation script in Javascript

}

I'll be honest, I am merging a loop script that works for me (making rows hidden and visible), with your script. There is some tyding up to do!!

Hope that helps,

Niall

Level 3

Niall,

Would you mind helping me out a little more? My initial calculation was in FormCalc and I'm not familiar with doing a sum calculation for a repeating table in JavaScript. How would you convert my previous calc script for a repeating table sum to JavaScript.

I have used the script you gave, but am unsure if it is working because my Total Sum field is set up to either subtract the total debits from 100 or enter a C depending on whether a sum calculation was done or a "" value was entered.

Dave

Level 10

Hi Dave,

I have gone back to the books (JP Terry's "Creating Dynamic Forms with Adobe LiveCycle Designer") and think we can improve on things...

var oFields = xfa.resolveNodes("ViolationsText[*].DebitVal"); // looks to resolve the repeating rows (I think)

var nNodesLength = oFields.length; // assigns the number of rows to a variable

var sumTotal = 0; // this is a temporary holding variable for the total, during the loop

for (var i = 0; i < nNodesLength; i++) // this loops through as previous example

{

if (oFields.ViolationsText[i].resolveNode("DebitVal").rawValue == "C")

{

sumTotal = "";

i = nNodesLength;  // stops the loop

}

else

{

sumTotal += oFields.ViolationsText[i].resolveNode("DebitVal").rawValue;

}

}

this.rawValue = sumTotal;

This would go in the calculate event of your Total Debit Points field. I would be amazed if it worked first time. I haven't run through LC. Give it a lash!! (a try!!)

Good luck,

Niall

Level 3

Hi Niall,

I sure appreciate your help with this.

I plugged in the code as you wrote it, but it doesn't appear to be working. I tried selecting items w/ point values, but the debit total sum didn't calculate. I also tried selecting the item with the "C" and the debit total sum remained "0" instead of going to blank.

Ideas?

Dave

Level 10

Hi Dave,

I am flying blind on this. Can you post the form or a version of the form with only the relevant table in it.

A good way to debug script is to use the javascript console when previewing the file. In acrobat press Control+J and then input some data into the fields and add rows. If there is a problem with the script it may show up in the console, which can help to tie down what the problem is.

In addition, you can temporarily put in script which will throw values to the console:

console.println("sum total is: " + sumTotal);

This is useful to track different values or that they are working at all.

I am not surprised it didn't work first time. If you can post the file that would help!

N.

Level 3

All,

Niall has answered my initial question offline (and a few others related to my form).

If it is helpful to anyone else what that solution was, let me know and I'll post the gist of it when I get time.

Thanks again Niall!

Dave

Level 1