Now you need to write the code to run the h-OrderCalcs.p procedure. When does this need to happen? Whenever a new row is selected in the list of Orders for a Customer. This is called the VALUE-CHANGED event for the browse.

To write the trigger for the browse:

  1. Click on the browse to select it, then click the Edit Code icon in the toolbar. The Section Editor appears.

    The default code block to define for a browse is the VALUE-CHANGED trigger, so this comes up automatically.

  2. Fill in the empty DO-END block with this code:
    DO:
      RUN h-OrderCalcs.p
        (INPUT Order.OrderNum,
        OUTPUT dTotalPrice, OUTPUT dTotalExt).
      dAvgDisc = (dTotalPrice - dTotalExt) / dTotalPrice.
      DISPLAY dTotalPrice dTotalExt dAvgDisc
        WITH FRAME CustQuery.
    END.

This code runs the procedure, passes in the current Order number, and gets back the two values that map to the fill-in fields you defined. These are really just variables, but the AppBuilder generates definitions with a special phrase added to turn the variable into a value that can be displayed in the window as a proper GUI control. This is the VIEW-AS phrase and here is one of the three variable definitions the AppBuilder generates:

DEFINE VARIABLE dTotalPrice AS DECIMAL FORMAT "->>,>>9.99":U INITIAL 0
  LABEL "Order Total"
  VIEW-AS FILL-IN
  SIZE 14 BY 1 NO-UNDO.

The SIZE width BY height phrase defines the display size of the fill-in field. A DEFINE VARIABLE statement can specify all the different kinds of ways a single field value can be displayed, including toggle boxes for logical values, selection lists, and editor controls, using the VIEW-AS phrase.

After the RUN statement, the code does a calculation of its own to determine the average discount and stores that value in the third fill-in field:
dAvgDisc = (dTotalPrice - dTotalExt) / dTotalPrice.
Finally, the code displays the three fields in the window’s frame:
DISPLAY dTotalPrice dTotalExt dAvgDisc
  WITH FRAME CustQuery.

As you can see, this DISPLAY statement looks exactly like the DISPLAY statements you have been using in the little examples earlier. But because the fields are given the specific display type of FILL-IN, they show up as GUI controls rather than just in the report-like format you get by default.

To see the effects of your changes, Run the procedure. When you first run it, the fields come up with zeroes in them. But when you click on another row of the browse, then the calculations appear correctly:

The VALUE-CHANGED event happens only when you actually select a row in the browse, not when it is first populated with data. To correct this, you need to apply that event when the window is first initialized and also whenever the user selects a different Customer.