Local class with tables in SAP OOABAP

Using tables in SAP ABAP local classes, se11 table types in SAP Local classes

+ -
The bwlow example explains you of using table in SAP local classes using SAP ABAP programming language.

Declare a table type

Create a table type in SE11(in this example we use ZSAPN_MARA which we have created already).

Define class and method

CLASS CL_USERDEFINED_TYPES DEFINITION.
  PUBLIC SECTION.
    METHODS : GET_MATERIALS_FOR_TYPE
              IMPORTING IM_MTART TYPE MARA-MTART
              EXPORTING ET_MARA TYPE ZSAPN_MARA. "table type in SE11
ENDCLASS.

Implement the defined class

CLASS CL_USERDEFINED_TYPES IMPLEMENTATION.
  METHOD GET_MATERIALS_FOR_TYPE.
    SELECT * FROM MARA
           INTO TABLE ET_MARA
           WHERE MTART = IM_MTART .
  ENDMETHOD.
ENDCLASS.

Use the class and method

Create an object for the class and call the method
DATA : IT_MARA TYPE TABLE OF MARA.
DATA : WA_MARA TYPE MARA.
CREATE OBJECT LO_MATERIAL. "CREATE OBJECT
CALL METHOD LO_MATERIAL->GET_MATERIALS_FOR_TYPE
  EXPORTING
    IM_MTART = P_MTART
  IMPORTING
    ET_MARA  = IT_MARA.

*PRINT OUTPUT
LOOP AT IT_MARA INTO WA_MARA.
  WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL.
ENDLOOP.

The final code will be


REPORT ZSAPN_LOCAL_CLASS_METHODS.
CLASS CL_USERDEFINED_TYPES DEFINITION DEFERRED.

PARAMETERS : P_MTART TYPE MARA-MTART.
DATA : LO_MATERIAL TYPE REF TO CL_USERDEFINED_TYPES. "DECLARE CLASS
DATA : IT_MARA TYPE TABLE OF MARA.
DATA : WA_MARA TYPE MARA.
CLASS CL_USERDEFINED_TYPES DEFINITION.
  PUBLIC SECTION.
    METHODS : GET_MATERIALS_FOR_TYPE
              IMPORTING IM_MTART TYPE MARA-MTART
              EXPORTING ET_MARA TYPE ZSAPN_MARA.
ENDCLASS.
CREATE OBJECT LO_MATERIAL. "CREATE OBJECT
CALL METHOD LO_MATERIAL->GET_MATERIALS_FOR_TYPE
  EXPORTING
    IM_MTART = P_MTART
  IMPORTING
    ET_MARA  = IT_MARA.

*PRINT OUTPUT
LOOP AT IT_MARA INTO WA_MARA.
  WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL.
ENDLOOP.
CLASS CL_USERDEFINED_TYPES IMPLEMENTATION.
  METHOD GET_MATERIALS_FOR_TYPE.
    SELECT * FROM MARA
           INTO TABLE ET_MARA
           WHERE MTART = IM_MTART .
  ENDMETHOD.
ENDCLASS.

Learner Questions


No Questions by learners, be first one to ask ..!!

    Please Sign in to ask a question

    Was this lesson helpful to you? Yes No 15 People out of 16 think this lesson helpful


    Lesson Navigation

    ←Previous Chapter
    Next Chapter →