Step 1. Create a Trigger Function
FRM105 - A Trigger Function
In this step you will create a file level trigger function for file PSLMST using a template. You will complete the trigger function based on the code supplied in Appendix A. FRM105. Note that you must change the email recipient to an email address to which you have access.
1. Create a Process iiiPRO02 – iii Trigger Functions
2. Create a new function iiiFN03 – Employee Salary Trigger, using template BBFILTRIG for file PSLMST
3. Remove all CASE, WHEN clauses except BEFUPD. Your code should look like the following:
Function Options(*DIRECT *NOMESSAGES *LIGHTUSAGE *MLOPTIMISE) Rcv_List(#TRIG_LIST) Trigger(*FILE PSLMST)
Def_List Name(#TRIG_LIST) Type(*WORKING) Entrys(2)
* Assume a "good" return initially
Change Field(#TRIG_RETC) To('OK')
Case Of_Field(#TRIG_OPER)
* Handle a before update event
When Value_Is('= BEFUPD')
* Handle an event not catered for
Otherwise
Abort Msgtxt('File PSLMST trigger function invalidly invoked/ used.')
Endcase
* Return control to the invoker
Return
Note: Comment lines have been removed.
4. Complete most of the trigger logic by copying the code provided in Appendix A. FRM105. This should be pasted after the Return command.
5. Review the supplied code.
- Subroutine SEND_EMAIL generates and send and email message, using the LANSA Email BIFs
- Subroutine ADD_TEXT is executed from SEND_EMAIL to create the email body text
- In a real application, the URL inserted into the text body would be for web application (a WAM), which would will enable the salary change to be confirmed simply by clicking on the URL to run it in the browser. To simplify this exercise, you will create a form to confirm the salary change.
6. Change the email recipient address to be any email address to which you have access.
7. Your email client may also require that the email originator address is a recognized address. Change this if necessary.
8. Complete the BEFUPD logic:
- If the salary increase is greater than 1,000 and business phone is not equal A100. This value indicates the update needs to be approved.
- Send an email message
- Reset salary to previous value
- else
- Set business phone to N/A
- Trigger functions receive a trigger list (working list TRIG_LIST) containing 0, 1 or 2 entries. TRIG_LIST contains the file data structure. That is, all file fields.
- For a file update, TRIG_LIST entry 1 contains the new record while entry 2 contains the old record.
- The TRIG_LIST is returned to the file OAM. This enables the trigger to modify field values, in a before-update trigger. This feature can be used to calculate virtual fields which cannot be produced using the standard virtual field derivation logic.
Your completed code should look like the following:
Case Of_Field(#TRIG_OPER)
* Handle an before update event
When Value_Is('= BEFUPD')
Define Field(#increase) Reffld(#salary)
Define Field(#newsal) Reffld(#salary)
Get_Entry Number(1) From_List(#TRIG_LIST)
#phonebusw := #phonebus
#newsal := #salary
Get_Entry Number(2) From_List(#TRIG_LIST)
#increase := #newsal - #salary
If ((#increase *GT 1000) *And (#phonebusw *NE 'A100'))
Execute Subroutine(send_EMAIL)
* reset salary to previous value
Get_Entry Number(1) From_List(#TRIG_LIST)
#salary := #newsal - #increase
Upd_Entry In_List(#trig_list)
Endif
* reset business phone if contains A100
Get_Entry Number(1) From_List(#TRIG_LIST)
If (#phonebus = 'A100')
#phonebus := N/A
Upd_Entry In_List(#trig_list)
Endif
* Handle an event not catered for
Otherwise
Abort Msgtxt('File PSLMST trigger function invalidly invoked/ used.')
Endcase
* Return control to the invoker
Return
9. Compile your trigger function.