Abstract Factory ALV OOABAP
Abstract Factory ALV OOABAP
The main feature of Object Oriented programming is representing real-time objects in the
form of class objects.
Object Oriented ABAP focus on representing real-time objects of classes.
SAP ABAP Object Oriented programming is available in two flavors .
One is Global Classes and another one is local class.
Global Class
Global Class is an ABAP object which can be accessible via SAP Class Builder, T-code for SAP
Class Builder is SE24.
Local Class
Local classes are classes which are available in ABAP programs, we can access them via ABAP
editor SE38.
ABAP Classes and It`s components
What is a Class ? A class is a user defined data type with attributes, methods, events, user-defined
types, interfaces etc for a particular entity or business application .
What are Objects ? Objects are nothing but instances of classes, each object has a unique identity
that is memory and it`s own attributes.
Syntax: DATA TYPE REF TO . "Declaring global class in ABAP program using
type ref to(type reference to )
CREATE OBJECT . "Create object for the declared instance
Components of a Class
Attributes: Attributes are variables, constants declared within a class .
Methods: Methods are coding blocks which provides some functionality .
Events: Event is a mechanism through which one method of a class can raise method of other
class, without hazard of instantiating that class.
Interfaces: Interfaces are similar to classes which contain methods Without any implementation.
Interfaces are mainly used to extend the scope or functionality of the class.
Instance and Static Components
Instance components : These components exist separately in each instance (object) of the class
and are referred using instance component selector using ->
Static components : These components exists globally for a class and are referred to using static
component selector => .
Static attributes define a common state of a class that is shared by all the instances of the class.
That is, if you change a static attribute in one object of a class, the change is visible to all
other objects of the class as well.
Visibility of Components of Class
Each class component has a visibility.
In ABAP Objects, the whole class definition is separated into three visibility sections:
PUBLIC .
PROTECTED .
PRIVATE.
Public section: Data declared in public section can be accessed by the class itself, by its
subclasses as well as by other users outside the class.
Protected section: Data declared in the protected section can be accessed by the class itself, and
also by its subclasses but not by external users outside the class.
Private Section: Data declared in the private section can be accessed by the class only, but not by
its subclasses and by external users outside the class.
Global Class and Local Class
Classes in ABAP Objects can be declared either globally or locally.
Global Class: Global classes and interfaces are defined in the Class Builder (Transaction SE24) in
the ABAP Workbench.
All of the ABAP programs in an R/3 System can access the global classes.
Local Class: Local classes are define in an ABAP program (Transaction SE38) and can only be
used in the program in which they are defined.
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.
Working with events and event handler methods in SAP classes in SAP ABAP
programming model
+-
What are events in SAP Classes?
Event is a mechanism by which method of one class can raise method of another class, without the
hazard of instantiating that class.
Follow the below steps to add a event
Define an event .
Define a method.
Link event and method and convert the method into event-handler method.
Create a triggering method which will raise the event.
Use set handler and register event handler method to a particular instance in the program.
SET HANDLER for . "here instance is the object created by using create
object
CONSTRUCTOR.
These methods can only have importing parameters, there will not be any exporting parameters.
The name of the CONSTRUCTOR method must be CONSTRUCTOR only.
CLASS CONSTRUCTOR (Also called as STATIC CONSTRUCTOR).
It is a type of constructor, this method will be executed automatically whenever a first call to the
class is made, the first call may be through instance or through class.
These CLASS CONSTRUCTORS are mainly used to set the default values globally i:e irrespective
of instances, these methods will not have any importing and exporting parameters.These methods
will be executed only once.
Name of the CLASS CONSTRUCTOR must be CLASS_CONSTRUCTOR.
Interfaces in SAP Classes, using interface concept with SAP Object Oriented
ABAP programming.
+-
Before going to interfaces, we must know about Polymorphism .
Interfaces are independent structure which are used in a class to extend the functionality of a
class.
Interfaces contains methods without any implementation. Where as a class contains
methods with implementation.
We need to define an interface with the required method names in SE24 TCODE.
The interfaces can be used by no of classes to extend the functionality of the class.
To implement an interface in a class just give the name of interface under the 'Interfaces' tab.
All the interface methods will be automatically copied to the classes in a particular class
without effecting the other classes.
The main use of interfaces is re-usability and maintain standard project framework.
In the above diagram, application is business application, Class A, Class B, Class C, Class D and
Class E are independent classes, all these classes are using one Interface called Interface( in
diagram).
In all the classes,the methods names are same but the implementation will be different from one
class to another class thereby achieving the concept called POLYMORPHISM. Polymorphism is
implemented in the from of interface.
*CLASS DEFINITION
CLASS DEFINITION. "THIS IS CLASS DEFINITION
ENDCLASS.
*CLASS IMPLEMENTATION
CLASS IMPLEMENTATION.
*METHODS, EVENTS IMPLEMENTATION
ENDCLASS.
ENDCLASS.
Local class with methods in SAP ABAP, defining methods in local classes,
implementing methods in SAP local classes
+-
Define a class
PUBLIC SECTION.
METHODS : GET_MATERAIAL_DETAILS
IMPORTING IM_MATNR TYPE MATNR
EXPORTING EX_MARA TYPE MARA.
CLASS-METHODS : GET_MATERIAL_DESCRIPTION
IMPORTING IM_MATNR TYPE MATNR
EXPORTING EX_MARA TYPE MARA.
ENDCLASS.
Implement class
METHOD GET_MATERIAL_DESCRIPTION.
SELECT * FROM MAKT
INTO EX_MAKT
WHERE MATNR = IM_MATNR.
ENDSELECT.
ENDMETHOD.
ENDCLASS.
Using class
*PRINT OUTPUT
WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MAKTL.
WRITE:/ WA_MAKT-MATNR, WA_MAKT-MAKTX.
DEFINITION DEFERED is a kwyword which indicates the class definition is delayed or postponed or
definition at some place in program.
REPORT ZSAPN_LOCAL_CLASS_METHODS.
CLASS CL_METHODS_EXAMPLE DEFINITION DEFERRED.
DATA : WA_MARA TYPE MARA.
DATA : WA_MAKT TYPE MAKT.
PARAMETERS : P_MATNR TYPE MARA-MATNR.
DATA : LO_MATERIAL TYPE REF TO CL_METHODS_EXAMPLE. "DECLARE CLASS
CLASS CL_METHODS_EXAMPLE DEFINITION. "DOUBLE CLICK ON CLASS NAME TO CREATE
IMPLEMENTATION
PUBLIC SECTION.
METHODS : GET_MATERIAL_DETAILS
IMPORTING IM_MATNR TYPE MATNR
EXPORTING EX_MARA TYPE MARA.
CLASS-METHODS : GET_MATERIAL_DESCRIPTION
IMPORTING IM_MATNR TYPE MATNR
EXPORTING EX_MAKT TYPE MAKT.
ENDCLASS.
CREATE OBJECT LO_MATERIAL. "CREATE OBJECT
*PRINT OUTPUT
WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL.
WRITE:/ WA_MAKT-MATNR, WA_MAKT-MAKTX.
CLASS CL_METHODS_EXAMPLE IMPLEMENTATION.
METHOD GET_MATERIAL_DETAILS.
SELECT SINGLE * FROM MARA
INTO EX_MARA
WHERE MATNR = IM_MATNR.
ENDMETHOD.
METHOD GET_MATERIAL_DESCRIPTION.
SELECT * FROM MAKT
INTO EX_MAKT
WHERE MATNR = IM_MATNR.
ENDSELECT.
ENDMETHOD.
ENDCLASS.
Using events in SAP Local classes, handling events in local classes in SAP
ABAP programming
+-
The below example explains of using events with local classes using SAP ABAP programming.
Define a Class
Define a local class CL_EVENTS and follow the below steps.
Define an event inside the local class definition under public section.
START-OF-SELECTION.
CREATE OBJECT LO_EVENT. "create object
SET HANDLER LO_EVENT->EVENT_HANDLER FOR LO_EVENT. "register event handler
method
for the object
CALL METHOD LO_EVENT->GET_MATERIAL_DETAILS "call method to get material
details
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.
WRITE :/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL.
CLASS CL_EVENTS IMPLEMENTATION. "class implementation
METHOD GET_MATERIAL_DETAILS.
SELECT SINGLE * FROM MARA
INTO EX_MARA
WHERE MATNR = IM_MATNR.
IF SY-SUBRC NE 0.
RAISE EVENT NO_MATERIAL. "trigger the event
ENDIF.
ENDMETHOD.
METHOD EVENT_HANDLER.
WRITE :/ 'No material found'. "event handler method implementation
ENDMETHOD.
ENDCLASS. "CL_EVENTS
Now test the program with invalid material number (material that is not available in MARA ), it will
raise the event.
Final Class is a class which can not be used for inheritance, it is a class property (check box under
properties of class).
If the final check box is selected, we can not use this class for inheritance, to use inheritance remove
the final check box.
Creating a class interface locally and using it in a local class program - SAP ABAP
programming
The below example explains you using interface concept in SAP ABAP local classes, most of the
times we don`t prefer interfaces in local classes, any how we will learn how to use them in local
calsses.
The below syntax is used to declare interface in local classes.
INTERFACE .
**Define methods and events
ENDINTERFACE.
CLASS DEFINITION.
PUBLIC SECTION. "visibility
INTERFACES .
ENDCLASS.
In the below example we are creating and using interfaces locally, please go through the example
of creating and using interface in global classes , the same we are doing in a local class.
Follow the below steps to create and use interfaces in local classes.
Define an interface
Define an interface and add methods which dosen`t need any implementation.
INTERFACE CL_INTERFACE.
METHODS : GET_MATERIAL_DETAILS
IMPORTING IM_MATNR TYPE MARA-MATNR
EXPORTING EX_MARA TYPE MARA.
METHODS : GET_MATERIAL_DESCRIPTIONS
IMPORTING IM_MATNR TYPE MARA-MATNR
EXPORTING EX_MAKT TYPE MAKT.
ENDINTERFACE.
METHOD CL_INTERFACE~GET_MATERIAL_DETAILS.
SELECT SINGLE * FROM MARA
INTO EX_MARA
WHERE MATNR = IM_MATNR.
ENDMETHOD.
METHOD CL_INTERFACE~GET_MATERIAL_DESCRIPTIONS.
SELECT * FROM MAKT
INTO EX_MAKT WHERE MATNR = IM_MATNR AND SPRAS = 'E'.
ENDSELECT.
ENDMETHOD.
ENDCLASS. "cl_clsss
Creat object and use it in the program
Now the class is created and implemented, we can use the call methods .
REPORT ZSAPN_LOCAL_CLASS_INTERFACE.
START-OF-SELECTION.
CREATE OBJECT LO_CLASS.
ENDMETHOD.
METHOD CL_INTERFACE~GET_MATERIAL_DESCRIPTIONS.
SELECT * FROM MAKT
INTO EX_MAKT WHERE MATNR = IM_MATNR AND SPRAS = 'E'.
ENDSELECT.
ENDMETHOD.
ENDCLASS. "cl_clsss
Final class
Final class is a class property which dosen`t allow to inherite, final class can be specified by using
keyword FINAL
Example:
CLASS CL_PARENT_CLASS DEFINITION FINAL . "Final class can use for inheritance
PUBLIC SECTION.
*METHODS, EVENTS, ATTRIUTES
ENDCLASS.
REPORT ZSAPN_LOCAL_CLASS_INHERITANCE.
CLASS CL_CHILD_CLASS DEFINITION DEFERRED. "definition deferred
DATA : LO_CHILD TYPE REF TO CL_CHILD_CLASS. "declare class
DATA : WA_MARA TYPE MARA. "declare work area to store mara data
DATA : WA_MAKT_TMP TYPE MAKT. "declare work area to store makt data
PARAMETERS P_MATNR TYPE MARA-MATNR. "parameter input field for material no
CLASS CL_PARENT_CLASS DEFINITION . "parent class definition not a final class
PUBLIC SECTION.
METHODS : GET_MATERIAL_DETAILS "method to get material details
IMPORTING IM_MATNR TYPE MARA-MATNR
EXPORTING EX_MARA TYPE MARA.
ENDCLASS.
START-OF-SELECTION.
CREATE OBJECT LO_CHILD. "Create object for child class
METHOD : GET_MATERIAL_DETAILS.
SELECT SINGLE * FROM MARA
INTO EX_MARA
WHERE MATNR = IM_MATNR.
ENDMETHOD.
ENDCLASS. "cl_parent_class
CLASS CL_CHILD_CLASS IMPLEMENTATION. "local class implementation
METHOD: GET_MATERIAL_DETAILS.
CALL METHOD SUPER->GET_MATERIAL_DETAILS
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.
SELECT * FROM MAKT
INTO WA_MAKT
WHERE MATNR = WA_MARA-MATNR AND SPRAS = 'E'.
ENDSELECT.
ENDMETHOD.
ENDCLASS. "cl_child_class
We all know how to develop ALV reports using standard SAP Function Modules, now we are going
to learn developing ALV reports using Object Oriented approach.
CL_GUI_ALV_GRID.
CL_GUI_CUSTOM_CONTAINER.
CL_DD_DOCUMENT.
CL_GUI_ALV_TREE_SIMPLE.
CL_GUI_CONTAINER.
CL_GUI_SPLITTER_CONTAINER.
We have 'n' number of events available in the classes when compared to ALV with function
modules which is flexible for programmer to develop ALV`s for various scenarios.
We can display more than one ALV grid in single screen.
By using Object Oriented approach we can control the size of ALV grid by using custom
container.
We can place other UI elements like input field, check box etc on the screen.
Steps need to follow to create OOALV
1. Create Screen
2. Insert Custom Container UI element.
3. Create Module.
4. Create instance for Custom Container and add instance to ALV.
5. Get data from tables
6. Set data to ALV
REPORT ZSAPN_ALV_MARA_FACTARY.
DATA : IT_MARA TYPE TABLE OF MARA,
WA_MARA TYPE MARA.
DATA : LR_ALV TYPE REF TO CL_SALV_TABLE.
SELECT * FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS.
Step2: Call static method FACTORY of class CL_SALV_TABLE, to get table instance with data.
* TRY.
CALL METHOD CL_SALV_TABLE=>FACTORY "get SALV factory instance
* EXPORTING
* LIST_DISPLAY = IF_SALV_C_BOOL_SAP=>FALSE
* R_CONTAINER =
* CONTAINER_NAME =
IMPORTING
R_SALV_TABLE = LR_ALV
CHANGING
T_TABLE = IT_MARA.
* CATCH CX_SALV_MSG .
* ENDTRY.
Sometimes in the real-time business scenarios, we need to have only one instance for a class at a
point of time, for this one we have the concept of singleton in ABAP objects.
Singleton pattern : is the one of the simplest design patterns which involves only one class
which instantiates itself to make sure that it creates one instance.Singleton ensues that it has
only one instance and provides global point of access to the object.
Transaction code for SAP class builder is SE24. SAP class builder(SE24) is used to create, display
and change SAP classes.
Go to SE24 and provide some class example ex: CL_ABAP_CHAR_UTILITIES, to see what are the
components of class.
Properties: The properties tab contains class properties like created user name, last changed user
name, package etc.
Interfaces: Contains the list of the interfaces that are implemented in this class( will discuss in later
lessons).
Friends: contains friend classes ( we will discuss later lessons).
Attributes: The attributes tab contains the list of attributes declared in that class.
Methods : Contains the methods with importing and exporting parameters and with some business
functionality (source code).
Events: Contains events declared and implemented in that class.
Types: The types tab contains user defined type decelerations.
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.
Select parameters button and declare two parameters as below IM_MATNR-IMPORTING- TYPE-
MARA-MATNR-Material Number EX_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
You will get material details for that material, now class with method is created we have to use it in
our program.
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.
Example of using Events in SAP
OOABAP classes
Using of event handler methods in SAP classes, implementing event handler
method in OOABAP
Learners directly coming for this lesson, please refer previous lesson Events in SAP
classes and Working with class methods in SAP 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.
Define an event
Go to Events tab and add a event as below.
NO_MATERIAL-INSTANCE-PUBLIC-No material entered
Define a method
Go to Methods tab and create a method as below
Link event and method and convert the method into event-handler method.
Now we have to link the method to event to make the method as event handler.
Go to methods and put cursor on method NO_MATERIAL_HANDLER, click on detail view icon(see
below image).
A pop up will open, provide description, select Event Handler for check box, provide our class name
as ZCL_SAPN_MATERISLS and event name as NO_MATERIAL (Press F4), enter.
You will see an icon(event handler icon), which means the method is event handler method.
METHOD GET_MATERIAL_DTAILS.
*Select material data from mara table into exporting parameter ex_mara (work
area) for a material no im_matnr
IF IM_MATNR IS INITIAL .
RAISE EVENT NO_MATERIAL.
ELSE.
SELECT SINGLE * FROM MARA
INTO EX_MARA
WHERE MATNR = IM_MATNR.
ENDIF.
ENDMETHOD.
Use set handler and register event handler method to a particular instance in the
program
Now we have to register this event in our SE38 program, to register a event handler method we use
below syntax.
SET HANDLER <INSTANCE>-><EVENT HANDLER METHOD> FOR <INSTANCE>. "here INSTANCE
is object and EVENT HANDLER METHOD handler method created in se24
REPORT ZSAPN_CLASS_EVENT.
DATA : LO_MATERIAL TYPE REF TO ZCL_SAPN_MATERIALS. "class decleration
DATA WA_MARA TYPE MARA. "work area to store material data
START-OF-SELECTION.
CALL METHOD LO_MATERIAL->GET_MATERIAL_DTAILS "call method
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.
Now execute the program with out giving any input, the result will be
Assignment for you: Create and implement a event and event handler method for material not
found in the above class, if user provided the input but the data is not there in MARA table, raise an
event.
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.
Click on parameter button and add below parameter( what ever the parameters you add, they will
become importing parameters only).
Select attributes tab and add below attribute.
Go to methods and double click on CONSTRUCTOR method and add below code.
Save and activate, go to methods tab and add one more method GET_MATERIAL_DESCRIPTIONS
to get material descriptions.
Select parameters button and add below parameters.
ENDMETHOD.
Step2: Create object for material class, the object can be created using ABAP patterns also.
To create object, click on pattern button.
A popup will open, select ABAP object patterns and enter.
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.
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.
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.
Execute the program, provide the material no, you will get out put where language = 'E' only
(Constructor method handles this).
Define a class
Implement class
Using class
DEFINITION DEFERED is a keyword which indicates the class definition is delayed or postponed or
definition at some place in program.
The below example explains you of using table in SAP local classes using SAP ABAP programming
language.
*PRINT OUTPUT
LOOP AT IT_MARA INTO WA_MARA.
WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL.
ENDLOOP.
*PRINT OUTPUT
LOOP AT IT_MARA INTO WA_MARA.
WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL.
ENDLOOP.
CLASS CL_USERDEFINED_TYPES IMPLEMENTATION.
METHOD GET_MATERIALS_FOR_TYPE.
SELECT * FROM MARA
INTO TABLE ET_MARA
WHERE MTART = IM_MTART .
ENDMETHOD.
ENDCLASS.
Creating Class Interface in SAP
OOABAP
Creating an interface in SAP classes
As per SAP Standard documentation all the standard interfaces in SAP starts with IF_ and all the
custom interfaces starts with ZIF_ , in the below example we will be learning how to create a use
interfaces in SAP.
All the custom classes is SAP starts with ZCL_ and all the custom interfaces will start with ZIF_,
based on the provided name (at the time of creation), SAP will automatically determine and creates
(interface or class).
Go to SE24, provide name as ZIF_SAPN_MATERIAL_EXAMPL, create.
Save, double click on each method and create your own implementation.
Double click on ZIF_SAPN_MATERIAL_EXAMPLE~GET_MATERIAL_DETAILS, and add below
code.
SELECT SINGLE * FROM MARA
INTO EX_MARA
WHERE MATNR = IM_MATNR.
REPORT ZSAPN_CLASS_INTERFACE.
DATA : LO_CLASS TYPE REF TO ZCL_SAPN_CLASS_EX. "declare class
START-OF-SELECTION.
CALL METHOD LO_CLASS->ZIF_SAPN_MATERIAL_EXAMPLE~GET_MATERIAL_DETAILS
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.
CALL METHOD LO_CLASS->ZIF_SAPN_MATERIAL_EXAMPLE~GET_MATERIAL_DESCRIPTIONS
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MAKT = WA_MAKT.
WRITE :/ 'Material Details - ' COLOR 5, WA_MARA-MATNR, WA_MARA-MTART,
WA_MARA-MBRSH, WA_MARA-MATKL.
WRITE :/ 'Material Descriptions - 'COLOR 6, WA_MAKT-MATNR, WA_MAKT-MAKTX,
WA_MAKT-SPRAS.
Similarly create another class and repeat the same process, we can add interfaces into multiple
classes.
Using inheritance in SAP
OOABAP
Working with inheritance in SAP classes, using inheritance in SAP classes
A pop up will open provide description, un-select final check box, save and save as local object.
Go to methods tab, provide method name as GET_MATERIAL_DETAILS-INSTANCE-PUBLIC.
Go back to methods, double click on method name and add below code.
METHOD GET_MATERIAL_DETAILS.
ENDMETHOD.
Super class is created, now we have to inherit this class and derive a new class(child class).
Go to SE24, provide name as ZCL_SAPN_CHILDCLASS, create.
A pop up will open provide description, click on create inheritance icon(see below image), provide
super class name.
Save, save as a local object, now you can see the inherited method, using inheritance we can
overwrite the method implementation to do this click on redifinition icon (see image below)
We will add additional functionality to get material description details along with material details, go
to attributes tab and add a attribute ls_makt-instance-public-makt.
Go back to methods, doublle click on method, uncomment the inherited code, pass parameters and
add additional code to get material descriptions.
METHOD GET_MATERIAL_DETAILS.
ENDMETHOD.
REPORT ZSAPN_CLASS_INHERITANCE.
DATA : LO_CLASS TYPE REF TO ZCL_SAPN_CHILDCLASS. "declare class
DATA : WA_MARA TYPE MARA. "declare MARA
DATA WA_MAKT TYPE MAKT. "declare MAKT
START-OF-SELECTION.
CALL METHOD LO_CLASS->GET_MATERIAL_DETAILS "get material details
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.
WA_MAKT = LO_CLASS->LS_MAKT. "access material description from class
attribute
WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL. "print
material details
WRITE:/ WA_MAKT-MATNR, WA_MAKT-MAKTX. "print material descriptions
Step 1: Data deceleration for ALV, custom container and internal table for MARA.
Step 2:Create Screen.
Step 3:Insert Custom container UI element into the screen.
Step 4:Create Modules PBO and PAO, add logic.
Step 5:Display ALV.
Step 1: Data deceleration for ALV, Custom Container and MARA.
Add data deceleration for ALV grid, custom container UI element and for MARA internal table.
START-OF-SELECTION.
**Here 100 is the screen number which we are going to create, you can create
any like: 200, 300 etc
CALL SCREEN 100. "double click on 100 to create a screen
When ever we use ALV factory methods to display ALV, we don`t need to create any field catalog,
we can directly add our user defined tables instance as it automatically determine fields and
displays.
REPORT ZSAPN_ALV_MARA_FACTARY.
TYPES: BEGIN OF TY_MARA,
MATNR TYPE MARA-MATNR,
MTART TYPE MARA-MTART,
MBRSH TYPE MARA-MBRSH,
MATKL TYPE MARA-MATKL,
MEINS TYPE MARA-MEINS,
END OF TY_MARA.
DATA : IT_MARA TYPE TABLE OF TY_MARA,
WA_MARA TYPE MARA.
DATA : LR_ALV TYPE REF TO CL_SALV_TABLE.
SELECT MATNR MTART MBRSH MATKL MEINS FROM MARA INTO TABLE IT_MARA UP TO 50
ROWS.
* TRY.
CALL METHOD CL_SALV_TABLE=>FACTORY "get SALV factory instance
* EXPORTING
* LIST_DISPLAY = IF_SALV_C_BOOL_SAP=>FALSE
* R_CONTAINER =
* CONTAINER_NAME =
IMPORTING
R_SALV_TABLE = LR_ALV
CHANGING
T_TABLE = IT_MARA.
* CATCH CX_SALV_MSG .
* ENDTRY.
LR_ALV->DISPLAY( ). "display grid
Go to methods tab, add a static method INSTANTIATE and click on parameters button .
METHOD INSTANTIATE.
ENDMETHOD.
REPORT ZSAPN_SINGLETON_GLOBAL.
REPORT ZSAPN_SINGLETON_GLOBAL.
DATA : LO_CLASS TYPE REF TO ZCL_SAPN_SINGLETON.
LO_CLASS = ZCL_SAPN_SINGLETON=>INSTANTIATE( ).
Now you can access all public instance components through the object.
AV_NAME-INSTANCE-PUBLIC-TYPE-CHAR25.
DATA : LR_CLASS TYPE REF TO ZCL_SAPN1 . "STEP1--WE DECLARE CLASSES USING REF
TO BECAUSE THEY ARE OBJECTS
CREATE OBJECT LR_CLASS. "STEP2--CREATE OBJECT FOR THE CLASS
*CALL CLASS COMPONENT WITH THE INSTANCE
LR_CLASS->AV_NAME = 'ATTRIBUTE NAME'. "USE CLASS COMPONENTS
WRITE:/ LR_CLASS->AV_NAME.
*OUT PUT WILL BE 'ATTRIBUTE NAME'
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 .
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.
Save, go back to methods and double click on GET_MATERIALS_FOR_TYPE and add the below
code.
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
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.
Program input.
Program output.
Using CLASS CONSTRUCTOR
in SAP OOABAP
What is a Class Constructor in OOABAP ?, Working with Class Constructor in
Object Oriented ABAP Programming
This is a type of CONSTRUCTOR, this method is executed whenever a first call to the class is
made, the call may be through instance or through class name.
These are also called as STATIC CONSTRUCTORS, the name must be CLASS_CONSTRUCTOR.
If you are coming directly for this lesson we highly recommend go through previous lesson Using
CONSTRUCTOR method in SAP classes for better understanding.
Example: We will add a class constructor to make default material type as 'FERT'(Finished Product)
Follow the below steps to create a class constructor.
Go to SE24, provide the class name ZCL_SAPN_MATERIALS, click on change.
Go to attributes tab and add an attribute to make default material type.
METHOD CLASS_CONSTRUCTOR.
*Set default material type as FERT
MAT_TYPE = 'FERT'.
ENDMETHOD.
Here we take a small example to test this one.The below on is a simple example and explains you a
lot.
REPORT ZSAPN_CLASS_CONSTRUCTOR.
DATA : LO_MATERIAL TYPE REF TO ZCL_SAPN_MATERIALS. "Declare class
CREATE OBJECT LO_MATERIAL "create object CONSTRUCTOR method will trigger
EXPORTING
IM_SPRAS = 'E'.
**When ever first call to a class is made class constructor will trigger
WRITE:/ 'Executed through class constructor', ZCL_SAPN_MATERIALS=>MAT_TYPE.
"Executed through Class Constructor
If we have both CONSTRUCTOR and CLASS_CONSTRUCTOR in our class, upon a class call
which will trigger first.....First CLASS_CONSTRUCTOR will be triggered.
*PRINT OUTPUT
LOOP AT IT_MARA INTO WA_MARA.
WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL.
ENDLOOP.
The final code will be
REPORT ZSAPN_LOCAL_CLASS_METHODS.
CLASS CL_USERDEFINED_TYPES DEFINITION DEFERRED.
*PRINT OUTPUT
LOOP AT IT_MARA INTO WA_MARA.
WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL.
ENDLOOP.
CLASS CL_USERDEFINED_TYPES IMPLEMENTATION.
METHOD GET_MATERIALS_FOR_TYPE.
SELECT MATNR MTART MEINS MATKL FROM MARA
INTO TABLE ET_MARA
UP TO 50 ROWS
WHERE MTART = IM_MTART .
ENDMETHOD.
ENDCLASS.
By using aliases concept we can provide alternate name for interface methods and we can call
methods with that name, to add alternate names click on aliases tab and provide altternate names.
Calling alias method is same as normal method CALL METHOD OBJECT->ALIASMETHDO
NAME, see below example.
Please refer previous lesson Using interface class in SAP Classes for better understanding.
REPORT ZSAPN_CLASS_INTERFACE.
DATA : LO_CLASS TYPE REF TO ZCL_SAPN_CLASS_EX. "declare class
START-OF-SELECTION.
CALL METHOD LO_CLASS->GET_MATERIAL_DETAILS "alias name
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.
CALL METHOD LO_CLASS->GET_MATERIAL_DESCRIPTIONS "alias name
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MAKT = WA_MAKT.
WRITE :/ 'Material Details - ' COLOR 5, WA_MARA-MATNR, WA_MARA-MTART,
WA_MARA-MBRSH, WA_MARA-MATKL.
WRITE :/ 'Material Descriptions - 'COLOR 6, WA_MAKT-MATNR, WA_MAKT-MAKTX,
WA_MAKT-SPRAS.
Step 1: Data deceleration's for ALV, custom container and internal table for
MARA(User defined types).
Step 2: Create Screen.
Step 3: Insert Custom container UI element into the screen.
Step 4: Create Modules PBO and PAO, add logic.
Step 5: Build field catalog.
Step 6: Display ALV.
Step 1: Data deceleration's for ALV, Custom Container and user defined types of
MARA.
Add data deceleration's for ALV grid, custom container UI element and for MARA internal table.Add
select-options for material number.
START-OF-SELECTION.
**Here 100 is the screen number which we are going to create, you can create
any like: 200, 300 etc
CALL SCREEN 100. "double click on 100 to create a screen
Provide short description, click on layout, layout designer will open(If you use it first it will take some
time, wait till it comes, if you face any issue while opening contact BASIS, there might be
configuration missing. ).
REPORT ZSAN_OOALV_FCAT.
TABLES: MARA.
TYPES: BEGIN OF TY_MARA,
MATNR TYPE MARA-MATNR,
MTART TYPE MARA-MTART,
MBRSH TYPE MARA-MBRSH,
MATKL TYPE MARA-MATKL,
MEINS TYPE MARA-MEINS,
END OF TY_MARA.
DATA : IT_MARA TYPE TABLE OF TY_MARA,
WA_MARA TYPE TY_MARA.
DATA : LO_CONT TYPE REF TO CL_GUI_CUSTOM_CONTAINER. "custom container
DATA : LO_ALV TYPE REF TO CL_GUI_ALV_GRID. "alv grid
DATA : IT_FIELDCATALOG TYPE LVC_T_FCAT,
WA_FIELDCATALOG TYPE LVC_S_FCAT.
SELECT-OPTIONS: S_MATNR FOR MARA-MATNR.
CALL SCREEN 100. "double click to create
MODULE STATUS_0100 OUTPUT.
* SET PF-STATUS 'xxxxxxxx'.
* SET TITLEBAR 'xxx'.
CREATE OBJECT LO_CONT
EXPORTING
* PARENT =
CONTAINER_NAME = 'CC_ALV'.
CREATE OBJECT LO_ALV
EXPORTING
I_PARENT = LO_CONT.
***Build Field Catalogue
WA_FIELDCATALOG-COL_POS = '1'. "Set column1
WA_FIELDCATALOG-FIELDNAME = 'MATNR'.
WA_FIELDCATALOG-TABNAME = 'MARA'.
WA_FIELDCATALOG-SCRTEXT_M = 'Material No'.
APPEND WA_FIELDCATALOG TO IT_FIELDCATALOG.
CLEAR : WA_FIELDCATALOG.
WA_FIELDCATALOG-COL_POS = '2'. "set column2
WA_FIELDCATALOG-FIELDNAME = 'MTART'.
WA_FIELDCATALOG-TABNAME = 'MARA'.
WA_FIELDCATALOG-SCRTEXT_M = 'Material Type'.
APPEND WA_FIELDCATALOG TO IT_FIELDCATALOG.
CLEAR : WA_FIELDCATALOG.
WA_FIELDCATALOG-COL_POS = '3'. "set column 3
WA_FIELDCATALOG-FIELDNAME = 'MBRSH'.
WA_FIELDCATALOG-TABNAME = 'MARA'.
WA_FIELDCATALOG-SCRTEXT_M = 'Industry Sector'.
APPEND WA_FIELDCATALOG TO IT_FIELDCATALOG.
CLEAR : WA_FIELDCATALOG.
WA_FIELDCATALOG-COL_POS = '4'. "set column 4
WA_FIELDCATALOG-FIELDNAME = 'MATKL'.
WA_FIELDCATALOG-TABNAME = 'MARA'.
WA_FIELDCATALOG-SCRTEXT_M = 'Material Group'.
APPEND WA_FIELDCATALOG TO IT_FIELDCATALOG.
CLEAR : WA_FIELDCATALOG.
WA_FIELDCATALOG-COL_POS = '5'. "set column 5
WA_FIELDCATALOG-FIELDNAME = 'MEINS'.
WA_FIELDCATALOG-TABNAME = 'MARA'.
WA_FIELDCATALOG-SELTEXT = 'Unit Of Measure'.
APPEND WA_FIELDCATALOG TO IT_FIELDCATALOG.
CLEAR : WA_FIELDCATALOG.
**End field catalogue
SELECT MATNR MTART MBRSH MATKL MEINS FROM MARA
INTO TABLE IT_MARA WHERE MATNR IN S_MATNR.
The below example explains you how to insert hotspot in a column using factory methods.
Step 1: Data deceleration, get alv factory instance.
REPORT ZSAPN_ALV_MARA_FACTORY.
TYPES: BEGIN OF TY_MARA,
MATNR TYPE MARA-MATNR,
MTART TYPE MARA-MTART,
MBRSH TYPE MARA-MBRSH,
MATKL TYPE MARA-MATKL,
MEINS TYPE MARA-MEINS,
END OF TY_MARA.
DATA : IT_MARA TYPE TABLE OF TY_MARA,
WA_MARA TYPE MARA.
DATA : LR_ALV TYPE REF TO CL_SALV_TABLE.
SELECT MATNR MTART MBRSH MATKL MEINS FROM MARA INTO TABLE IT_MARA UP TO 50
ROWS.
* TRY.
CALL METHOD CL_SALV_TABLE=>FACTORY "get SALV factory instance
* EXPORTING
* LIST_DISPLAY = IF_SALV_C_BOOL_SAP=>FALSE
* R_CONTAINER =
* CONTAINER_NAME =
IMPORTING
R_SALV_TABLE = LR_ALV
CHANGING
T_TABLE = IT_MARA.
* CATCH CX_SALV_MSG .
* ENDTRY.
REPORT ZSAPN_ALV_MARA_FACTORY.
TYPES: BEGIN OF TY_MARA,
MATNR TYPE MARA-MATNR,
MTART TYPE MARA-MTART,
MBRSH TYPE MARA-MBRSH,
MATKL TYPE MARA-MATKL,
MEINS TYPE MARA-MEINS,
END OF TY_MARA.
DATA : IT_MARA TYPE TABLE OF TY_MARA,
WA_MARA TYPE MARA.
DATA : LR_ALV TYPE REF TO CL_SALV_TABLE.
SELECT MATNR MTART MBRSH MATKL MEINS FROM MARA INTO TABLE IT_MARA UP TO 50
ROWS.
* TRY.
CALL METHOD CL_SALV_TABLE=>FACTORY "get SALV factory instance
* EXPORTING
* LIST_DISPLAY = IF_SALV_C_BOOL_SAP=>FALSE
* R_CONTAINER =
* CONTAINER_NAME =
IMPORTING
R_SALV_TABLE = LR_ALV
CHANGING
T_TABLE = IT_MARA.
* CATCH CX_SALV_MSG .
* ENDTRY.
REPORT ZSAPN_SINGLETON_CLASS.
ENDCLASS.
User-defined types in SAP
Classes
How to use user-defined types in SAP Classes and it's methods ? Working with
user-defined types in Object Oriented ABAP
+-
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.
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.
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.
REPORT ZSAPN_GET_MATERIALS_FOR_DATE.
START-OF-SELECTION.
The below example explains you of using events in ALV factory methods, first thing I recommend
you is to go through what are events on Object Oriented ABAP .
Step 1: Data declarations , get instance of ALV factory.
In-order to implement hotspot click there is a event called LINK_CLICK, we need to implement this
event to implement hotspot click.
Step 3: Follow below steps to implement LINK_CLICK event.
*Register events
DATA: LR_EVENTS TYPE REF TO CL_SALV_EVENTS_TABLE.
LR_EVENTS = LR_ALV->GET_EVENT( ). "get event
REPORT ZSAPN_ALV_MARA_FACTARY.
CLASS LCL_HANDLE_EVENTS DEFINITION DEFERRED. "class definition deferred
TYPES: BEGIN OF TY_MARA, "MARA internal table
MATNR TYPE MARA-MATNR,
MTART TYPE MARA-MTART,
MBRSH TYPE MARA-MBRSH,
MATKL TYPE MARA-MATKL,
MEINS TYPE MARA-MEINS,
END OF TY_MARA.
DATA : IT_MARA TYPE TABLE OF TY_MARA,
WA_MARA TYPE TY_MARA.
DATA : LR_ALV TYPE REF TO CL_SALV_TABLE. "SALV table instance
SELECT MATNR MTART MBRSH MATKL MEINS FROM MARA INTO TABLE IT_MARA UP TO 50
ROWS. "fetch data
* TRY.
CALL METHOD CL_SALV_TABLE=>FACTORY "get SALV factory instance
* EXPORTING
* LIST_DISPLAY = IF_SALV_C_BOOL_SAP=>FALSE
* R_CONTAINER =
* CONTAINER_NAME =
IMPORTING
R_SALV_TABLE = LR_ALV
CHANGING
T_TABLE = IT_MARA.
* CATCH CX_SALV_MSG .
* ENDTRY.
*Register events
DATA: LR_EVENTS TYPE REF TO CL_SALV_EVENTS_TABLE.
LR_EVENTS = LR_ALV->GET_EVENT( ). "get event
CREATE OBJECT GR_EVENTS.
SET HANDLER GR_EVENTS->ON_LINE_CLICK FOR LR_EVENTS. "register event handler
method
**set standard ALV functions visible
LR_ALV->SET_SCREEN_STATUS(
PFSTATUS = 'SALV_STANDARD'
REPORT = 'SALV_TEST_FUNCTIONS'
SET_FUNCTIONS = LR_ALV->C_FUNCTIONS_ALL ).
Output
Click on any material no and test
Totals and Subtotals in ALV
factory methods
Last Updated: November 2nd 2016 by Ashok Kumar Reddy
Display totals and subtotals in ALV with factory methods, aggregations in ALV,
sort in ALV
+-
Most of the times, we may need to display totals and subtotals in ALV reports, in this lesson you will
be able to learn how to display totals and subtotals in ALV with factory methods.
Requirement:Display purchase order details for a range of purchase orders, display totals and
subtotals for a purchase order number .
Requirement Analysis: For the above requirement, we need to get data from EKPO(purchase
order item table), display subtotals for each purchase order number(EBELN field)(one purchase
order can have multiple items) and display totals of all purchase orders at the bottom.
Step 1: Data declarations, get data and get factory instance.
REPORT ZSAPN_ALV_FACTORY_TOTALS.
TABLES: EKKO.
SELECT-OPTIONS : S_EBELN FOR EKKO-EBELN. "po number input
TYPES: BEGIN OF TY_EKPO, "user defined types
EBELN TYPE EKPO-EBELN,
EBELP TYPE EKPO-EBELP,
MATNR TYPE EKPO-MATNR,
BUKRS TYPE EKPO-BUKRS,
MENGE TYPE EKPO-MENGE,
END OF TY_EKPO.
DATA : IT_EKPO TYPE TABLE OF TY_EKPO, "internal table
WA_EKPO TYPE TY_EKPO. "work area
START-OF-SELECTION.
SELECT EBELN EBELP MATNR BUKRS MENGE FROM EKPO INTO TABLE IT_EKPO WHERE
EBELN IN S_EBELN. "get po data
DATA : LR_ALV TYPE REF TO CL_SALV_TABLE. "alv referance
*TRY.
CALL METHOD CL_SALV_TABLE=>FACTORY "load factory instance
* EXPORTING
* LIST_DISPLAY = IF_SALV_C_BOOL_SAP=>FALSE
* R_CONTAINER =
* CONTAINER_NAME =
IMPORTING
R_SALV_TABLE = LR_ALV
CHANGING
T_TABLE = IT_EKPO.
* CATCH CX_SALV_MSG .
*ENDTRY.
* TRY.
CALL METHOD LR_SORT_COLUMN->SET_SUBTOTAL "add subtotal
EXPORTING
VALUE = IF_SALV_C_BOOL_SAP=>TRUE.
* CATCH CX_SALV_DATA_ERROR .
* ENDTRY.
REPORT ZSAPN_ALV_FACTORY_TOTALS.
TABLES: EKKO.
SELECT-OPTIONS : S_EBELN FOR EKKO-EBELN. "po number input
TYPES: BEGIN OF TY_EKPO, "user defined types
EBELN TYPE EKPO-EBELN,
EBELP TYPE EKPO-EBELP,
MATNR TYPE EKPO-MATNR,
BUKRS TYPE EKPO-BUKRS,
MENGE TYPE EKPO-MENGE,
END OF TY_EKPO.
DATA : IT_EKPO TYPE TABLE OF TY_EKPO, "internal table
WA_EKPO TYPE TY_EKPO. "work area
START-OF-SELECTION.
SELECT EBELN EBELP MATNR BUKRS MENGE FROM EKPO INTO TABLE IT_EKPO WHERE
EBELN IN S_EBELN. "get po data
DATA : LR_ALV TYPE REF TO CL_SALV_TABLE. "alv referance
*TRY.
CALL METHOD CL_SALV_TABLE=>FACTORY "load factory instance
* EXPORTING
* LIST_DISPLAY = IF_SALV_C_BOOL_SAP=>FALSE
* R_CONTAINER =
* CONTAINER_NAME =
IMPORTING
R_SALV_TABLE = LR_ALV
CHANGING
T_TABLE = IT_EKPO.
* CATCH CX_SALV_MSG .
*ENDTRY.
Display top of page, end of page using ALV factory methods, top of list in ALV factory
methods
+-
The below example explains you how to add top of page(top of list) and end of page to ALV report
with factory method.
Requirement:Display purchase order details for a range of purchase orders, display totals and subtotals
for a purchase order number .Display top of page(top of list) and end of list.
Requirement Analysis: For the above requirement, we need to get data from EKPO(purchase
order item table), display subtotals for each purchase order number(EBELN field)(one purchase
order can have multiple items) and display totals of all purchase orders at the bottom.Display top of
page(top of list) and end of list
Step 1: Data deceleration, get data and get factory instance.
REPORT ZSAPN_ALV_FACTORY_TOTALS.
TABLES: EKKO.
SELECT-OPTIONS : S_EBELN FOR EKKO-EBELN. "po number input
TYPES: BEGIN OF TY_EKPO, "user defined types
EBELN TYPE EKPO-EBELN,
EBELP TYPE EKPO-EBELP,
MATNR TYPE EKPO-MATNR,
BUKRS TYPE EKPO-BUKRS,
MENGE TYPE EKPO-MENGE,
END OF TY_EKPO.
DATA : IT_EKPO TYPE TABLE OF TY_EKPO, "internal table
WA_EKPO TYPE TY_EKPO. "work area
START-OF-SELECTION.
SELECT EBELN EBELP MATNR BUKRS MENGE FROM EKPO INTO TABLE IT_EKPO WHERE
EBELN IN S_EBELN. "get po data
DATA : LR_ALV TYPE REF TO CL_SALV_TABLE. "alv referance
*TRY.
CALL METHOD CL_SALV_TABLE=>FACTORY "load factory instance
* EXPORTING
* LIST_DISPLAY = IF_SALV_C_BOOL_SAP=>FALSE
* R_CONTAINER =
* CONTAINER_NAME =
IMPORTING
R_SALV_TABLE = LR_ALV
CHANGING
T_TABLE = IT_EKPO.
* CATCH CX_SALV_MSG .
*ENDTRY.
* TRY.
CALL METHOD LR_SORT_COLUMN->SET_SUBTOTAL "add subtotal
EXPORTING
VALUE = IF_SALV_C_BOOL_SAP=>TRUE.
* CATCH CX_SALV_DATA_ERROR .
* ENDTRY.
REPORT ZSAPN_ALV_FACTORY_TOPOFLIST.
TABLES: EKKO.
SELECT-OPTIONS : S_EBELN FOR EKKO-EBELN. "po number input
TYPES: BEGIN OF TY_EKPO, "user defined types
EBELN TYPE EKPO-EBELN,
EBELP TYPE EKPO-EBELP,
MATNR TYPE EKPO-MATNR,
BUKRS TYPE EKPO-BUKRS,
MENGE TYPE EKPO-MENGE,
END OF TY_EKPO.
DATA : IT_EKPO TYPE TABLE OF TY_EKPO, "internal table
WA_EKPO TYPE TY_EKPO. "work area
DATA : LV_CNT TYPE I.
START-OF-SELECTION.
SELECT EBELN EBELP MATNR BUKRS MENGE FROM EKPO INTO TABLE IT_EKPO WHERE
EBELN IN S_EBELN. "get po data
DESCRIBE TABLE IT_EKPO LINES LV_CNT.
DATA : LR_ALV TYPE REF TO CL_SALV_TABLE. "alv reference
*TRY.
CALL METHOD CL_SALV_TABLE=>FACTORY "load factory instance
* EXPORTING
* LIST_DISPLAY = IF_SALV_C_BOOL_SAP=>FALSE
* R_CONTAINER =
* CONTAINER_NAME =
IMPORTING
R_SALV_TABLE = LR_ALV
CHANGING
T_TABLE = IT_EKPO.
* CATCH CX_SALV_MSG .
*ENDTRY.
Output