Expand my Community achievements bar.

SOLVED

simple calculation in a form

Avatar

Level 1

I am trying to creat a simple javascript calculation in LiveCycle and I am completly lost.

I have 3 numeric fields (A1, A2, A3) and one calculated field. When users input numbers into any of the first three fields, I would like the calculated field ("cost") to populate automatically. I have tried to piece together the javascript from reading other posts, but I cannot get my form to process the calculation. (The field is left blank.) Any help would be greatly appreciated.

Here is the calculation I am trying to make:

(A1/A2) * A3

1 Accepted Solution

Avatar

Correct answer by
Former Community Member

On the calculate event of the field that will hold the result of this calculation you woudl add this line of javascript code:

this.rawValue = (A1.rawValue/A2.rawValue) * A3.rawValue;

Hope that helps

Paul

View solution in original post

9 Replies

Avatar

Correct answer by
Former Community Member

On the calculate event of the field that will hold the result of this calculation you woudl add this line of javascript code:

this.rawValue = (A1.rawValue/A2.rawValue) * A3.rawValue;

Hope that helps

Paul

Avatar

Level 1

Yes, thank you, the calculation is working!

But I am having an additional problem. The calculation shows up on the form when the user tabs to the calculated field. However, it shows with an infinite number of decimal places. When the user tabs or clicks off the field, it does run the validation pattern. (I want the number to display as currency, with two decimal places.) But is there any way to round the number as it calculates it, or prevent it from initally showing all those decimal places?  

Also, although I intially set the validation pattern for my numeric field, now that I have set the Value type to Calculated - Read only, I can no longer set the validation pattern (it is grayed out).

???

Thank you

Avatar

Former Community Member

Make your field "User Entered - Optional". This will allow you to modify the Validation Patterns. Set the disply pattern to num.currency{}. Now change filed back to "Calculated ReadOnly" and the pattern will be applied to your result.

Paul

Avatar

Level 1

It still doesn't display correctly until after the user clicks out of the field?

Avatar

Level 1

Correct - the calculated field.

Avatar

Former Community Member

But if the field is set as Calculated ReadOnly how are you entering the field?

Once you make a change to A1, A2 or A3 the calculation wil fire automagically and the formatting will be applied to your result and put into your field.

Unless I am missing something.

Paul

Avatar

Level 1

I'm not actually entering in the field - when I tab out of the last field, A3, the cursor lands in the calculated field, and highlights the calculated amount, with extraneous decimal places. I cannot edit it. When I then tab out of the field, it displays correctly. It's just when the cursor lands on that field initially. I'm not sure a user would tab out of the field, so I would prefer it not have the extra decimal places.

Thanks

Avatar

Former Community Member

Under the Object palette/ Value Tab what is the Type Dropdown set to (my guess is Calculated Read Only). Try changing it to readOnly and try again.

Paul

The following has evaluated to null or missing: ==> liqladmin("SELECT id, value FROM metrics WHERE id = 'net_accepted_solutions' and user.id = '${acceptedAnswer.author.id}'").data.items [in template "analytics-container" at line 83, column 41] ---- Tip: It's the step after the last dot that caused this error, not those before it. ---- Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: #assign answerAuthorNetSolutions = li... [in template "analytics-container" at line 83, column 5] ----