Showing posts with label OOP. Show all posts
Showing posts with label OOP. Show all posts

May 28, 2026

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, 2016

ALV Using OOP



ALV Using Object Oriented Programming Concept

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.

Nov 10, 2010

Editable ALV with OOPS

*&---------------------------------------------------------------------*
*& Report  YKC_ALV_OOPS
*&
*&---------------------------------------------------------------------*
*& This prog will help in understanding ALV OOPS
*& EDIT on SAVE
*& Tool bar button addition
*&---------------------------------------------------------------------*
REPORT  ykc_alv_oops.

TABLES: mara.
DATABEGIN OF it_tab OCCURS 0,
      matnr LIKE mara-matnr,
      ersda LIKE mara-ersda,  "creation date
      ernam LIKE mara-ernam,  "person created
      pstat LIKE mara-pstat,  "maint stat
      lvorm LIKE mara-lvorm,  "flg for deletion
      mtart LIKE mara-mtart,  "mat type
      meins LIKE mara-meins,  "uom
      END OF it_tab.
DATA: wa_it_tab LIKE LINE OF it_tab.  "making work area
DATA: i_modified TYPE STANDARD TABLE OF mara,"For getting modified rows
      w_modified TYPE mara.
CLASS lcl_events_d0100 DEFINITION DEFERRED.
DATA: event_receiver1  TYPE REF TO lcl_events_d0100,
      i_selected_rows TYPE lvc_t_row,                "Selected Rows
      w_selected_rows TYPE lvc_s_row.

*---------------------------------------------------------------------*
*       CLASS lcl_events_d0100 DEFINITION
*---------------------------------------------------------------------*
CLASS lcl_events_d0100 DEFINITION.
  PUBLIC SECTION.
    METHODS
        handle_hotspot_click
        FOR EVENT hotspot_click OF cl_gui_alv_grid
        IMPORTING
             e_row_id
             e_column_id
             es_row_no
             sender.
*---code addition for ALV pushbuttons
*--for placing buttons
    METHODS handle_toolbar_set
        FOR EVENT toolbar OF cl_gui_alv_grid
        IMPORTING
              e_object
              e_interactive.
*---user command on clicking a button
    METHODS handle_user_command
        FOR EVENT user_command OF cl_gui_alv_grid
        IMPORTING
             e_ucomm.
ENDCLASS.                    "lcl_events_d0100 DEFINITION

TYPE-POOLS cndp.
DATA ok_code TYPE sy-ucomm.


*----------------------------------------------------------------------*
*                       FOR VARIANT
*----------------------------------------------------------------------*
DATA st_var TYPE disvariant .
DATA save TYPE c.
st_var-report = 'YKC_ALV_OOPS'.
save = 'A'.

*----------------------------------------------------------------------*
*         FOR LAYOUT
*----------------------------------------------------------------------*
DATA loyo TYPE lvc_s_layo.
loyo-zebra = 'X'.
loyo-detailinit = 'X'.
loyo-info_fname = 'RED'.

*----------------------------------------------------------------------*
*           FOR FIELD CATALOG
*----------------------------------------------------------------------*
DATA fcat TYPE lvc_t_fcat.
DATA wa_fcat LIKE LINE OF fcat.
*--Declaration for toolbar buttons
DATA : ty_toolbar      TYPE stb_button.
DATA : e_object        TYPE REF TO cl_alv_event_toolbar_set,
       io_alv_toolbar  TYPE REF TO cl_alv_event_toolbar_set.
*---custom container
DATA container TYPE REF TO cl_gui_custom_container.
DATA ref_grid TYPE REF TO cl_gui_alv_grid.
CREATE OBJECT container
  EXPORTING
    container_name = 'CONTAINER'."name of container in module pool
CREATE OBJECT ref_grid
  EXPORTING
    i_parent = container.

*---------------------------------------------------------------------*
*       CLASS lcl_events_d0100 IMPLEMENTATION
*---------------------------------------------------------------------*
CLASS lcl_events_d0100 IMPLEMENTATION.
*---method for hotspot
  METHOD handle_hotspot_click.
    DATA:ls_col_id   TYPE lvc_s_col.
    READ TABLE it_tab INTO wa_it_tab
                             INDEX e_row_id-index.
    IF sy-subrc = 0.
      CHECK ( wa_it_tab-matnr IS NOT INITIAL ).
      CASE e_column_id-fieldname.
        WHEN 'MATNR'.
          LEAVE PROGRAM.
*---put your own logic as per requirement on hotspot click
        WHEN OTHERS.
*       do nothing
      ENDCASE.
      CALL METHOD ref_grid->set_current_cell_via_id
        EXPORTING
          is_row_id    = e_row_id
          is_column_id = ls_col_id.
    ENDIF.
  ENDMETHOD.                    "handle_hotspot_click
**---method for handling toolbar
  METHOD handle_toolbar_set.
    CLEAR ty_toolbar.
    ty_toolbar-function = 'EDIT'"name of btn to  catch click
    ty_toolbar-butn_type = 0.
    ty_toolbar-text = 'EDIT'.
    APPEND ty_toolbar    TO e_object->mt_toolbar.
  ENDMETHOD.                    "handle_toolbar_set
  METHOD handle_user_command.
    DATA: wr_data_changed TYPE REF TO cl_alv_changed_data_protocol.
    DATA: lt_rows TYPE lvc_t_row,
          lt_index TYPE  lvc_s_row-index.
    CASE e_ucomm.
      WHEN 'EDIT'.
        PERFORM save_database.
        CALL METHOD ref_grid->refresh_table_display.
    ENDCASE.
  ENDMETHOD.                    "handle_user_command
