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

...Previous

4. In the PAI Event (Module USER_COMMAND_9001), write the following code

*&------------------------------------------------------------*
*&      Module USER_COMMAND_9001  INPUT
*&------------------------------------------------------------*
*       text
*-------------------------------------------------------------*
MODULE user_command_9001 INPUT.
  DATA :
    lh_norec      TYPE i,
    lh_total(5)   TYPE c,
    lh_succ(40)   TYPE c,
    lwa_del       TYPE ty_mod,
    lwa_mod       TYPE ty_mod,
    lh_totdel     TYPE i,
    lh_flag       TYPE c,
    li_fieldcat   TYPE lvc_t_fcat,
    lwa_fieldcat  TYPE lvc_s_fcat.
  CASE sy-ucomm.
    WHEN 'CHANGE'.
      SET PF-STATUS 'ALV1'.
    WHEN 'SAVE'.
*     Lock the table
      CALL FUNCTION 'ENQUEUE_E_TABLE'
        EXPORTING
          mode_rstable   = 'E'
          tabname        = viewname
        EXCEPTIONS
          foreign_lock   = 1
          system_failure = 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.
      IF NOT grid1 IS INITIAL .
        CALL METHOD grid1->check_changed_data .
      ENDIF .
      SORT i_mod BY row.
      DELETE ADJACENT DUPLICATES  FROM i_mod COMPARING row.
      lh_norec = LINES( i_mod ).
      lh_total = lh_norec.
      SHIFT lh_total LEFT DELETING LEADING space.
*  Standard Table Update
      IF i_mod[] IS NOT INITIAL.
        LOOP AT i_mod INTO lwa_mod.
          READ TABLE <i_itab> INTO <wa_tab> INDEX lwa_mod-row.
          IF sy-subrc = 0 .
            IF lh_flag NE 'X'.
              MODIFY (viewname) FROM <wa_tab>.
            ELSE.
              INSERT (viewname) FROM <wa_tab>.
              IF sy-subrc NE 0.
                MESSAGE e009.
              ENDIF.
            ENDIF.
          ENDIF.
        ENDLOOP.
        CONCATENATE lh_total '  ' text-s01 INTO lh_succ SEPARATED BY space.
        MESSAGE i000 WITH lh_succ.
      ELSE.
        MESSAGE i000 WITH text-s02.
      ENDIF.
*     Unlock the table
      CALL FUNCTION 'DEQUEUE_E_TABLE'
        EXPORTING
          mode_rstable = 'E'
          tabname      = viewname.
      CLEAR : i_mod[],
              lh_flag.
    WHEN 'DELETE'.
*      Selecting Selected Rows
      PERFORM handle_user_command USING sy-ucomm.
      IF i_del[] IS NOT INITIAL.
        LOOP AT i_del INTO lwa_del.
          READ TABLE <i_itab> INTO <wa_tab> INDEX lwa_del-row.
          IF sy-subrc = 0.
            DELETE (viewname) FROM <wa_tab>.
          ENDIF.
        ENDLOOP.
        lh_totdel = LINES( i_del ).
        MESSAGE i011 WITH lh_totdel.
        CLEAR : i_del[], lh_totdel .
      ELSE.
        MESSAGE i000 WITH text-s03.
      ENDIF.
    WHEN 'EXIT'   OR
         'BACK'   OR
         'CANCEL'.
      CLEAR : li_fieldcat[],
              i_exclude[],
              i_del[],
              lh_totdel.
      LEAVE TO SCREEN 0.
  ENDCASE.
ENDMODULE.                 " USER_COMMAND_9001  INPUT

5. Write the following forms at the end of the program

*&---------------------------------------------------------------------*
*&      Form  handle_data_changed
*&---------------------------------------------------------------------*
FORM handle_data_changed  USING    p_er_data_changed TYPE REF TO cl_alv_changed_data_protocol.
  DATA :
    lwa_mod_cell TYPE lvc_s_modi,
    lwa_mod      TYPE ty_mod.
  LOOP AT p_er_data_changed->mt_good_cells INTO lwa_mod_cell.
    lwa_mod-row  = lwa_mod_cell-row_id.
    APPEND lwa_mod TO i_mod.
  ENDLOOP.
ENDFORM.                    " handle_data_changed
*&---------------------------------------------------------------------*
*&      Form  exclude_tb_functions
*&---------------------------------------------------------------------*
FORM exclude_tb_functions CHANGING pt_exclude TYPE ui_functions .
  DATA:
    lwa_exclude TYPE ui_func.
  lwa_exclude = cl_gui_alv_grid=>mc_fc_loc_append_row.
  APPEND lwa_exclude TO pt_exclude.
  lwa_exclude = cl_gui_alv_grid=>mc_fc_loc_copy.
  APPEND lwa_exclude TO pt_exclude.
  lwa_exclude = cl_gui_alv_grid=>mc_fc_loc_copy_row.
  APPEND lwa_exclude TO pt_exclude.
  lwa_exclude = cl_gui_alv_grid=>mc_fc_loc_cut.
  APPEND lwa_exclude TO pt_exclude.
  lwa_exclude = cl_gui_alv_grid=>mc_fc_loc_delete_row.
  APPEND lwa_exclude TO pt_exclude.
  lwa_exclude = cl_gui_alv_grid=>mc_fc_loc_insert_row.
  APPEND lwa_exclude TO pt_exclude.
  lwa_exclude = cl_gui_alv_grid=>mc_fc_loc_move_row.
  APPEND lwa_exclude TO pt_exclude.
  lwa_exclude = cl_gui_alv_grid=>mc_fc_loc_paste.
  APPEND lwa_exclude TO pt_exclude.
  lwa_exclude = cl_gui_alv_grid=>mc_fc_loc_paste_new_row.
  APPEND lwa_exclude TO pt_exclude.
ENDFORM.                    "exclude_tb_functions
*&---------------------------------------------------------------------*
*&      Form  handle_user_command
*&---------------------------------------------------------------------*
FORM handle_user_command  USING  p_ucomm TYPE syucomm.
  DATA :
    i_selected_rows  TYPE lvc_t_roid .
  DATA :
    lwa_selected_row TYPE lvc_s_roid,
    lwa_del          TYPE ty_mod.
  CALL METHOD grid1->get_selected_rows
    IMPORTING
      et_row_no = i_selected_rows.
  LOOP AT i_selected_rows INTO lwa_selected_row.
    lwa_del-row = lwa_selected_row-row_id.
    APPEND lwa_del TO i_del.
  ENDLOOP.
ENDFORM.                    " handle_user_command

6. Create PF Status like “ALV” 

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