The ON APPEND trigger
The ON APPEND
trigger defines an action named
"append"
, which is dedicated to row creation at the end of the record
list.
Inside a DISPLAY ARRAY
dialog, defining the ON APPEND
block
will automatically create an "append"
action for the dialog, that can be fired by
the user with the Append button, to create a new row.
Best practice is to use the UNBUFFERED
attribute with
DISPLAY ARRAY
modification triggers.
The ON APPEND
block typically implements an INPUT
instruction, binding a record variable
to the form fields of a new row at the end of the TABLE
container used by the
DISPLAY ARRAY
dialog.
After the INPUT
dialog terminates, the int_flag
register is
used to validate or cancel the row creation, when the control goes back to the DISPLAY
ARRAY
dialog. If the user input is validated, the program can try to insert a new row into
the database table. If the SQL INSERT
succeeds, assign the entered values to the
new row of the program array. If the SQL INSERT
fails, display an error message and
force the int_flag to TRUE
in order to cancel the operation. If
int_flag
is TRUE
, the DISPLAY ARRAY
dialog resets
the current row data as before the INPUT
. If int_flag
is
FALSE
, the DISPLAY ARRAY
dialog keeps the new created row, with
the data of the corresponding row of the program array.
The INPUT
instruction must be defined with the FROM
clause and
the screen array of the form, with a screen array index equal to the current screen line returned by
the scr_line()
build-in function. This is the new append line in the
TABLE
container, as seen by the user.
ON APPEND
code example: 1 ON APPEND
2 LET x = DIALOG.getCurrentRow("sa_cust")
3 INITIALIZE rec.* TO NULL
4 LET rec.cust_num = 1000 + x
5 LET rec.cust_name = "<undefined>"
6 INPUT rec.* WITHOUT DEFAULTS FROM sa_cust[scr_line()].* ;
7 IF NOT int_flag THEN
8 TRY
9 INSERT INTO customer VALUES (rec.*)
10 LET custarr[x] = rec
11 CATCH
12 ERROR SQLERRMESSAGE
13 LET int_flag = TRUE
14 END TRY
15 END IF
- Line
1
declares theON APPEND
trigger. - Line
2
assigns the current row index to thex
variable defined asINTEGER
. - Line
3
resets all fields of therec
record toNULL
. - Line
4
sets a new primary key value for thecust_num
field. Best practice would be to use an SQLSEQUENCE
, to produce a unique number. For convenience, in this sample we just add1000
to the new row index. - Line
5
sets a default value of the customer name. - Line
6
implements theINPUT
dialog to let the user enter new customer record data. Note that theINPUT
uses theFROM
clause, with thesa_cust
screen array, at the current screen line returned by the build-inscr_line()
function. - Line
7
checks theint_flag
register. - Line
8
thru14
: here we try toINSERT
the new record into the SQL table. This SQL statement may fail because of some SQL table constraints (NOT NULL
,UNIQUE
, etc). Therefore, we want to protect the code in aTRY/CATCH
block and show an error message in case of problem. If the SQL fails we must setint_flag
toTRUE
to force the dialog to cancel the new row creation in the record list. - Line
9
implements the SQLINSERT
statement, using therec
record values. - Line
10
assigns the values of the input record to the new row created in the dynamic array variablecustarr
. This is mandatory, otherwise the new row would be empty when the control goes back to theDISPLAY ARRAY
dialog.