Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

JavaScript/FormCalc Calculating Total Sum

Avatar

Former Community Member

Hi, I am building a fillable PDF payment form with LCD and have run into a problem with calculating the sum. I have a 10-Rows table where people can fill-in the prices and I have a another "Total Amount" box that I wish will automaticlly add up the total sum of all the prices. However, after spent nearly an entire week of trying, I can not get it to work....

I am a newbie at this and as of now, I have tried renaming the price boxes into "price1, price2, price3..." and used "Sum=(amount[Price1, Price2, Price3...])" in the Script Editor and I have tried with both languages(javascript/formcalc)..... but everytime when I go to preview, it either tells me that there is an error or the formula just doesn't do anything at all.... I don't understand where did I do wrong. It seems to me that I have done everything suggested on the discussions

Please do help if you can and THANK YOU VERY MUCH!!!!

PS: How do I upload the file?? it would be much easier to understand what I mean if you can see the form yourself.

1 Accepted Solution

Avatar

Correct answer by
Level 7

From that error message it seems your fields are not in a table, rather just numericfields in a subform. The scripting above will only work in a table. If you just have fields the calculation is just (in formcalc):

$ = price1 + price2 + price3 etc.

If the rest of the fields are in a table and just the calcuation field is not then the calculation needs to be:

$ = sum(TableName.Row2[*].price)

but you need to rename your fields either in the Hierarchy on the left-hand side of your screen or in the Object >Binding > Name tab on the right-hand side. The field in the error message above is called NumericField1[5].

View solution in original post

5 Replies

Avatar

Level 7

In a table you will need to reference each row - so your calculation would be something like (in formcalc):

$ = Row1.price1 + Row2.price2 + Row3.price3

or however you have named them. The easiest thing to do is to call each row the same thing (eg. Row1) and each cell in the column the same thing (eg. price) and then you can just do a formcalc formula like this:

$ = sum(Row1[*].price)

The * will take all the rows called Row1 (which will automatically be renamed Row1[0], Row1[1], Row1[2], etc).

Avatar

Former Community Member

Firstly, Thank you very much for your reply!!

I changed the name of all the rows into "row2" at "XML Source" and all the boxes into "price".

but when I used the formula:$ = sum(Row2[*].price)  and go to Preview PDF, it told me:

Script Failed (Language is fromcalc; context is

xfa[0].form[0].form1[0].#subform[0].NumericField1[5])

script=$ = sum(Row2[*].price)

Error: accessor 'Row2[*].price' is unknown.

have I not changed the names properly that the system doesn't recognise the boxes??

Avatar

Correct answer by
Level 7

From that error message it seems your fields are not in a table, rather just numericfields in a subform. The scripting above will only work in a table. If you just have fields the calculation is just (in formcalc):

$ = price1 + price2 + price3 etc.

If the rest of the fields are in a table and just the calcuation field is not then the calculation needs to be:

$ = sum(TableName.Row2[*].price)

but you need to rename your fields either in the Hierarchy on the left-hand side of your screen or in the Object >Binding > Name tab on the right-hand side. The field in the error message above is called NumericField1[5].

Avatar

Former Community Member

Ah! I see! Thank you SO much! it now works beautifully! you are a genius

Avatar

Level 1

This post is really helpful. Due to certain reasons, I wanted to do this using Javascript. Is there a way to solve this using that? I have no idea how to approach that.

I would really appreciate if you could help me!!

MJ