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 FUNCTIONNote:
- Line
1defines the function with aui.Dialogobject as parameter. The dialog object will be provided by the dialog instruction by passing theDIALOGkeyword as parameter. The function returnsTRUEon success. - Line
3checks it the order form fields have been touched. If nothing was changed, we can returnTRUEwithout updating the SQL row. - Line
5checks of the current values of the order header fields are valid by calling theorder_validatefunction. If there is an input error, we returnFALSE. The user must correct the error before trying a new update. - Lines
7thru18try to perform the SQLUPDATEcommand, with the current values of theorder_recrecord. - Lines
16and17are executed if the SQL statement fails. Here we returnFALSE. - Line
20reset the touched flags of the fields, to re-initialize the modification status of the record. - Line
21show a message to the user indicating that the record has been saved into the database. - Line
23returnsTRUEto the caller.