Example: orders.4gl (Function order_update)

This function validates that the values in the order_rec program record are correct, then executes a SQL statement to update the row in the orders database table.

Function order_update (orders.4gl):
  1 PRIVATE FUNCTION order_update(d ui.Dialog) RETURNS BOOLEAN
  2 
  3   IF NOT d.getFieldTouched("orders.*") THEN RETURN TRUE END IF
  4 
  5   IF NOT order_validate(d) THEN RETURN FALSE END IF
  6 
  7   TRY
  8      UPDATE orders SET
  9               cust_num  = order_rec.cust_num,
 10               order_date = order_rec.order_date,
 11               fac_code   = order_rec.fac_code,
 12               ship_instr = order_rec.ship_instr,
 13               promo      = order_rec.promo
 14         WHERE orders.order_num = order_rec.order_num
 15   CATCH
 16      CALL comutils.mbox_ok(title1,SQLERRMESSAGE)
 17      RETURN FALSE
 18   END TRY
 19 
 20   CALL d.setFieldTouched("orders.*", FALSE)
 21   MESSAGE SFMT(msg17, order_rec.order_num)
 22 
 23   RETURN TRUE
 24 
 25 END FUNCTION
Note:
  • Line 1 defines the function with a ui.Dialog object as parameter. The dialog object will be provided by the dialog instruction by passing the DIALOG keyword as parameter. The function returns TRUE on success.
  • Line 3 checks it the order form fields have been touched. If nothing was changed, we can return TRUE without updating the SQL row.
  • Line 5 checks of the current values of the order header fields are valid by calling the order_validate function. If there is an input error, we return FALSE. The user must correct the error before trying a new update.
  • Lines 7 thru 18 try to perform the SQL UPDATE command, with the current values of the order_rec record.
  • Lines 16 and 17 are executed if the SQL statement fails. Here we return FALSE.
  • Line 20 reset the touched flags of the fields, to re-initialize the modification status of the record.
  • Line 21 show a message to the user indicating that the record has been saved into the database.
  • Line 23 returns TRUE to the caller.