Creating triggers

Complete these instructions to create a trigger.

Procedure

  1. Write a FlashBASIC subroutine with this general outline. Note the ACCESS () function is functional at the file-level, that is, when the subroutine is defined as part of the correlative of a file-defining item. The functionality of the ACCESS function in the instance is different from that described in Correlatives.
    subroutine subname(record)
       * "record" contains list of columns separated by
       * 0xFE characters (minus the primary key).
       * To get the primary key, use the access(10)
       * function.
       primarykey = access(10); 
    
       * Do any desired trigger processing
       * When complete, the resulting record may be
       * modified by modifying the "record" variable.Record = …
     
    
       * If the update should fail due to an integrity
       * violation or some other reason, then the
       * "inputerr" statement displays an error message
       * and rolls back ALL updates in the current
       * transaction. 
    
       if update.fails then inputerr 'Error Message' 
    
    return
  2. Compile the program using the compile TCL command.
  3. Attach the program to the D3 file with the callx trigger. To do this:
    • Log to the account where the D3 file corresponding to the SQL table exists.

    • Enter ud file_name

    • Due to SQL naming conventions, the D3 filename, file_name, may not be the same as the SQL tablename.

      Note that the older ed editor can be used if you are more comfortable with that editor.

    • If using the ud command, press <Return> several times until the cursor is on the row marked correlative.

    • Entercallx account, file, subroutine_name
    • where account and file are the account and file where the FlashBASIC program resides, and subroutine_name is the name of the program.

    • Press CTRL-X followed by the F key to file, or apply, the changes.

    With the trigger in place, any database update, whether from SQL or from a legacy application, will result in a call to the applied code.

    Note: If using transactions with D3 SQL statements, the trigger is not called until after a COMMIT. Therefore, errors returned by a callx trigger are not reported until after the transaction is committed.