• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 821
  • Last Modified:

Trigger

Hi Experts,

I have added an Insert trigger and an Update trigger to a file that will be exported from our UPS system to the trigger file on the AS400.  However, neither trigger works when the file is updated.  Any suggestions?

Thanks,
Kathy
0
fknott
Asked:
fknott
  • 3
  • 2
  • 2
  • +3
4 Solutions
 
ScottParkerCommented:
it would be helpful if you showed the full command you used to add the trigger to the file.
0
 
fknottAuthor Commented:
sorry, I am new to this.  

 ===> ADDPFTRG FILE(TESTKB/SHUPSEX) TRGTIME(*AFTER) TRGEVENT(*INSERT) PGM(TESTKB
/SH604) TRG(SHUPSITRG) TRGLIB(TESTKB) ALWREPCHG(*YES)                          

0
 
_b_hCommented:
Hi, Kathy

The first place to look is in the job log of a job that is inserting a record, which is where the trigger executes. For testing purposes, you could just use DFU to try to add a record.
You may see message CPF502B in your job log, which indicates that the trigger program encountered an error, and there may be a previous message to indicate what happened.

Barry
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
_b_hCommented:
Kathy,
The next step would be to ensure that your call parameters match what the trigger interface expects. What language is the trigger program in?
Here is a sample RPG trigger program from the IBM v5r3 Infocenter, which you find by searching for "Insert trigger written in RPG" at this link:
http://publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp

* Program Name : INSTRG
       * This is an insert trigger for the application
       * file.  The application inserts the following three
       * records into the ATMTRANS file.
       *
       * ATMID   ACCTID   TCODE    AMOUNT
       * --------------------------------
       * 10001   20001      D      100.00
       * 10002   20002      D      250.00
       * 10003   20003      D      500.00
       *
       * When a record is inserted into ATMTRANS, the system calls
       * this program, which updates the ATMS and
       * ACCTS files with the correct deposit or withdrawal amount.
       * The input parameters to this trigger program are:
       *  - TRGBUF : contains trigger information and newly inserted
       *             record image of ATMTRANS.
       *  - TRGBUF Length : length of TRGBUF.
       *
      H        1
       *
       * Open the ATMS file and the ACCTS file.
       *
      FATMS    UF  E                    DISK         KCOMIT
      FACCTS   UF  E                    DISK         KCOMIT
       *
       * DECLARE THE STRUCTURES THAT ARE TO BE PASSED INTO THIS PROGRAM.
       *
      IPARM1       DS
       * Physical file name
      I                                        1  10 FNAME
       * Physical file library
      I                                       11  20 LNAME
       * Member name
      I                                       21  30 MNAME
       * Trigger event
      I                                       31  31 TEVEN
       * Trigger time
      I                                       32  32 TTIME
       * Commit lock level
      I                                       33  33 CMTLCK
       * Reserved
      I                                       34  36 FILL1
       * CCSID
      I                                    B  37  400CCSID
       * Reserved
      I                                       41  48 FILL2
       * Offset to the original record
      I                                    B  49  520OLDOFF
       * length of the original record
      I                                    B  53  560OLDLEN
       * Offset to the original record null byte map
      I                                    B  57  600ONOFF
       * length of the null byte map
      I                                    B  61  640ONLEN
       * Offset to the new record
      I                                    B  65  680NOFF
       * length of the new record
      I                                    B  69  720NEWLEN
       * Offset to the new record null byte map
      I                                    B  73  760NNOFF
       * length of the null byte map
      I                                    B  77  800NNLEN
       * Reserved
      I                                       81  96 RESV3
       * Old record ** not applicable
      I                                       97 112 OREC
       * Null byte map of old record
      I                                      113 116 OOMAP
       * Newly inserted record of ATMTRANS
      I                                      117 132 RECORD
       * Null byte map of new record
      I                                      133 136 NNMAP
      IPARM2       DS
      I                                    B   1   40LENG
       ******************************************************************
       * SET UP THE ENTRY PARAMETER LIST.
       ******************************************************************
      C           *ENTRY    PLIST
      C                     PARM           PARM1
      C                     PARM           PARM2
       ******************************************************************
       * Use NOFF, which is the offset to the new record, to
       * get the location of the new record from the first
       * parameter that was passed into this trigger program.
       *   - Add 1 to the offset NOFF since the offset that was
       *     passed to this program started from zero.
       *   - Substring out the fields to a CHARACTER field and
       *     then move the field to a NUMERIC field if it is
       *     necessary.
       ******************************************************************
      C                     Z-ADDNOFF      O       50
      C                     ADD  1         O
       ******************************************************************
       *         - PULL OUT THE ATM NUMBER.
       ******************************************************************
      C           5         SUBSTPARM1:O   CATM    5
       ******************************************************************
       *         - INCREMENT "O", WHICH IS THE OFFSET IN THE PARAMETER
       *           STRING.  PULL OUT THE ACCOUNT NUMBER.
       ******************************************************************
      C                     ADD  5         O
      C           5         SUBSTPARM1:O   CACC    5
       ******************************************************************
       *         - INCREMENT "O", WHICH IS THE OFFSET IN THE PARAMETER
       *           STRING.  PULL OUT THE TRANSACTION CODE.
       ******************************************************************
      C                     ADD  5         O
      C           1         SUBSTPARM1:O   TCODE   1
       ******************************************************************
       *         - INCREMENT "O", WHICH IS THE OFFSET IN THE PARAMETER
       *           STRING.  PULL OUT THE TRANSACTION AMOUNT.
       ******************************************************************
      C                     ADD  1         O
      C           5         SUBSTPARM1:O   CAMT    5
      C                     MOVELCAMT      TAMT    52
       *************************************************************
       *  PROCESS THE ATM FILE.                     ****************
       *************************************************************
       * READ THE FILE TO FIND THE CORRECT RECORD.
      C           ATMN      DOUEQCATM
      C                     READ ATMS                     61EOF
      C                     END
      C   61                GOTO EOF
       * CHANGE THE VALUE OF THE ATM BALANCE APPROPRIATELY.
      C           TCODE     IFEQ 'D'
      C                     ADD  TAMT      ATMAMT
      C                     ELSE
      C           TCODE     IFEQ 'W'
      C                     SUB  TAMT      ATMAMT
      C                     ELSE
      C                     ENDIF
      C                     ENDIF
       * UPDATE THE ATM FILE.
      C           EOF       TAG
      C                     UPDATATMFILE
      C                     CLOSEATMS
       *************************************************************
       *  PROCESS THE ACCOUNT FILE.                 ****************
       *************************************************************
       * READ THE FILE TO FIND THE CORRECT RECORD.
      C           ACCTN     DOUEQCACC
      C                     READ ACCTS                    62  EOF2
      C                     END
      C   62                GOTO EOF2
       * CHANGE THE VALUE OF THE ACCOUNTS BALANCE APPROPRIATELY.
      C           TCODE     IFEQ 'D'
      C                     ADD  TAMT      BAL
      C                     ELSE
      C           TCODE     IFEQ 'W'
      C                     SUB  TAMT      BAL
      C                     ELSE
      C                     ENDIF
      C                     ENDIF
       * UPDATE THE ACCT FILE.
      C           EOF2      TAG
      C                     UPDATACCFILE
      C                     CLOSEACCTS
       *
      C                     SETON                     LR

