Showing posts with label Dialog Programming. Show all posts
Showing posts with label Dialog Programming. Show all posts

May 28, 2026

F4 Help to Get File from AL11 in Selection Screen

To provide F4 value help for files on the application server (AL11), the most effective way is to use specific SAP function modules within your ABAP code. Since standard search help doesn't automatically browse application server directories, you must implement the logic in the AT SELECTION-SCREEN ON VALUE-REQUEST event.

Recommended Function Modules
For browsing and selecting files directly from the application server, you can use these common modules:
  • /SAPDMC/LSM_F4_SERVER_FILE: This is widely used to open a pop-up that allows users to navigate the SAP AL11 directories and select a file.
  • F4_DXFILENAME_TOPRECURSION: Another standard function module specifically designed for search help on application server files.
Implementation Example
To add this help to a selection screen parameter (e.g., p_file), use the following logic in your report:

abap
PARAMETERS: p_file TYPE string.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  CALL FUNCTION '/SAPDMC/LSM_F4_SERVER_FILE'
    IMPORTING
      serverfile       = p_file
    EXCEPTIONS
      canceled_by_user = 1
      OTHERS           = 2.

Key Considerations
  • Permissions: Ensure the user has the necessary authorizations (e.g., S_DATASET) to access the specific AL11 directory paths.
  • Directory-only Help: If you only need to select a directory (not a specific file), you can call the program RSWATCH0 or use F4IF_INT_TABLE_VALUE_REQUEST after fetching directory lists via EPS_GET_DIRECTORY_LISTING.
  • Presentation Server Difference: For files on your local PC (Presentation Server), use CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG instead

Sample Code of Using CL_GU_FRONTEND_SERVICES=>FILE_OPEN_DIALOG

The CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG method is the standard object-oriented way in ABAP to trigger a file selection popup on a user's local machine. It replaces older function modules like WS_FILENAME_GET. 

Basic Sample Code
This example demonstrates how to call the dialog and retrieve the selected file path to populate a selection screen parameter


abap
REPORT z_file_dialog_demo.

DATA: lt_file_table TYPE filetable, " Table to store selected files
      lv_rc         TYPE i.         " Return code

PARAMETERS: p_file TYPE localfile. " Selection screen parameter

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

  " Call the File Open Dialog
  CALL METHOD cl_gui_frontend_services=>file_open_dialog
    EXPORTING
      window_title            = 'Select your Excel File'
      default_extension       = '*.xlsx'
      file_filter             = 'Excel Files (*.XLSX)|*.XLSX|All Files (*.*)|*.*'
      multiselection          = abap_false " Set to 'X' to allow multiple files
    CHANGING
      file_table              = lt_file_table
      rc                      = lv_rc
    EXCEPTIONS
      file_open_dialog_failed = 1
      cntl_error              = 2
      error_no_gui            = 3
      not_supported_by_gui    = 4
      OTHERS                  = 5.

  IF sy-subrc = 0 AND lv_rc > 0.
    " Retrieve the first selected file path
    READ TABLE lt_file_table INTO DATA(ls_file) INDEX 1.
    IF sy-subrc = 0.
      p_file = ls_file-filename.
    ENDIF.
  ENDIF.


Key Parameters Explained
  • window_title: The text that appears at the top of the popup window.
  • default_extension: Sets the initial file type the dialog looks for (e.g., .txt or .xlsx).
  • file_filter: Controls the "Files of type" dropdown. Use the format Description (*.ext)|*.ext.
  • multiselection: Set to abap_true ('X') if you want the user to be able to select multiple files at once.
  • file_table: A table of type FILETABLE that will hold the paths of the selected files.
  • rc: A return code that typically indicates the number of files selected by the user. 
Common Use Cases
  • F4 Help: Often used within the AT SELECTION-SCREEN ON VALUE-REQUEST event to provide a browse button for a file path input field.
  • Multi-File Selection: Unlike some older function modules, this method natively supports selecting several files at once for batch processing.
  • File Uploading: Typically followed by a call to CL_GUI_FRONTEND_SERVICES=>GUI_UPLOAD to bring the selected file's data into an internal SAP table


Ref:

