Implementing Scripting

Objectives

After completing this lesson, you will be able to:
  • Explain scripting for form elements
  • Implement scripting for form elements

Adobe LiveCycle Designer Script Editor

You can attach scripts to all kinds of non-static objects. However, scripting that is attached to a non-static object can access all other objects of the form, including static objects.

To add scripting to an object, select the object and type the script in the Adobe LiveCycle Designer Script Editor.

You can choose between different events to determine when the system executes your code.

For each event of an object, you can choose between the following scripting languages:

  • JavaScript
  • FormCalc

ABAP is not supported. We recommend that you use FormCalc for simple tasks and JavaScript for more complex tasks.

Scripting can be evaluated at the client (front-end) or at the server. Since print forms do not require any user interaction, scripting should run on the server side.

To avoid setting the scripting language and the location for coding execution each time, set the preferred language and the place in the form properties (you can change it for individual scripting).

Script Editor

If your coding is more complex or if you have several script events for one object, you can enlarge the Adobe LiveCycle Designer Script Editor to see the Show Events with Scripts option.

You can see all coding in a form as follows:

  1. Choose Show Events with Scripts.
  2. On the Hierarchy tab page, select all elements (select the top element, then shift-click on the bottom element).

You can change the scripting language for one event as follows:

  1. Right-click in the multi-line editor.
  2. Choose your preferred language.

If you already have several events for one object and you want to change the script language for all of them, choose your preferred language from the Language field.

You can access the subfields or properties of layout elements by typing a period. An input help dialog box shows you the available options (you can switch off this input help by choosing ToolsOptionsWorkspace and deselecting Show Statement Completion Options).

Scripting Events

You can choose between different events to determine when your coding is executed.

The events are as follows:

  • initialize

    The object is initialized after the system merges the data from the data stream.

    One reason to use this event is to determine the field value dynamically.

  • calculate

    The system processes this event after the data merge, but before the display. You can query the field’s value and edit it. The results from the ordinary data binding are overridden. Calculation is always triggered again when a calculation-relevant value changes. Hence, avoid self-references.

  • form:ready

    The system processes this event after the form and data are merged and loaded, and any calculations and validation events have been triggered. Since the system processes this event before pagination finishes, you can use it to hide objects dynamically. If the objects are positioned in a subform with type Flowed, this results in no empty space. Instead, the next object in the sequence takes up the space of the hidden objects.

  • layout:ready

    The system processes this event after the application of the layout, that is, after page breaks have been inserted. You can use this event to access objects placed on master pages.​

Scripting Syntax Elements

