|
Here is a sample trigger program and a couple of it's /COPY's: TRIGGERS - main program TRIGGERD - /COPYed D specifications TRIGGERC - /COPYed calculation specs common to all trigger programs Code is a little old, so cut me some slack. You will be missing the following copies HSPEC SRVPGMCPY I can send those later. SRVPGMCPY tends to make one rather /COPY phobic. So I might only send you a subset, but it should have all you need. Don't want to scare you off. TRIGGERS /DEFINE HSpec /INCLUDE ROUTINES/QRPGLESRC,HSPEC /UNDEFINE HSpec *************************************************************************** * Program: This is a sample trigger program just to show you the basic * * constructs. * * This one will show you the following things a trigger can do: * * 1) Log access to another file. * * 2) Lock a person out of a particular record * * 3) Change the record buffer to record who updated this record last and * * when. * * * * Replace occurances of TESTFILE with the name of the file you want to * * trigger off of. * * * * Modification log: * * 02/20/02 by R.Berendt, CCP Group Dekko Services, LLC * * Created. * * * * Compilation instructions: * * (no special instructions this time.) * * * *************************************************************************** F* Log of changes to file. FTESTLOGF O E DISK BLOCK(*NO) /DEFINE DSpec /COPY ROUTINES/QRPGLESRC,SRVPGMCPY /COPY ROUTINES/QRPGLESRC,TRIGGERD /UNDEFINE DSpec D NEW E DS EXTNAME(TESTFILE) QUALIFIED /COPY ROUTINES/QRPGLESRC,TRIGGERC /free // If the person is not ROB, then don't let them read the record with the key 'yabba'. // This could be easily changed to record types, groups of users, etc. if user<>'ROB'; select; when (focDesc='INSERT' and new.reckey='yabba') or (focDesc<>'INSERT' and old.reckey='yabba'); exsr fail; return; endsl; endif; // Write out changes to log file. if focDesc='INSERT' or focDesc='UPDATE'; lreckey=new.reckey; laction=event; lusername=User; ltimestamp=%timestamp(); write testlog; endif; // On writes or updates, change the record buffer to record user and time // of change. if focDesc='INSERT' or focDesc='UPDATE'; new.username=User; new.dattim=%timestamp(); %subst(datastuff:nwoff-80+1:nwrcl)=new; endif; return; // end of mainline code begsr fail; // This user may not access this record. Abort the trigger. CALLP QMHSNDPM('CPF9898': 'QCPFMSG QSYS ': 'Not authorized to record': 300: '*ESCAPE': '*': 1: MsgKey: errc0100); endsr; /end-free TRIGGERD D ORNOFF 57 60I 0 D* Length of the original byte map. Should equal # of fields D ORNLEN 61 64I 0 D* Offset to the new record D NWOFF 65 68I 0 D* Length of the new record D NWRCL 69 72I 0 D* Offset to the new record null byte map D NWNOFF 73 76I 0 D* Length of the new record null byte map D NWNLEN 77 80I 0 D* Reserved data and record data. D DATASTUFF 81 32000a D TRIGLENDS DS D* Trigger buffer length D TRIGLEN 10I 0 D* External description of file being updated. Must specify file in your program! D* Therefore, copy the following line into your program, remove the comment, and D* specify the correct file name. D*NEW E DS EXTNAME(filename) QUALIFIED D OLD DS LIKEDS(NEW) D AryElem s 10i 0 D FailedTrigger s n inz(*off) TRIGGERC C *ENTRY PLIST C PARM TRIG C PARM TRIGLENDS /free FailedTrigger=*off; AryElem=%lookup(lcklvl:clcCode); if AryElem>0; clcDesc=clcDescA(Aryelem); else; clcDesc=*blanks; endif; AryElem=%lookup(event:focCode); if AryElem>0; focDesc=focDescA(Aryelem); else; focDesc=*blanks; endif; AryElem=%lookup(trtime:tmcCode); if AryElem>0; tmcDesc=tmcDescA(Aryelem); else; tmcDesc=*blanks; endif; // If this is a delete, change or read then move the old record into the // old record data structure. if focDesc='DELETE' or focDesc='UPDATE' or focDesc='READ'; old=%subst(datastuff:oroff-80+1:orrcl); endif; // If this is a add or change then move the new record into the new // record data structure if focDesc='INSERT' or focDesc='UPDATE'; new=%subst(datastuff:nwoff-80+1:nwrcl); endif; /end-free Rob Berendt -- Group Dekko Services, LLC Dept 01.073 PO Box 2000 Dock 108 6928N 400E Kendallville, IN 46755 http://www.dekko.com "Smith, Mike" <Mike_Smith@xxxxxxxxxxxxxxxx> Sent by: rpg400-l-bounces@xxxxxxxxxxxx 10/26/2004 03:01 PM Please respond to RPG programming on the AS400 / iSeries <rpg400-l@xxxxxxxxxxxx> To <rpg400-l@xxxxxxxxxxxx> cc Fax to Subject Trigger? We have a situation were I need to add a trigger to a file to circumvent a problem. We should really fix the bug, but this is a temporary situation so it has been decided to try this approach . We have a file that in certain situations is updating a field, as a result of this field being populated, other processes are being affected. So the idea was when a record is inserted to have the trigger program CLEAR this field. I have tried to accomplish this directly in the trigger program(both before and after the insert) and also, in a called program from the trigger. Does this sound like I can accomplish this in the trigger. If so, what should the approach be. Michael Smith iSeries.mySeries. -- This is the RPG programming on the AS400 / iSeries (RPG400-L) mailing list To post a message email: RPG400-L@xxxxxxxxxxxx To subscribe, unsubscribe, or change list options, visit: http://lists.midrange.com/mailman/listinfo/rpg400-l or email: RPG400-L-request@xxxxxxxxxxxx Before posting, please take a moment to review the archives at http://archive.midrange.com/rpg400-l.
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2025 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.