Please post back any questions!
Barry
0
 
_b_hCommented:
Kathy,
Sorry for the multiple replies! An easy way to test the triggering itself is use a very simple trigger program like this:
PGM ( &P1 &P2)              
DCL &P1  *CHAR 136          
DCL &P2  *CHAR  40          
SNDPGMMSG 'TRIGGER CALLED'  
ENDPGM                      

You will see the TRIGGER CALLED message in your joblog.

An additional programming note: make sure you use the offset values provided in the parameters to find the actual data. If the format of the trigger parameter changes, the offset will point the new starting location of the data; therwise your trigger program may stop working after a release upgrade!

Barry
0
 
tliottaCommented:
Kathy:

What version of OS/400? (Just for background.)

Obvious question is how do you know that the trigger doesn't run? (As opposed to runs but transactions are rolled back or some other action makes it _seem_ that it didn't run.)

Tom
0
 
daveslaterCommented:
Hi Kathy
there are several trigger events

*insert
*Update
*delete
*read

as you have only specified *Insert the trigger will only be activated when records are added

for update use
===> ADDPFTRG FILE(TESTKB/SHUPSEX) TRGTIME(*AFTER) TRGEVENT(*UPDATE) PGM(TESTKB
/SH604) TRG(SHUPSITRG) TRGLIB(TESTKB) ALWREPCHG(*YES)                          


Have Fun
dave

PS

*DELETE is dangerous as you can not use CLRPFM


0
 
daveslaterCommented:
pps
you have to add the trigger for each event ie
*insert will only affect new records
*update will only affect updated records
*delete will only affect deleted records

etc.....

there is not a global option.
0
 
tliottaCommented:
Minor note... the SQL CREATE TRIGGER statement can be used for more granularity on UPDATE triggers. You can tie the trigger to updates of particular columns. (I don't have version/release info handy, but works on current releases.)

Tom
0
 
Dave FordSoftware Developer / Database AdministratorCommented:
Having coded triggers in at least three different AS/400 languages, I firmly believe they're MUCH easier in SQL.  Rather than having to code the trigger-buffer manually, it's all "built-in" to SQL.

e.g.

create trigger SQLtrigger3              
  after update of anotherField on MyTable
  referencing new row as n                    
                   old row as o                    
  for each row                                
begin                                        
  insert into gotit values('autotest');
end;

HTH,
DaveSlash
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

  • 3
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now