dataEvent_record_BeforeDeleteRow

Function called before deleting a row.

Syntax

PUBLIC FUNCTION dataEvent_record_BeforeDeleteRow(
   uniqueKey record-type)
   RETURNS (INTEGER, STRING, uniqueKey)

The function has this parameter:

  1. uniqueKey. This is a RECORD type defined in the BAM-generated common file (my_entity_common.4gl). It includes the unique key fields from the schema of the database table.
It returns these values:
  1. An integer with the SQLCA.SQLCODE error number. Codes are defined as constants in the libdbappSql and libdbappCore libraries. The errors that are relevant to this function are shown in Table 1.
    Table 1. SQLCA.SQLCODE
    Description Constant Value
    Success ERROR_SUCCESS 0
    Failure ERROR_FAILURE -1
  2. A string is returned with the SQLERRMESSAGE error message.
  3. uniqueKey. This is a RECORD type defined in the BAM-generated common file (my_entity_common.4gl). It includes the unique key fields from the schema of the database table.

Usage

When you select the Before Delete Row property for the creation of the event, a function shell is created. Enter your code in the function.

This function is called before deleting a row in the database. For example, you can use this function to write the row of data to a backup table before the row is deleted.

Example: BeforeDeleteRow

This example uses the Before Delete Row code event for the Account table in the OfficeStore demo.

In the example the row of data referenced by the variable uniqueKey is written to a backup table with a call to the function writeDataToAccBackup.

The writeDataToAccBackup function has a parameter that stores the row of data from the account table.

# AccountForm.4gl

-- import user-defined functions
IMPORT FGL myAccountFunc

PUBLIC FUNCTION dataEvent_recAccount_BeforeDeleteRow(uniqueKey recAccount_br_uk_type)
    RETURNS (INTEGER, STRING, recAccount_br_uk_type)

    DEFINE errNo INTEGER
    DEFINE errMsg STRING
    DEFINE l_data RECORD LIKE account.*

    DISPLAY "dataEvent_recAccount_BeforeDeleteRow (Row scope) is raised"
    SELECT * INTO l_data.* FROM account WHERE @userid = uniqueKey.account_userid
    CALL myAccountFunc.writeDataToAccBackup(l_data) RETURNING errNo
    IF errNo = libdbappCore.ERROR_FAILURE THEN
       LET errMsg = " - BeforeDeleteRow failed to insert in backup "  
    END IF
    DISPLAY "dataEvent_recAccount_BeforeDeleteRow (Row scope) is exited"

    RETURN errNo, errMsg, uniqueKey.*
END FUNCTION

In this section there is an example of the function writeDataToAccBackup called to write data to a backup account table. It is assumed that you have created the backup table for account before saving the row to it.

All functions that you would reuse across forms and applications would reside in a module you create. To use these functions in your project, you would import your module into the entity module (for example, AccountForm.4gl) using the IMPORT FGL statement.

# MyAccountFunc.4gl

IMPORT FGL libdbappCore

FUNCTION writeDataToAccBackup(dataRec RECORD LIKE account.*) 
  RETURNS (INTEGER)
  
  DEFINE errNo INTEGER
    
  TRY
    INSERT INTO account_backup VALUES (dataRec.*)
  CATCH
     DISPLAY "SQL ERROR: ", sqlca.sqlcode
     LET errNo = libdbappCore.ERROR_FAILURE
  END TRY
  RETURN errNo
 
END FUNCTION