Rpg Trigger Program Example
Trigger programs are also useful in starting a process without user intervention. As an example, orders are now received from our customers many ways. These may include an order-entry program, EDI, a Java application, Web-based shopping carts and the like.
You can write a trigger program to alert someone that an order has been received and to begin processing the order-fulfillment process.Before we begin to understand and write a simple trigger program, the example below uses only 11 lines of source code (RPG/400) to process the data. This process IS easy and with practice is also practical.When you create a trigger program, you need to bring two parameters into your program. The first parameter is Database file information much like the INFDS. It contains the trigger information, along with the record information. The second parameter is a 4-byte binary field that contains the length of the trigger buffer. Has a table that defines the trigger buffer I used.Since the records in our databases have different lengths, the value in the 'New record offset' field contains the beginning position within the first parameter that the new record data begins. This record matches the record buffer, so if you have three 10-byte alpha fields, the 'New Record' section of parameter 1 is 30 bytes containing the contents of the record that was just added.Now lets put this together and write TRGPGM to call PGM passing the order number when a record is added to the file ORDERS.Create TRGPGM and allow two parameters in.
Rpg Program
The first parameter is a data structure that is 2000 bytes long and has the following sub fields: IPARM1 DS 2000I B 65 680NEWOFSI B 69 720NEWLNGIPARM2 DSI B 1 40NWLGTHNow bring in the database file as an externally described data structure. IDATA EIDSORDERSAnd then get the starting position of the new record and move the contents into our ORDERS file. C Z-ADDNEWOFS X 30C ADD 1 XC NEWLNG SUBSTPARM1:X DATA PAll of the data is now loaded into the database fields, and we access the data just as if we read a file. Call program PGM and pass the order number.
C CALL 'PGM'C PARM ORDNUMThe last thing to do is add TRGPGM to the ORDERS file. You do this by running the following command: ADDPFTRG FILE(ORDERS) TRGTIME(.AFTER) TRGEVENT(.INSERT) PGM(TRGPGM)The following are recommended for a trigger program:(Copied from the above IBM Web site). Create the trigger program so that it runs under the user profile of the user who created it. Then users who do not have the same level of authority to the program will not encounter errors. Create the program with USRPRF(.OWNER) and.EXCLUDE public authority, and do not grant authorities to the trigger program to USER(.PUBLIC). Avoid having the trigger program altered or replaced by other users. The database invokes the trigger program whether or not the user causing the trigger program to run has authority to the trigger program.
Rpg Trigger Program Example For Students
Create the program as ACTGRP(.CALLER) if the program is running in an ILE environment. This allows the trigger program to run under the same commitment definition as the application.
Open the file with a commit lock level the same as the application's commit lock level. This allows the trigger program to run under the same commit lock level as the application. Create the program in the physical file's library.
Use commit or rollback in the trigger program if the trigger program runs under a different activation group than the application. Signal an exception if an error occurs or is detected in the trigger program. If an error message is not signaled from the trigger program, the database assumes that the trigger ran successfully. This may cause the user data to end up in an inconsistent state.-About the author: John Kohan is a senior programmer analyst at. He is also an adjunct instructor teaching AS/400 classes at his local state collage.
Rpg Trigger Program Examples
As one of search400's site expert John also participates in our Ask the Expert feature. If you have a question for him, you mayMore information on this topicIf you're looking for answers to your programming questions, check out.
Published:Could you please show me some examples of triggers using RPG 400 or RPG IV?Triggers have changes from V4R5 and later. V4R5 uses the structures found in There should also be some threads in on triggers.MORE INFORMATION ON THIS TOPIC: tips, tutorials and more.Ask your programming questions-or help out your peers by answering them-in our.yourself: Our application development gurus are waiting to answer your programming questions.