https://community.sap.com/t5/application-development-and-automation-discussions/cl-gui-frontend-services-gt-file-open-dialog-initial-directory-ignored/td-p/416130

https://community.sap.com/t5/application-development-and-automation-discussions/file-open-dialog-no-extension/td-p/12747028

Mar 16, 2015

ALV GRID Set Row and Column Position in Editing

Problem:
I have editable ALV, after edit I refresh ALV, but problem is, that position is set to position 1.1. I need to keep cursor at edited position.

Answer:
In order to refresh the ALV and keep the scroll position, read the scroll position before refresh and set the scroll position after the refresh..

The codes:

*&---------------------------------------------------------------------*
*&      Form  refresh_table_display
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM refresh_table_display .

  DATA: li_rows    TYPE lvc_s_roid,
        li_col     TYPE lvc_s_col,
        lws_row_id TYPE lvc_s_roid,
        lws_col_id TYPE lvc_s_col,
        ls_col     TYPE lvc_s_col,
        ls_num     TYPE lvc_s_roid.

* Get the latest field position
  CALL METHOD c_grid->get_current_cell
    IMPORTING
      es_col_id = li_col
      es_row_no = li_rows.

  CALL METHOD c_grid->get_scroll_info_via_id
    IMPORTING
      es_col_info = ls_col
      es_row_no   = ls_num.

* refresh ALV
  CALL METHOD c_grid->refresh_table_display
    EXCEPTIONS
      finished = 1
      OTHERS   = 2.
  IF sy-subrc = 0.
*   MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
*           WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

* Position back to the latest field
  CALL METHOD c_grid->set_scroll_info_via_id
    EXPORTING
      is_col_info = ls_col
      is_row_no   = ls_num.

  lws_row_id-row_id    = li_rows-row_id.
  lws_col_id-fieldname = li_col-fieldname.

  CALL METHOD c_grid->set_current_cell_via_id
    EXPORTING
      is_column_id = lws_col_id
      is_row_no    = lws_row_id.

ENDFORM.                    " refresh_table_display

Sep 29, 2014

Create and Delete Folder in Frontend system using Class CL_GUI_FRONTEND_SERVICES

Procedure:
  1. Create Selection Screen with two radio buttons “Create Folder” and “Delete Folder”. And parameters to specify path for creating and deleting the folder.
  2. Give path and folder name in the selection screen
  3. Creating folder select the “Create Folder” Radio Button
  4. Deleting folder select the “Delete Folder” Radio Button  

Creation Process:












Deletion Process:









ABAP Codes:

*&----------------------------------------------------------------*
*& Report  Z_TEST001
*&
*&----------------------------------------------------------------*
*&
*&
*&----------------------------------------------------------------*

REPORT z_test001.

* Selection Screen Declarations
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETERS: p_cr RADIOBUTTON GROUP rgb1 USER-COMMAND uco
                                    MODIF ID mod DEFAULT 'X',
            p_dr RADIOBUTTON GROUP rgb1 MODIF ID mod,
            p_cdir TYPE string,
            p_ddir TYPE string.
SELECTION-SCREEN END OF BLOCK b1.

* Data declarations
DATA: result TYPE char1,
      rc TYPE i,
      stripped_name TYPE string,
      v_string TYPE string.

************************************
* At Selection-Screen Output Event
************************************

AT SELECTION-SCREEN OUTPUT.

* Create Folder Checkbox Checked, Don’t display 
* the “Path to Delete Folder” parameter
  IF p_cr = 'X'.
    LOOP AT SCREEN.
      IF screen-name = 'P_DDIR' OR
        screen-name = '%_P_DDIR_%_APP_%-TEXT'.
        screen-input = 0.
        screen-invisible = 1.
        MODIFY SCREEN.
      ENDIF.
    ENDLOOP.
  ELSE.

* Delete Folder Checkbox Checked, Don’t display the 
* “Path to Create Folder” parameter
    LOOP AT SCREEN.
      IF screen-name = 'P_CDIR' OR
        screen-name = '%_P_CDIR_%_APP_%-TEXT'.
        screen-input = 0.
        screen-invisible = 1.
        MODIFY SCREEN.
      ENDIF.
    ENDLOOP.
  ENDIF.