A comment starts with a semicolon (;) or two slashes (//). It ends with the next line of coding.

Commands may stretch over several lines.

FormCalc has approximately 40 keywords (for example, if, do, and or). They are case-insensitive.

FormCalc has the usual calculation operators (+, -, *, and /) and comparison operators (==, <>, <, >, <=, and >=). You can also use English short forms for the comparison operators (eq, ne, lt, gt, le, and ge). Programmers who are used to ABAP should take care not to confuse the assignment operator (=) with the equals operator (= =).

Variables are case-sensitive.

Elements from the form that you can use as fields in the coding are called accessors. An accessor allows you to query or change a field value or an object property.

To access a field value from the form context (Data View), prefix the name with $record. Give all parts of the name as it appears in the Data View, separated by a period. The original ABAP representation is irrelevant.

Accessing Context Field Values

You can access all context fields in the Data View, even those you have not included in your layout.

When accessing a field of an internal table, note the following:

  • Take care to follow the path shown in the Data View. Due to the XML representation of internal tables, there is always a layer called DATA between the internal table and its fields.
  • If you do not specify which dataset you want to access, the first one will be shown (there is also a more general form of this rule – if two objects at the same hierarchy level happen to have the same name, any unspecific reference to them is actually considered a reference to the first object of that name).
  • If you want to access a specific dataset of an internal table, include the number of that dataset in square brackets. For example, $record.IT_SUMS.DATA[4].CURRENCY. Note that unlike sy-tabix in ABAP, the first line is number 0, the second line is number 1, and so on.
  • If you want to access all datasets of the internal table, include an asterisk in square brackets. For example, $record.IT_SUMS.DATA[*].CURRENCY.

You can also access a field value by giving its name as shown in the Hierarchy palette. You need this for all fields with no data binding, that is, for all fields that do not come from the Data View. Furthermore, you must use this method if you want to change object properties at runtime.

Accessing Fields from the Hierarchy

Use $ to access the current object.

You can access objects by giving just their names, as follows:

  • At the same hierarchy level
  • At the level of one of its enclosing subforms
  • Direct children (if the object to which scripting is attached is a subform)

In the example, within scripting for subform SUB1, you can access the objects SUB2 and MAIL by giving their names.

An easy test to determine whether an object is accessible through its name is to type the name in Adobe LiveCycle Designer Script Editor, followed by a period. If an input help appears, the object is accessible directly.

You can access children anywhere along a subform’s hierarchy. To do so, specify the traversed subforms or summarize them with two periods. For example, starting from SUB1, you could access PHONE with $. .PHONE.

You can access all fields by giving their fully qualified paths and names. Start with the name of the master page or the corresponding page (design view), and insert all hierarchy levels on the way down to the object. If the hierarchy path contains unnamed subforms, you should leave these out. An easy way of getting to the name of an object is to position the cursor on the object in the hierarchy, choose the Script Editor, and display one random event. The first line is always a comment, such as data.Masters.FIRST.SUB1.SUB2.NAME: : ready: layout. The name of the element precedes the double colon. In this case, it would be data.Masters.FIRST.SUB1.SUB2.NAME.

Specify the minimal definition of the field for performance reasons.

Value Assignment

Here are some examples of value assignment:

Code Snippet
123456789101112
;event initialize of text field PHONE_NO if ($record.IS_CUSTOMER.COUNTRY == "US") then $ = "+49 6227 888888" else $ = "+49 6227 777777" endif ;event calculate of text field PHONE_NO if ($record.IS_CUSTOMER.COUNTRY == "US") then "+49 6227 888888" else "+49 6227 777777" endif

In the calculate event, no assignment of the ordinary kind (for example, $ = a) is required if you want to set the value for the field for which the coding is written. Instead, the field takes the value of the last expression. In the example, the text field PHONE_NO has the value +49 6227 888888 if the IS_CUSTOMER.COUNTRY field equals US; otherwise, it is +49 6227 777777.

For a value assignment in events other than CALCULATE, an ordinary assignment, such as $ = a is required.

Variables

For your scripting, you can also define variables within the form without changing the form interface or context. You can do this in the form properties on the Variables tab page. These variables are globally known within the form. They also appear in the Hierarchy, but they cannot be integrated in the form like ordinary text fields from the context, nor can they be used for the Default Binding.

Variables can be text variables (with several lines) or numbers. Variable names are not case-sensitive. You can also use scripting to change object properties.

Some examples of changing object properties are as follows:

Code Snippet
12345678910
FOOTER.presence = "hidden" StaticImage1.rotate = "90" StaticText1.x = "12cm" StaticText1.caption.font.fill.color.value = "200,0,0" ;RGB values TextField1.caption.value.text = "Dynamic caption" TextField1.assist.toolTip = "Better explanation" $.break.after = "contentArea" ;page break after current subform ;can be processed only for subforms

To access the properties of a static object, you must attach the relevant scripting to a non-static object, because static objects do not have events and, therefore, they have no scripting.

If the values you want to set contain units, for example, 12cm, you must enclose the values in quotation marks.

Hiding a subform hides all of its content as well.

Some properties include RGB values, which are combinations of three digits between 0 and 255 that specify how strong the red, green, or blue parts of the form should be.

For example, 0, 0, 0 represents black; 255, 0, 0 red; and 255, 255, 255 represents white.

For details, check the object reference in Adobe LiveCycle Designer online help. To find a suitable scripting command, you might find it helpful to create a dummy object that has the desired settings, and look at the XML representation.

For example, the caption of a text field called TextField1 would look like this:

Code Snippet
1234567
<caption reserve="35mm"> <font typeface="Arial" size="12pt"/> <para vAlign="middle"/> <value> <text>Here is the caption</text> </value> </caption>

You can dynamically assign a caption as:

Code Snippet
1
TextField1.caption.value.text = "Dynamic caption"

To change the caption’s font dynamically, write:

Code Snippet
1
TextField1.caption.font.typeface = "Times New Roman"

Examples of FormCalc Versus JavaScript

Accessors in JavaScript Compared to FormCalc

The table shows accessors in JavaScript compared to Form Calc:

 Form CalcJavaScript
Current Field or Object$this
Root of the Context Data$recordxfa.record
Value of Field<field>

e.g. $ = 7

<field>.rawValue

e.g. this.rawValue = 7

Value of Variable<variable>

e.g.count = 7

<variable>.value

e.g. count.value = 7

For more complicated calculations, JavaScript might prove easier to write than FormCalc. The method for accessing fields, their values, and their properties differs between FormCalc and JavaScript.

FormCalc Functions

In assignments, you can use a wide range of functions, including logical functions, string functions, and arithmetic functions. For an overview of available functions, press the green fn button on the Adobe LiveCycle Designer Script Editor toolbar.

Has Value will return 1 if the queried field has a value; 0 if not.

Exists (v) checks whether the given field is valid. If it returns 1, it is valid. One way to check whether an index entry exists for a repeated subform:

Code Snippet
12
if (exists(TABLE.DATA[0].FIELD) == 0) if (exists(TABLE.DATA[0].FIELD) == 0).

Substr (s, start, len) returns the part of a string s that starts with character number start and has up to len characters. The first character of a string has the number 1.

Refer to the documentation for details on functions.

Function names are not case-sensitive, but their arguments (that is, the variables) are case-sensitive.

Log in to track your progress & complete quizzes