Tips and Tricks for Interactive SAP ALV
In the wake of chipping away at various activities and utilizing intuitive ALV Uh oh ideas broadly, I considered drafting one record with some valuable data, which will assist each engineer with defeating the vast majority of the plan prerequisites in any intuitive ALV Report.
Do you be aware, you can Really take a look at the Consistency of an ALV with simply SHIFT + 2 RIGHT Snaps? Let’s dive into the Tips and Tricks for Interactive SAP ALV!
Allow us to begin our The present subject.
1. Create a Container in the screen, say ‘ALV_CONT’.
2. Create Reference Object for the Container:
CREATE OBJECT container_r EXPORTING container_name = 'ALV_CONT' EXCEPTIONS cntl_error = 1 cntl_system_error = 2 create_error = 3 lifetime_error = 4 lifetime_dynpro_dynpro_link = 5.
3. Create Reference Object for the ALV Grid as below:
CREATE OBJECT grid_r EXPORTING i_parent = container_r.
4. Define the ALV Layout as below:
gw_layout-cwidth_opt = abap_true. gw_layout-zebra = abap_true. gw_layout-sel_mode = ‘D’.
5. Populate the Field Catalog data for display as below:
lv_pos = lv_pos + 1. “Position number CLEAR wa_fieldcat. wa_fieldcat-fieldname = 'BUKRS'. "Company Code wa_fieldcat-col_pos = lv_pos. wa_fieldcat-ref_table = 'T001'. APPEND wa_fieldcat TO gt_fieldcat.
5.1. For Field Editable:
wa_fieldcat-edit = abap_true.
Also Read: ALV with an Editable Row.
5.2. For field No Display in output:
wa_fieldcat-no_out = abap_true. “Check for field display
5.3. For field display as a Checkbox:
wa_fieldcat-checkbox = abap_true.
5.4. For the field where customized F4-Help is required:
wa_fieldcat-f4availabl = abap_true.
5.5. For the field where the Summation is required:
wa_fieldcat-do_sum = abap_true.
Moreover, the field in light of which summation will show up should be characterized as below(eg. ANLKL):
CLEAR li_sort. lst_sort-fieldname = 'ANLKL'. lst_sort-subtot = abap_true. APPEND lst_sort TO li_sort
6. To Restrict Functions from ALV display:
lw_exclude = cl_gui_alv_grid=>mc_fc_check. APPEND lw_exclude TO gt_exclude. lw_exclude = cl_gui_alv_grid=>mc_fc_refresh. APPEND lw_exclude TO gt_exclude. lw_exclude = cl_gui_alv_grid=>mc_fc_loc_copy_row. APPEND lw_exclude TO gt_exclude. lw_exclude = cl_gui_alv_grid=>mc_fc_loc_delete_row. APPEND lw_exclude TO gt_exclude. lw_exclude = cl_gui_alv_grid=>mc_fc_loc_append_row. APPEND lw_exclude TO gt_exclude. lw_exclude = cl_gui_alv_grid=>mc_fc_loc_insert_row. APPEND lw_exclude TO gt_exclude.
7. Populate the Data in table GT_DATA.
8. Call the method set_table_for_first_display for Display of data.
CALL METHOD grid_r->set_table_for_first_display EXPORTING i_structure_name = 'ZFI_S_PIS' “Structure is_layout = gw_layout it_toolbar_excluding = gt_exclude i_save = gc_save_a "A i_default = abap_true CHANGING it_outtab = gt_data[] “Table with ALV data It_sort = li_sort it_fieldcatalog = gt_fieldcat[].
CALL METHOD cl_gui_control=>set_focus EXPORTING control = grid_r.
Data declaration for Reference:
Data: container_r TYPE REF TO cl_gui_custom_container, "Container for ALV PIS data grid_r TYPE REF TO cl_gui_alv_grid, "Grid for ALV PIS data gt_exclude TYPE ui_functions, "Exclude functions from ALV gt_fieldcat TYPE lvc_t_fcat, "Field catalog for ALV PIS data ref_aplic TYPE REF TO lcl_application_alv, gw_layout TYPE lvc_s_layo. "Layout for PIS
9. If the Data just needs to be Refreshed:
CALL METHOD grid_r->refresh_table_display EXCEPTIONS finished = 1 OTHERS = 2.
10. To make fields Editable, call the below method while calling ALV for display:
CALL METHOD grid_r->register_edit_event EXPORTING i_event_id = cl_gui_alv_grid=>mc_evt_modified. " enter. "MC_EVT_MODIFIED
11. Now to Validate the Data Changed in the grid we need to do the following:
11.1. Define a class (lcl_application_alv) definition and implementation and define event (handle_data_changed) for event data_changed of class cl_gui_alv_grid in it.
CLASS lcl_application_alv DEFINITION. PUBLIC SECTION. METHODS: handle_data_changed FOR EVENT data_changed OF cl_gui_alv_grid IMPORTING er_data_changed e_onf4_after. ENDCLASS. "lcl_application_alv DEFINITION
11.2. In method handle_data_changed call methods get_cell_value and modify_cell to get and modify cell values like below –
LOOP AT er_data_changed->mt_good_cells INTO lw_good. WHEN 'KUNNR'. CALL METHOD er_data_changed->get_cell_value EXPORTING i_row_id = lw_good-row_id i_fieldname = lw_good-fieldname IMPORTING e_value = gv_kunnr. * Get name1 for the kunnr SELECT SINGLE name1 FROM kna1 INTO lw_pis-name1 WHERE kunnr = gv_kunnr . IF sy-subrc = 0. * update name1 in table CALL METHOD er_data_changed->modify_cell EXPORTING i_row_id = lw_good-row_id i_fieldname = 'NAME1' i_value = lv_name1. ELSE. MESSAGE i000 WITH 'Invalid Customer number in row'(027) lw_good-row_id. ENDIF.
12. Now before calling method set_table_for_first_display, write the below code:
CREATE OBJECT ref_aplic. SET HANDLER ref_aplic->handle_data_changed FOR grid_r.
13. Assuming that the Field Inventory is expected to change when various activities are executed for the ALV lattice information show, call strategy set_frontend_fieldcatalog subsequent to populating the field index information to revive the field list settings.
CALL METHOD grid_r->set_frontend_fieldcatalog EXPORTING it_fieldcatalog = gt_fieldcat[].
14. In the event that Contingent F4 Help is expected to be populated in the ALV, express in view of one information in ALV, another field F4-Help information should be populated then do the accompanying:
14.1. Prior to calling strategy set_table_for_first_display, we need to Enlist the fields we need to have the tweaked F4-help like beneath:
lw_f4-fieldname = 'ZFI_BRN_LOC'. "IFSC code lw_f4-register = abap_true. lw_f4-getbefore = space. lw_f4-chngeafter = abap_true. INSERT lw_f4 INTO TABLE lt_f4. CLEAR lw_f4. lw_f4-fieldname = 'STGRD'. "Reason for Reversal lw_f4-register = abap_true. lw_f4-getbefore = space. INSERT lw_f4 INTO TABLE lt_f4. CALL METHOD grid_r->register_f4_for_fields EXPORTING it_f4 = lt_f4.
14.2. In class lcl_application_alv, define the method and then implement it.
METHODS: handle_on_f4 FOR EVENT onf4 OF cl_gui_alv_grid IMPORTING sender e_fieldname e_fieldvalue es_row_no er_event_data et_bad_cells e_display.
14.3. In class lcl_application_alv implementation, implement the method handle_on_f4.
*------------------------------------------------------------- * Add the custom F4 values for IFSC code *------------------------------------------------------------- IF e_fieldname = 'ZFI_BRN_LOC'. IF gv_flag_inst_type = abap_true. "For chq only * IF gt_data[] IS INITIAL. CLEAR lt_knbk. CLEAR gt_data. READ TABLE gt_pis INTO lw_pis INDEX es_row_no-row_id. IF sy-subrc = 0. SELECT bankl bkref FROM knbk INTO TABLE lt_knbk WHERE kunnr = lw_pis-kunnr. "gv_kunnr. IF sy-subrc = 0. LOOP AT lt_knbk INTO lw_knbk. lw_data-zfi_brn_loc = lw_knbk-bankl. lw_data-zfi_bnk_code = lw_knbk-bkref. APPEND lw_data TO gt_data. CLEAR lw_data. ENDLOOP. ENDIF. ENDIF. * ENDIF. *Call the function module to display the custom F4 values CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST' EXPORTING retfield = 'ZFI_BRN_LOC' window_title = 'List of IFSC entries'(026) value_org = gc_val_org TABLES value_tab = gt_data[] return_tab = lt_ret[] EXCEPTIONS parameter_error = 1 no_values_found = 2 OTHERS = 3. * (note: gt_ret[] contains the row id selected by the user from the list of f4 values) IF sy-subrc = 0. READ TABLE lt_ret INTO lw_sel INDEX 1. ASSIGN er_event_data->m_data->* TO <itab>. READ TABLE gt_pis INDEX es_row_no-row_id INTO lw_pis. lw_modi-row_id = es_row_no-row_id. lw_modi-fieldname = 'ZFI_BRN_LOC'. lw_modi-value = lw_sel-fieldval. APPEND lw_modi TO <itab>. READ TABLE gt_data INTO lw_data WITH KEY zfi_brn_loc = lw_modi-value+0(11). IF sy-subrc = 0. lw_modi-row_id = es_row_no-row_id. lw_modi-fieldname = 'ZFI_BNK_CODE'. lw_modi-value = lw_data-zfi_bnk_code. APPEND lw_modi TO <itab>. ENDIF. ENDIF. ENDIF. ENDIF. *------------------------------------------------------------- * Add the custom F4 values for Reversal Reason *------------------------------------------------------------- IF e_fieldname = 'STGRD'. CLEAR: lt_t041ct, lt_return. IF gv_uname = gc_depo_userid. "BTDEPO SELECT stgrd txt40 FROM t041ct INTO TABLE lt_t041ct WHERE spras = sy-langu AND stgrd = gc_stgrd_06. IF sy-subrc = 0. * do nothing ENDIF. ELSE. SELECT stgrd txt40 FROM t041ct INTO TABLE lt_t041ct WHERE spras = sy-langu AND stgrd IN (gc_stgrd_06, gc_stgrd_08, gc_stgrd_09, gc_stgrd_10). IF sy-subrc = 0. * do nothing ENDIF. ENDIF. IF sy-subrc = 0. CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST' EXPORTING retfield = 'STGRD' window_title = 'List of Rev reason entries'(025) value_org = gc_val_org TABLES value_tab = lt_t041ct return_tab = lt_return EXCEPTIONS parameter_error = 1 no_values_found = 2 OTHERS = 3. IF sy-subrc = 0. READ TABLE lt_return INTO lw_sel INDEX 1. ASSIGN er_event_data->m_data->* TO <itab>. READ TABLE gt_pis INDEX es_row_no-row_id INTO lw_pis. lw_modi-row_id = es_row_no-row_id. lw_modi-fieldname = 'STGRD'. lw_modi-value = lw_sel-fieldval. APPEND lw_modi TO <itab>. ENDIF. ENDIF. ENDIF. er_event_data->m_event_handled = abap_true. "(to inform grid that f4 was handled manually)
14.4. Now after Register_F4_for_Fields is called, add below code:
SET HANDLER ref_aplic->handle_on_f4 FOR grid_r.
15. When the Back button is pressed, clear and refresh as below:
CALL METHOD grid_r->refresh_table_display. CALL METHOD grid_r->free. CALL METHOD container_r->free. CALL METHOD cl_gui_cfw=>flush.
16. To make an ALV invisible add below code:
CALL METHOD inv_grid_r->set_visible( EXPORTING visible = '0' ).
17. To make Row Selection through code: Pass the row ids into lt_lvc_s_roid-
lw_lvc_s_roid-row_id = sy-tabix. APPEND lw_lvc_s_roid TO lt_lvc_s_roid. CALL METHOD grid_r->set_selected_rows EXPORTING it_row_no = lt_lvc_s_roid.
18. To Reset Scroll Bar Position after selecting rows through code, add the below code:
CALL METHOD grid_r->get_scroll_info_via_id IMPORTING es_row_no = lw_row_no es_row_info = lw_row_info es_col_info = lw_col_info. CALL METHOD grid_r->set_selected_rows EXPORTING it_row_no = lt_lvc_s_roid. CALL METHOD grid_r->set_scroll_info_via_id( EXPORTING is_row_no = lw_row_no is_row_info = lw_row_info is_col_info = lw_col_info ).
19. Display Top-of-Page data:
19.1. To show To-of-page subtleties first the primary holder should be parted into equal parts – one will be alloted to the Network and the other for populating the top header subtleties as underneath:
* Create TOP-Document CREATE OBJECT g_dyndoc_id EXPORTING style = 'ALV_GRID'. * Create Splitter for custom_container CREATE OBJECT g_splitter EXPORTING parent = g_custom_container rows = 2 columns = 1.
CALL METHOD g_splitter->get_container EXPORTING row = 1 column = 1 RECEIVING container = g_parent_top.
CALL METHOD g_splitter->get_container EXPORTING row = 2 column = 1 RECEIVING container = g_parent_grid.
* Set height for g_parent_html CALL METHOD g_splitter->set_row_height EXPORTING id = 1 height = 20. CREATE OBJECT g_grid EXPORTING i_parent = g_parent_grid EXCEPTIONS error_cntl_create = 1 error_cntl_init = 2 error_cntl_link = 3 error_dp_create = 4 OTHERS = 5.
19.2. Now in the local class, define method handle_top_of_page as below:
METHODS: *Define method to handle Toolbar handle_top_of_page FOR EVENT top_of_page OF cl_gui_alv_grid IMPORTING e_dyndoc_id.
19.3. In the class implementation, define the method details:
METHOD handle_top_of_page. PERFORM f_event_top_of_page USING g_dyndoc_id. ENDMETHOD. "EVENT_HANDLER
19.4. Logic in the Subroutine:
FORM f_event_top_of_page USING dg_dyndoc_id TYPE REF TO cl_dd_document.
DATA: lv_name TYPE lvc_fname, "Name li_t093b TYPE STANDARD TABLE OF ty_t093b INITIAL SIZE 0, "T093B temp table lst_t093b TYPE ty_t093b, "T093b wa lv_reptext TYPE reptext, "report Text lv_text TYPE sdydo_text_element. "Final text
* Header1 lv_name = c_header1. PERFORM f_read_text USING lv_name CHANGING lv_reptext. lv_text = lv_reptext. CALL METHOD dg_dyndoc_id->add_text EXPORTING text = lv_text sap_style = cl_dd_area=>heading sap_fontsize = cl_dd_area=>large sap_color = cl_dd_area=>list_heading_int. * Add new-line CALL METHOD dg_dyndoc_id->new_line.
* Header2 WRITE s_budat-low TO g_fdate. WRITE s_budat-high TO g_tdate. lv_name = c_header2. PERFORM f_read_text USING lv_name CHANGING lv_reptext. REPLACE '&G_FDATE&' INTO lv_reptext WITH g_fdate. REPLACE '&G_TDATE&' INTO lv_reptext WITH g_tdate. lv_text = lv_reptext. CALL METHOD dg_dyndoc_id->add_gap. CALL METHOD g_dyndoc_id->add_text EXPORTING text = lv_text sap_emphasis = cl_dd_area=>heading. * Add new-line CALL METHOD dg_dyndoc_id->new_line.
* Header3 lv_name = c_header3. PERFORM f_read_text USING lv_name CHANGING lv_reptext. lv_text = lv_reptext. CALL METHOD dg_dyndoc_id->add_gap. CALL METHOD g_dyndoc_id->add_text EXPORTING text = lv_text sap_emphasis = cl_dd_area=>heading. * Add new-line CALL METHOD dg_dyndoc_id->new_line.
* Header4 lv_name = c_header4. PERFORM f_read_text USING lv_name CHANGING lv_reptext. lv_text = lv_reptext. li_t093b[] = i_t093b[]. SORT li_t093b BY waers. DELETE ADJACENT DUPLICATES FROM li_t093b COMPARING waers. LOOP AT li_t093b INTO lst_t093b. CONCATENATE lv_text lst_t093b-waers INTO lv_text SEPARATED BY space. ENDLOOP. CALL METHOD dg_dyndoc_id->add_gap. CALL METHOD g_dyndoc_id->add_text EXPORTING text = lv_text sap_emphasis = cl_dd_area=>heading.
* Display output PERFORM f_display. ENDFORM. " EVENT_TOP_OF_PAGE
*&---------------------------------------------------------------------* *& Subroutine F_DISPLAY *&---------------------------------------------------------------------* * Display data *----------------------------------------------------------------------* FORM f_display. * Creating html control IF g_html_cntrl IS INITIAL. CREATE OBJECT g_html_cntrl EXPORTING parent = g_parent_top. ENDIF. CALL METHOD g_dyndoc_id->merge_document. g_dyndoc_id->html_control = g_html_cntrl. * Display document CALL METHOD g_dyndoc_id->display_document EXPORTING reuse_control = abap_true parent = g_parent_top EXCEPTIONS html_display_error = 1. IF sy-subrc NE 0. * Error in displaying top-of-page MESSAGE i023. LEAVE LIST-PROCESSING. ENDIF. ENDFORM. " display
19.5. Before method set_table_for_first_display is called write the below code:
SET HANDLER ref_aplic->handle_top_of_page FOR grid_r. CALL METHOD g_dyndoc_id->initialize_document EXPORTING background_color = cl_dd_area=>col_textarea. * Processing events CALL METHOD g_grid->list_processing_events EXPORTING i_event_name = 'TOP_OF_PAGE' i_dyndoc_id = g_dyndoc_id.
Some data declaration help:
g_dyndoc_id TYPE REF TO cl_dd_document, "Object ref for document header
g_splitter TYPE REF TO cl_gui_splitter_container, "Object ref for splitter
g_parent_grid TYPE REF TO cl_gui_container, "Object ref for grid container
g_parent_top TYPE REF TO cl_gui_container, "Object ref for top container
g_html_cntrl TYPE REF TO cl_gui_html_viewer, "Object ref for html control
g_custom_container TYPE REF TO cl_gui_custom_container, "Object ref for custom container
i_fcat TYPE lvc_t_fcat. "Field catalog
YOU MAY LIKE THIS
ABAP Applications for the Cloud: Modernizing for the Future
Top SAP ABAP Reports Interview Questions: Be Prepared