Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Report SALV using OO Local Class

jandrivay
Participant
0 Kudos

Hi Expert,

im a begginer and I just learned to make reports using local class, but I am confused about how this class structure works.

I can't define

lo_display type ref to lcl_display.

your advice means a lot to me! Thank you

*&---------------------------------------------------------------------*
*& Report ZTEST_REPORT_OO_JANDRI
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ztest_report_oo_jandri.
"Type Declaration

CLASS lcl_report DEFINITION DEFERRED.
TYPES : BEGIN OF ty_ekpo,
          ebeln      TYPE ekpo-ebeln,
          ebelp      TYPE ekpo-ebelp,
          statu      TYPE ekpo-statu,
          aedat      TYPE ekpo-aedat,
          matnr      TYPE ekpo-matnr,
          menge      TYPE ekpo-menge,
          meins      TYPE ekpo-meins,
          netpr      TYPE ekpo-netpr,
          peinh      TYPE ekpo-peinh,
          line_color TYPE lvc_t_scol,
          flag(1),
        END OF ty_ekpo.

DATA : it_ekpo TYPE TABLE OF ty_ekpo,
       wa_ekpo TYPE ty_ekpo.
DATA : alv_table  TYPE REF TO cl_salv_table,
       alv_column TYPE REF TO cl_salv_columns_table.
DATA : lo_report TYPE REF TO lcl_report.
DATA : lo_display TYPE REF TO lcl_display.

PARAMETERS : p_ebeln TYPE ekpo-ebeln.


*CLASS lcl_display DEFINITION DEFERRED.


"Class Report
CLASS lcl_report DEFINITION.
  PUBLIC SECTION.
    METHODS: get_data.
*      IMPORTING im_ebeln TYPE ekpo
*      CHANGING  ch_itab  TYPE ty_ekpo.
    METHODS : display_data.
ENDCLASS.

"Class Display
CLASS lcl_display DEFINITION.

  PUBLIC SECTION.
    DATA : alv_table2    TYPE REF TO cl_salv_table,
           alv_column2   TYPE REF TO cl_salv_columns_table,
           single_column TYPE REF TO cl_salv_column,
           lo_aggrs      TYPE REF TO cl_salv_aggregations,
           lo_sort       TYPE REF TO cl_salv_sorts.

    METHODS : sort,
      total,
      build_layout,
      build_fieldcat,
      build_toolbar,
      report_settings,
      show_alv.
ENDCLASS.
CREATE OBJECT lo_report.

START-OF-SELECTION.
  lo_report->get_data( ).
  lo_report->display_data( ).
*&---------------------------------------------------------------------*
*& Class (Implementation) LCL_REPORT
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
CLASS lcl_report IMPLEMENTATION.
  METHOD get_data.
    SELECT ebeln ebelp statu aedat matnr menge meins netpr peinh
    FROM ekpo
    INTO CORRESPONDING FIELDS OF TABLE it_ekpo
    UP TO 50 ROWS
    WHERE ebeln EQ p_ebeln.
  ENDMETHOD.

  METHOD display_data.
    CALL METHOD lo_display->sort( ).
  ENDMETHOD.
ENDCLASS.
*&---------------------------------------------------------------------*
*& Class (Implementation) lcl_display
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
CLASS lcl_display IMPLEMENTATION.
  METHOD sort.
    lo_sort = alv_table->get_sorts( ).

    TRY.
        CALL METHOD lo_sort->add_sort
          EXPORTING
            columnname = 'MATNR'
*           POSITION   = '
*           SEQUENCE   = IF_SALV_C_SORT=>SORT_UP
            subtotal   = if_salv_c_bool_sap=>true.
