These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Struggling with a script.

levi.rogers
Registered: Nov 18 2008
Posts: 49
Answered

I will post a link to the document I am creating.

I have a dynamic pdf I am creating in Adobe LiveCycle Designer.

In the middle of a subform I have an additional subform which you can add lines too in order to input accounts and a closing amount. You can add an unlimited number of these.

Where I am struggling is getting a total in another part of the subform that shows the total "closing amt".

I would like to point out I'm a bit of a nub so please be patient with me. This is what i have and it isn't working...as a matter of fact it doesn't do anything.

 form1.accountTitleChange.closeChangeForm.formTotal.totalAmt::calculate - (JavaScript, client)
var oTotal = xfa.ResolveNodes("closeAmt[*]");
var oSum = 0;
for (var i=0; i <= oTotal.length-1; i++)
	{
			oSum = oSum + oTotal.item(i).rawValue;
	}
event.value = oSum;

Any help would be much appreciated.

LR

LINK TO DOC:

http://www.nnb-krny.com/test/test_Account_Change_Dyn.pdf

My Product Information:
LiveCycle Designer, Windows
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
I can't test it right now, but try this:

var oTotal = xfa.resolveNodes("closeAmt[*]");var oSum = 0; // Loop through the fieldsfor (var i = 0; i < oTotal.length; i++)  { // Convert current field value to a number and add it to the sumoSum += +oTotal.item(i).rawValue; } // Set this field's value to the sumthis.rawValue = oSum;

George
levi.rogers
Registered: Nov 18 2008
Posts: 49
George,

It is almost like it isn't trying to calculate it at all.

The array that they are pulling from is in a different subform does this matter?

I think the problem lies somewhere in this part of the code:

var oTotal = xfa.ResolveNodes("closeAmt[*]")
I will keep looking for an answer hopefully someone on here can help me though.

LR
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
Give resolveNodes a little help by specifying the subform on which the fields reside, something like:

subform3.resolveNodes("closeAmt[*]");
George
levi.rogers
Registered: Nov 18 2008
Posts: 49
Okay now it is at least taking the closeAmt out of the first box.

Now it isn't actually looping through the array correctly.

This is how it is set up.


I have a button addAcct that adds another instance of the subform (accountList) that contains the field I want calculated (closeAmt). I want it to be able to total all of the "closeAmt" fields even after I create a new instance of the entire subform accountList.

I think now it is a matter of syntax in the loop but I am not sure.

This is the code i have now.

Button Code:
 form1.accountTitleChange.closeChangeForm.ctrlForm.addField::click - (JavaScript, client)form1.accountTitleChange.closeChangeForm.accountList.instanceManager.addInstance(1);

Total Field Code:
 form1.accountTitleChange.closeChangeForm.formTotal.totalAmt::calculate - (JavaScript, client)var oTotal = accountList.resolveNodes("closeAmt[*]");var oSum = 0; // Loop through the fieldsfor (var i = 0; i < oTotal.length; i++)  { // Convert current field value to a number and add it to the sumoSum += +oTotal.item(i).rawValue; } // Set this field's value to the sumthis.rawValue = oSum;
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
Below is some code from the LiveCycle Designer Scripting reference that might help:

// Access a field in a repeating subform by looping through the node list.var oFields = xfa.resolveNodes("Subform2[*].NumericField4");var nNodesLength = oFields.length;var nSum = 0;for (var nNodeCount = 0; nNodeCount < nNodesLength; nNodeCount++) {nSum += oFields.item(nNodeCount).rawValue;}TextField1.rawValue = nSum;

You would change the last line to this.rawValue = ...

George
levi.rogers
Registered: Nov 18 2008
Posts: 49
This is the output that I am receiving it actually doesn't calculate the fields in concatenates them.

giving me output that looks like this:

0011100200300

from this input:
011100
200
300


So now it is actually reviewing each field but it is not actually adding them it is just reporting them to the output field.

LR
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
Can you post the new code you're using?

George
levi.rogers
Registered: Nov 18 2008
Posts: 49
 form1.accountTitleChange.closeChangeForm.formTotal.totalAmt::calculate - (JavaScript, client)// Access a field in a repeating subform by looping through the node list.var oFields = xfa.resolveNodes("accountList[*].closeAmt");var nNodesLength = oFields.length;var nSum = 0;for (var nNodeCount = 0; nNodeCount < nNodesLength; nNodeCount++) {nSum += oFields.item(nNodeCount).rawValue;}this.rawValue = nSum;

This is what I am using.

Thoughts?
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
In the first script I posted I had it explicitly convert the rawValue to a number specifically to avoid concatentation, which can happen if the field is a text field, as opposed to a numeric field. So the following should fix it:

    nSum += +oFields.item(nNodeCount).rawValue;
The unary + operator will convert a string representation of a number to a number, as well as convert the special JavaScript value of null to zero. A blank field with have a rawValue of null.

George
levi.rogers
Registered: Nov 18 2008
Posts: 49
This worked out just great.

Now another question.


I have a delete button that is a part of this subform. When I delete one of the forms it does not take the total out of the whole total. Any reason why this is?


Thanks again for your help I really appreciate you gettnig this working.

LR
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
Use the execCalculate method after adding/deleting. This method is documented in the LiveCycle Designer Scripting reference.

George