Create, Modify and Delete entries dynamically from any custom table by using Object Oriented ALV

By Padmanaban Ramakrishnan, Capgemini

Objective: 

This program is used to create, modify and delete entries dynamically from any custom table by using object oriented ALV. 

Step by Step procedure 

1. Go to SE80-> create a program and write the following code 

TYPE-POOLS:
     vimty.
TYPES :
   BEGIN OF ty_mod,
      row TYPE i,
   END OF ty_mod.
DATA:
  g_container        TYPE scrfname VALUE 'CUSTOM_CONTAINER',
  grid1              TYPE REF TO cl_gui_alv_grid,
  g_custom_container TYPE REF TO cl_gui_custom_container.
DATA:
  i_table            TYPE REF TO data,
  wa_all             TYPE REF TO data.
DATA:
  org_crit_inst      TYPE vimty_oc_type,
  old_rc             LIKE sy-subrc,
  act_level          LIKE authb-actvt,
  only_show_allowed  TYPE c,
  i_exclude          TYPE ui_functions.
DATA :
  i_mod              TYPE STANDARD TABLE OF ty_mod,
  i_del              TYPE STANDARD TABLE OF ty_mod.
FIELD-SYMBOLS:
        <i_itab>     TYPE table,
        <wa_tab>     TYPE ANY.
DATA: BEGIN OF header OCCURS 1.
        INCLUDE STRUCTURE vimdesc.
DATA: END OF header.
DATA: BEGIN OF namtab OCCURS 50.
        INCLUDE STRUCTURE vimnamtab.
DATA: END OF namtab.
DATA: vim_wheretab LIKE vimwheretb OCCURS 10.
DATA: dba_sellist LIKE vimsellist OCCURS 10.
SELECTION-SCREEN BEGIN OF BLOCK bb WITH FRAME TITLE text-100.
PARAMETER: viewname TYPE tvdir-tabname.
SELECTION-SCREEN SKIP 2.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN PUSHBUTTON 20(10) text-101 USER-COMMAND b1. "Display
SELECTION-SCREEN PUSHBUTTON 36(10) text-102 USER-COMMAND b2. "Change
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK bb.
AT SELECTION-SCREEN.
  CASE sy-ucomm.
    WHEN 'B1'.
      SET PF-STATUS  'ALV'.
      CALL SCREEN 9001.
    WHEN 'B2'.
      SET PF-STATUS 'ALV1'.
      CALL SCREEN 9001.
  ENDCASE.
* Class used to get changed data
CLASS lcl_event_handler DEFINITION .
  PUBLIC SECTION .
    METHODS: handle_data_changed
              FOR EVENT data_changed OF cl_gui_alv_grid
              IMPORTING er_data_changed.
ENDCLASS.                    "lcl_event_handler DEFINITION
* Class used to get changed data
CLASS lcl_event_handler IMPLEMENTATION .
*  Handle Data Changed
  METHOD handle_data_changed .
    PERFORM handle_data_changed USING er_data_changed .
  ENDMETHOD.                    "handle_data_changed
ENDCLASS.                    "lcl_event_handler IMPLEMENTATION 

Save and activate. 

2. Create a screen 9001 with custom container. 

                    

Screen 9001 flow logic looks like the following 

 

3. In the PBO event (Module STATUS_9001), write the following code. 

*&---------------------------------------------------------------------*
*&      Module  STATUS_9001  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE status_9001 OUTPUT.
  DATA :
    gr_event_handler TYPE REF TO lcl_event_handler .
* Creating an instance for the event handler
  CREATE OBJECT gr_event_handler .
  TRY.
      CREATE DATA i_table TYPE TABLE OF (viewname).
      ASSIGN i_table->* TO <i_itab>.
      CREATE DATA wa_all LIKE LINE OF <i_itab>.
      ASSIGN wa_all->* TO <wa_tab>.
*     Selecting data dynamically
      SELECT * FROM (viewname) INTO TABLE <i_itab>.
*     Building the fieldcatelog
      CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
        EXPORTING
          i_structure_name       = viewname
        CHANGING
          ct_fieldcat            = li_fieldcat
        EXCEPTIONS
          inconsistent_interface = 1
          program_error          = 2
          OTHERS                 = 3.
      IF sy-subrc <> 0.
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
          WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      ENDIF.
*     Making fields editable except key fields
      IF sy-ucomm = 'UPD' OR  sy-ucomm = 'CHANGE'.
        LOOP AT li_fieldcat INTO lwa_fieldcat.
          IF lwa_fieldcat-key = space.
            lwa_fieldcat-edit = 'X'.
            MODIFY li_fieldcat FROM lwa_fieldcat.
          ENDIF.
        ENDLOOP.
      ENDIF.
*     Making fields editable
      IF sy-ucomm = 'NEW'.
        LOOP AT li_fieldcat INTO lwa_fieldcat.
          lwa_fieldcat-edit = 'X'.
          MODIFY li_fieldcat FROM lwa_fieldcat.
        ENDLOOP.
        lh_flag = 'X'.
        CLEAR : <i_itab>.
        DO 100 TIMES.
          APPEND <wa_tab> TO <i_itab>.
        ENDDO.
      ENDIF.
*     Exclude buttons
      PERFORM exclude_tb_functions CHANGING i_exclude.
      IF g_custom_container IS INITIAL.
        CREATE OBJECT g_custom_container
          EXPORTING
            container_name = g_container.
        CREATE OBJECT grid1
          EXPORTING
            i_parent = g_custom_container.
      ENDIF.
*     Making all fields non-editable if display mode
      IF sy-ucomm = 'SHOW'.
        LOOP AT li_fieldcat INTO lwa_fieldcat.
          lwa_fieldcat-edit = ' '.
          MODIFY li_fieldcat FROM lwa_fieldcat.
        ENDLOOP.
      ENDIF.
      IF sy-ucomm = 'SAVE'.
        LOOP AT li_fieldcat INTO lwa_fieldcat.
          IF lwa_fieldcat-key NE space.
            lwa_fieldcat-edit = space.
            MODIFY li_fieldcat FROM lwa_fieldcat.
          ENDIF.
        ENDLOOP.
      ENDIF.
*     Displaying ALV Grid
      CALL METHOD grid1->set_table_for_first_display
        EXPORTING
          i_structure_name     = viewname
          it_toolbar_excluding = i_exclude
        CHANGING
          it_outtab            = <i_itab>
          it_fieldcatalog      = li_fieldcat.
      IF sy-subrc NE 0.
        EXIT.
      ENDIF.
*      Getting the changed data
      SET HANDLER gr_event_handler->handle_data_changed FOR grid1 .
    CATCH cx_sy_create_data_error.
  ENDTRY.
ENDMODULE.                 " STATUS_9001  OUTPUT
Click here to continue...
 

Please send us your feedback/suggestions at webmaster@SAPTechnical.COM 

HomeContribute About Us Privacy Terms Of Use • Disclaimer • SafeCompanies: Advertise on SAPTechnical.COM | Post JobContact Us  

Graphic Design by Round the Bend Wizards

footer image footer image