Generic trigger program to capture create/change date and time

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.

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.


This was first published in September 2004

Dig deeper on iSeries CL programming

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchEnterpriseLinux

SearchDataCenter

Close