02.working With Class Methods in OOABAP
02.working With Class Methods in OOABAP
+ -
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.
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
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.
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
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.
Step2: Program declerations.
REPORT ZSAPN_GET_MATERIAL_DETAILS.
DATA : LO_MATERIAL TYPE REF TO ZCL_SAPN_MATERIALS.
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
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 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
Program input.
Program output.
Learner Questions
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.
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.
REPORT ZSAPN_GET_MATERIALS_FOR_DATE.
START-OF-SELECTION.
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.