Example: orders.4gl (Function item_insert)

This function implements the SQL row creation for a new order item row.

Function item_insert (orders.4gl):
  1 PRIVATE FUNCTION item_insert(x INTEGER) RETURNS BOOLEAN
  2 
  3   TRY
  4      INSERT INTO items (
  5         order_num,
  6         stock_num,
  7         quantity,
  8         price
  9      ) VALUES (
 10         order_rec.order_num,
 11         orditems[x].stock_num,
 12         orditems[x].quantity,
 13         orditems[x].price
 14      )
 15   CATCH
 16      CALL comutils.mbox_ok(title2,SQLERRMESSAGE)
 17      RETURN FALSE
 18   END TRY
 19 
 20   MESSAGE msg08
 21   CALL comp_order_total()
 22   RETURN TRUE
 23 
 24 END FUNCTION
Note:
  • Line 1 declares the function with the x integer parameter representing the index in the orditems array, and returns a boolean to indicate if the SQL command succeeds.
  • Lines 4 thru 15 implement the SQL INSERT command, to insert a new row into the items table.
  • Lines 16 and 17 are executed when an SQL error occurs, and returns from the function with FALSE.
  • Line 20 shows a message to the user to indicate that the SQL operation succeeded.
  • Line 13 re-computes the order total by calling the comp_order_total function.
  • Line 14 returns TRUE to the caller.