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

Form Calculations Using Listbox Values as a Validation

levi.rogers
Registered: Nov 18 2008
Posts: 49

Okay i will try and put this into words as best as I can:

I have a form that has subform1 -> table1 -> cell2

Cell2 is a listbox field with predefined values and captions:

Caption 1 = 1
Caption 2 = 2

I have another subform2 -> table2 -> cell3

Cell3 needs to total the amounts from subform1.table1.cell4 depending on what the value in subform1.table1.cell2.

Something like the following:

If cell2 is equal to 1 than add the contents of cell3 and put them in subform2.table2.cell3.

A bit more information - the first table uses the instance manager to add and remove instances of row1. This is where I am really getting hung up.

This is what I'm thinking, I some how need to use an array and than perform the calculation.

Any help would be greatly appreciated.

EDIT: I am not using a list box I am using a drop-down list.

My Product Information:
LiveCycle Designer, Windows
levi.rogers
Registered: Nov 18 2008
Posts: 49
Okay this is what I have came up with so far - it still isn't working but I think I am getting closer:





Note: Sorry it wouldn't let me put the code in it says I haven't posted enough.

gonna post a couple so I can.
levi.rogers
Registered: Nov 18 2008
Posts: 49
and again...
levi.rogers
Registered: Nov 18 2008
Posts: 49
//Sum Variables
var fldSum = 0;

//Acquire info from field
var fldGrp = this.getField("offageForm.Table1.Row1.Cell3[*]");
//Get Array of fields in group
var fld = fldGrp.getArray();

