ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Update File in RPG Trigger Program

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

  • Update File in RPG Trigger Program

    Hello,

    I am trying to get a trigger program to update a field in a file with the current timestamp to show when that record was last updated. I have the trigger set to this program whenever an update operation is performed on the file itself. In this trigger program, I am setting the timestamp field of the file in the after image to the current timestamp. Then I am updating the after image of the trigger buffer. I run this through debug and everything looks good but after the program runs, there is no change to the timestamp field in the file. Can anybody help me get this to work? Or is this even possible? My trigger program is below.

    Thank you!

    Code:
     
    d PGMNAME         pr
     d  TrgBuffer_                         like(TrgBuffer)
     d  TrgBufLen_                         like(TrgBufLen)
    
     d BefImg        e ds                  extname(FILENAME) prefix(b_)
     d AftImg        e ds                  extname(FILENAME) prefix(a_)
    
     d FILENAMER       pi
     d  p$TrgBuffer                        like(TrgBuffer)
     d  p$TrgBufLen                        like(TrgBufLen)
    
        TrgBuffer = p$TrgBuffer;
        TrgBufLen = p$TrgBufLen;
    
        NRO = NewRecOff + 1;
        NRL = NewRecLen;
        ORO = OrgRecOff + 1;
        ORL = OrgRecLen;
    
        // Set Before / After Images
        BefImg = %subst(TrgBuffer:ORO:ORL);
        AftImg = %subst(TrgBuffer:NRO:NRL);
    
        select;
    
        // Update
         when TrgEvent = '3';
    
         // Set last updated timestamp on record.
         a_lstupd = %timestamp();
         %subst(TrgBuffer:NRO:NRL) = AftImg;
         p$TrgBuffer = TrgBuffer;

  • #2
    I haven't messed with rpg triggers in a while, but this is how I do this with sql in case you want to try it.


    Code:
                                                                                                                                                                                                                                        
    CREATE TRIGGER im_shipment_insert_audit_info_tr                                                                                                                                                                                     
        NO CASCADE BEFORE INSERT ON im_shipment                                                                                                                                                                                            
        REFERENCING  NEW AS new                                                                                                                                                                                                            
        FOR EACH ROW MODE DB2SQL                                                                                                                                                                                                           
    begin                                                                                                                                                                                                                               
    set new.create_user = session_user;                                                                                                                                                                                                 
    set new.create_datetime = current_timestamp;                                                                                                                                                                                        
    end;
    Walt

    Comment


    • #3
      Greetings Jallen!

      One of the thoughts that occurred to me is the question, "is Commitment Control" used on the file that you are expecting to see changes, but you're not seeing those changes actually happening? If the file is under this kind of control, you will not see the changes you expect to see until a "COMMIT" instruction is processed against that file. The "COMMIT" command is what actually updates your file, not necessarily your timestamp program. Until that COMMIT command happens, the actual file is NOT changed, and you could see the results you describe.

      This would be the Before record image you find in your file journal, not the After image...

      Beings as Commitment Control can be a bit gnarly, I'm not going to go there during this answer... for more info on that, you can check out;




      Best Regards and Good Luck!

      Fred Williams

      Comment


      • #4
        You need a BEFORE UPDATE Trigger, that will update the timestamp immediately before the trigger is written.
        Additionally you need to set the Allow Repeated Change (ALWREPCHG) Option in the ADDPFTRG Command to *YES.

        In an AFTER TRIGGER the record is already written and cannot be changed any more.

        BTW in an SQL Defined Table you can define a timestamp column with the ROW CHANGE TIMESTAMP Attribute. After this timestamp column is automatically updated whenever the row is modified.

        Birgitta
        Last edited by B.Hauser; June 7, 2018, 11:31 PM.

        Comment


        • #5
          Another thought or two.

          If the field is null capable you will also need to set the associated null map entry.

          Is the file being blocked? If it is you will not see the update immediately.

          Comment

          Working...
          X