Interactive ALV report using ALV IDA

Scenario: Create an interactive ALV report which will display Customer data, Sales data data upon double clicking customer number in Customer data ALV report.

Approach:
    
I have initially created simple CDS views of KNA1 and VBAK tables.
@AbapCatalog.sqlViewName: 'ZCDSVRKNA1'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'demo view kna1'
@Metadata.ignorePropagatedAnnotations: true
define view ZCDS_VR_KNA1 as select from kna1
{
    key kunnr as CUSTOMER_NO,
    land1 as COUNTRY,
    name1 as NAME,
    ort01 as CITY,
    erdat as CREATED_ON,
    ernam as CREATED_BY,
    lifnr as SUPPLIER
}

@AbapCatalog.sqlViewName: 'ZCDSVRVBAK'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'demo view vbak'
@Metadata.ignorePropagatedAnnotations: true
define view ZCDS_VR_VBAK as select from vbak
{
    key vbeln as SALES_DOC,
    erdat as CREATED_ON,
    ernam as PERSON_RESPONSIBLE,
    vbtyp as SALES_DOC_CATEGORY,
    netwr as NET_VALUE,
    waerk as CURRENCY,
    kunnr as CUSTOMER
}


I have created an event handler class to handle 'Double click event'. Logic to display VBAK data upon double clicking on Customer number is written in the method. The event handler is registered before display KKNA1 data. Below is the code implementation of the report.


*&---------------------------------------------------------------------*
*& Report zdemo_alv_ida_vr
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zdemo_alv_ida_vr.

DATA: lv_kunnr TYPE kna1-kunnr.

CLASS lcl_handler DEFINITION.
  PUBLIC SECTION.
    METHODS: handle_dc FOR EVENT double_click OF if_salv_gui_table_display_opt
      IMPORTING ev_field_name eo_row_data.
ENDCLASS.

CLASS lcl_handler IMPLEMENTATION.
  METHOD handle_dc.
    TYPES: BEGIN OF ty_kna1,
             customer_no TYPE kna1-kunnr,
             country     TYPE kna1-land1,
             name        TYPE kna1-name1,
             city        TYPE kna1-land1,
             created_on  TYPE kna1-erdat,
             created_by  TYPE kna1-ernam,
             supplier    TYPE kna1-lifnr,
           END OF ty_kna1.
    DATA: lwa_kna1 TYPE ty_kna1.

    CASE ev_field_name.
      WHEN 'CUSTOMER_NO'.
        "Get selected row data
        TRY.
            eo_row_data->get_row_data(
              IMPORTING
                es_row               = lwa_kna1
            ).
          CATCH cx_salv_ida_contract_violation INTO DATA(lo_icv1).
            MESSAGE |{ lo_icv1->get_longtext(  ) }| TYPE 'I'.
          CATCH cx_salv_ida_sel_row_deleted INTO DATA(lo_srd).
            MESSAGE |{ lo_srd->get_longtext(  ) }| TYPE 'I'.
        ENDTRY.

        IF lwa_kna1 IS NOT INITIAL.
          "Create table object for VBAK CDS View
          DATA(lo_table1) = cl_salv_gui_table_ida=>create_for_cds_view(
                   iv_cds_view_name      = 'ZCDS_VR_VBAK'  ).

          IF lo_table1 IS BOUND.
            "generate condition object
            TRY.
                DATA(lo_condition) = lo_table1->condition_factory( )->equals(
                  EXPORTING
                    name         = 'CUSTOMER'
                    value        = lwa_kna1-customer_no

                ).
              CATCH cx_salv_ida_unknown_name INTO DATA(lo_iun1).
                MESSAGE |{ lo_iun1->get_longtext(  ) }| TYPE 'I'.
            ENDTRY.

            "pass condition object to alv ida table
            TRY.
                lo_table1->set_select_options(
                  io_condition = lo_condition
                ).
              CATCH cx_salv_ida_associate_invalid INTO DATA(lo_iai1).
                MESSAGE |{ lo_iai1->get_longtext( ) }| TYPE 'I'.
              CATCH cx_salv_db_connection INTO DATA(lo_dc1).
                MESSAGE |{ lo_dc1->get_longtext( ) }| TYPE 'I'.
              CATCH cx_salv_ida_condition_invalid INTO DATA(lo_ici1).
                MESSAGE |{ lo_ici1->get_longtext(  ) }| TYPE 'I'.
              CATCH cx_salv_ida_unknown_name INTO lo_iun1.
                MESSAGE |{ lo_iun1->get_longtext(  ) }| TYPE 'I'.
            ENDTRY.

            "set display options
            lo_table1->display_options( )->enable_alternating_row_pattern( ).
            lo_table1->display_options( )->set_title( iv_title = 'Sales data' ).

            "display data
            TRY.
                lo_table1->fullscreen( )->display( ).
              CATCH cx_salv_ida_contract_violation INTO lo_icv1.
                MESSAGE |{ lo_icv1->get_longtext(  ) }| TYPE 'I'.
            ENDTRY.

          ENDIF.
        ENDIF.
      WHEN OTHERS.
        MESSAGE |You can only double click customer number| TYPE 'I'.
    ENDCASE.
  ENDMETHOD.
