How to Create Scripts for Online Forms with Adobe LiveCycle Designer
- The Benefits of Scripting
- FormCalc and JavaScript
- The Script Editor
- Basic Scripting
- The Form Object Model
- Events
- Script Objects
- Debugging Scripts
- Moving On...
- A good programmer is someone who always looks both ways before crossing a one-way street.
- —Doug Linder
Scripting is optional in Designer forms. You can achieve a great deal of functionality and customization without scripting. And you can set most of the important property values of your form objects at design time by using Designer's palettes. You have already learned a lot about these property settings in previous chapters, and you will learn more in Chapter 5, "Form Data." But this chapter focuses on setting and getting these property values at runtime through scripting.
Scripting gives you full control over your form's functionality at runtime. Through scripting you have the power to manipulate your form's interactive controls to provide your users with a richer and more intuitive experience. Designer scripting is similar to HTML scripting in the following ways:
- Use of JavaScript: You use JavaScript to write custom scripts that add functionality to your forms like you use JavaScript to add custom functionality to a Web page. Designer also includes another language called FormCalc, which is covered in this chapter.
- Use of an object model: Like Web programming, Designer also has an object model that you can use for your scripting. You can call methods and properties of the object model to provide powerful functionality to your forms.
- Range of scripts: Because JavaScript is a rich, object-oriented scripting language, you can write an incredible range of scripts, from very basic one-line scripts to complex and detailed, object-oriented scripts that can span hundreds and even thousands of lines.
If you are familiar with scripting on the Web or another platform, you will find the scripting concepts in Designer to be very familiar. However, the execution details of scripting in Designer are unique and are reviewed in this chapter.
The Benefits of Scripting
In many cases, scripting improves the functionality and usability of your forms. You can see this in action by opening the Purchase Order form from the Samples folder (it can also be found in Designer's Help System). A number of useful scripts are included in this form. One of these handy scripts is found in the Add Item button's click event (Figure 4.1). This script creates a new line item in the purchase order each time the button is clicked. In the previous chapter, you saw how this was made possible by the form's subform structure. However, it is the script that calls the subform's instance manager with the directive to create a new instance. This script runs on the button's click event.
Figure 4.1 Two scripts from the Purchase Order form show the benefits of adding scripting to your forms.
Another practical script is found in the Grand Total text field (numGrandTotal). This script automatically calculates the grand total for the purchase order every time a financial event on the purchase order occurs. When a new item is entered or when a new tax is applied, the grand total is automatically recalculated. This is a very useful script for form fillers because they can see how all of their changes and additions to the purchase order affect the bottom line. Unlike the previous script, which ran on the click event, this script runs on the text field's calculate event. You'll learn how these different events affect your form's performance in the "Events" section later in this chapter.
Although you don't absolutely need scripting in your forms, a small investment in scripting will yield all of the following benefits to you and your form fillers:
- Automatic calculations at runtime. As described in the purchase order example, the addition of automatic calculations makes your forms much more useful to your form fillers.
- Ability to control the appearance of form objects at runtime. In the previous chapter, you set the visual properties of your form objects at design time. With scripting, you can make changes to the visual properties of your form at runtime to provide assistance to your form fillers. For instance, before your users submit a form, you can use a validation script to check to make sure that all of the required fields have been filled in with data. If there are required fields without data, you can call a script to highlight them in yellow. This way, the form fillers' attention is visually drawn to the required task.
- Enforcement of business rules. You can use form scripting to enforce the business rules of your company or industry. For instance, the purchase order example could have a script that takes an action whenever the grand total goes above a certain threshold. You could create a script that limits users from going above a certain amount, or you could allow them to go above the amount and automatically route the purchase order for a senior manager's approval.
- Data validation and formatting. In addition to using patterns and properties to enforce data validation, you can use custom scripting. This is very valuable because you will be able to correct many data entry mistakes before they get to your back-end system.