Use a physical file trigger to alter data before insert

When IBM processes a physical file trigger on the IBM i they keep track of 'before' and 'after' images of the data. It is possible to take data being inserted into a physical file and create a 'before insert' trigger and run a trigger program to alter that data.

For example, one system has a plant code of two characters and the second system has a plant code with four alpha characters. If these two systems interface we can guarantee the format of the plant code being inserted on the receiving system. The trigger program will find the plant code out in the before data and format the value to match the receiving system. For example, a plant code originating as '01' is concatenated with '00' to give value '0001'. Thus the after image is altered and that is what is actually being inserted in the physical file.

The physical file receiving the interface data needs a trigger created on the i5. The trigger should call the trigger program in the correct library. The trigger should be set up as insert, before, and allow repeated change as Yes.

Example of displaying trigger information using command 'dspfd testlib/locmast'.

Trigger Description
Trigger name . . . . . . . . . . . . . . . : PLANT_INSERT_TRG
Trigger library  . . . . . . . . . . . . . : CUSTLIB
Trigger state  . . . . . . . . . . . . . . : STATE *ENABLED 
Trigger status   . . . . . . . . . . . . . : *OPERATIVE 
Trigger event  . . . . . . . . . . . . . . : TRGEVENT *INSERT 
Trigger time   . . . . . . . . . . . . . . : TRGTIME *BEFORE 
Allow repeated change  . . . . . . . . . . : ALWREPCHG *YES 
Program Name   . . . . . . . . . . . . . . : PGM PLTCOD_TRG 
Library    . . . . . . . . . . . . . . . . : CUSTLIB 
Program is threadsafe  . . . . . . . . . . : THDSAFE *UNKNOWN 
Multithreaded job action   . . . . . . . . : MLTTHDACN *SYSVAL 
Trigger type   . . . . . . . . . . . . . . : *SYS 
Trigger orientation  . . . . . . . . . . . : *ROW 

  If the 'allow repeated change' parameter is not set to "*YES" the trigger can have record and file locking issues. Listing of all the important code from PLTCOD_TRG:  

  FMT *   *. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+
0025.00  * FILES                                                            
0026.00  *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
0027.00  *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
0028.00  * PROGRAM DATA STRUCTURE                                           
0029.00  *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
0030.00 D                SDS                                                
0031.00 D  STDPGM           *PROC                                           
0032.00 D  STSTUS           *STATUS                                         
0033.00 D  STDMSG                91    170                                  
0034.00 D  STDJOB               244    253                                  
0035.00 D  STDUSR               254    263                                  
0036.00 D  STJOB#               264    269  0                               
0037.00  *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
0038.00  * WO Data Structure                                                
0039.00  *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
0040.00 D INPWO         E DS                  EXTNAME(WOPCODE)              
0041.00  *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
0042.00  * MISCELLANEOUS FIELDS                                            
0043.00  *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
0044.00 D PRMNEW          DS          5000    OCCURS(1)                    
0045.00 D PARM1           DS          5000    OCCURS(1)                    
0046.00  *                                                                 
0047.00 D TRIG            DS                                               
0048.00  * Offset to the new record                                        
0049.00 D  NOFF                  65     68B 0                              
0050.00  *                                                                 
0051.00  * length of the new record                                        
0052.00 D PARM2           DS                                               
0053.00 D  LENG                   1      4B 0                              
0054.00  *                                                                 
0055.00 D TASK            S              4    INZ                          
0056.00  *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
0057.00  * PARAMETER LISTS                                                 
0058.00  *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
0059.00 C     *ENTRY        PLIST                                        
0060.00 C                   PARM                    PARM1                
0061.00 C                   PARM                    PARM2                
0062.00  * SEND PARAMETER TO INFO DS                                     
0063.00 C                   MOVEL     PARM1         TRIG                 
0064.00  * CALCULATE OFFSET FOR NEW RECORD                               
0065.00 C                   Z-ADD     NOFF          O                 5 0
0066.00 C                   ADD       1             O                    
0067.00  *                                                               
0068.00 C                   EVAL      TASK = *BLANKS                     
0069.00 C                   IF        %subst(PARM1:232:2) <> *blanks and 
0070.00 C                             %subst(PARM1:234:2) = *blanks      
0071.00 C                   eval      task = '00' + %subst(PARM1:232:2)  
0072.00 C                   eval      %subst(PARM1:232:4) = task         
0073.00 C                   ENDIF                                        
0074.00  * SEPARATE TASK number and concat from two to four characters   
0075.00  * Note: 95 is the record length of WOPCODE                      
0076.00 C     95            SUBST     PARM1:O       PRMNEW                
0077.00 C                   EVAL      TASK = *BLANKS                      
0078.00 C                   IF        %subst(PRMNEW:24:2) <> *blanks and  
0079.00 C                             %subst(PRMNEW:26:2) = *blanks       
0080.00 C                   eval      task = '00' + %subst(PRMNEW:24:2)   
0081.00 C                   eval      %subst(PARM1:24:4) = task           
0082.00 C                   ENDIF                                         
0083.00  *                                                                
0084.00 C                   MOVEL     PRMNEW        INPWO                 
0085.00  *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
0086.00  * Exit program                                                   
0087.00  *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
0088.00 C                   EVAL      *INLR  = *ON                        
0089.00 C                   RETURN                                        
0090.00 **                                  


Testing the code is simple and straight-forward. Simply use a file editor to insert a record after ensuring the physical has the proper trigger information. Create the record with a plant code of '01'. After the record is inserted check the physical and the plant field should be '0001'.