Create, Modify and Delete Entries Dynamically From Any Custom Table by Using Object Oriented ALV

18
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.

description

Create, Modify and Delete Entries Dynamically From Any Custom Table by Using Object Oriented ALV

Transcript of Create, Modify and Delete Entries Dynamically From Any Custom Table by Using Object Oriented ALV

Create, Modify and Delete entries dynamically from any custom table by using Object Oriented ALVBy Padmanaban Ramakrishnan, CapgeminiObjective: 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: TYPE table, 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. "DisplaySELECTION-SCREEN PUSHBUTTON 36(10) text-102 USER-COMMAND b2. "ChangeSELECTION-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 dataCLASS 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 dataCLASS lcl_event_handler IMPLEMENTATION .* Handle Data Changed METHOD handle_data_changed . PERFORM handle_data_changed USING er_data_changed . ENDMETHOD. "handle_data_changedENDCLASS. "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 . CREATE DATA wa_all LIKE LINE OF . ASSIGN wa_all->* TO .* Selecting data dynamically SELECT * FROM (viewname) INTO TABLE .* 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 : . DO 100 TIMES. APPEND TO . 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 = 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 OUTPUT4. 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 INTO INDEX lwa_mod-row. IF sy-subrc = 0 . IF lh_flag NE 'X'. MODIFY (viewname) FROM . ELSE. INSERT (viewname) FROM . 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 INTO INDEX lwa_del-row. IF sy-subrc = 0. DELETE (viewname) FROM . 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 INPUT5. 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_command6. Create PF Status like ALV

7. Create PF Status ALV1 8. Create Text Elements 9. Create transaction for the program (T-code SE93)

Save and activate 10. Execute the transaction (ZOOALV) Select any custom table (starting with z or y). Ex- ZZZTESTTIME Press Display button Output

Press Change Button Press Create Button Type data and SAVE

Press Change Button Before Changing Changing Value

Save

After saving Delete After Delete