%CodeTPLValidationCode #GROUP

Top  Previous  Next

This #GROUP is the "workhorse" of the Code template. It generates the actual file lookup code to validate the data entry. It takes the information provided in the prompts and combines it with the %ControlUse symbol to generate a GET statement into the lookup file. If the GET is successful, the data is valid. If not, it calls the lookup procedure.

#GROUP(%CodeTPLValidationCode)

%LookupField = %ControlUse                        #<! Move value for lookup

#FIND(%Field,%LookupField)                         #! FIX field for lookup

GET(%File,%LookupKey)                             #<! Get value from file

IF ERRORCODE()                                    #<! IF record not found

  GlobalRequest = SelectRecord                    #<! Set Action for Lookup

  %LookupProcedure                                #<! Call Lookup Procedure

  LocalResponse = GlobalResponse                  #<! Save Returned Action

  GlobalResponse = RequestCancelled               #<! Clear the Action Value

  IF LocalResponse = RequestCompleted             #<! IF Lookup successful

    %ControlUse = %LookupField                    #<! Move value to control field

#IF(%ControlEvent='Accepted')                      #! IF a Post-Edit Validation

  ELSE                                            #<! ELSE (IF Lookup NOT...)

    SELECT(%Control)                              #<! Select the control

    CYCLE                                         #<! Go back to ACCEPT

#ENDIF                                             #! END (IF a Pre-Edit...)

  END                                             #<! END (IF Lookup successful)

#IF(%ControlEvent='Selected')                      #! IF a Pre-Edit Validation

  SELECT(%Control)                                #<! Select the control

#ENDIF                                             #! END (IF a Pre-Edit...)

END                                               #<! END (IF record not found)

 

This #GROUP starts by generating the %LookupField = %ControlUse assignment. This assigns the control's USE variable to the field named in the second prompt; the key field that should contain the correct value.

The #FIND(%Field,%LookupField) statement looks through all the fields in the data dictionary, looking for a matching field to the one contained in %LookupField. This fixes %Field and %File to the correct values to generate the GET(%File,%LookupKey) statement. This becomes a GET(file,key) form of the GET statement to get a single record from the lookup file with matching key field values.

The IF ERRORCODE() structure checks for a successful GET operation. If an error occurred, the GET was unsuccessful and the GlobalRequest = SelectRecord statement sets up the call to the lookup procedure, generated by the %LookupProcedure statement.

After return from the lookup procedure, LocalResponse = GlobalResponse saves the lookup procedure's response code. Then the GlobalResponse = RequestCancelled statement cleans up so any other execution thread does not get an incorrect response. This must be done immediately, before the user has a chance to change execution threads.

The IF LocalResponse = RequestCompleted structure detects a user choice from the lookup procedure and the %ControlUse = %LookupField statement assigns the choice to the control's USE variable.

The #IF(%ControlEvent='Accepted') detects when the Code template is generating for EVENT:Accepted and adds the ELSE clause to SELECT the control and CYCLE back to the top of the ACCEPT loop.

The END statement terminates the IF LocalResponse = RequestCompleted structure. The #IF(%ControlEvent='Selected') structure generates the SELECT statement for the control when generating for EVENT:Selected.

The END statement terminates the IF ERRORCODE() structure. Obviously, if there was no error on the GET statement, the data is valid and no further code is necessary.