Home > AS/400 Tips > iSeries programmer tips > Use trigger programs to track record changes
iSeries 400 Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

ISERIES PROGRAMMER TIPS

Use trigger programs to track record changes


John Kohan
08.15.2001
Rating: -3.54- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


One of the issues programmers have to deal with is recording when a record was changed. Since we now have users accessing data from RPG programs, Cobol programs, DFU, Microsoft Access, Java, etc., it's getting harder to keep track of all the changes and who actually performed them. Trigger programs are one way for you to track these changes.

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. IBM's DB2 Web site 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. The first parameter is a data structure that is 2000 bytes long and has the following sub fields:

IPARM1 DS        2000
I                                    B  65  680NEWOFS
I                                    B  69  720NEWLNG
IPARM2       DS                                      
I                                    B   1   40NWLGTH

Now bring in the database file as an externally described data structure.

IDATA      EIDSORDERS

And then get the starting position of the new record and move the contents into our ORDERS file.

C                           Z-ADDNEWOFS     X        30 
C                           ADD    1                 X          
C      NEWLNG    SUBSTPARM1:X    DATA      P

All 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   ORDNUM

The 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.
  • 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 CT Codeworks. He is also an adjunct instructor teaching AS/400 classes at his local state collage. As one of search400's site expert John also participates in our Ask the Expert feature. If you have a question for him, you may submit it.

More information on this topic

If you're looking for answers to your programming questions, check out search400's Programmer Discussion Forum.

Rate this Tip
To rate tips, you must be a member of Search400.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
iSeries CL programming
Taking advantage of CL advancements, starting with V5R3
Checking in on your IBM i authorization lists
Running PHP open source applications: NOBODY needs authority
Simplify the process of converting a spool file from iSeries into an Excel spreadsheet
CL program for daily backups
An automated CL method of moving a query from AS/400 to Excel
Changing user password expiration
Eight steps for creating program documentation using AS/400 utilities
DAYSPAST CLLE program for AS/400: Compares object creation date with today's date
Advanced Job Scheduler help

iSeries programmer tips
Enhancing RPG with external SQL stored procedures
Tracking data changes on IBM i with triggers
Introduction to SQLRPGLE on IBM i: Making a report
Implementing a browser interface in COBOL: Displaying database fields
Taking advantage of CL advancements, starting with V5R3
TAATOOL: Useful tools for programmers on IBM i
Implementing a browser interface in COBOL: Creating your graphic Web page
Implementing a browser interface in COBOL: Getting started
Making the most of RPG data handling on IBM i
Groovy programming on IBM i

RPG iSeries programming
Enhancing RPG with external SQL stored procedures
Introduction to SQLRPGLE on IBM i: Making a report
Making the most of RPG data handling on IBM i
IBM i shop boosts online sales with RPG-based Web platform
Migrating from RPG to EGL on IBM i
Allow access to data from a stored procedure result set using COBOL or RPG
EGL Rich UI on IBM i: Do you Dojo?
Programming for the Web on the IBM i, what is possible
A taste of COMMON: ILE, IBM releases, Web applications and new products
Documenting nested program structures on the AS/400

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
Report Program Generator  (Search400.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



iSeries Security - Security Tools, Physical Security and System Security
HomeNewsTopicsITKnowledge ExchangeTipsBlogsAsk the ExpertsMultimediaWhite PapersProducts
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 1999 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts