Using table types in methods in SAP classes

Using table types in SAP class methods, use of table types in SAP classes

+ -
Requirement: Develop a report to display list of materials for a material type, use Object Oriented ABAP to get materials. Requirement analysis: For this requirement we need to get materials from MARA(material Master) table for the specified material type, a material type may have more than one materials, so we have to get materials into an internal table.In SAP classes we don`t have options to directly specify tables, for this one we need to create table type in SE11(Data Dictionary).
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.
Before creating method to get material details for a material type, we need to create a table type in SE11 .

Go to SE11, select data type radio, provide name as ZSAPN_MARA, create.

Using types in SAP class methods

A popup will open, select table type radio, enter.

Using types in SAP class methods

Provide short text, provide line type as MARA.

Using types in SAP class methods

Save and Activate.

Now table type is created for MARA, to create a method to get material details for a material type, go to SE24, provide class name as ZCL_SAPN_MATERIALS( we previously created this class see previous lesson) and click change.

Using types in SAP class methods

Go to methods tab and add a method with name GET_MATERIALS_FOR_TYPE-INSTANCE-PUBLIC-Get materials for material type.

Using types in SAP class methods

Click on parameters button and add parameters as below.

Using types in SAP class methods

Save, go back to methods and double click on GET_MATERIALS_FOR_TYPE and add the below code.

Using types in SAP class methods

*Get data from mara for a material type MTART
    SELECT * FROM MARA
      INTO TABLE ET_MARA
      up to 50 rows     
      WHERE MTART = IM_MTART.
*For testing purpose im getting first 50 rows for a material type
Save, activate and test the method.

Using class method in SE38 program to get Material details for a material type.

Go to SE38 and create a program ZSAPN_GET_MATERIALS_FOR_TYPE and add below code.
REPORT ZSAPN_GET_MATERIALS_FOR_TYPE.
DATA : IT_MARA TYPE TABLE OF MARA. "internal table to store materials
DATA : WA_MARA TYPE MARA. "work area for materials to loop
DATA : LO_MATERIALS TYPE REF TO ZCL_SAPN_MATERIALS. "declare materials class

PARAMETERS : P_MTART TYPE MARA-MTART. "material type input

CREATE OBJECT LO_MATERIALS. "Create object for material class

START-OF-SELECTION.
  CALL METHOD LO_MATERIALS->GET_MATERIALS_FOR_TYPE "call method to get materials
    EXPORTING
      IM_MTART = P_MTART
    IMPORTING
      ET_MARA  = IT_MARA.

  LOOP AT IT_MARA INTO WA_MARA.
    WRITE :/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS.
  ENDLOOP.
Program input.

Using types in SAP class methods

Program output.

Using types in SAP class methods

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 75 People out of 75 think this lesson helpful


    Lesson Navigation

    ←Previous Chapter
    Next Chapter →