Show TOC

Procedure documentationValidating Logical File Names Entered By the User Locate this document in the navigation structure

Procedure

  1. Define a constant to use for the logical file name.

    For each parameter for a physical file name in your application create one global constant of type FILEINTERN (logical file name). Set the value of this constant to the logical file name used for file name validation in this context.

    Syntax Syntax

    1. CONSTANTS gc_fname TYPE fileintern VALUE 'EXAMPLE_FIN1'.
    End of the code.
  2. During PBO, call the function module FILE_LOGFILE_ALIAS_PBO.

    This function module performs the following:

    • It checks whether validation for your logical file name is active.

    • If validation is active, it attempts to change the parameter on your screen to a list box. It fills the list box with the logical file names allowed in this context.

      Validation for a logical file name is defined as active if the system administrator has set up the logical file name whitelist to use for validation or maintained aliases.

      Syntax Syntax

      1. AT SELECTION-SCREEN OUTPUT.
          CALL FUNCTION 'FILE_LOGFILE_ALIAS_PBO'
            EXPORTING
              ed_logfile_appl   = gc_logfile
              ed_parameter_name = 'PA_FILE'
            CHANGING
              cd_logfile_param  = pa_file.
      End of the code.

      Note Note

      Some programs provide an input field that can be used for either a physical file name or a logical file name. In such a scenario, you may have to adjust the call above depending on the context. For example, if you do not provide a field name in ed_parameter_name, the field is not converted to a list box. If cd_logfile_param is empty, the function module sets the hard-coded logical file name as a default.

      End of the note.
  3. During PAI, call the function module FILE_LOGFILE_ALIAS_PAI.

    This function module performs the following:

    • It checks whether validation for your logical file name is active.

    • If validation is active, and if the user specified or selected a logical file name, the function module checks whether the specified logical file name is allowed in this context. If it is not allowed, it raises the exception EXC_INVALID_FILENAME with a corresponding error message. You must handle this error message according to the context of your application. In most cases, you can use the error message as is.

      This call is necessary when loading a variant. In this case, the UI automatically adds an entry to the list box containing the value from the variant. Therefore, even when using a list box, it is possible that the user selects a logical file name which has not been defined as a valid alias.

      Syntax Syntax

      1. AT SELECTION-SCREEN.
          CALL FUNCTION 'FILE_LOGFILE_ALIAS_PAI'
            EXPORTING
              ed_logfile_appl            = gc_logfile
            CHANGING
              cd_logfile                 = pa_file
            EXCEPTIONS
              OTHERS                     = 1.
          IF sy-subrc <> 0.
            MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
              WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
          ENDIF.
      End of the code.

      Note Note

      If the parameter pa_file can be used for either physical and logical file names you, only have to call this function module if the specified file name is a logical file name. The recommended procedure is to call the function module FILE_GET_NAME first.

      If the result from FILE_GET_NAME is that the content of pa_file is a logical file name, call the function module FILE_LOGFILE_ALIAS_PAI as illustrated above.

      If the result from FILE_GET_NAME is that the content of pa_file is not a logical file name, call the function module FILE_VALIDATE_NAME and provide the hard-coded logical file name in the EXPORT parameter logical_filename and the content of pa_file for the CHANGING parameter physical_filename.

      End of the note.
  4. Add a call to function module FILE_LOGFILE_ALIAS_F4.

    Use this call to in case the user tries to use the value help for entering a logical file name. This function module performs the following:

    • If the validation for your logical file name is active, only the logical file names allowed in this context are available with the value help. The user cannot select a logical file name that he or she is not allowed to use when executing the program.

    • If the validation for your logical file name is not active, the function module will start a dialog which allows the user to select any logical file name defined in the system.

      Syntax Syntax

      1. AT SELECTION-SCREEN ON VALUE-REQUEST FOR pa_file.
          CALL FUNCTION 'FILE_LOGFILE_ALIAS_F4'
            EXPORTING
              ed_logfile_appl   = gc_logfile
              ed_parameter_name = 'PA_FILE'
            CHANGING
              cd_logical_file   = pa_file.
      End of the code.

      Note Note

      If the parameter pa_file can be used for either physical and logical file names, you need to decide whether you want to provide value help for a logical file name (in that case use FILE_LOGFILE_ALIAS_F4) or a physical file name (for example, by using cl_gui_frontend_services=>file_open_dialog).

      End of the note.
  5. Validate the file name.

    Validate the physical file name directly before the OPEN DATASET or related statement.

    Syntax Syntax

    1. CALL FUNCTION 'FILE_GET_NAME'
          EXPORTING
            logical_filename  = pa_file
      * any parameters defined for that logical file name
          CHANGING
            physical_filename = ld_physfile.
       
      CALL FUNCTION 'FILE_VALIDATE_NAME'
          EXPORTING
            logical_filename  = gc_fname
      * any parameters defined for that logical file name
          CHANGING
            physical_filename = ld_physfile
          EXCEPTIONS
            OTHERS            = 1.
       
        IF sy-subrc <> 0.
      * Implement suitable error handling here, for example,
      *    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
      *      WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
        ENDIF.
       
        OPEN DATASET ld_physfile
           FOR INPUT IN TEXT MODE ENCODING DEFAULT.
        IF sy-subrc <> 0.
      * ...
        ENDIF.
    End of the code.