Creating Portable Programmed Virtual Fields

Date:Archived
Product/Release:LANSA - All Platforms
Abstract:Virtual fields in RDML can be created that are then portable to various platforms
Submitted By:LANSA Technical Support

Sometimes virtual fields need to be created but the standard virtual field conversions (substring, concatenation, mathematical calculations and date conversions) are not enough.

RPG or C can be used to program these fields. Alternatively, they can be created using RDML, giving the advantage of being portable between platforms.

Creating virtual fields in RDML is done by writing a trigger function and using it AFTER READ.

For example, the following trigger calculates the number of days a product has been owned:

File     : XXXXXX xyzxyzxyzxyz
Field    : LCCNOD Number of Days Owned

Order to process / source . . . . . 10 / FILE DEFINITION
User description of trigger . . . . Calculate the no. days owned
Trigger Function Name . . . . . . . VIRT001
                                            Before     After
Trigger Point(s) . . . . . . . . . . Open
                                     Close
                                     Read                Y
                                     Insert
                                     Update
                                     Delete
                    And/Or Field     Op     Field/Literal
Trigger When . . . . . LCCNOD        REF

Note: To improve performance, use REF in the "Trigger When" on the virtual field. This will call the trigger function only when the user function has requested LCCNOD instead of all the time.

The following function calculates the number of days between the purchase date and today and puts it in the virtual field (#LCCNOD).

/*================================ */      
/* Copyright .....: (C)      Aspect Computing       , 1997 */      
/* Type ..........: XXXXXX File Level Trigger              */      
/* Description ...: Trigger to calculate LCCNOD field      */      
/*================================ */      
FUNCTION  OPTIONS(*DIRECT *NOMESSAGES) RCV_LIST(#TRIG_LIST) 
	  TRIGGER(*FILE XXXXXX)                
DEF_LIST   NAME(#TRIG_LIST) TYPE(*WORKING) ENTRYS(2)               
CHANGE     FIELD(#TRIG_RETC) TO('OK')                              
/*                                                         */      
CASE       OF_FIELD(#TRIG_OPER)                                    
/* Handle an after read event                              */      
WHEN       VALUE_IS('= AFTRED')                                    
GET_ENTRY  NUMBER(1) FROM_LIST(#TRIG_LIST)                         
USE        BUILTIN(DATEDIFFERENCE) WITH_ARGS(#PURDATE D #TODAY D)
	   TO_GET(#LCCNOD)                                         
UPD_ENTRY  IN_LIST(#TRIG_LIST)                                     
/* Handle an event not catered for                         */      
OTHERWISE                                                          
ABORT      MSGTXT('File XXXXXX trigger function invalidly invoked/used.')
ENDCASE                                                            
/* Return control to the invoker                           */      
RETURN