Interactive ALV Report Using Object Oriented Programming

Interactive ALV Report Using Object Oriented Programming

To create an interactive ALV report using Object oriented approach we must create events and event handler method to handle user actions on ALV report

Events class: CL_SALV_EVENTS_TABLE

The process is as follows:

If a user performs link click action on ALV report.

1) Capture the event on ALV using GET_EVENT method.

2) Create and Implement event handler method for link click of class CL_SALV_EVENTS_TABLE. This involves reading the selected line and using the data to call another transaction.

3) Set the even handler method.

4) Set the screen status

5) Display the report.

The below code displays ALV report of BKPF data and when any line is selected it will display Accounting Data by calling the FB03 transaction.


REPORT ztest_alv.

TYPES: BEGIN OF ty_bkpf,
         bukrs TYPE bukrs,
         belnr TYPE belnr_d,
         gjahr TYPE gjahr,
         xblnr TYPE xblnr1,
       END OF ty_bkpf.

DATA: lt_bkpf TYPE TABLE OF ty_bkpf.

DATA: lo_alv     TYPE REF TO cl_salv_table,
      lo_columns TYPE REF TO cl_salv_columns_table,
      lo_column  TYPE REF TO cl_salv_column_table.

CLASS lcl_events DEFINITION DEFERRED.

DATA: gr_events TYPE REF TO lcl_events.

CLASS lcl_events DEFINITION.
  PUBLIC SECTION.
  METHODS on_click FOR EVENT link_click OF cl_salv_events_table
  IMPORTING row column.
ENDCLASS.

START-OF-SELECTION.

  SELECT bukrs belnr gjahr xblnr
    FROM bkpf
    INTO TABLE lt_bkpf
    UP TO 10 ROWS.
  IF sy-subrc IS INITIAL.

*1. GET ALV INSTANCE.
    cl_salv_table=>factory(
      IMPORTING
        r_salv_table   = lo_alv
      CHANGING
        t_table        = lt_bkpf[]
    ).

*2. GET COLUMNS FROM ALV INSTANCE
    lo_columns = lo_alv->get_columns( ).

*3. GET BUKRS COLUMN USING FIELD NAME FROM COLUMNS
    lo_column ?= lo_columns->get_column( columnname = 'BUKRS' ).

*4. SET THE HOTSPOT FOR BUKRS COLUMN
    lo_column->set_cell_type(
        value = if_salv_c_cell_type=>hotspot
    ).

*5. REGISTER EVENTS FOR ACTIONS
    DATA: lo_events TYPE REF TO cl_salv_events_table.

    lo_events = lo_alv->get_event( ).

    CREATE OBJECT gr_events.

    SET HANDLER gr_events->on_click FOR lo_events.

    lo_alv->set_screen_status(
      EXPORTING
        report        = 'SALV_TEST_FUNCTIONS'   
        pfstatus      = 'SALV_STANDARD'         
        set_functions = lo_alv->c_functions_all 
      ).

*5. DIPLAY ALV OUTPUT
    lo_alv->display( ).

  ENDIF.

*&---------------------------------------------------------------------*
*& Class (Implementation) lcl_events
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
CLASS lcl_events IMPLEMENTATION.
  METHOD on_click.

    READ TABLE lt_bkpf INTO DATA(ls_bkpf) INDEX row.
    IF sy-subrc IS INITIAL.
      SET PARAMETER ID 'BLN' FIELD ls_bkpf-belnr.
      SET PARAMETER ID 'BUK' FIELD ls_bkpf-bukrs.
      SET PARAMETER ID 'GJR' FIELD ls_bkpf-gjahr.

      CALL TRANSACTION 'FB03' AND SKIP FIRST SCREEN.
    ENDIF.
  ENDMETHOD.
ENDCLASS.

Output:

When you click on hotspot in first line the below screen will be displayed.



1 Comments

  1. Great article! well organized and described. Thank you!

    ReplyDelete
Previous Post Next Post