//Loop it up
for (var i=0; i
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
A couple of points before I get into the solution. Originally your post shows up in the forum list with "0" replies. These are the questions people look at most often when they are going to answer. If you post 3 more times it look like you're having a conversation. The best way to get an answer it to leave it at "0" replies.

Ok, to start off, you've got a LiveCycle form. The code you've shown above is a mixture of AcroForm, FormCalc, and XFA JavaScript code. It won't work.

I know that you are a member at www.pdfscripting.com. There is a compresensive set of videos explaining XFA JavaScript programming and dynamic form design. This is a good place to start.

Next, you need a better explaination of the calculation you want to implement. Explain it in terms of the real calculation, not cell names. You also need to name the fields on the form. To this end there are two sections on the form.

It looks like the form is divided into two sections. The first section is a dynamic table where daily info is entered. The second section is a table summizing the entered data, and this is where the calculation needs to be. Is this correct?

If so, the first step in the summary calculation is to use the "resolveNodes" function to get a list of all the dynamically created rows from the first section. There's information on this in the videos and there are also sample files on the site.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

levi.rogers
Registered: Nov 18 2008
Posts: 49
thomp wrote:
A couple of points before I get into the solution. Originally your post shows up in the forum list with "0" replies. These are the questions people look at most often when they are going to answer. If you post 3 more times it look like you're having a conversation. The best way to get an answer it to leave it at "0" replies.
Noted, I will just edit my original thread. Now I know....

thomp wrote:
Ok, to start off, you've got a LiveCycle form. The code you've shown above is a mixture of AcroForm, FormCalc, and XFA JavaScript code. It won't work.
This comes from me trying to piece this together from multiple sources and just an overall lack of understanding. Know that I am trying to do my research as best I can to help solve this myeslf.

thomp wrote:
I know that you are a member at www.pdfscripting.com. There is a compresensive set of videos explaining XFA JavaScript programming and dynamic form design. This is a good place to start.
I have been reading/watching all morning long trying to grasp what I am missing in this scenario.

thomp wrote:
Next, you need a better explaination of the calculation you want to implement. Explain it in terms of the real calculation, not cell names. You also need to name the fields on the form. To this end there are two sections on the form.
In the form itself I have given names to all the fields, I was using general terms above as I didn't think my variable names would mean much to anyone else. Also as you know since you have seen this document I work for an organization that would prefer I don't flash all over the web where I work and what I do.

As to the explanation of the calculation:
I need to sum amounts from the first dynamic portion of the document depending on another selection in the dynamic table.

In the 2nd cell in the dynamic table I have a drop down list box that has a list of selections that are each assigned a value. I was planning on using this value as the validation for which numbers from the amount column should be summed and than reported into the table at the bottom in the appropriate cell.

thomp wrote:
It looks like the form is divided into two sections. The first section is a dynamic table where daily info is entered. The second section is a table summizing the entered data, and this is where the calculation needs to be. Is this correct?
Yes this is dead on exactly what I am doing.

thomp wrote:
If so, the first step in the summary calculation is to use the "resolveNodes" function to get a list of all the dynamically created rows from the first section. There's information on this in the videos and there are also sample files on the site.
I am looking at this now. Hopefully I can find something that helps me find a measure of clarity on the question at hand.

This is what I am thinking now, correct me when and where I go wrong:

I was thinking of creating a function as this calculation will need to be used in multiple places, however, I am not sure this is the right way as the calculation parameters will change for each line in the bottom table. If I could do this in a function than I could just reuse that function repeatedly in the bottom table as neccessary I could even combine items so one function can manage doing the calculation for each of the left three rows in the bottom table.

This is what I have started so far doing things the proper way:
this info came from
http://www.adobe.com/devnet/livecycle/articles/lc_designer_scripting_basics/lc_designer_scripting_basics.pdf
pg 103.

var tlrField = xfa.resolveNodes("offageForm.offageTable.Row1[*].amt");
var total = 0;

for (var i=0; i <=tlrField.length; i++)
{
total = total + fields.item(i).rawValue;
}
this.rawValue = total;

At this point I am going to get the calculation working first and than build in the logic.


There is more but I want to make sure that I am even starting out the proper way.

Thanks again for your help and I will be looking on your site to find answers while awaiting your response.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Yes, get started in the proper way. The total script at the bottom has the correct structure for just about any table like calculation you need to do. However, for writing the conditional calculation you'll need to resolve the rows, not a specific field:

var tlrField = xfa.resolveNodes("offageForm.offageTable.Row1[*]");

Writing generalized function that can be used on any line is also the right way to go. The main difference between the calulations on each line of the summary is that the filter number changes. This number can be aquired from the text at the beginning of each line.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

levi.rogers
Registered: Nov 18 2008
Posts: 49
var tlrField = xfa.resolveNodes("offageForm.offageTable.Row1[*]")
var total =0;


So now that I am using the row how do I specify which column I want it to pull amounts from?


I have this working for the calculation:

var tlrField = xfa.resolveNodes("offageForm.offageTable.Row1[*].amt")
var total = 0;

for (var i=0; i
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
The resolve nodes function:
var tlrField = xfa.resolveNodes("offageForm.offageTable.Row1[*]")

returns a list of subforms. Each subform contains the fields in that row. "amt" is one of those fields, so you'd access it like this.

total += tlrField.item(i).amt.rawValue;

The problem with the calculation is that the sum operator is wrong in your script. Rather than summing its just assigning total the last value. The operator is "+=".

The removeInstance function requires an input, which is the ID of the row to delete. In the examples I've done I put a remove button on the line. It's all explained in the videos.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

levi.rogers
Registered: Nov 18 2008
Posts: 49
As to the removeInstance I would like to leave that button where it is for "pretty design" reasons. Is there a way that I can always make it remove the last instance?


Thanks for pointing out that my operator was backwards...

./facepalm (nuff said)


So I am assuming the way that I get to the amt field I can use a similar manner to extract the data from the dropdown list to use in my validation statement.


So now after testing:

Okay so success (partially) the calculation now works.

So now I can start trying to build in the validation.

Let me shoot you what I am going to start with:

var tlrField = xfa.resolveNodes("offageForm.offageTable.Row1[*]")
var total = 0;

for (var i=0; i
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
To remove the last instance use the instance count "_Row1.count" in the removeInstance function

Your loop test is incorrect. The whole point of getting the row subform is to get access to the local fields in the row. So the "if" should look like this:

if (tlrField.item(i).tellerNum.rawValue == 1)

"tellerNum" is a field so you have to use "rawValue" to get the field value. Also "==" is the comparison operator. If you just use "=" the line will assign "1" to the pulldown.

Do not include the "else" portion. This will reset the total. Don't want to do that.

It is not necessary, and a bit counter productive to assign the result of the calculation to the field value. I know this is how it's done in a lot of official Adobe samples, but they are wrong. The last value evaluated by the script is automatically assigned to the field. So the last line of the script should be:

total;

all by itself.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

levi.rogers
Registered: Nov 18 2008
Posts: 49
Amazingly I had actually started to get this one figured out a bit. This is what I have now:

var tlrField = xfa.resolveNodes("offageForm.offageTable.Row1[*]")
var total = 0;

for (var i=0; i
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
The total variable is already set to 0 at the top of the script. If there are no fields that match the filter then the field value will be set to 0 by default.

The removeInstance function needs to be attached to the correct SOM path leading from the button down to the repeatable subform. So starting with Row1 is not correct. The "_" means instance manager. "_Row1" is short hand for "Row1.instanceManager" They are interchangable, but can't be used together.

As for the global function. It should be placed in a scripting object. I don't remember if this is covered in the videos. Let me know when you get there. Get this other stuff working first.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

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

Everything previously is working now.


Now I want to know how to put this into a global script that can do the processing for each individual field in the lower table.

1.) What is the most memory efficient way to do this?
2.) How do I make the call in the field or do I do it in the amt field in the first table?

I could not find this in the documentation at pdfscripting.com if there is a place point me there and I will try and figure it out.

I can't thank you enough this has been a great learning experience.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
The first thing to do is to add a "Scripting Object" to the form hirearchy. Add it to the subform immediately above the repeatable subform. Give the scritping object a name, like "CalcScripts", and add in your function. For now just pass the comparison number into the fucntion so you'd call it from the calculation event like this:

CalcScripts.MyCalcFn(1);

That's a start

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

levi.rogers
Registered: Nov 18 2008
Posts: 49
Thomp -

Ive got to work out of the office today, but tomorrow I will dive back in. Thanks again for your help thus far.
levi.rogers
Registered: Nov 18 2008
Posts: 49
Okay so this is what I have -

I put the global function in the subform that contains the dynamic fields.

This is what I have:

function calcTotal (validSum)
var tlrField = xfa.resolveNodes("offageForm.offageTable.Row1[*]")
var total = 0;

for (var i=0; i
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
The calculation script places the last thing evaluated into the field value. In this case that's the function call, which doesn't return a value. Make the last line of the script:

return total;

Otherwise it looks good structually. But there are two possible issue. The SOM path used in the resolve Nodes function and the SOM path to the function used in the calculation script. If either of these is off the script won't work. Make sure to check the console window for errors.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

levi.rogers
Registered: Nov 18 2008
Posts: 49
This is the error message I'm getting:


CalcScripts is not defined?
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Then the path to scripting object is incorrect. You'll need to figure out what the correct path is. LiveCycle should help you with this. As you type elements into a line of code in the script editor it provides a menu of options. The "CalcScripts" object should be in there, but the function won't.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

levi.rogers
Registered: Nov 18 2008
Posts: 49
form1.subForm.mainForm.offageForm.variables.CalcScripts.calcTotal(1) is not a function

This is the error I'm getting now, does it still have to do with it not being called correctly.

The documentation says the following:



Something is invoked as a function, and it does not exist.

Example:
var f = "No function"; f{};


I am not following what they are saying here?
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
The error indicates that "calcTotal" does not exist. You've got the path to the scripting object correct, but there was an error in the scripting object that prevented the function from being defined. Looking back at your previous post I can see that there is a syntax error in the function. It's missing a "{" at the beginning.

Errors in the scripting object are not reported to the console window. So the way to debug this is to copy the code from the scripting object into the console window and run it there where you'll see the syntax errors.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

levi.rogers
Registered: Nov 18 2008
Posts: 49
Okay so I think it will call the function correctly now. However it is still not returning a value to the cell in the second table. I was thinking we needed something like this.

form1.subForm.mainForm.offageForm.variables.CalcScripts.calcTotal(1) = this.rawValue;

However this doesn't appear to work either.

Do i need to define a variable to assign the return value too and than do: this.rawValue = someVar;

?

Edit 1:48pm CST:

I tried this as well to no avail:

var amtSum = offageForm.variables.CalcScripts.calcTotal(1);
this.rawValue = amtSum;

Edit 1:53PM CST :

The console is giving: total is not defined.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Do not use either of the code configurations in your last post.

Is the function returning a value? Is the function working? These are the first things you need to test and the best way to do that is by placing console.println() statements into the function. Have you watched the video on using the console window?

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

levi.rogers
Registered: Nov 18 2008
Posts: 49
I am having problems with the console window.

When I first opened it, it automatically started showing me errors, now it shows nothing and when I type in something it just says undefined.


Okay I put in a println in the function to out put the value of total: total is showing the value as zero.

So I am guessing there is an error in my function.

I am thinking on this line:

from here:
****
function calcTotal(validSum)
{
var tlrField = xfa.resolveNodes("offageTable.Row1[*]")
var total = 0;
console.println(validSum)
****

from here it is returning the following:
1
It is giving this return regardless of the selection that I am picking, I am thinking this is where the problem is.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
I think you need to go back to my earlier comments. One good possibility is that the SOM path used in the "resolveNodes" call is wrong. Print the number of elements returned to the console window.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

levi.rogers
Registered: Nov 18 2008
Posts: 49
Okay well it was some syntax errors, this is the output I am getting now from the console:

***Console Output***
tlrField.item(i).offageForm has no properties
9:1Exception in line 9 of function calcTotal, script Doc:Init
Exception in line 1 of function top_level, script XFA:form1[0]:subForm[0]:mainForm[0]:totalsForm[0]:Table2[0]:teller2[0]:amt_2[0]:calculate

tlrField.item(i).offageForm has no properties
9:12
***Function Code***
form1.subForm.mainForm.offageForm.#variables[0].CalcScripts - (JavaScript, client)
function calcTotal(validSum)
{
var tlrField = xfa.resolveNodes("offageForm.offageTable.Row1[*]")
var total = 0;
console.println(validSum);
console.println(total);
for (var i=0; i
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
The "resolveNodes" call is acquiring a list of "Row1" subforms. So all paths starting from "tlrField" are to the members of "Row1". The code in the "if" statement should reference:

tlrField.item(i).tellerNum.rawValue

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

levi.rogers
Registered: Nov 18 2008
Posts: 49
Okay great that was the issue, I had put that in there but should have known better. I think I have this all fixed up. Thanks again for your help.