ENDCLASS.                    "lcl_events_d0100 IMPLEMENTATION

START-OF-SELECTION.
  PERFORM get_data.
  PERFORM field_catalog.

*&---------------------------------------------------------------------*
*&      Form  get_data
*&---------------------------------------------------------------------*
*       text : getting data into internal table
*----------------------------------------------------------------------*
FORM get_data .
  SELECT matnr ersda ernam pstat lvorm mtart meins
         INTO TABLE it_tab
         FROM mara
         WHERE matnr GE '000000000000000001'.
ENDFORM.                    " get_data

*&---------------------------------------------------------------------*
*&      Form  field_catalog
*&---------------------------------------------------------------------*
*       text  :setting field cat
*----------------------------------------------------------------------*
FORM field_catalog .
  REFRESH fcat.
  DATA: lv_pos TYPE i.
  lv_pos = lv_pos + 1.
  wa_fcat-fieldname = 'MATNR'.
  wa_fcat-coltext = 'Material No'.
  wa_fcat-col_pos = lv_pos.
  wa_fcat-hotspot = 'X'.
  wa_fcat-outputlen = 18.
  APPEND wa_fcat TO fcat.
  CLEAR wa_fcat.
  lv_pos = lv_pos + 1.
  wa_fcat-fieldname = 'ERSDA'.
  wa_fcat-coltext = 'Creation Date'.
  wa_fcat-col_pos = lv_pos.
  wa_fcat-edit = 'X'.
  wa_fcat-outputlen = 18.
  APPEND wa_fcat TO fcat.
  CLEAR wa_fcat.
  lv_pos = lv_pos + 1.
  wa_fcat-fieldname = 'ERNAM'.
  wa_fcat-coltext = 'Person Created'.
  wa_fcat-col_pos = lv_pos.
  wa_fcat-outputlen = 18.
  APPEND wa_fcat TO fcat.
  CLEAR wa_fcat.
  lv_pos = lv_pos + 1.
  wa_fcat-fieldname = 'PSTAT'.
  wa_fcat-coltext = 'Maint Stat'.
  wa_fcat-col_pos = lv_pos.
  wa_fcat-outputlen = 18.
  APPEND wa_fcat TO fcat.
  CLEAR wa_fcat.
  lv_pos = lv_pos + 1.
  wa_fcat-fieldname = 'LVORM'.
  wa_fcat-coltext = 'Flag For Deletion'.
  wa_fcat-col_pos = lv_pos.
  wa_fcat-outputlen = 18.
  APPEND wa_fcat TO fcat.
  CLEAR wa_fcat.
  lv_pos = lv_pos + 1.
  wa_fcat-fieldname = 'MTART'.
  wa_fcat-coltext = 'Material Type'.
  wa_fcat-col_pos = lv_pos.
  wa_fcat-outputlen = 18.
  APPEND wa_fcat TO fcat.
  CLEAR wa_fcat.
  lv_pos = lv_pos + 1.
  wa_fcat-fieldname = 'MEINS'.
  wa_fcat-coltext = 'UOM'.
  wa_fcat-col_pos = lv_pos.
  wa_fcat-outputlen = 18.
  APPEND wa_fcat TO fcat.
  CLEAR wa_fcat.
  CREATE OBJECT event_receiver1.
*---setting event handlers
  SET HANDLER event_receiver1->handle_toolbar_set   FOR ref_grid.
  SET HANDLER event_receiver1->handle_user_command  FOR ref_grid.
  SET HANDLER event_receiver1->handle_hotspot_click FOR ref_grid.

*----------------------------------------------------------------------*
*           ALV GRID DISPLAY
*----------------------------------------------------------------------*
  CALL METHOD ref_grid->set_table_for_first_display
    EXPORTING
      is_variant      = st_var
      i_save          = save
      is_layout       = loyo
    CHANGING
      it_outtab       = it_tab[]
      it_fieldcatalog = fcat.
  CALL SCREEN 100.
ENDFORM.                    " field_catalog

*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
*  CREATE OBJECT gr_events_d0100.
*
*  SET HANDLER gr_events_d0100->double_click
*
*                 FOR ref_grid.
  CALL METHOD ref_grid->register_edit_event
    EXPORTING
      i_event_id = cl_gui_alv_grid=>mc_evt_modified.
  SET PF-STATUS 'S100'.
  SET TITLEBAR 'XXX'.
ENDMODULE.                 " STATUS_0100  OUTPUT

*&---------------------------------------------------------------------*
*&      Module  exit  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE exit INPUT.
  CASE ok_code.
    WHEN 'EXI' .
      CLEAR ok_code.
      LEAVE PROGRAM.
  ENDCASE.
ENDMODULE.                 " exit  INPUT

*&---------------------------------------------------------------------*
*&      Form  SAVE_DATABASE
*&---------------------------------------------------------------------*
*       text : saving into DDIC from internal table
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM save_database .
*--- Getting the selected rows index
  CALL METHOD ref_grid->get_selected_rows
    IMPORTING
      et_index_rows = i_selected_rows.
*--- Through the index capturing the values of selected rows
  LOOP AT i_selected_rows INTO w_selected_rows.
    READ TABLE it_tab INTO wa_it_tab INDEX w_selected_rows-index.
    IF sy-subrc EQ 0.
      MOVE-CORRESPONDING wa_it_tab TO w_modified.
      APPEND w_modified TO i_modified.
    ENDIF.
  ENDLOOP.
  MODIFY mara FROM TABLE i_modified.
ENDFORM.                    " SAVE_DATABASE