ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Problem with File Trigger

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with File Trigger

    I have problem with a file trigger where field newrec is blank. I am doing an insert to a file and when I debug the "for" loop, the field newrec is empty

    Code:
    *--------------------------------------
    * Format for Trigger Buffer            
    *--------------------------------------
    TrgBuffer        ds         32767      
     TFileName               1     10      
     TLibName               11     20      
     TMemName            21     30      
     TEvent                   31     31      
     TTime                     32     32      
     TCommitLock          33     33      
     TFiller1                    34     36      
     TCCSID                   37     40b 0   
     TFiller2                    41     48      
     TOldOffset              49     52b 0   
     TOldLength             53     56b 0   
     TOldNullOff              57     60b 0   
     TOldNullOffset         61     64b 0   
     TNewOffset             65     68b 0   
    TNewLength             69     72b 0  
    TNewNullOff             73     76b 0  
    TNewNullOffset        77     80b 0  
    TTReserve                81     96     
    
    *------------------------------------
    * Buffer Length                      
    TrgBufferLen     s             10i 0 
    *------------------------------------
    
    ------------------------------------------------------
     Record Buffer                                        
    ------------------------------------------------------
    newrec         e ds                  extname(fhpf001)  
    
     *Inzsr        Begsr                                    
                                                            
     *Entry        Plist                                    
                       Parm                    TrgBuffer        
                       Parm                    TrgBufferLen     
    
    c                   eval      newrec = %addr(TrgBuffer) + TNewOffset  
                                                                          
    c                   for        x = 1 to %elem(xlarray)                 
    c                   eval      foundpos   = %scan(',':newrec:startpos) 
    c                   if           foundpos > 0                            
    c                   eval      xlarray(x) = %subst(newrec:startpos     
    c                                         : (foundpos - startpos))     
    c                   eval      startpos   = foundpos + 1               
    c                   endif                                             
    c                   endfor

  • #2
    Re: Problem with File Trigger

    havent used with ponters but here is an example without

    Code:
    ***********************************************************************
         FTRGORDDETPO    E             DISK
          *----------------------------------------------------------------
         D Buffer          DS         32767
         D  FileName               1     10
         D  LibraryName           11     20
         D  MemberName            21     30
         D  TrgEvent              31     31
         D  TrgTime               32     32
         D  CommitLckLvl          33     33
         D  Filler1               34     36
         D  CCSID                 37     40B 0
         D  Filler2               41     48
         D  OldOff                49     52B 0
         D  OldLen                53     56B 0
         D  OldNullOffset         57     60B 0
         D  OldNullLength         61     64B 0
         D  NewOff                65     68B 0
         D  NewLen                69     72B 0
         D  NewNullOffset         73     76B 0
         D  NewNullLength         77     80B 0
         D  Resv3                 81     96
          *----------------------------------------------------------------
          * Total Buffer length
         D BufLen          DS
         D  Leng                   1      4B 0
          *----------------------------------------------------------------
          *----------------------------------------------------------------
          * Map the fields from the DB file
          *  to take the before/after images apart.
         D Before        E DS                  EXTNAME(SROORSPL)Prefix(B)
         D After         E DS                  EXTNAME(SROORSPL)Prefix(A)
         D*
          *----------------------------------------------------------------
          *  Work fields
         D Start           s              5P 0
         D Count           s              9S 0
         D Loop            s              9S 0
         D WhoCalled       s             10A
         D Caller          s             10A
          *----------------------------------------------------------------
         D*
         D* Program Info
         D*
         D                SDS
         D  @PGM                 001    010
         D  @PARMS               037    039  0
         D  @JOB                 244    253
         D  @USER                254    263
         D  @JOB#                264    269  0
         D*
         D*----------------------------------------------------------------
         C     *entry        Plist
         C     Buffer        Parm                    Buffer
         C     BufLen        Parm                    BufLen
          *----------------------------------------------------------------
          *  Determine trigger event and take appropriate action
         C                   Select
         C*
         C*  1 = Add and 3 = Update
         C*
         C                   When      TrgEvent = '1'
         C                   Exsr      $CheckInsert
         C                   Exsr      $WriteAudit
         C*
         C                   When      TrgEvent = '3'
         C                   Clear                   Count
         C                   Exsr      $CheckUpdate
         C                   Exsr      $WriteAudit
         C*
         C                   Endsl
         C*
         C                   Eval      *Inlr = *On
          *----------------------------------------------------------------
         C     $CheckInsert  Begsr
         C*
         C                   Exsr      $GetNewFields
         C*
         C                   Endsr
          *----------------------------------------------------------------
         C     $CheckUpdate  Begsr
         C*
         C                   Exsr      $GetOldFields
         C*
         C                   Exsr      $GetNewFields
         C*
         C                   Endsr
          *----------------------------------------------------------------
         C     $GetOldFields Begsr
         C* "Before" image
         C*  move original record to field layout
         C                   Eval      Start = OldOff + 1
         C                   Eval      Before = %subst(Buffer:Start:OldLen)
         C*
         C                   Endsr
         C*----------------------------------------------------------------
         C     $GetNewFields Begsr
         C*
         C*  move new record to field layout
         C*
         C                   Eval      Start = NewOff + 1
         C                   Eval      After  = %subst(Buffer:Start:NewLen)
         C*
         C                   Endsr
         C*----------------------------------------------------------------
         C     $WriteAudit   Begsr
         C*
         C*  Check to see what has changed.
         C*
         C                   If        BOLSTAT<>AOLSTAT or
         C                             BOLORNO<>AOLORNO or
         C                             BOLLINE<>AOLLINE or
         C                             BOLMLIN<>AOLMLIN or
         C                             BOLORLI<>AOLORLI or
         C                             BOLROLI<>AOLROLI or
         C                             BOLLTYP<>AOLLTYP or
         C                             BOLCUNO<>AOLCUNO or
         C                             BOLSROM<>AOLSROM or
         C                             BOLORDT<>AOLORDT or
         C                             BOLBALC<>AOLBALC or
         C                             BOLPRDC<>BOLPRDC or
         C                             BOLOQTY<>AOLOQTY or
         C                             BOLOQTS<>AOLOQTS or
         C                             BOLDTIM<>AOLDTIM or
         C                             BOLRDTI<>AOLRDTI or
         C                             BOLSALP<>AOLSALP or
         C                             BOLFOCC<>AOLFOCC or
         C                             BOLAMOU<>AOLAMOU or
         C                             BOLDELT<>AOLDELT or
         C                             BOLRDDT<>AOLRDDT or
         C                             BOLROCO<>AOLROCO or
         C                             BOLSTRU<>BOLSTRU or
         C                             BOLPLNO<>BOLPLNO or
         C                             BOLPLLI<>BOLPLLI or
         C                             BOLORDS<>BOLORDS or
         C                             BOLSENC<>BOLSENC or
         C                             BOLCBYD<>BOLCBYD
         C*
         C                   Call      'WHOCALLED'
         C                   Parm                    WhoCalled
         C                   Parm      @PGM          Caller
         C*
         C                   Clear                   Loop
         C*
         C                   Dou       %Subst(WhoCalled:1:1) <> 'Q'
         C                             Or Loop = 11.
         C                   Eval      Loop = (Loop + 1)
         C                   Movel(p)  WhoCalled     Caller
         C                   Call      'WHOCALLED'
         C                   Parm      *Blanks       WhoCalled
         C                   Parm                    Caller
         c                   Enddo
         C*
         C                   Movel(p)  WhoCalled     PROGRAM
         C*
         C                   Select
         C                   When      TrgEvent = '1'
         C                   Eval      Type = 'A'
         C                   When      TrgEvent = '3'
         C                   Eval      Type = 'U'
         C*
         C*   do the first 5 changed fields
         C*
         C                   If        BOLDTIM<>AOLDTIM
         C                   Eval      Count = (Count + 1)
         C                   Select
         C                   When      Count = 1.
         C                   Eval      CHANGEFLD1 = 'AOLDTIM'
         C                   When      Count = 2.
         C                   Eval      CHANGEFLD2 = 'AOLDTIM'
         C                   When      Count = 3.
         C                   Eval      CHANGEFLD3 = 'AOLDTIM'
         C                   When      Count = 4.
         C                   Eval      CHANGEFLD4 = 'AOLDTIM'
         C                   When      Count = 5.
         C                   Eval      CHANGEFLD5 = 'AOLDTIM'
         C                   When      Count = 6.
         C                   Eval      CHANGEFLD6 = 'AOLDTIM'
         C                   Endsl
         C                   Endif
         C*
         C                   If        BOLRDTI<>AOLRDTI
         C                   Eval      Count = (Count + 1)
         C                   Select
         C                   When      Count = 1.
         C                   Eval      CHANGEFLD1 = 'AOLRDTI'
         C                   When      Count = 2.
         C                   Eval      CHANGEFLD2 = 'AOLRDTI'
         C                   When      Count = 3.
         C                   Eval      CHANGEFLD3 = 'AOLRDTI'
         C                   When      Count = 4.
         C                   Eval      CHANGEFLD4 = 'AOLRDTI'
         C                   When      Count = 5.
         C                   Eval      CHANGEFLD5 = 'AOLRDTI'
         C                   When      Count = 6.
         C                   Eval      CHANGEFLD6 = 'AOLRDTI'
         C                   Endsl
         C                   Endif
         C*
         C*
         C                   If        BOLDELT<>AOLDELT
         C                   Eval      Count = (Count + 1)
         C                   Select
         C                   When      Count = 1.
         C                   Eval      CHANGEFLD1 = 'AOLDELT'
         C                   When      Count = 2.
         C                   Eval      CHANGEFLD2 = 'AOLDELT'
         C                   When      Count = 3.
         C                   Eval      CHANGEFLD3 = 'AOLDELT'
         C                   When      Count = 4.
         C                   Eval      CHANGEFLD4 = 'AOLDELT'
         C                   When      Count = 5.
         C                   Eval      CHANGEFLD5 = 'AOLDELT'
         C                   When      Count = 6.
         C                   Eval      CHANGEFLD6 = 'AOLDELT'
         C                   Endsl
         C                   Endif
         C*
         C                   If        BOLRDDT<>AOLRDDT
         C                   Eval      Count = (Count + 1)
         C                   Select
         C                   When      Count = 1.
         C                   Eval      CHANGEFLD1 = 'AOLRDDT'
         C                   When      Count = 2.
         C                   Eval      CHANGEFLD2 = 'AOLRDDT'
         C                   When      Count = 3.
         C                   Eval      CHANGEFLD3 = 'AOLRDDT'
         C                   When      Count = 4.
         C                   Eval      CHANGEFLD4 = 'AOLRDDT'
         C                   When      Count = 5.
         C                   Eval      CHANGEFLD5 = 'AOLRDDT'
         C                   When      Count = 6.
         C                   Eval      CHANGEFLD6 = 'AOLRDDT'
         C                   Endsl
         C                   Endif
         C*
         C                   If        BOLCBYD<>AOLCBYD
         C                   Eval      Count = (Count + 1)
         C                   Select
         C                   When      Count = 1.
         C                   Eval      CHANGEFLD1 = 'AOLCBYD'
         C                   When      Count = 2.
         C                   Eval      CHANGEFLD2 = 'AOLCBYD'
         C                   When      Count = 3.
         C                   Eval      CHANGEFLD3 = 'AOLCBYD'
         C                   When      Count = 4.
         C                   Eval      CHANGEFLD4 = 'AOLCBYD'
         C                   When      Count = 5.
         C                   Eval      CHANGEFLD5 = 'AOLCBYD'
         C                   When      Count = 6.
         C                   Eval      CHANGEFLD6 = 'AOLCBYD'
         C                   Endsl
         C                   Endif
         C*
         C                   If        BOLPRMD<>AOLPRMD
         C                   Eval      Count = (Count + 1)
         C                   Select
         C                   When      Count = 1.
         C                   Eval      CHANGEFLD1 = 'AOLPRMD'
         C                   When      Count = 2.
         C                   Eval      CHANGEFLD2 = 'AOLPRMD'
         C                   When      Count = 3.
         C                   Eval      CHANGEFLD3 = 'AOLPRMD'
         C                   When      Count = 4.
         C                   Eval      CHANGEFLD4 = 'AOLPRMD'
         C                   When      Count = 5.
         C                   Eval      CHANGEFLD5 = 'AOLPRMD'
         C                   When      Count = 6.
         C                   Eval      CHANGEFLD6 = 'AOLPRMD'
         C                   Endsl
         C                   Endif
         C                   Endsl
         C*
         C                   Eval      USER = @USER
         C                   Eval      TRANSDATE = %Date()
         C                   Eval      TRANSTIME = %Time()
         C                   Write     ORSPLX
         C                   Endif
         C*
         C                   Endsr
          *----------------------------------------------------------------
    All my answers were extracted from the "Big Dummy's Guide to the As400"
    and I take no responsibility for any of them.

    www.code400.com

    Comment


    • #3
      Re: Problem with File Trigger

      You may need to add one to the address. Here's a sample trigger I use. Note that dropping the column numbers on the fields helps avoid length errors.
      Attached Files

      Comment


      • #4
        Re: Problem with File Trigger

        Originally posted by jamief View Post
        havent used with ponters but here is an example without

        Code:
             C     *entry        Plist
             C     Buffer        Parm                    Buffer
             C     BufLen        Parm                    BufLen

        pardon my ignorance, but if i will code this as my trigger program, how or who will pass the entry paramters when the program is called?
        idiot and stoopid with SQL

        Comment


        • #5
          Re: Problem with File Trigger

          Originally posted by EddieBanz View Post
          pardon my ignorance, but if i will code this as my trigger program, how or who will pass the entry paramters when the program is called?
          The buffer data comes from the system, the database if you like, when the action meets the requirements of the trigger. You just need to have them defined in the handling program.
          the smoking gnu

          Comment


          • #6
            Re: Problem with File Trigger

            A trigger program is linked to a table/physical file or SQL view and activated by the database manager depending on the trigger event (INSERT, UPDATE, DELETE) and trigger time (BEFORE, AFTER, INSTEAD OF). It is the database manager that fills and passes the parameter information. So you do not have to do anything else than receiving the information.

            Birgitta

            Comment


            • #7
              Re: Problem with File Trigger

              thank you chrisr and birgitta for the quick reply. now i have a clearer understanding on how to code my program.

              one more thing just in case someone from my client ask. I have some files with both journal and trigger program. the database manager will write to the journal first before it calls the trigger program right? just need to know what is the sequence of events between journal and trigger pgms.
              idiot and stoopid with SQL

              Comment


              • #8
                Re: Problem with File Trigger

                The system (i.e. the database) calls you and supplies the parms.

                That said this is not a good model to follow (sorry Jamie) because of the unnecessary overhead of moving the fields. The pointer approach is a much better choice becuase it avoids the data movement.

                There are several articles that explain the principles - but of course I'm going to point you to one of mine http://www.ibmsystemsmag.com/ibmi/de...rigger-Happy-/

                Comment


                • #9
                  Re: Problem with File Trigger

                  Hey no need to be sorry ... that post was from Novermber of 2005.
                  That was a long time ago..
                  All my answers were extracted from the "Big Dummy's Guide to the As400"
                  and I take no responsibility for any of them.

                  www.code400.com

                  Comment


                  • #10
                    Re: Problem with File Trigger

                    Jeepers - didn't notice that - how did _that_ one get resurrected! It showed up for me as being active in the last few days. Weird.

                    Oh well - in the words of the prophet - it's all grist for the archives!

                    Comment


                    • #11
                      Re: Problem with File Trigger

                      Jon,
                      Happens all the time, whoever runs this site should archive some posts
                      All my answers were extracted from the "Big Dummy's Guide to the As400"
                      and I take no responsibility for any of them.

                      www.code400.com

                      Comment


                      • #12
                        Re: Problem with File Trigger

                        @Jamie

                        I suspect this happens when a spammer posts to an old thread, and then an admin deletes the spammy post. By then it is too late and the thread has the new-post indicator (flag or date last post) already set.
                        Regards

                        Kit
                        http://www.ecofitonline.com
                        DeskfIT - ChangefIT - XrefIT
                        ___________________________________
                        There are only 3 kinds of people -
                        Those that can count and those that can't.

                        Comment


                        • #13
                          Re: Problem with File Trigger

                          Jamie does that whenever he's bored, it's kinda cruel really ...
                          Greg Craill: "Life's hard - Get a helmet !!"

                          Comment


                          • #14
                            Re: Problem with File Trigger

                            not as cruel as writing a subfile in CLLE!
                            that would be malevolent!!
                            All my answers were extracted from the "Big Dummy's Guide to the As400"
                            and I take no responsibility for any of them.

                            www.code400.com

                            Comment


                            • #15
                              Re: Problem with File Trigger

                              Originally posted by notRelevantSinceThePostIsSoOld

                              Code:
                                   C     *entry        Plist
                                   C     Buffer        Parm                    Buffer
                                   C     BufLen        Parm                    BufLen
                              I know it's a very old post from an old thread, but I just want to point out that coding factor 1 for the trigger PARM operations is very dangerous. (I realize the IBM documentation for trigger programs used to have an RPG example that coded the PARM operations this way.)

                              Coding factor 1 for the PARM operations causes the parameters to be copied to themselves for the declared length of the parameters. That's no problem for BufLen, but it can cause an error at runtime for the Buffer parameter if the size of the passed parameter is less than the coded size (32767 in this case).

                              If the physical storage segment containing the parameter ends before all 32767 bytes can be copied, it would cause an offset error.

                              Or worse, it could cause corruption in the caller's (database's) storage following the actual buffer parameter, because of not handling pointers correctly.

                              Comment

                              Working...
                              X