7.19.2 DEF_COND Examples
Example 1: Define the condition "A is less than B multiplied by C" so that it can be used repeatedly in other commands:
DEF_COND NAME(*ALTBC) COND('#A *LT (#B * #C)')
IF COND(*ALTBC)
ENDIF
DOWHILE COND(*ALTBC)
ENDWHILE
DOUNTIL COND(*ALTBC)
ENDUNTIL
BEGINCHECK
CONDCHECK FIELD(#A) COND(*ALTBC) MSGTXT('A must be not be less than B times C')
ENDCHECK
Example 2: Use the DEF_COND command so that the CHANGE and DELETE function keys in a DISPLAY command are only enabled when the user's name is FRED or MARY or BILL:
DEF_COND NAME(*AUTHORISE) COND('(#USER = FRED) *OR (#USER = MARY) *OR (#USER = BILL)')
DISPLAY FIELDS(......) CHANGE_KEY(*YES *NEXT *AUTHORISE)
DELETE_KEY(*YES *NEXT *AUTHORISE)
Example 3: Use the CHECK_AUTHORITY Built-In Function to generalise the previous example into an enquire and maintain program using a product master file called PRODMST:
DEF_COND NAME(*ALLOWCHG) COND('#CHANGE = Y')
DEF_COND NAME(*ALLOWDLT) COND('#DELETE = Y')
USE BUILTIN(CHECK_AUTHORITY) WITH_ARGS(PRODMST '''*LIBL''' 'FD' 'CH') TO_GET(#CHANGE)
USE BUILTIN(CHECK_AUTHORITY) WITH_ARGS(PRODMST '''*LIBL''' 'FD' 'DL') TO_GET(#DELETE)
REQUEST FIELDS(.....)
FETCH FIELDS(......) FROM_FILE(PRODMST) WITH_KEY(......)
DISPLAY FIELDS(......) CHANGE_KEY(*YES *NEXT *ALLOWCHG) DELETE_KEY(*YES *NEXT *ALLOWDLT)
IF_MODE IS(*CHANGE)
UPDATE FIELDS(........) IN_FILE(PRODMST)
ENDIF
IF_MODE IS(*DELETE)
DELETE FROM_FILE(PRODMST)
ENDIF
This is an effective way to safely and simply combine an enquiry program and maintenance program into one program. The CHANGE and DELETE function keys will only ever be enabled when the user is authorised to change or delete information, so no further program checking is required.
Example 4: Prevent field #SALARY from appearing on a screen panel unless the department number is 464:
DEF_COND NAME(*HEADOFF) COND('#DEPTMENT = ''464''')
DISPLAY FIELDS(#A #B #C (#SALARY *HEADOFF) #E #F #G)
Example 5: Simplify complex conditions by breaking into smaller parts:
DEF_COND NAME(*SELCUST) COND('((#CUSTTYPE = ''A'') *OR (#CUSTTYPE = ''B'') *OR (#CUSTTYPE = ''C'')) *AND ((#BALANCE *GT 10000) *AND (#BALANCE *LT 100000)) *AND (#LASTSALE > *MONTHSTART)')
The previous condition can be made more maintainable and understandable by:
DEF_COND NAME(*SELTYPE) COND('(#CUSTTYPE = ''A'') *OR (#CUSTTYPE = ''B'') *OR (#CUSTTYPE = ''C'')')
DEF_COND NAME(*SELBAL) COND('(#BALANCE *GT 10000) *AND (#BALANCE *LT 100000)')
DEF_COND NAME(*SELSALE) COND('#LASTSALE > #MONTHSTRT')
DEF_COND NAME(*SELCUST) COND('*SELTYPE *AND *SELBAL *AND *SELSALE')
Conditions can be used individually, or as part of a larger expression:
IF COND(*SELCUST)
DOWHILE COND('*SELCUST *OR (#ACTIVE *EQ ''Y'')')