*         GROUP      = IF_SALV_C_SORT=>GROUP_NONE
**         OBLIGATORY = IF_SALV_C_BOOL_SAP=>FALSE
*        .
      CATCH cx_salv_not_found .
      CATCH cx_salv_existing .
      CATCH cx_salv_data_error .
    ENDTRY.
  ENDMETHOD.

  METHOD total.
    lo_aggrs = alv_table->get_aggregations( ).
    TRY.
        CALL METHOD lo_aggrs->add_aggregation
          EXPORTING
            columnname  = 'NETPR'
            aggregation = if_salv_c_aggregation=>total.

      CATCH cx_salv_data_error .
      CATCH cx_salv_not_found .
      CATCH cx_salv_existing .
    ENDTRY.

  ENDMETHOD.

  METHOD build_layout.
    DATA : layout TYPE REF TO cl_salv_layout.
    DATA : layout_key TYPE salv_s_layout_key.

    layout = alv_table->get_layout( ).
    layout_key-report = sy-repid.
    layout->set_key( layout_key ).
    layout->set_save_restriction( if_salv_c_layout=>restrict_none ).
  ENDMETHOD.

  METHOD build_fieldcat.
    DATA : err_notfound TYPE REF TO cx_salv_not_found.
    TRY .
        single_column = alv_column->get_column( 'EBELN' ).
        single_column->set_short_text( 'P.Order' ).
        single_column->set_medium_text( 'Purchase Order' ).
        single_column->set_long_text( 'Purchase Order' ).
        single_column->set_output_length( '15' ).

        single_column = alv_column->get_column( 'MENGE' ).
        single_column->set_short_text( 'Qty' ).
        single_column->set_medium_text( 'Quantity' ).
        single_column->set_long_text( 'Quantity' ).
        single_column->set_output_length( '20' ).

        single_column = alv_column->get_column( 'NETPR' ).
        single_column->set_short_text( 'Net Price' ).
        single_column->set_medium_text( 'Net Price' ).
        single_column->set_long_text( 'Net Price' ).
        single_column->set_output_length( '20' ).
        single_column->set_currency( 'IDR' ).
      CATCH cx_salv_not_found INTO err_notfound.
    ENDTRY.
  ENDMETHOD.

  METHOD build_toolbar.
    DATA : toolbar_functions TYPE REF TO cl_salv_functions_list.
    toolbar_functions = alv_table->get_functions( ).
    toolbar_functions->set_all( ).
  ENDMETHOD.

  METHOD report_settings.
    DATA : report_settings TYPE REF TO cl_salv_display_settings.

    report_settings = alv_table->get_display_settings( ).
    report_settings->set_striped_pattern( if_salv_c_bool_sap=>true ).
    report_settings->set_list_header( 'Latihan SALV' ).
  ENDMETHOD.

  METHOD show_alv.
    DATA : err_message TYPE REF TO cx_salv_msg.
    TRY .
        cl_salv_table=>factory(
        IMPORTING
          r_salv_table = alv_table
        CHANGING
          t_table = it_ekpo ).

        alv_column = alv_table->get_columns( ).
        alv_column->set_optimize( 'X' ).
        alv_column->set_color_column( 'LINE_COLOR' ).

        alv_table->display( ).
      CATCH cx_salv_msg INTO err_message.

    ENDTRY.
  ENDMETHOD.
ENDCLASS.
1 ACCEPTED SOLUTION

joltdx
Active Contributor

Hi!

In this case, in that point in the report where you define lo display:

DATA : lo_display TYPE REF TO lcl_display.

The lcl_display class is not yet defined. It is unknown so it can not be used.

Using the style you have in this program, you should add a line with DEFINITION DEFERRED for this class as well:

CLASS lcl_report DEFINITION DEFERRED.
CLASS lcl_display DEFINITION DEFERRED.         " <-- This line is missing

Also, please take a look at the ABAP clean code guidelines online

3 REPLIES 3

joltdx
Active Contributor

Hi!

In this case, in that point in the report where you define lo display:

DATA : lo_display TYPE REF TO lcl_display.

The lcl_display class is not yet defined. It is unknown so it can not be used.

Using the style you have in this program, you should add a line with DEFINITION DEFERRED for this class as well:

CLASS lcl_report DEFINITION DEFERRED.
CLASS lcl_display DEFINITION DEFERRED.         " <-- This line is missing

Also, please take a look at the ABAP clean code guidelines online

Hi Jorgen,

Thankyou so much, my problem solved, im just try to learn about OO in report SALV.
your suggest is very helpful.

Sandra_Rossi
Active Contributor

Your question doesn't concern ALV, it's only a question about Object-Oriented Programming.

You don't need DEFERRED if you put everything in order. DEFERRED is only needed in special cases.

I recommend to have minimum code outside classes, avoid global variables, one line per program event is sufficient:

LOAD-OF-PROGRAM.
  DATA(global) = NEW lcl_global( ). " called once when the program starts
START-OF-SELECTION.
  global->start_of_selection( ).

All your class definitions should be defined at the top of the program, before DATA statements.

Dynpro programming requires global variables to exchange data, but I recommend that you organize them as static attributes of classes with CLASS-DATA. In your case, that concerns IT_EKPO only, the rest can be declared as instance attributes of classes with DATA).

All your types can be declared inside classes too.