Using Intermediate Variables

When you start a new script and select the Full Script option button on the Add Script Field dialog, the skeleton code that is provided includes a variable declaration for the result variable, as shown in the following image.

In complex scripts, you may need to store an intermediate value that you are going to use to derive the final result.

You can declare your own variables for this purpose, as shown in the following image in which a variable named tax is declared.

In the following example, it is supposed that a tax was applied to all sales items during the second half of the year, but for some reason it was never applied to destination vacation sales items. For each of these sales items, the 12.5% tax component that should have been applied is printed on the end of the detail line of the sales report.

The conditions for printing the tax component are that the sales item has a code of "DEST" and the sales date is later than June.

One more requirement is that the tax amount is to be printed along with a message that describes what it is; for example:

Add 12.5% Tax $1439.55

As there can be only one result from a script and the message "Add 12.5% Tax" and the tax amount are different types (string and decimal), the tax amount must be converted to a string so that it can be incorporated into the result. This is very easily done, and demonstrates the use of the methods that are provided on the Add Script Field dialog.

In the following example, the currencyFormat method is used to convert a decimal result into a string in the currency format of the current locale. This Decimal primitive type method must be added to the reporting view the on which report is based in the configuration phase. For details, see "Selecting Script Methods", in Chapter 3.

To create the conditional instructions

The first instruction tests to see if this is a vacation destination item.

In the second if instruction, only the sales date has been selected at this point, as shown in the following image.

As the sales date is a generic date, you can extract the month portion of the date by applying one of the Date methods listed in the Methods list box of the Add Script Field dialog so that you can determine whether the date is later than June.

To compare the month portion of the sales date

  1. In your script code, position the insertion point by clicking at the end of the date field in the second if instruction.

  2. Open the Date folder in the Methods list box and then select (double-click) the month method, as shown in the following image.

    The month method returns the month portion of the date as an integer (whole number) value.

    When you select the method, it is applied to the sales date, as shown in the following image.

    Having obtained the month portion of the sales date, test it to see if it is greater than 6; that is, it is later than June, the sixth month of the year.

  3. Select the greater than sign (>) from the Comparison operators folder in the Commands list box (or you can type it).

  4. Enter the value 6.

The resulting script code is shown in the following image.

The result field then needs to be calculated. The result of the calculation needs to be assigned to an intermediate decimal variable, as the tax value is to be part of a message string in the final result.

To create an intermediate variable to contain the tax field

  1. In your script code, position the insertion point by clicking below the result variable.

  2. Open the Variable declarations folder in the Commands list box and then double-click the Decimal variable command, as shown in the following image.

    The variable is then inserted at your caret, as shown in the following image.

  3. Enter a meaningful name for the variable in place of the default name represented by the x character that has been assigned.

    In this example, it is named simply tax, as shown in the following image.

    You could change the maximum length and the number of decimal places, by overwriting the values within the square brackets. For this example, the default length of 12 and the number of decimal places (2) is adequate.

To assign the result of the tax calculation to the tax variable

  1. Within the second if condition, enter the variable and the assignment operator (:=), as shown in the following image.

    If you do not know the assignment symbol (:=), you can select it from the Commands folder in the Commands list box.

  2. Select the sales price field from the Field Catalog in the Fields list box.

  3. Select (or type in) the multiplication symbol (*).

  4. Enter the multiplier. In this example, the multiplier is 0.125 (that is, 12.5%), as shown in the following image.

    For detailed instructions about performing steps 2 through 4, see "Using Arithmetic Expressions" under "Single Expression Script Examples", earlier in this appendix.

At this point in the code, the tax variable contains the sales price multiplied by 12.5% and this value is stored in Decimal primitive type format. It is to be assigned to the result as part of a message string.

To incorporate the numeric tax value into a message string

  1. Move the result := ; line to the line after the tax calculation and then click in the space in front of the semicolon character (;) to make the insertion point.

  2. Enter the text part of the message within quotation marks, as shown in the following image.

    Note that a space has been entered at the end of the text string and before the last quotation mark. This produces a space between the text and the tax amount when they are concatenated.

  3. Open the String operators folder in the Commands list box and then double-click the concatenation symbol (&).

  4. Enter your tax variable, as shown in the following image.

    This produces an error, as tax is a numeric value and the result is specified as having a return type of String. However, the Decimal primitive type has methods that you can apply to create string versions of the decimal field.

  5. Open the Decimal folder in the Methods list box and select the currencyFormat method.

    When this method is applied to the tax field, it returns a string version of the field in the currency format specified for the JADE Report Writer Designer session.

    The currencyFormat method must be included in the view before you can select it in the JADE Report Writer Designer. For details, see "Selecting Script Methods", in Chapter 3.

    The following image shows the completed script.

  6. To check your script for errors, click the Save button as you did in Example 1, earlier in this appendix.

The following image shows a section of a report into which this script field has been inserted into the column at the right.

The third sales item in the previous example is the only one that meets both conditions that were coded into the script, because it is a destination vacation item and it has a sales date that is later than June.

For reference material to help you create complex scripts, see:

If you encounter errors in your scripts, see "Resolving Errors in Scripts", in the following section.