************************************
* Start of Selection Event
************************************
START-OF-SELECTION.

  IF p_cr = 'X'.
    "create folder radio button is checked.

*   Clear result variable
    CLEAR result.

*     Get the folder name
    CALL FUNCTION 'SO_SPLIT_FILE_AND_PATH'
      EXPORTING
        full_name     = p_cdir
      IMPORTING
        stripped_name = stripped_name
      EXCEPTIONS
        x_error       = 1
        OTHERS        = 2.

*   Check if the folder name exists under the specified
*   directory which you want to create
    CALL METHOD cl_gui_frontend_services=>directory_exist
      EXPORTING
        directory            = p_cdir
      RECEIVING
        result               = result
      EXCEPTIONS
        cntl_error           = 1
        error_no_gui         = 2
        wrong_parameter      = 3
        not_supported_by_gui = 4
        OTHERS               = 5.

*   If the folder name already exists then display a message.
    IF result = 'X'.

      CLEAR v_string.
      CONCATENATE 'ALREADY CONTAINS A FOLDER NAMED'
                  stripped_name
                  INTO v_string SEPARATED BY space.

      MESSAGE v_string TYPE 'I'.
      LEAVE LIST-PROCESSING.

*   If the folder name is not exist in the specified directory
    ELSE.

*     Clear return code
      CLEAR rc.

*     Create a new folder under the specified directory
      CALL METHOD cl_gui_frontend_services=>directory_create
        EXPORTING
          directory                = p_cdir
        CHANGING
          rc                       = rc
        EXCEPTIONS
          directory_create_failed  = 1
          cntl_error               = 2
          error_no_gui             = 3
          directory_access_denied  = 4
          directory_already_exists = 5
          path_not_found           = 6
          unknown_error            = 7
          not_supported_by_gui     = 8
          wrong_parameter          = 9
          OTHERS                   = 10.

      IF rc = 0.
        CLEAR v_string.
        CONCATENATE 'CREATED FOLDER NAMED'
                    stripped_name
                    INTO v_string SEPARATED BY space.

        MESSAGE v_string TYPE 'I'.
      ENDIF.

    ENDIF.

  ELSE.  "Delete folder radio button is checked.

*   Clear result variable
    CLEAR result.

*   Get the folder name
    CALL FUNCTION 'SO_SPLIT_FILE_AND_PATH'
      EXPORTING
        full_name     = p_ddir
      IMPORTING
        stripped_name = stripped_name
      EXCEPTIONS
        x_error       = 1
        OTHERS        = 2.

*   Check if the folder name exists under the specified
*   directory which you want to create
    CALL METHOD cl_gui_frontend_services=>directory_exist
      EXPORTING
        directory            = p_ddir
      RECEIVING
        result               = result
      EXCEPTIONS
        cntl_error           = 1
        error_no_gui         = 2
        wrong_parameter      = 3
        not_supported_by_gui = 4
        OTHERS               = 5.

    IF result <> 'X'.

      CLEAR v_string.

      CONCATENATE 'THERE IS NO FOLDER NAMED'
                  stripped_name
                  INTO v_string SEPARATED BY space.

      MESSAGE v_string TYPE 'I'.
      LEAVE LIST-PROCESSING.

*   If the folder name exist, delete that folder from the
*   specified directory
    ELSE.

*     Clear return code
      CLEAR rc.

*      Delete folder from the specified directory
      CALL METHOD cl_gui_frontend_services=>directory_delete
        EXPORTING
          directory               = p_ddir
        CHANGING
          rc                      = rc
        EXCEPTIONS
          directory_delete_failed = 1
          cntl_error              = 2
          error_no_gui            = 3
          path_not_found          = 4
          directory_access_denied = 5
          unknown_error           = 6
          not_supported_by_gui    = 7
          wrong_parameter         = 8
          OTHERS                  = 9.
      IF rc = 0.
        CLEAR v_string.

        CONCATENATE 'DELETED FOLDER NAMED'
                    stripped_name
                    INTO v_string SEPARATED BY space.

        MESSAGE v_string TYPE 'I'.
        LEAVE LIST-PROCESSING.

      ENDIF.
    ENDIF.
  ENDIF.