Expand my Community achievements bar.

calculation field

Avatar

Former Community Member
ISSUE BACKGROUND

I have created an order form where users can indicate the quantity of how many products they would like to purchase from me. I have several of these same type of fields, one under another. At the bottom I have place a field where I wish to sum up all the above "quantity" fields.



QUESTION

Is there an easy way to do this calculation?



I've only got as far as knowing that the field should be a "calculated - read only" type, and that there needs to be a calculation script added. So I activate the script editor window, show calculate and that's where I'm at. I don't know how to do the Javascript code yet, and have searched for tutorials, but the ones I've looked at thus far all seem to be over my head.



If someone knows of a good basic tutorial for me to work through I would appreciate it.



After I figure this one out, the next issues are:

1. an if/then statement which will input a unit cost that is dependant on the quantity ordered subtotal (I have 4 different price categories)

2. a multiplication field where the derived unit cost is multiplied by the quantity ordered

3. a calculation field (shipping cost) which will be dependant on the quantity ordered

4. a final addition field where the answers from 2. and 3. are added.



Thanks.
17 Replies

Avatar

Former Community Member
In the calculate event of the total qty field use something like:



Sum(quantity[*])



Set the script to form calc, and make sure all quantity fields are named quantity and are on the same level in the hierarchy (i.e. no subforms, etc)



The accessor [*] tells the form script engine to return all nodes matching quantity within the same hierarchy level.



If they are not in the same level, then you need to have a fully qualified expression when you do the sum. Like



Sum(<form>.<page>.<subform>.quantity[*])

Avatar

Former Community Member
I tried this, but the field in which I wish to have the calculated, summed-up number still is blank when I enter values in the quantity fields.



What name do I give the total qty field?

Should the field type be "numeric field"?

Should the default binding be set to normal?

In the script editor, what should "run at" be set to?

Avatar

Former Community Member
You can name the total qty field something like total_qty and since it is a calculated field you can use either numeric or text. Use text for now, to make sure that no formatting is taking place. The binding should be normal and in the script editor set the script to FormCalc and runAt to Client.



Make sure all your quantity fields you wish to calc are called qty - appears in the hierarchy as qty[0], qty[1], etc



and have the script on the calculate event of the total_qty field

Sum(qty[*])

Avatar

Former Community Member
Curtis,



a good sample of an calculating po form comes with the product. Look in the samples directory by default C:\Program Files\Adobe\Acrobat 7.0\Designer 7.0\EN\Samples\Purchase Order\Interactive\Forms\Purchase Order.pdf



The default calculation engine is excellent for the sums etc specially when the fields to be summed have same name.

JavaScript takes a bit more work for that, but is better in other areas..

Avatar

Former Community Member
I'm working with Livecycle. I need a script to do the following: if there is no data in Field1, then I don't want anything in Field3, not even $0.00.(FYI: Field1 * Field2 = Field 3).

Please help!

Thx.

Avatar

Level 7
See Stefan Cameron on Forms Building intelligent forms using Adobes LiveCycle Designer post for Calculate Scripts, http://forms.stefcameron.com/2006/05/15/calculate-scripts/ .



if (Field1 ne 0 and Field2 ne 0) then

$.presence = "visible"; // show field

Field1 * Field2; // compute value

else

$.presence = "invisible"; hide field

0; // insert zero

endif

Avatar

Former Community Member
Hello,



I found post #6 above and it looked like exactly what I needed. I put it in the calculate event of a date field. It does show and hide the field based on the if condition, but... when the date field is visible, the word "visible" actually appears in the field, AND when I try to choose a date, I get a popup message that says "Calculation Override: You are not allowed to modify this field." The funny thing is, then I can select a date and it shows in the field.



I know the calculation override message is due to the script, because as soon as I remove the script the date field functions normally.



Any suggestions? :)



Thanks!



Kathryn

Avatar

Former Community Member
Regarding my post above--I figured out the problem. I had the script in the calculate event. As soon as I moved it to the initialize event, the problem went away.



Kathryn

Avatar

Former Community Member
Hi, I'm new to Adobe Lifecycler Designer 8 and used it to turn a PDF into a form. I have 2 numeric fields - one is called "Best" and one is "Eighty", which contains a simple calculation ( Best * 0.8 ). I want the field to be left blank if nothing is entered in "Best", instead of the default zero. I can't seem to get any of the sample scripts to work for me (I tried the ones reference above in post #6)... not sure what I'm doing wrong. Please help!



Thanks,

Annie

Avatar

Former Community Member
Annie,



Try this (using Formcalc):



if (hasValue(Best)) then

$.rawValue = Best.rawValue * .8

else

$.rawValue = ""

endif



HTH,

Kathryn

Avatar

Former Community Member
Thank you, Kathryn! That worked perfectly!



Annie

Avatar

Former Community Member
I have some Concat scripts running - and they work - but only if the original data field is on the same page as the concat'ed field.



How do you make it work when they're on separate pages?



Thanks to all who consider my plea....

CAT

Avatar

Former Community Member
CAT,



Are you just using field names in the concat functions? If so, try using fully qualified references (e.g., form1.subform2.mytable.myfield). You can grab the fully qualified reference from the top of the script window when focus is on a field. Using fully qualified references should work for any field anywhere in the document.



HTH,

Kathryn

Avatar

Former Community Member
Hi there

I am very new to this

Does anyone know how to do a calculation that requires log e conversion

Thanks

Also is there a good reference on how to use script editor for someone who is a complete novice

Thanks

Kelly

Avatar

Former Community Member
Kelly,



Sorry, I don't know anything about log e conversion. But LCD has a script reference in their Help. If you tried that and it wasn't what you were looking for, I'd try googling "livecycle script editor reference" or something like that.



HTH,

Kathryn

Avatar

Former Community Member
Greetings,



I am trying to sum the hrs on a work log in LiveCycle 8.0, and have 7 fields named "Lesson", with the total field named "TotalLesson". In the total field, I have a calculate field with sum(lesson[*]), language JS, run at client.



It's not working. Any idea why?



Thanks so much.



Dan

Avatar

Level 7
You are using FormCalc syntax. You have to get the FormCalc function name capitialized corretly. You may also have to provide the full accessor path information and resolve the "lesson[*}" reference or the property in JavaScript. You should also provide a target for result of the calculation.



JavaScirpt:



$.rawValue = Sum(lesson[*].rawValue);



or:



// Access a field in a repeating subform by looping through the node list.

var oFields = xfa.resolveNodes("lesson[*]");

var nNodesLength = oFields.length;

var nSum = 0;

for (var nNodeCount = 0; nNodeCount < nNodesLength; nNodeCount++) {

nSum += oFields.item(nNodeCount).rawValue;

}

$.rawValue = nSum;



FormCalc



Sum(lesson[*])



You might want to look at the documentaion at: http://www.adobe.com/devnet/livecycle/designing_forms.html



You can use JavaScript and the "Math" objects "LOG10E" or "LOG2E" methods.