Functions
A function lets you access data from a file and use that data to access records in another file. On some forms, you cannot directly access a record using only the fields in the form. You might need data from a file that is related to one of the fields in the form.
The following entries make up a function definition. The number of DEFINE FUNCTION statements in a form rules file is not limited.
Use | To |
---|---|
DEFINE FUNCTION |
Begin a function definition. Put the function name to the right of the words DEFINE FUNCTION. The function name is required. It must be a single word and end with left and right parentheses. |
FILENAME | Name the file that this function accesses data in. |
INDEX | Name the index used to access this data. |
KEYRNG |
Name the key fields or values used to access this data. Separate entries with a comma. The number of entries next to KEYRNG can be equal to or less than the number of key fields in the index. If the number of entries is less than the number of key fields, this rule searches for the first record for that key fields match the fields entered here. The order of the entries next to KEYRNG must follow the order of the index fields. The following are valid entries for KEYRNG:
|
RETURNS | Name the 4GL field from the primary file (FILENAME) whose value the function retrieves. A function definition can retrieve only one field. |
Example for Function Definition
Assume that the key fields in a form are Company and Customer. If the value in the Company field is 1, using the Next action should access the first customer in company 1. However, the key fields for the Customer file are CUST-GROUP and GROUP-CUSTOMER, yet CUST-GROUP is not a field in this form. Because CUST-GROUP is in the Company file, you can define a function to retrieve the CUST-GROUP from the Company file. Then you can use the value that the function retrieves to find the next customer for company 1.
In any form where the Company field has a key number of 01 assigned, the Company Chart function (CPY-CHART()) returns the Chart Name (CHART-NAME) to GL10.1 from the GLSYSTEM file when the company number is entered on the form. The Chart Name field is retrieved using the index GLSSET1.
DEFINE FUNCTION CPY-CHART()
FILENAME GLSYSTEM
INDEX GLSSET1
KEYRNG COMPANY-01
RETURNS CHART-NAME
You can use this function to retrieve the Chart Name for a company in other kinds of DEFINE statements. For example, the DEFINE SCRDTL shown below uses the Chart Name as the first key in identifying the Multiple Account (MULTI-ACCOUNT-04M) form field value.
DEFINE SCRDTL "Detail Account"
ID IF-GLM-D-0009
SCRFLD MULTI-ACCOUNT-04M
FILENAME GLCHARTDTL
INDEX GDTSET2
KEYRNG CPY-CHART(),CHART-ACCOUNT-04C,\
CHART-SUB-ACCOUNT-05C