Using Constructor method in SAP OOABAP

Using CONSTRUCTOR method in SAP Classes to set global default value for the call

+ -
This CONSTRUCTOR method is very useful in setting default value in a class, the below is the example of using CONSTRUCTOR method in a SAP Class.Requirement: Display for material description for a material, and depends upon language selected.

All material descriptions are stored in MAKT table, for a material there may be different descriptions example for English one description, for German one description etc, we need to get description based on the language, for this one we create a class method for re-usablity(the same method can be used in different programs to get descriptions based on the selected languages).

Go to SE24,provide class ZCL_SAPN_MATERIAL and click on change.

Using Constructor method in SAP Classes

Go to Methods and provide method name as CONSTRUCTOR, enter

Using Constructor method in SAP Classes

Once you press enter, you will see a symbol (Constructor symbol), it indicates that the method is a constructor method.

Using Constructor method in SAP Classes

Now put cursor on method name and click on parameters button.

Using Constructor method in SAP Classes

Click on parameter button and add below parameter( what ever the parameters you add, they will become importing parameters only).

Using Constructor method in SAP Classes

Select attributes tab and add below attribute.

Using Constructor method in SAP Classes

Go to methods and double click on CONSTRUCTOR method and add below code.

Using Constructor method in SAP Classes

Save and activate, go to methods tab and add one more method GET_MATERIAL_DESCRIPTIONS to get material descriptions.

Using Constructor method in SAP Classes

Select parameters button and add below parameters.

Using Constructor method in SAP Classes

Save, go back to methods and double click on method name GET_MATERIAL_DESCRIPTIONS, add below code.

Using Constructor method in SAP Classes

  METHOD GET_MATERIAL_DESCRIPTIONS.

    SELECT * FROM MAKT INTO EX_MAKT
      WHERE MATNR = IM_MATNR
          AND SPRAS = LANGUAGE. "LANGUAGE IS THE ATTRIBUTE DEFINED IN METHOD
    ENDSELECT.
    
  ENDMETHOD.

Using CONSTRUCTOR method with ABAP program

Go to SE38 and create a program ZSAPN_MATERIAL_DESCRIPTION and follow below steps. 
Step1: Data declarations
DATA : LO_MATERIAL TYPE REF TO ZCL_SAPN_MATERIALS. "Declare class
DATA : WA_MAKT TYPE MAKT. "declare work area for makt
PARAMETERS P_MATNR TYPE MARA-MATNR. "material input

Step2: Create object for material class, the object can be created using ABAP patterns also.

To create object, click on pattern button.

Using Constructor method in SAP Classes

A popup will open, select ABAP object patterns and enter.

Using Constructor method in SAP Classes

One more pop up will come, select create object, provide instance name (We declared at the declerations step LO_MATERIAL ), provide class name (our class name is ZCL_SALN_MATERIALS) and enter.

Using Constructor method in SAP Classes

Now the CONSTRUCTOR method is triggered, it will ask for a parameter IM_SPRAS (Which is importing parameter of constructor method).It will set language key as English.

Finally call the method and get material descriptions.

The final code will be .
REPORT ZSAPN_GET_MATERIAL_DESCRIPTION.
DATA : LO_MATERIAL TYPE REF TO ZCL_SAPN_MATERIALS.
DATA : WA_MAKT TYPE MAKT.
PARAMETERS P_MATNR TYPE MARA-MATNR.
CREATE OBJECT LO_MATERIAL
  EXPORTING
    IM_SPRAS = 'E'.

START-OF-SELECTION.
  CALL METHOD LO_MATERIAL->GET_MATERIAL_DESCRIPTIONS
    EXPORTING
      IM_MATNR = P_MATNR
    IMPORTING
      EX_MAKT  = WA_MAKT.

  WRITE : WA_MAKT-MATNR, WA_MAKT-MAKTX.

Testing the above program

Go to MAKT table and get a material number with multiple language descriptions.

Constructor method in SAP Classes

Execute the program, provide the material no, you will get out put where language = 'E' only (Constructor method handles this).

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


    Lesson Navigation

    ←Previous Chapter
    Next Chapter →