0% found this document useful (0 votes)
64 views

02.working With Class Methods in OOABAP

This document discusses several topics related to using methods in SAP classes: 1. It describes how to create a class method to retrieve material master details for a given material number by selecting data from the MARA table. 2. It explains how to use table types in class methods, demonstrating a method that retrieves material details for a specified material type into an internal table. 3. It covers using user-defined types in class methods, showing an example method that retrieves selected fields for materials matching a given creation date into a user-defined table type.

Uploaded by

Shashank Yerra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

02.working With Class Methods in OOABAP

This document discusses several topics related to using methods in SAP classes: 1. It describes how to create a class method to retrieve material master details for a given material number by selecting data from the MARA table. 2. It explains how to use table types in class methods, demonstrating a method that retrieves material details for a specified material type into an internal table. 3. It covers using user-defined types in class methods, showing an example method that retrieves selected fields for materials matching a given creation date into a user-defined table type.

Uploaded by

Shashank Yerra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

# Content

1 Class methods and Creating methods in SAP classes

2 Class method to get material details in SAP OOABAP

3 Using table types in methods in SAP classes

4 User-defined types in SAP Classes

Class methods and Creating


methods in SAP classes
Last Updated: November 6th 2013 by Ashok Kumar Reddy

Creating a method in SAP Classes using Object Oriented ABAP programming

+ -
What is a method in a Class ?

Methods are coding blocks of a class, which can provide some business functionality (ex: read
material data etc), these methods are similar to Function Modules.

Methods can access all attributes of a class (defined under attributes tab), can access user defined
types ( declared under types tab).

The methods can be called using key word CALL METHOD in SAP ABAP programs.

Uses of methods in SAP classes?

These methods can be reusable in multiple ABAP programs, a class may contain more than one
method.

We will define methods under methods tab of a class, to define a method provide a method name,
level( instance or static), visibility (Public,Private,Protected) and method description.
The importing and exporting parameters of a method will defined under parameters area.

Add a method, click on parameter button and you will go to parameter interface where you can add
parameters to a particular method. (Parameters are method specific).

To write source code, click on methods tab and double click on method name (METHOD in the
above example), you will go to source code editor where you can add code.
This is how we create methods in SAP classes, we will learn some advanced concepts of using
methods in next lessons.

Learner Questions

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

Class method to get material


details in SAP OOABAP
Last Updated: November 6th 2013 by Ashok Kumar Reddy

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.
Provide description, save.

Save, save it in a local object, go to methods tab and declare a method.GET_MATERIAL_DETAILS-


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

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.
Save, activate and execute the method( press F8).

Click on execute.
Provide a material no and execute.

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 ..!!


Using table types in methods in
SAP classes
Last Updated: November 6th 2013 by Ashok Kumar Reddy

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.

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


Provide short text, provide line type as MARA.

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.
Go to methods tab and add a method with name GET_MATERIALS_FOR_TYPE-INSTANCE-
PUBLIC-Get materials for material type.

Click on parameters button and add parameters as below.


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

*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.

Program output.
Learner Questions

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

User-defined types in SAP Classes


Last Updated: April 4th 2015 by Ashok Kumar Reddy

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.

Go to methods tab and add a method with name GET_MATERIALS_FOR_DATE-INSTANCE-


PUBLIC-Get materials for a date.

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


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.

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.

Save, go back to methods, put mouse cursor on GET_MATERIAL_FOR_DATE method, select


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

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.


Learner Questions

 The scenario is to write out the specified fields for the entered date. For the date entered, it is
displaying all the fields. Foe example- for the entered date if there is only one record, it
should be displaying only that record, whereas here it is displaying all the records
irrespective of date. Hope u got my point.

You might also like