Home > AS/400 Tips > iSeries programmer tips > Generic trigger program to capture create/change date and time
iSeries 400 Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

ISERIES PROGRAMMER TIPS

Generic trigger program to capture create/change date and time


Roland Thibault
09.21.2004
Rating: -3.12- (out of 5)


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


Instead of adding code to several programs to track data record changes, this program can be added to any physical file to track changes from any source. Trigger program tracks create and change user / date / time / and program.

  
* GENERIC TRIGGER PROGRAM TO CAPTURE CREATE/CHANGE DATE AND TIME.
      * IF THE TRACKING FIELDS ARE NOT AT THE END OF THE RECORD, SEE NOTES
      *   BELOW FOR CALCULATING OFFSET.
      *
      *  Add tracking fields to your existing file.
      *  TRACKING FIELDS MUST BE CONTIGUOUS AND IN THE
      *  FOLLOWING FORMAT
      *      CRTBY         10
      *      CRTPGM        10
      *      CRTDAT         7P 0
      *      CRTTIM         6P 0
      *      CHGBY         10
      *      CHGPGM        10
      *      CHGDAT         7P 0
      *      CHGTIM         6P 0
      *
      *  add triggers to the file to be tracked as follows:
      *
      *     ADDPFTRG FILE(xxxxxxxx)  /*replace xxxxxx with your file name */
      *      TRGTIME(*BEFORE)
      *      TRGEVENT(*UPDATE)
      *      PGM(TRACKCHG)
      *      ALWREPCHG(*YES)
      *
      *     ADDPFTRG FILE(xxxxxxxx)  /*replace xxxxxx with your file name */
      *      TRGTIME(*BEFORE)
      *      TRGEVENT(*INSERT)
      *      PGM(TRACKCHG)
      *      ALWREPCHG(*YES)
      *
      *
     H DATEDIT(*YMD) OPTION(*NODEBUGIO)
     D GetCaller       PR                  Extpgm('QWVRCSTK')
     D                             2000
     D                               10I 0
     D                                8    CONST
     D                               56
     D                                8    CONST
     D                               15
      *
     D Var             DS          2000
     D  BytAvl                       10I 0
     D  BytRtn                       10I 0
     D  Entries                      10I 0
     D  Offset                       10I 0
      *
     D VarLen          S             10I 0 Inz(%size(Var))
     D ApiErr          S             15
     D Doffset         S             10I 0
      *
     D JobIdInf        DS
     D  JIDQName                     26    Inz('*')
     D  JIDIntID                     16
     D  JIDRes3                       2    Inz(*loval)
     D  JIDThreadInd                 10I 0 Inz(1)
     D  JIDThread                     8    Inz(*loval)
      *
     D Entry           DS           256
     D  EntryLen                     10I 0
     D  PgmNam                       10    Overlay(Entry:25)
     D  PgmLib                       10    Overlay(Entry:35)
     D
      *
     D JIDUser         S             10    inz(*user)
     D JIDDate         S               D   Inz(*sys)
     D JIDTime         S               t   inz(*sys)
     D TKOFFSET        S              5  0
     D I               S              5  0
     D oldrec          S           1000
     D newrec          S           1000
      *
     Doverlays         ds
     D crtdata                 1      7
     D crtdatn                 1      7  0
     D crttima                 8     13
     D crttimn                 8     13  0
     D chgdata                14     20
     D chgdatn                14     20  0
     D chgtima                21     26
     D chgtimn                21     26  0
      *
     DTRACKING         DS
     D ALLFIELDS               1     56
     D CRTBY                   1     10
     D CRTPGM                 11     20
     D CRTDAT                 21     24P 0
     D CRTTIM                 25     28P 0
     D CHGBY                  29     38
     D CHGPGM                 39     48
     D CHGDAT                 49     52P 0
     D CHGTIM                 53     56P 0
     dPARM1            ds          2000
      * Trigger event
     d TEVEN                  31     31
      * Offset to the original record
     d OLDOFF                 49     52B 0
      * Offset to the new record
     d NOFF                   65     68B 0
      * length of the new record
     d NEWLEN                 69     72B 0
      *
     dPARM2            DS
     d LENG                    1      4B 0
      *
      * program status data structure
     dpssr            sds
     d thispgm           *proc
      ******************************************************************
      * SET UP THE ENTRY PARAMETER LIST.
      ******************************************************************
     C     *ENTRY        PLIST
     C                   PARM                    PARM1
     C                   PARM                    PARM2
     C/FREE
       //
       // GET CALLING PROGRAM NAME
       //
       // NOTE THAT THE CSTK0100 AND JIDF0100 ARE LITERALS TO IDENTIFY
       //  THE FORMATS USED IN THE API, DON'T CHANGE THESE
       //
           GetCaller(Var:VarLen:'CSTK0100':JobIdInf
           :'JIDF0100':ApiErr);
           FOR i = 1 TO Entries ;
            Entry = %subst(Var:Offset + 1) ;
            if  pgmnam <> thispgm and
                pgmlib <> 'QSYS'           ;
                leave                      ;
               endif                       ;
             Offset = Offset + EntryLen    ;
             Endfor                        ;

        // check for no changes made
           oldrec = %subst(parm1:oldoff:newlen) ;
           newrec = %subst(parm1:noff:newlen)   ;

           if oldrec <> newrec ;

        //
        // if the tracking data is not at the end of the record
        //  change the calc for TKOFFSET to be equal to the first
        //  position of the tracking data instead of "newlen - 55"
        //  55 is then length of the tracking data plus 1
        //
        //  TKOFFSET = 265 ;
           TKOFFSET = newlen - 55  ;


        // RETRIEVE THE TRACKING DATA
        //
           allfields = ' ' ;
           doffset = NOFF + tkoffset ;
           ALLFIELDS = %SubSt(Parm1:doffset:56) ;
        //
        // update tracking data fields
        //
           if teven = '1' ;
             crtby  = JIDUser ;
             crtdata = %char(JIDDate:*cymd0) ;
             crttima = %char(JIDTime:*hms0)  ;
             crtdat  = crtdatn ;
             crttim  = crttimn ;
             crtpgm = PgmNam ;
            elseif teven = '3' ;
             chgby  = JIDUser ;
             chgdata = %char(JIDDate:*cymd0) ;
             chgtima = %char(JIDTime:*hms0)  ;
             chgdat  = chgdatn ;
             chgtim  = chgtimn ;
             chgpgm = PgmNam ;
            endif ;
        //
        // put tracking data back into buffer
        //
           %SubSt(Parm1:doffset:56) = ALLFIELDS ;
            endif ;
           *inlr = '1' ;
      /end-free

  
  


==================================
MORE INFORMATION ON THIS TOPIC
==================================

The Best Web Links: tips, tutorials and more.

Ask your programming questions--or help out your peers by answering them--in our live discussion forums.

Ask the Experts yourself: Our application development gurus are waiting to answer your programming questions.


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

Application Development
iSeries calling an .exe
Top 10 programmer tips
Formatted work job scheduler
Convert system date and time
Mixing free format code with embedded SQL
SQL update a field in one file from a field in another file
Webcasts for iSeries programmers
Programming advice from the pros
Easy code copying via the drag and drop method
Setting FTP time-outs

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

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