Create a Class method to get material details in SAP OOABAP

Creating a SAP class to get material master details for a material number using table MARA

+ -
Requirement: Create a class method to get material details for a material number. Requirement analysis: For the above requirement we need to create a class method to get details of a material for a material input, for this method the importing parameter is material no (to pass material input to method) and exporting parameter is of type MARA(work area one material no contains only one record in MARA).Go to SE24, provide class name as ZCL_SAPN_MATERIALS.

Creating SAP classes

Provide description, save.

Creating SAP classes

Save, save it in a local object, go to methods tab and declare a method.GET_MATERIAL_DETAILS-INSTANCE-PUBLIC-Get Material details

Creating SAP classes

Select parameters button and declare two parameters as belowIM_MATNR-IMPORTING- TYPE-MARA-MATNR-Material NumberEX_MARA-EXPORTING-TYPE-MARA-General Master Data

Creating SAP classes

Double click on CODE icon or Go back to methods and double click on method name and add below code.
*Select material data from mara table into exporting parameter ex_mara (work 

area) for a material no im_matnr
    SELECT SINGLE * FROM MARA
      INTO EX_MARA
      WHERE MATNR = IM_MATNR.

Creating SAP classes

Save, activate and execute the method( press F8).

Creating 

SAP classes

Click on execute.

Creating SAP classes

Provide a material no and execute.

Creating SAP classes

You will get material details for that material, now class with method is created we have to use it in our program.

Using Class method in SE38 Program

Go to SE38 and create a program ZSAPN_GET_MATERIAL_DETAILS and follow below steps to add code.

Step1: Declare class and create object.

DATA : LO_MATERIAL TYPE REF TO ZCL_SAPN_MATERIALS.
CREATE OBJECT LO_MATERIAL.

Step2: Program declerations.

PARAMETERS : P_MATNR TYPE MARA-MATNR.
DATA : WA_MARA TYPE MARA. "work area to store material details

Step3: Call class method and print out put.

  CALL METHOD LO_MATERIAL->GET_MATERIAL_DTAILS
    EXPORTING
      IM_MATNR = P_MATNR
    IMPORTING
      EX_MARA  = WA_MARA.
  WRITE :/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS.
Final code will be

REPORT ZSAPN_GET_MATERIAL_DETAILS.
DATA : LO_MATERIAL TYPE REF TO ZCL_SAPN_MATERIALS.

PARAMETERS : P_MATNR TYPE MARA-MATNR.
DATA : WA_MARA TYPE MARA.
CREATE OBJECT LO_MATERIAL.

START-OF-SELECTION.
  CALL METHOD LO_MATERIAL->GET_MATERIAL_DTAILS
    EXPORTING
      IM_MATNR = P_MATNR
    IMPORTING
      EX_MARA  = WA_MARA.
  WRITE :/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS.

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


    Lesson Navigation

    ←Previous Chapter
    Next Chapter →