Click on any phrase to play the video from that point.
[♪ Music ♪]
[Adobe LiveCycle ES2]
[Adobe LiveCycle ES2] [Using Scripting to Perform Calculations in Forms]
Scripting can be used to perform simple calculations, aggregate data,
and perform advanced form processing.
We'll start off by viewing a demo of a monthly budget form that uses script calculations
to demonstrate how scripting can be used.
This is the monthly budget form which allows customers to calculate their monthly income,
total expenses, and discretionary income.
We'll fill in some information to demonstrate how it works.
Notice that when you fill in information, the totals fields automatically update.
This customer's monthly discretionary income is calculated to be $2,500.
Now we're going to look at how to create the expense portion of the form,
which adds up all the values the user has entered in the different expense categories.
We'll begin by adding objects to the form, and once this is complete,
we'll add the scripting calculations.
We'll start off by creating a blank form and drag a text object onto the page.
This is going to be our heading.
Now we need to format the heading.
Make the text bigger.
And bold.
So finally, we're going to change the color.
Next, we're going to add instructions for the customer.
Now drag a numeric field object from the library palette onto the form.
First, adjust the width, then change the label.
Now we're going to specify how we want the values displayed.
Click the patterns button in the objects palette.
There are four tabs at the top of the patterns dialog box,
display the patterns to find how data is displayed, edit patterns to find
the syntax for entering data, validation patterns to find how to validate user input,
and data patterns to find the syntax of bound or saved data.
On the left is a number of predefined patterns you can use.
We are going to use the currency pattern.
This pattern will ensure that the values of user inputs are displayed
in the standard currency format.
Now we're going to set the default value of our field to zero
on the value tab of the objects palette.
Next we'll give the field a name in the binding tab.
Each fields name should describe what it contains.
All the field names in this example also happen to be what is written in the captions,
but that won't always be the case with every form that you develop.
Now we're going to format the fields so that the values are aligned to the right.
Open the paragraph palette and click the small arrow near the top right.
Since we're just editing properties for the value portion of the fields, select "edit value."
Click the align right button and close the palette.
Now we're going to duplicate the field once for the food,
transportation and entertainment categories.
Duplicate it by selecting "edit duplicate" or pressing Control D on your keyboard.
Position the new field beneath the first field and keep duplicating until you have five fields.
Now change the name and label for each new field.
I've added all the fields and changed their names and labels,
and now the form is fully laid out.
At this point, it is a good idea to preview the form on the Preview PDF tab
to ensure that your form appears as you intend.
Now that we have the necessary objects on our form, we can add the calculation script.
First, let's take a look at the script editor.
The script editor is where form developers enter code.
By default, it is located at the top of the screen with the other tool bars.
It can be triggered through the window menu or by pressing Control Shift F5.
Or you can use the script editor's handlebars to open, close, or resize it.
To edit scripting for an object, first select the object, choose the event on which to
execute the script, choose the scripting language,
and finally, choose where to run the script.
If you choose client, the script will be run on the client's computer.
If you choose server, the script will be run where it is rendered on the server.
If you choose client and server, your form will have both capabilities.
With the script editor, you can also toggle a list of functions and check form calc syntax.
There are two scripting languages each geared towards the needs
of a particular type of form developer, FormCalc and JavaScript.
We're going to use FormCalc to add scripting to this form.
FormCalc is an easy to use language based on spreadsheet functionality.
It uses built-in functions to manipulate numbers, financial figures and text.
However, it only carries over to HTML forms if the script are set to execute on the server.
JavaScript offers more flexibility and scripting power.
It's good for developers who are already familiar with the language.
However, LiveCycle does not check its syntax.
Now we're going to look at how to use FormCalc to add the calculation script.
Start off by selecting the total expenses field.
Choose the calculate event which will be triggered each time any of the fields
we reference in this calculation change values.
Choose FormCalc as the scripting language and have the script only run with the client.
The first method of writing the addition script in FormCalc is using a simple expression.
We're going to assign the value of the total expenses field to be the total
of all the values of the other fields.
The raw value property is used to assign and retrieve values of fields.
First, reference the total expenses field by typing in its name.
Then enter a period and type "raw value."
Type an equal sign and add up the raw values of each other object
using their respective names.
You can also add objects to the script editor by holding down Control
and selecting the object with the mouse.
Notice how when you begin typing, a list of objects, properties and functions appears.
You can also select the raw value property from this list.
A simple script is now written.
When a user fills in any of the expense categories, the total is updated.
If you wanted to write the script in JavaScript, the syntax would be identical.
Now, for the purpose of your form, you don't need to know the individual names
and values of each particular expense category.
You can give them all the same name and use the sum function to add them up.
This takes much less time than the previous method.
We're going to give each expense category field the same name.
We'll call them all expense.
Now we have four occurrences of the expense field.
The occurrence of a field is indicated in the gray box beside the fields name.
When we want to reference an object that has multiple occurrences, we use square brackets
to specify which occurrence we're referring to.
We could easily use the sum function to add up each individual occurrence,
like I'm doing right now, but that would defeat the whole purpose.
Instead, we use the asterisk wild card to indicate we want the sum
of all the occurrences of the expense field, like this.


