Example: orders.4gl (Function comp_order_total)
This function implements the SQL row deletion for an order item row.
Function
comp_order_total
(orders.4gl): 1 PRIVATE FUNCTION comp_order_total() RETURNS ()
2 DEFINE x INTEGER
3 LET order_total = 0
4 FOR x = 1 TO orditems.getLength()
5 LET order_total = order_total + NVL(orditems[x].line_total,0)
6 END FOR
7 END FUNCTION
Note:
- Line
1
declares the function without parameter and without return value. - Line
2
defines thex
integer variable, used as array index in theFOR
loop. - Line
3
initializes the total variable to zero. - Lines
4
to8
implement theFOR
loop to scan the rows of theorditems
dynamic array. - Line 5 adds the value of the current order line to the total. Note the use of the
NVL()
operator: ThisNVL()
expression will return to zero, if the line_total field isNULL
. AddingNULL
the the order_total variable would result toNULL
.