Example 1 - Using One if Instruction

In this example, sales prices greater than one million dollars are to be highlighted with a message that prompts the user of the report to check and verify that figure.

The expression has the following generic format.

if <value> greater than 1000000 then result := "CHECK THIS ONE".

When the script field is inserted at the end of the detail line in which the sales value is printed, the warning message is printed only when the value exceeds one million dollars, as shown in the following report extract.

Using the features on the Add Script Field dialog, you can build the correct code for this script simply by pointing and clicking.

To create a new full script

  1. Click the New button on the Catalog of Available Fields dialog. The Add Script Field dialog is then displayed.

  2. Enter a name for your script in the Script Name text box.

  3. Select the Full Script option button.

In the example in the following image, the script has been named alert.

When the Full Script option button is selected, skeleton code is displayed, as shown in the previous image. In this example, the required code is to be inserted between the begin and end statements. The intention is to put the result that is required into the result variable. The result variable is of type String. The name and type of this, or any other variable, is declared after the vars statement and before the begin statement.

When your script field is inserted into your report layout, the JADE Report Writer application prints whatever value is in the result variable.

You could simply insert the value you wanted into the result := line, as follows.

result := "*** CHECK THIS ONE ***";

In the above example, the string value is assigned to the result variable by using the JADE assignment := operator. However, if you applied this script to your report detail section, the message *** CHECK THIS ONE *** would be printed for every detail line. The expression therefore needs to be qualified with the condition that this message text is applied to the result variable only if the sales price is greater than one million dollars. It requires a conditional instruction; that is, an instruction that tests if certain conditions are met, as shown in the following example.

if <sales price> greater than 1000000

The if instruction and then clause is used to do this.

To insert a conditional instruction

  1. Create a blank line for your conditional instruction before the result := line, as follows.

    1. Click at the start of the result := line.

    2. Press Enter. A new line is then created above the result := line.

    3. Click at the start of the new line, as shown in the following image.

  2. Open the Commands folder in the Commands list box, as shown in the following image.

  3. Select (double-click) the if then command in the Commands folder. The if instruction and its then and endif clauses are then inserted into the Script Code sheet, as shown in the following image.

    The if instruction is always accompanied by a then clause, and all of the instructions conditional on that if instruction must be inserted between the then and endif clauses. The code therefore has the following form.

    if <sale price> greater than 1000000 then
        result := "CHECK THIS ONE";
    endif;

    In the above example, there are spaces between the elements of the if instruction and then clause that are left blank for the code elements that are added in the next step.

  4. Click in the space between the if instruction and the then clause to set your insertion point.

    You then select the field to which you want to apply the condition. In this example, the sale price field is the field to which the condition is to be applied and it is one of the report fields.

  5. Open up the Report Fields folder in the Fields list box, as shown in the following image.

  6. Select (double-click) the Clients::allRetailSales->mySaleItem->RetailSaleItem.price field in the Reports Field folder.

    The field is then inserted at the insertion point between the if instruction and then clause, as shown in the following image.

    You then apply the comparison operator, which in this example is the greater than sign.

  7. Open the Comparison Operators folder in the Commands list box, as shown in the following image.

  8. Select (double-click) the greater than operator, as shown in the previous image.

    The greater than sign (>) is then inserted into your expression, as shown in the following image.

    If you know the symbol for the greater than sign, you can simply type it into the expression.

    To complete the comparison, enter the comparison value. In this example, the sale price is to be compared with the value one million and the result set if it is greater than one million.

  9. Enter (type in) the comparison value after the greater than sign and before the then clause, as shown in the following image.

    At this point, the code will check to see if the sale price is greater than one million. If this is the case, the code assigns the required value to the result. In this example, we want to print a warning message beside sales values that are greater than one million. The required code is as follows.

    if <sale price> greater than 1000000 then
        result := "CHECK THIS ONE";
    endif;          
  10. Move the result := variable declaration so that it is positioned after the then clause and before the endif clause.

    You can cut and paste it or you can delete it from its current position and type it again.

    The result is shown in the following image.

    Note that the result := variable declaration has been indented. You can use the Tab key to indent the lines of code. It is good practice to indent the code that lies between the if instruction and the endif clause so that it is obvious which code is conditional. Note also that the endif clause is indented to line up with the if instruction. This is also good practice.

    A literal message must be inserted to complete the script expression.

  11. Type the message into the result := line.

    Literal values must be entered between quotation marks, as shown in the following image.

    The asterisk characters have been entered to make the message easily visible when it is printed. These asterisks are part of the literal message and have no other meaning.

    In this example, you do not have to set the return type, as String[100] is the default value and the character string that is created here is less than thirty characters long.

  12. Click the Save button to check your script for errors. Any errors are displayed on the Errors sheet. After you have reviewed any errors, click on the Script Code sheet to correct your code. Click the Save button to ensure that there are no further errors and repeat this process as required.

    Do not remove the semicolon characters (;). Each statement (other than the begin statement) must terminate with a semicolon character (;). For details about the script language, see "JADE Language Syntax", in Chapter 1 of the JADE Developer's Reference.

The script is then complete.

When this script field is applied to your report, sales items priced at greater than one million dollars are highlighted with the alert message, as shown in the following report extract.

Drag the completed script field from the Catalog of Available Fields dialog to the required position in your report layout, which is the field farthest right of the detail line in this example, as shown marked with an arrow in the following image.

In this script code, the result field is not set if the conditional instruction is not satisfied; that is, for sales values of one million dollars or less. You do not need to code this, as the JADE Report Writer puts a null value in the result field by default ,and nothing is printed in your field when the condition is not satisfied.

However, you might want to output a different message if the condition is not satisfied. This is described in Example 2, in the following section, which uses the else clause of the if instruction to provide an alternative action if the original condition is not satisfied.

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

If you encounter errors in your scripts, see "Resolving Errors in Scripts", later in this appendix.