ENDCLASS.

"Selection screen
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE t1.
  SELECT-OPTIONS: s_kunnr FOR lv_kunnr.
SELECTION-SCREEN END OF BLOCK b1.

INITIALIZATION.
  t1 = 'Enter Customer details'.

START-OF-SELECTION.
  "Create a table object for KNA1 CDS view
  DATA(lo_table) = cl_salv_gui_table_ida=>create_for_cds_view(
                     iv_cds_view_name      = 'ZCDS_VR_KNA1'  ).

  IF lo_table IS BOUND.

    "create a range table for ida
    DATA(lo_range) = NEW cl_salv_range_tab_collector( ).
    lo_range->add_ranges_for_name(
      iv_name   = 'CUSTOMER_NO'
      it_ranges = s_kunnr[]
    ).

    lo_range->get_collected_ranges(
      IMPORTING
        et_named_ranges = DATA(lt_range)
    ).

    "pass the range table to ida
    TRY.
        lo_table->set_select_options(
          it_ranges    = lt_range ).
      CATCH cx_salv_ida_associate_invalid INTO DATA(lo_iai).
        MESSAGE |{ lo_iai->get_longtext( ) }| TYPE 'I'.
      CATCH cx_salv_db_connection INTO DATA(lo_dc).
        MESSAGE |{ lo_dc->get_longtext( ) }| TYPE 'I'.
      CATCH cx_salv_ida_condition_invalid INTO DATA(lo_ici).
        MESSAGE |{ lo_ici->get_longtext(  ) }| TYPE 'I'.
      CATCH cx_salv_ida_unknown_name INTO DATA(lo_iun).
        MESSAGE |{ lo_iun->get_longtext(  ) }| TYPE 'I'.
    ENDTRY.

    "set display options
    lo_table->display_options( )->enable_alternating_row_pattern( ).
    lo_table->display_options( )->set_title( iv_title = 'Customer Data' ).
    "enable double click
    lo_table->display_options( )->enable_double_click( ).

    "Created event handler object and register the handler
    DATA(lo_handler) = NEW lcl_Handler( ).
    SET HANDLER lo_handler->handle_dc FOR ALL INSTANCES.

    "display data
    TRY.
        lo_table->fullscreen( )->display( ).
      CATCH cx_salv_ida_contract_violation INTO DATA(lo_icv).
        MESSAGE |{ lo_icv->get_longtext(  ) }| TYPE 'I'.
    ENDTRY.

  ENDIF.

Execution of the report:

Selection screen:


KNA1 report:


Double clicking on any other field:


Double click on Customer No:


Comments

Popular posts from this blog

Simple AMDP program for CRUD opertaions

Select options in AMDP