User-defined types in SAP Classes

How to use user-defined types in SAP Classes and it's methods ? Working with user-defined types in Object Oriented ABAP

+ -
Requirement: Develop a report to display material number(MATNR), material type(mtart), material group(MAKTL) and unit of measure(MEINS) for a given date( Created Date). Requirement analysis: For this requirement we need to get limited fields (MATNR, MTART, MATKL, MEINS, ERSDA from MARA (material Master) table for the specified date(ERSDA-Created date), materials for the given date may be more than one so we need to use tables and we need to get limited fields(columns) only (no need to get all fields data from MARA), for this one we need to use concept of user defined table in SAP class method.

Learners directly coming for this lesson, please refer previous lesson creating class method to display materials as we are adding one more method for the same class.

Go to SE24, provide class name as ZCL_SAPN_MATERIALS( we previously created this class see previous lesson) and click change.

Using user defined types in SAP classes

Go to methods tab and add a method with name GET_MATERIALS_FOR_DATE-INSTANCE- PUBLIC-Get materials for a date.

Using user defined types in SAP classes

We can declare user-defined types under types tab, go to types tab.

Using user defined types in SAP classes

Click on Direct Type entry icon, save, it will take you to a editor
Don' change any thing, just replace types TY_MARA . with the below code.

Using user defined types in SAP classes

TYPES: BEGIN OF TY_MARA,
        MATNR TYPE MARA-MATNR,
        ERSDA TYPE MARA-ERSDA,
        MTART TYPE MARA-MTART,
        MATKL TYPE MARA-MATKL,
        MEINS TYPE MARA-MEINS,
      END OF TY_MARA.

TYPES : TT_MARA TYPE TABLE OF TY_MARA.

Using user defined types in SAP classes

Save, go back to methods, put mouse cursor on GET_MATERIAL_FOR_DATE method, select parameters button and add below parameters.

Using user defined types in SAP classes

Go back to methods and double click on method GET_MATERIAL_FOR_DATE, write the below code.

Using user defined types in SAP classes

METHOD GET_MATERIAL_FOR_DATE.
*Get material no, created date, material type, material group, units of measue
*from MARA table
  SELECT MATNR ERSDA MTART MATKL MEINS
    FROM MARA INTO TABLE ET_MARA.
ENDMETHOD.
Save and activate.

Go to SE38 and create a program and add below code.

REPORT  ZSAPN_GET_MATERIALS_FOR_DATE.

TYPES: BEGIN OF TY_MARA,
      MATNR TYPE MARA-MATNR,
      ERSDA TYPE MARA-ERSDA,
      MTART TYPE MARA-MTART,
      MATKL TYPE MARA-MATKL,
      MEINS TYPE MARA-MEINS,
    END OF TY_MARA .
DATA : IT_MARA TYPE TABLE OF TY_MARA.
DATA : WA_MARA TYPE TY_MARA.

DATA : LO_MATERIAL TYPE REF TO ZCL_SAPN_MATERIALS.
PARAMETERS : P_DATE TYPE MARA-ERSDA.
CREATE OBJECT LO_MATERIAL.

START-OF-SELECTION.

  CALL METHOD LO_MATERIAL->GET_MATERIAL_FOR_DATE
    EXPORTING
      IM_DATE = P_DATE
    IMPORTING
      ET_MARA = IT_MARA.

  LOOP AT IT_MARA INTO WA_MARA.
    WRITE:/ WA_MARA-MATNR, WA_MARA-ERSDA, WA_MARA-MTART, WA_MARA-MATKL, WA_MARA-MEINS.
  ENDLOOP.
Activate the program and test.

Please Sign in to ask a question

Was this lesson helpful to you? Yes No 64 People out of 69 think this lesson helpful


Lesson Navigation

←Previous Chapter
Next Lesson → Events in Object Oriented ABAP