Expand my Community achievements bar.

Advice regarding form script to calculate running total

Avatar

Level 3

I am designing a simple purchase requisition form which requires the following:

1. For each data entry row (only one row is shown below), multiply user inputs (Quantity * UnitPrice) and put into LineTotalTotal field

2. Check to see if Taxable radio button returns true value.

3. If value is true, calculate tax amount (Linetotal * .0825) = Tax and put in Tax Total field

4. Go to the next line and calculate next LineTotal, if the UnitPrice is taxable, calculate Tax and add it to the Tax Total

5. Add all LineTotals and put the result in the Subtotal field

6. Add Subtotal, Tax Total and Shipping (user input) and put result in Estimated Total field

CapturePR.JPG

I got some of the individual scripts (JavaScript) working but I'm not sure how to go about setting up the Tax Total Field which increments as line items are added.

Don't know which approach is the most efficient (or really how to set up a stack) do I need to create a variable?

Is it better to use numeric fields or decimal fields for calculating currency amounts?

Any help is appreciated.

7 Replies

Avatar

Level 10

You would generally have a loop that goes through the table adding up the required fields. It's easier to use FormCalc for this kind of thing because you can use wildcards to get the different instances. With JavaScript you need to write a loop to go through the fields.

If you check the help and search for "to perform calculations in a table" there is an example of using FormCalc for this.

For adding up the Tax Total field you would need an if statement to check if the item is marked Taxable and then add to the Tax Total or not.

Also, check out this sample from Niall, it might help:

http://www.assuredynamics.com/index.php/portfolio/looping-through-repeating-rows/

Avatar

Level 3

Thanks! I'll take a look. I had started with a table but switched to individual fields because i thought that might be simpler...apparently not.

Avatar

Level 10

You definitely want to use repeating fields/subforms so that you can use the Instance numbers (the number in square brackets you see in the hierarchy with repeated objects; [0], [1], [2], etc.) in the calculation.

How are you generating your extra rows currently?

With repeating table rows or subforms (table rows are a special subset of subforms) you can do things like:

subTotal = sum(Table1.Row1[*].Total) (in FormCalc) this would look for every instance of Row1 and sum the Total field (I think the syntax is correct, going off the top of my head).

Avatar

Level 3

I was planning to get the first row working properly and then just cloning the following rows (6 in all) and then giving unique identifiers to Qty., Unit Price, and Total. Seems very cumbersome, which is why I thought it might be a good idea to get advice regarding my intended direction first.

What would be slick is the ability to generate rows as needed (which is what I think you are hinting at with subforms) with a limit of no more than six instances.

I really appreciate your feedback and advice; as you might already have guessed my programming skills are mostly conceptual without much more.

Avatar

Level 7

As Jono said if you set it up as a table then you can have a "add extra row" button with the code (Javascript): Table1.Row1.instanceManager.addInstance(1); (renaming table1 anf row1 to whatever yours are called).

Just make sure in the binding palette for the row you want to repeat you have the "Repeat Row for Each Data Item" box cheked. There you can also specify that the max number of rows is 6.

Avatar

Level 10

One of the tutorials that comes with LiveCycle is a purchase order with repeating rows, you might want to take a look at that.

Also take a look at another great sample by Niall on dealing with tables:

http://www.assuredynamics.com/index.php/portfolio/building-dynamic-tables/

Avatar

Level 3

Hi Jono & WITMe,

Thanks for your suggestions and pointers to resources. I think I have a much better idea regrading the path to take...and a lot of learning to do along the way.

Thanks again!

Nadeem