Create OO ALV report using CL_SALV_TABLE

0
18124

Dear SAPLearners, in this blog post we will learn on how to create ABAP ALV report using CL_SALV_TABLE

CL_SALV_TABLE is factory ALV class used to build reports of type ALV.

Important and basic methods in this class are FACTORY ( ), DISPLAY ( ).

In this tutorial we will report the data of table SFLIGHT in ALV.

1

Create a program in SE38 and copy the below code. Below program is written object oriented way, you can also write the same in normal abap report program.

REPORT  zsl_demo_oop_alv.
*----------------------------------------------------------------------*
*       ALV using class CL_SALV_TABLE(Factory Class)                   *
*----------------------------------------------------------------------*
*                      www.saplearners.com
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
*       CLASS lcl_sflight DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_sflight DEFINITION.

  PUBLIC SECTION.
    TYPES: BEGIN OF lty_sflight,
            carrid      TYPE s_carr_id,
            connid      TYPE s_conn_id,
            fldate      TYPE s_date,
            price       TYPE s_price,
            currency    TYPE s_currcode,
            planetype   TYPE s_planetye,
            seatsmax    TYPE s_seatsmax,
            seatsocc    TYPE s_seatsocc,
          END OF lty_sflight.

    METHODS: get_sflight_data,
             get_alv_instance,
             display.

    DATA: lo_alv      TYPE REF TO cl_salv_table,
          gt_sflight  TYPE STANDARD TABLE OF lty_sflight.

ENDCLASS.                    "lcl_sflight DEFINITION

*----------------------------------------------------------------------*
*       CLASS lcl_sflight IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_sflight IMPLEMENTATION.

* Get SFLIGHT data
  METHOD get_sflight_data.
    SELECT carrid
           connid
           fldate
           price
           currency
           planetype
           seatsmax
           seatsocc
           INTO TABLE me->gt_sflight
           FROM sflight.
  ENDMETHOD.                    "get_sflight_data

* Get ALV instance
  METHOD get_alv_instance.
    TRY.
        CALL METHOD cl_salv_table=>factory
*      exporting
*        list_display   = IF_SALV_C_BOOL_SAP=>FALSE
*        r_container    =
*        container_name =
        IMPORTING
        r_salv_table   = lo_alv
        CHANGING
        t_table        = gt_sflight.
      CATCH cx_salv_msg.
    ENDTRY.
  ENDMETHOD.                    "get_alv_instance

* Display ALV
  METHOD display.
    CALL METHOD lo_alv->display.
  ENDMETHOD.                    "display
ENDCLASS.                    "lcl_sflight IMPLEMENTATION


START-OF-SELECTION.
  DATA lo_cl_sflight TYPE REF TO lcl_sflight.

  CREATE OBJECT lo_cl_sflight.
  lo_cl_sflight->get_sflight_data( ).
  lo_cl_sflight->get_alv_instance( ).
  lo_cl_sflight->display( ).

This is basic Object oriented ABAP ALV report using CL_SALV_TABLE.

As you can see the standard ALV toolbar is missing here, to enable that please click here see my next tutorial.

Please feel free to comment and let us know your feedback. Subscribe for more updates

If you liked it, please share it! Thanks!