0% found this document useful (0 votes)
56 views129 pages

Abstract Factory ALV OOABAP

The document provides an introduction to Object Oriented ABAP, detailing key concepts such as classes, objects, methods, events, and interfaces. It distinguishes between global and local classes, explaining their definitions, implementations, and how to create and use methods and events in ABAP programming. Additionally, it covers the visibility of class components and the concept of polymorphism through interfaces.

Uploaded by

dipnar6379
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)
56 views129 pages

Abstract Factory ALV OOABAP

The document provides an introduction to Object Oriented ABAP, detailing key concepts such as classes, objects, methods, events, and interfaces. It distinguishes between global and local classes, explaining their definitions, implementations, and how to create and use methods and events in ABAP programming. Additionally, it covers the visibility of class components and the concept of polymorphism through interfaces.

Uploaded by

dipnar6379
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/ 129

Introduction to Object Oriented ABAP, Concepts and principles of Object

Oriented ABAP programming

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

Declaring Classes and Objects

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 .

 These methods are similar to Function Modules in ABAP.


 Methods can access all attributes of it`s own class.
 Methods are defined in definition part and implemented in implementation part.
 We can call methods using CALL METHOD statement.

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.

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

Use below syntax to register a event handler method for a instance

SET HANDLER for . "here instance is the object created by using create
object

What is a constructor method in SAP Classes ? and Uses of Constructor method


in OOABAP
+-
What is a constructor in a class ?
CONSTRUCTOR is a special type of method, it is executed automatically whenever a object is
created or instantiated.
These methods are mainly used to set default values in classes.
CONSTRUCTORS are two types in SAP classes.

1. CONSTRUCTOR ( Instance Constructor).


2. CLASS CONSTRUCTOR (Static Constructor).

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 .

What is Polymorphism in Object Oriented Programming model ?


It is a concept by which the same method names will behave differently in different classes i.e each
method will have its own implementation in different different classes but with the same name.
Interface is one of the concept in Object Oriented ABAP to achieve Polymorphism.

What is an interface in Object Oriented ABAP ?

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

Local classes in SAP ABAP programming, properties of local classes in SAP


ABAP
+-
Classes are two types in SAP Classes, one is Global class which can be created in SE24 and
second one is local class.Local class is a class which have definition and implementation in a
program .
Syntax for creating local class is :

*CLASS DEFINITION
CLASS DEFINITION. "THIS IS CLASS DEFINITION

ENDCLASS.

*CLASS IMPLEMENTATION
CLASS IMPLEMENTATION.
*METHODS, EVENTS IMPLEMENTATION
ENDCLASS.

Defining attributes, methods and events in local classes


All the attributes, methods, events must be defined in between CLASS DEFINITION and
ENDCLASS.
Syntax for declaring attributes in local classes

CLASS CL_EXAMPLE DEFINITION. "CLASS DEFINITION


PUBLIC SECTION. "VISIBILITY
DATA : TYPE . "DECLARING INSTANCE ATTRIBUTE
CLASS-DATA: TYPE . "DECLARING STATIC ATTRIBUTE
ENDCLASS.

Syntax for declaring methods in local classes

CLASS CL_EXAMPLE DEFINITION. "CLASS DEFINITION


PUBLIC SECTION. "VISIBILITY
*DECLARING INSTANCE METHOD
METHODS :
IMPORTING TYPE
EXPORTING TYPE .
*DECLARING STATIC METHOD
CLASS-METHODS :
IMPORTING TYPE
EXPORTING TYPE .
ENDCLASS.

Syntax for declaring EVENTS in local classes

CLASS CL_EXAMPLE DEFINITION. "CLASS DEFINITION


PUBLIC SECTION. "VISIBILITY
*DECLARING INSTANCE EVENT
EVENT : .

*DECLARING STATIC EVENT


CLASS-EVENT : .
ENDCLASS.

Syntax for declaring CONSTRUCTOR in local classes

CLASS CL_EXAMPLE DEFINITION. "CLASS DEFINITION


PUBLIC SECTION. "VISIBILITY
*DECLARING INSTANCE CONSTRUCTOR
METHODS : CONSTRUCTOR
IMPORTING TYPE .
*DECLARING STATIC CONSTRUCTOR(CLASS CONSTRUCTOR)
CLASS-METHODS: CLASS_CONSTRUCTOR.

ENDCLASS.

Local class with methods in SAP ABAP, defining methods in local classes,
implementing methods in SAP local classes
+-

Local class is a class definition and implementation is available in a program.


Go to SE38, create a program ZSAPN_CLASS_METHODS1 and follow steps to add code

Define a class

CLASS CL_METHODS_EXAMPLE DEFINITION. "DOUBLE CLICK ON CLASS NAME TO CREATE


“ IMPLEMENTATION

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

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 class

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
CREATE OBJECT LO_MATERIAL. "CREATE OBJECT

CALL METHOD LO_MATERIAL->GET_MATERIAL_DETAILS


EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA .

CALL METHOD LO_MATERIAL->GET_MATERIAL_DESCRIPTION


EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MAKT = WA_MAKT .

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

Final code will be

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

CALL METHOD LO_MATERIAL->GET_MATERIAL_DETAILS


EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.

CALL METHOD CL_METHODS_EXAMPLE=>GET_MATERIAL_DESCRIPTION


EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MAKT = WA_MAKT.

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

 Step 1:Define an event .


 Step 2:Define a method and make it as event handler.
 Step 4:Create a triggering method which will raise the event.
 Step 5:Use set handler and register event handler method to a particular instance in the
program.

Define an event inside the local class definition under public section.

EVENTS : NO_MATERIAL. "event

Define a method and makt it as Event handler method

METHODS : EVENT_HANDLER FOR EVENT NO_MATERIAL OF CL_EVENTS. "event


handler method

Create one more method for this example.


METHODS : GET_MATERIAL_DETAILS
IMPORTING IM_MATNR TYPE MARA-MATNR
EXPORTING EX_MARA TYPE MARA.

Create a triggering point


Create a triggering point with keyword RAISE.
CLASS CL_EVENTS 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. "triggering point
ENDIF.
ENDMETHOD.
METHOD EVENT_HANDLER.
WRITE :/ 'No material found'.
ENDMETHOD.
ENDCLASS. "CL_EVENTS

Register event using SET HANDLER and use it in our program


Define class, create object and register the event with the object using SET HANDLER keyword.
DATA LO_EVENT TYPE REF TO CL_EVENTS.
DATA : WA_MARA TYPE MARA.
CREATE OBJECT LO_EVENT.
SET HANDLER LO_EVENT->EVENT_HANDLER FOR LO_EVENT.
CALL METHOD LO_EVENT->GET_MATERIAL_DETAILS
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.

Final Program code will be


REPORT ZSAPN_LOCAL_CLASS_EVENTS.
CLASS CL_EVENTS DEFINITION DEFERRED.
DATA LO_EVENT TYPE REF TO CL_EVENTS. "declare class
DATA : WA_MARA TYPE MARA. "declare work area
PARAMETERS P_MATNR TYPE MARA-MATNR. "Material no input
CLASS CL_EVENTS DEFINITION. "class definition
PUBLIC SECTION.
EVENTS : NO_MATERIAL. "event
METHODS : GET_MATERIAL_DETAILS
IMPORTING IM_MATNR TYPE MARA-MATNR
EXPORTING EX_MARA TYPE MARA.
METHODS : EVENT_HANDLER FOR EVENT NO_MATERIAL OF CL_EVENTS. "event
handler method
ENDCLASS.

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.

What is inheritance in SAP Classes, uses of inheritance, properties of inheritance


in SAP OOABAP
+-

What is Inheritance in SAP OOABAP ?


Inheritance is the concept of passing the behavior of one class to another class.
You can use an existing class to derive a new class.
Derived class inherits the data and methods of a super class.
However they can overwrite the existing methods also add new code. Advantage of this property is
re-usability.
This means we can add additional features to an existing class without modifying it.
SUPER is the keyword used to represent the super class in oops, you can access the methods and
attributes the super class using this word super.
REDIFINATION is the keyword which is used to overwrite the parent class methods with new
definition.
NOTE: In Object Oriented ABAP we have only single inheritance. There is no multiple inheritance.
Inheritance in general
Super Class Is a main class by using this we can derive a new class which will be called as Child
class.

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.

To implement the local interface in a local class, we use below syntax.

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.

Define a class and add interface


Define a local class and add the above interface into it .

CLASS CL_CLASS DEFINITION.


PUBLIC SECTION.
INTERFACES CL_INTERFACE.
ENDCLASS.

Implement the methods in local class


Implement the methods of interface in the defined local class.

CLASS CL_CLASS IMPLEMENTATION.

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 .

DATA LO_CLASS TYPE REF TO CL_CLASS.


DATA : WA_MARA TYPE MARA.
DATA : WA_MAKT TYPE MAKT.
PARAMETERS P_MATNR TYPE MARA-MATNR.
START-OF-SELECTION.
CREATE OBJECT LO_CLASS.

CALL METHOD LO_CLASS->CL_INTERFACE~GET_MATERIAL_DETAILS


EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.
CALL METHOD LO_CLASS->CL_INTERFACE~GET_MATERIAL_DESCRIPTIONS
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MAKT = WA_MAKT.

WRITE :/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL.

WRITE :/ WA_MAKT-MATNR, WA_MAKT-MAKTX.

The final program source will be

REPORT ZSAPN_LOCAL_CLASS_INTERFACE.

CLASS CL_CLASS DEFINITION DEFERRED.


DATA LO_CLASS TYPE REF TO CL_CLASS.
DATA : WA_MARA TYPE MARA.
DATA : WA_MAKT TYPE MAKT.
PARAMETERS P_MATNR TYPE MARA-MATNR.
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.

CLASS CL_CLASS DEFINITION.


PUBLIC SECTION.
INTERFACES CL_INTERFACE.
ENDCLASS.

START-OF-SELECTION.
CREATE OBJECT LO_CLASS.

CALL METHOD LO_CLASS->CL_INTERFACE~GET_MATERIAL_DETAILS


EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.
CALL METHOD LO_CLASS->CL_INTERFACE~GET_MATERIAL_DESCRIPTIONS
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MAKT = WA_MAKT.

WRITE :/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL.

WRITE :/ WA_MAKT-MATNR, WA_MAKT-MAKTX.


CLASS CL_CLASS IMPLEMENTATION.
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

Using inheritance in local


classes in SAP ABAP
Concept if inheritance, using inheritance in local class using SAP ABAP
programming language

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.

Final class can not be used for inheritance.

Example of using inheritance in local classes


In the below example we are converting example explained in lesson Using inheritance in SAP
Classes into local classes. Refer the lessons side-by-side for better understanding.

Define a parent class.

CLASS CL_PARENT_CLASS DEFINITION FINAL .


PUBLIC SECTION.
METHODS : GET_MATERIAL_DETAILS
IMPORTING IM_MATNR TYPE MARA-MATNR
EXPORTING EX_MARA TYPE MARA.
ENDCLASS.

Implement parent class.

CLASS CL_PARENT_CLASS IMPLEMENTATION.


METHOD : GET_MATERIAL_DETAILS.
SELECT SINGLE * FROM MARA
INTO EX_MARA
WHERE MATNR = IM_MATNR.
ENDMETHOD.
ENDCLASS. "cl_parent_class

INHERITING FROM is a keyword which is used to derive a class by using inheritance.


REDEFINITION is a keyword which is used to overwrite paraent class method. Define child class

Include an attribute wa_makt to get material description details by using redefinition.

CLASS CL_CHILD_CLASS DEFINITION INHERITING FROM CL_PARENT_CLASS. "inheritance


PUBLIC SECTION.
DATA : WA_MAKT TYPE MAKT. "attribute for material descriptions
METHODS : GET_MATERIAL_DETAILS REDEFINITION. "we will add additional code
ENDCLASS.

Implement child class.


SUPER is a keyword which is used to access parent class methods in child classes.

CLASS CL_CHILD_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

The final code will be


Please try to understand step by step, if you face any difficulty....please comment at the bottom.

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.

CLASS CL_CHILD_CLASS DEFINITION INHERITING FROM CL_PARENT_CLASS. "child class


using inheritance
PUBLIC SECTION.
DATA : WA_MAKT TYPE MAKT. "local attribute to get material descriptions
METHODS : GET_MATERIAL_DETAILS REDEFINITION. "inherited method using
redefinition
ENDCLASS.

START-OF-SELECTION.
CREATE OBJECT LO_CHILD. "Create object for child class

CALL METHOD LO_CHILD->GET_MATERIAL_DETAILS "call method to get material


details
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.
WA_MAKT_TMP = LO_CHILD->WA_MAKT. "material descriptions

WRITE:/ 'Material Details:', WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS,


WA_MARA-MATKL.
WRITE:/ 'Material Descriptions:', WA_MAKT_TMP-MATNR, WA_MAKT_TMP-MAKTX.

CLASS CL_PARENT_CLASS IMPLEMENTATION. "parent class implementation

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

Introduction to ALV using


OOABAP
Developing ALV report using Object Oriented ABAP programming

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.

List of most commonly used classes for OOALV.

 CL_GUI_ALV_GRID.
 CL_GUI_CUSTOM_CONTAINER.
 CL_DD_DOCUMENT.
 CL_GUI_ALV_TREE_SIMPLE.
 CL_GUI_CONTAINER.
 CL_GUI_SPLITTER_CONTAINER.

What are the advantages of Object oriented ALV in SAP?

 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

Common FAQ on OOALV


What is UI element : It is a user interface element, which can hold certail type of data to be visible
for user.

ALV Using Factory Methods


using OOABAP
Developing ALV reports using object oriented factory methods, example ALV
report using factory methods
ALV Factory Methods:
After ALV with Function Modules, Object Oriented Concepts with custom container
(CL_GUI_ALV_GRID), SAP introduces one more ALV programming model with Object Oriented
concepts, the main purpose of this ALV model is to reduce developer time and at the same time
providing maximum possible user specific functionality by using object oriented methodologies.

Follow below steps to develop a simple ALV


Step1: Data decelerations for required internal tables and work areas, get data from data base.

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.

Step3: Call method 'Display' to display ALV.

LR_ALV->DISPLAY( ). "display grid


Singleton Concept in Object
Oriented ABAP
Working with singleton design patterns in Object oriented ABAP, Singleton class
in OOABAP

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.

Example - Logger Classes


The Singleton pattern is used in the design of logger classes. This classes are usually implemented
as a singletons, and provides a global logging access point in all the application components without
being necessary to create an object each time a logging operations is performed.

Steps to be followed to create singleton class

 Create a private class.


 Add a private attribute with reference to the same class.
 Create a public static method with returning value of type reference to same class.
 Create implementation and create object in the implementation of public static method.
 Call static method in any program to create instance for the singleton class.

SAP Class Builder (SE24)


introduction
What is the T-Code to create class in SAP ? What is SAP Class builder and
introduction to transaction SE24 in SAP

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.

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

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

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.
Example of using Events in SAP
OOABAP classes
Using of event handler methods in SAP classes, implementing event handler
method in OOABAP

This is the basic example of using events in SAP classes.

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.

Steps for creating and using event in SAP classes

 Step 1: Define an event .


 Step 2: Define a method.
 Step 3: Link event and method and convert the method into event-handler method.
 Step 4: Create a triggering method which will raise the event.
 Step 5: Use set handler and register event handler method to a particular instance in the
program.

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

NO_MATERIAL_HANDLER-INSTANCE-PUBLIC-Event Handler Method.

Save, double click on the method and add some code.

WRITE:/ 'NO material entered'.


Save and activate immediately.

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.

Create a triggering method which will raise the event


Event handler method is created, now we have to trigger that event, the vent can be triggered by
using below syntax.

RAISE EVENT <EVENT NAME>.


We will trigger the event for the method GET_MATERIAL_DTAILS (we previously created Get
material details ), double click on the method GET_MATERIAL_DTAILS and add the below code.

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

Go to SE38, create a program ZSAPN_CLASS_EVENT and write the below code

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

PARAMETERS P_MATNR TYPE MARA-MATNR. "material input

CREATE OBJECT LO_MATERIAL. "create object for material calsss


SET HANDLER LO_MATERIAL->NO_MATERIAL_HANDLER FOR LO_MATERIAL. "register event
handler method

START-OF-SELECTION.
CALL METHOD LO_MATERIAL->GET_MATERIAL_DTAILS "call method
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.

WRITE : WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS.

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.

Go to Methods and provide method name as CONSTRUCTOR, enter


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

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

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.

Save, go back to methods and double click on method name GET_MATERIAL_DESCRIPTIONS,


add below code.
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.
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.

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.

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

Local class with attributes in


OOABAP
Creating local class with attributes in SAP ABAP programming, defining attributes in
SAP local classes
Local class is a class definition and implementation is available in a program.
Go to SE38, create a program ZSAPN_CLASS_ATTRIBUTES and follow steps to add code

Define a class

CLASS CL_ATTRIBUTES DEFINITION. "DOUBLE CLICK ON CLASS NAME TO CREATE


IMPLEMENTATION
PUBLIC SECTION.
DATA : PA_NAME TYPE CHAR25. "PUBLIC INSTANCE ATTRIBUTE
CLASS-DATA : PA_NAME_ST TYPE CHA25. "PUBLIC STATIC ATTRIBUTE
ENDCLASS.

Implement class

CLASS CL_ATTRIBUTES IMPLEMENTATION.


*LEAVE BLANK FOR THIS EXAMPLE
ENDCLASS.

Using class

DATA : LO_ATTRIBUTES TYPE REF TO CL_ATTRIBUTES. "DECLARE CLASS


CREATE OBJECT LO_ATTRIBUTES. "CREATE OBJECT

LO_ATTRIBUTES->PA_NAME = 'SACHNTHENDULAKR'. "ASSIGN A VALUE TO INSTANCE


ATTRIBUTE
CL_ATTRIBUTES=>PA_NANE_ST = 'PONTING'. "ASSIGN VALUE TO STATIC ATTRIBUTES

*PRINT ATTRIBUTES VIA CLASS


WRITE:/ 'INSTANCE ATTRIBUTE:', LO_ATTRIBUTES->PA_NAME.
WRITE:/ 'STATIC ATTRIBUTE :', CL_ATTRIBUTE=>PA_NAME_ST.

DEFINITION DEFERED is a keyword which indicates the class definition is delayed or postponed or
definition at some place in program.

Final code will be

CLASS CL_ATTRIBUTES DEFINITION DEFERED.


DATA : LO_ATTRIBUTES TYPE REF TO CL_ATTRIBUTES. "DECLARE CLASS
CREATE OBJECT LO_ATTRIBUTES. "CREATE OBJECT

CLASS CL_ATTRIBUTES DEFINITION. "DOUBLE CLICK ON CLASS NAME TO CREATE


IMPLEMENTATION
PUBLIC SECTION.
DATA : PA_NAME TYPE CHAR25. "PUBLIC INSTANCE ATTRIBUTE
CLASS-DATA : PA_NAME_ST TYPE CHA25. "PUBLIC STATIC ATTRIBUTE
ENDCLASS.

LO_ATTRIBUTES->PA_NAME = 'SACHNTHENDULAKR'. "ASSIGN A VALUE TO INSTANCE


ATTRIBUTE
CL_ATTRIBUTES=>PA_NANE_ST = 'PONTING'. "ASSIGN VALUE TO STATIC ATTRIBUTES

*PRINT ATTRIBUTES VIA CLASS


WRITE:/ 'INSTANCE ATTRIBUTE:', LO_ATTRIBUTES->PA_NAME.
WRITE:/ 'STATIC ATTRIBUTE :', CL_ATTRIBUTE=>PA_NAME_ST.
CLASS CL_ATTRIBUTES IMPLEMENTATION.
*LEAVE BLANK FOR THIS EXAMPLE
ENDCLASS.

Local class with tables in SAP


OOABAP
Using tables in SAP ABAP local classes, se11 table types in SAP Local classes

The below example explains you of using table in SAP local classes using SAP ABAP programming
language.

Declare a table type


Create a table type in SE11(in this example we use ZSAPN_MARA which we have created already).

Define class and method

CLASS CL_USERDEFINED_TYPES DEFINITION.


PUBLIC SECTION.
METHODS : GET_MATERIALS_FOR_TYPE
IMPORTING IM_MTART TYPE MARA-MTART
EXPORTING ET_MARA TYPE ZSAPN_MARA. "table type in SE11
ENDCLASS.

Implement the defined class

CLASS CL_USERDEFINED_TYPES IMPLEMENTATION.


METHOD GET_MATERIALS_FOR_TYPE.
SELECT * FROM MARA
INTO TABLE ET_MARA
WHERE MTART = IM_MTART .
ENDMETHOD.
ENDCLASS.

Use the class and method


Create an object for the class and call the method

DATA : IT_MARA TYPE TABLE OF MARA.


DATA : WA_MARA TYPE MARA.
CREATE OBJECT LO_MATERIAL. "CREATE OBJECT
CALL METHOD LO_MATERIAL->GET_MATERIALS_FOR_TYPE
EXPORTING
IM_MTART = P_MTART
IMPORTING
ET_MARA = IT_MARA.

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

PARAMETERS : P_MTART TYPE MARA-MTART.


DATA : LO_MATERIAL TYPE REF TO CL_USERDEFINED_TYPES. "DECLARE CLASS
DATA : IT_MARA TYPE TABLE OF MARA.
DATA : WA_MARA TYPE MARA.
CLASS CL_USERDEFINED_TYPES DEFINITION.
PUBLIC SECTION.
METHODS : GET_MATERIALS_FOR_TYPE
IMPORTING IM_MTART TYPE MARA-MTART
EXPORTING ET_MARA TYPE ZSAPN_MARA.
ENDCLASS.
CREATE OBJECT LO_MATERIAL. "CREATE OBJECT
CALL METHOD LO_MATERIAL->GET_MATERIALS_FOR_TYPE
EXPORTING
IM_MTART = P_MTART
IMPORTING
ET_MARA = IT_MARA.

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

Provide description, save, save it in a local object.


Go to methods tab, add method name as GET_MATERIAL_DETAILS-INSTANCE, click on
parameters button and add below parameters.

Save, Go back to methods and add one more method GET_MATERIAL_DESCRIPTIONS.

Click on parameters and provide parameters as below.

Save and activate.


We have created an interface with two methods (which dosen`t have implementation).
No Go to SE24 and create a class ZCL_SAPN_CLASS_EX
Provide description, save it in a local object.

Go to interfaces tab and add interface name as ZIF_SAPN_MATERIAL_EXAMPLE, enter


Now go to methods tab, you can see all the methods in the interface are automatically copied.

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.

Double click on ZIF_SAPN_MATERIAL_EXAMPLE~GET_MATERIAL_DESCRIPTIONS, add below


code

SELECT * FROM MAKT INTO EX_MAKT


WHERE MATNR = IM_MATNR
AND SPRAS = 'E'. "English description only
ENDSELECT.

Save and activate the class.

Using class in program


Create a SE38 program and follow the steps below. The interface method will be clalled by
using INTERFACENAME~METHODNAME.

REPORT ZSAPN_CLASS_INTERFACE.
DATA : LO_CLASS TYPE REF TO ZCL_SAPN_CLASS_EX. "declare class

DATA : WA_MARA TYPE MARA. "mara decleration


DATA : WA_MAKT TYPE MAKT. "makt decleration

PARAMETERS P_MATNR TYPE MARA-MATNR.

CREATE OBJECT LO_CLASS. "create object for the 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

Go to SE24, provide name as ZCL_SAPN_SUPERCLASS, create

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 to parameters tab and add below paramaters.

Go back to methods, double click on method name and add below code.
METHOD GET_MATERIAL_DETAILS.

SELECT SINGLE * FROM MARA


INTO EX_MARA
WHERE MATNR = IM_MATNR.

ENDMETHOD.

Save and activate the class.

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.

CALL METHOD SUPER->GET_MATERIAL_DETAILS


EXPORTING
IM_MATNR = IM_MATNR
IMPORTING
EX_MARA = EX_MARA.
***Additional code to get material descriptions
SELECT * FROM MAKT
INTO LS_MAKT
WHERE MATNR = EX_MARA-MATNR.
ENDSELECT.

ENDMETHOD.

Using Class inprogram


Create a program in SE38 and follow below steps(code).

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

PARAMETERS P_MATNR TYPE MARA-MATNR. "material input


CREATE OBJECT LO_CLASS. "create instance for class

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

ALV with structure using


OOABAP
Requirement: Display list of materials in grid format using Object Oriented techniques.

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.

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_MARA TYPE TABLE OF MARA. "MARA internal table

Step 2: Create Screen


Create Screen in start of selection by using key work CALL SCREEN .

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

After calling screen, double click on 100, click create.


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

Step 3: Insert Custom Container UI element


Drag and drop custom container UI element, double click and provide a name and save, activate
and close layout designer.
Step 4: Create Modules
Click on flow logic, un comment MODULES, double click on each one(one at one time), click yes
and select main program.
MODULE STATUS_0100 OUTPUT.
**Create object for Custom container
CREATE OBJECT LO_CONT
EXPORTING
* PARENT =
CONTAINER_NAME = 'CC_ALV' "container name whcih we have created
.
**Create Object for ALV Grid
CREATE OBJECT LO_ALV
EXPORTING
I_PARENT = LO_CONT "Object of custom container
.
**Get data from MARA
SELECT * FROM MARA INTO TABLE IT_MARA UP TO 100 ROWS.
**Display ALV data using structure
CALL METHOD LO_ALV->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
* I_BUFFER_ACTIVE =
* I_BYPASSING_BUFFER =
* I_CONSISTENCY_CHECK =
I_STRUCTURE_NAME = 'MARA'
* IS_VARIANT =
* I_SAVE =
* I_DEFAULT = 'X'
* IS_LAYOUT =
* IS_PRINT =
* IT_SPECIAL_GROUPS =
* IT_TOOLBAR_EXCLUDING =
* IT_HYPERLINK =
* IT_ALV_GRAPHICS =
* IT_EXCEPT_QINFO =
* IR_SALV_ADAPTER =
CHANGING
IT_OUTTAB = IT_MARA
* IT_FIELDCATALOG =
* IT_SORT =
* IT_FILTER =
* EXCEPTIONS
* INVALID_PARAMETER_COMBINATION = 1
* PROGRAM_ERROR = 2
* TOO_MANY_LINES = 3
* OTHERS = 4
.
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF.

ENDMODULE. " STATUS_0100 OUTPUT


ALV factory method with user
defined types
ALV report with user defined types using Object Oriented Factory methods

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.

Example program using ALV factory methods

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

Creating global singleton class


in OOABAP
Follow below steps to create singleton class in Object oriented ABAP
Step 1: Create a private class in Se24
Go to SE24, provide name as ZSAPN_CL_SINGLETON and click on create.

Provide description, select class as private and save.


Go to attributes tab, add a private attribute of type reference to same class.

Go to methods tab, add a static method INSTANTIATE and click on parameters button .

Add a returning parameter as below.


Go back to methods, double click on method INSTANTIATE and add below code.

METHOD INSTANTIATE.

IF LV_INST IS NOT BOUND.


CREATE OBJECT RO_INST.
LV_INST = RO_INST.
ELSE.
RO_INST = LV_INST.
ENDIF.

ENDMETHOD.

Save and activate the class, go to SE38, create a program ZSAPN_SINGLETON_GLOBAL.


Try to create instance directly.

REPORT ZSAPN_SINGLETON_GLOBAL.

DATA : LO_CLASS TYPE REF TO ZCL_SAPN_SINGLETON.

create OBJECT lo_class.

It will through an error.

Now create instance through static method.

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.

Write First SAP class


Writing first SAP Class using transaction code SE24 with SAP ABAP
Programming

Create a simple class for using attributes

Go to SE24( Class Builder).


Give the class name ZCL_SAPN1 and create.

Provide short description, save.


Save it in a Local Object or in your test package.
Go to attributes tab and define an attribute as below.

AV_NAME-INSTANCE-PUBLIC-TYPE-CHAR25.

Save and Activate.


Now class is created, we have to use this in our program.
Create a ABAP program in SE38 and add below code

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'

Using multiple objects of class

DATA : LR_CLASS1 TYPE REF TO ZCL_SAPN1 . "Declare first class object


DATA : LR_CLASS2 TYPE REF TO ZCL_SAPN1 . "Declare second class object

CREATE OBJECT LR_CLASS1. "Create a first object


CREATE OBJECT LR_CLASS2. "Create a second object
*CALL CLASS COMPONENT WITH THE INSTANCE
LR_CLASS1->AV_NAME = 'FIRST ATTRIBUTE NAME'. "Assign value to first object

LR_CLASS2->AV_NAME = 'SECOND ATTRIBUTE NAME'. "Assign value to second object


WRITE:/ LR_CLASS1->AV_NAME. "OUT PUT WILL BE 'FIRST ATTRIBUTE NAME'

WRITE:/ LR_CLASS2->AV_NAME. "OUT PUT WILL BE 'SECOND ATTRIBUTE NAME'

Using table types in methods in


SAP classes
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.
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.

Go to methods and add a method name CLASS_CONSTRUCTOR as below.


We can not add any exporting and importing parameters to a CLASS_CONSTRUCTOR method.
Double click on the method CLASS_CONSTRUCTOR and add below code.

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

WRITE:/ 'Executed through Constructor', LO_MATERIAL->LANGUAGE . "Executed


through constructor method

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.

Local class with user-defined


types in SAP OOABAP
Defining a local class with user defined types in SAP ABAP programming, local classes
with user defined table types
It is very important for us to use user-defined types when using local classes in SAP ABAP
programs, the below example explains using user-defined types in SAP ABAP programs.
The below example explains you of using user defined types in SAP Local classes in SAP ABAP
programming.

Declare a table type


Declare a table type with required fields

TYPES : BEGIN OF TY_MARA,


MATNR TYPE MARA-MATNR,
MTART TYPE MARA-MTART,
MEINS TYPE MARA-MEINS,
MATKL TYPE MARA-MATKL,
END OF TY_MARA.
TYPES : TT_MARA TYPE TABLE OF TY_MARA.
Define class and method

CLASS CL_USERDEFINED_TYPES DEFINITION.


PUBLIC SECTION.
METHODS : GET_MATERIALS_FOR_TYPE
IMPORTING IM_MTART TYPE MARA-MTART
EXPORTING ET_MARA TYPE TT_MARA.
ENDCLASS.

Implement the defined class

CLASS CL_USERDEFINED_TYPES IMPLEMENTATION.


METHOD GET_MATERIALS_FOR_TYPE.
SELECT MATNR MTART MEINS MATKL FROM MARA
INTO TABLE ET_MARA
WHERE MTART = IM_MTART .
ENDMETHOD.
ENDCLASS.

Use the class and method


Create an object for the class and call the method

DATA : IT_MARA TYPE TABLE OF TY_MARA.


DATA : WA_MARA TYPE TY_MARA.
CREATE OBJECT LO_MATERIAL. "CREATE OBJECT
CALL METHOD LO_MATERIAL->GET_MATERIALS_FOR_TYPE
EXPORTING
IM_MTART = P_MTART
IMPORTING
ET_MARA = IT_MARA.

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

PARAMETERS : P_MTART TYPE MARA-MTART.


DATA : LO_MATERIAL TYPE REF TO CL_USERDEFINED_TYPES. "DECLARE CLASS
TYPES : BEGIN OF TY_MARA,
MATNR TYPE MARA-MATNR,
MTART TYPE MARA-MTART,
MEINS TYPE MARA-MEINS,
MATKL TYPE MARA-MATKL,
END OF TY_MARA.
TYPES : TT_MARA TYPE TABLE OF TY_MARA.
DATA : IT_MARA TYPE TABLE OF TY_MARA.
DATA : WA_MARA TYPE TY_MARA.
CLASS CL_USERDEFINED_TYPES DEFINITION.
PUBLIC SECTION.
METHODS : GET_MATERIALS_FOR_TYPE
IMPORTING IM_MTART TYPE MARA-MTART
EXPORTING ET_MARA TYPE TT_MARA.
ENDCLASS.
CREATE OBJECT LO_MATERIAL. "CREATE OBJECT
CALL METHOD LO_MATERIAL->GET_MATERIALS_FOR_TYPE
EXPORTING
IM_MTART = P_MTART
IMPORTING
ET_MARA = IT_MARA.

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

Using aliases for interface


methods in SAP OOABAP
Using aliases concept in SAP classes, provide alternate names for interface
methods in SAP classes
Aliases is a concept of providing alternate method names for interface methods in implemented
class(where interface is implemented).

Whenever we implement an interface in a class, the method will be copied with


INTERFACENAME~METHODNAME, see below example.

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

DATA : WA_MARA TYPE MARA. "mara decleration


DATA : WA_MAKT TYPE MAKT. "makt decleration

PARAMETERS P_MATNR TYPE MARA-MATNR.

CREATE OBJECT LO_CLASS. "create object for the 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.

Using field catalog in OOALV


Requirement: Display material no, material type, industry sector, material group and base unit of
measure of list of materials in grid format using Object Oriented techniques.

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.

DATA : LO_CONT TYPE REF TO CL_GUI_CUSTOM_CONTAINER. "Custom Container


DATA : LO_ALV TYPE REF TO CL_GUI_ALV_GRID. "ALV Grid
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.
TABLES: MARA.
SELECT-OPTIONS: S_MATNR FOR MARA-MATNR.

Step 2: Create Screen


Create Screen in start of selection by using key work CALL SCREEN .

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

After calling screen, double click on 100, click create.

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

Step 3: Insert Custom Container UI element


Drag and drop custom container UI element, double click and provide a name and save, activate
and close layout designer.
Step 4:Create Modules
Click on flow logic, uncomment MODULES, double click on each one(one at one time), click yes and
select main program.
Step 5, 6: Build Field Catalog.
Field Catalog: It is a structure which is used to specify out put structure with field names, field
positions, descriptions and additional properties.To see the list of properties of OOALV field catalog,
go to se11 and check the structure LVC_S_FCAT.

Building of field catalog is give below.

**Declare field catalog table and structure as per class method


DATA : IT_FIELDCATALOG TYPE LVC_T_FCAT,
WA_FIELDCATALOG TYPE LVC_S_FCAT.
***Build Field Catalogue
WA_FIELDCATALOG-COL_POS = '1'. "Set column1
WA_FIELDCATALOG-FIELDNAME = 'MATNR'. "set field
WA_FIELDCATALOG-TABNAME = 'MARA'. "set table
WA_FIELDCATALOG-SCRTEXT_M = 'Material No'. "set column description
APPEND WA_FIELDCATALOG TO IT_FIELDCATALOG. "append to table
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-SCRTEXT_M = 'Unit Of Measure'.
APPEND WA_FIELDCATALOG TO IT_FIELDCATALOG.
CLEAR : WA_FIELDCATALOG.
**End field catalogue
MODULE STATUS_0100 OUTPUT.
**Create object for Custom container
CREATE OBJECT LO_CONT
EXPORTING
* PARENT =
CONTAINER_NAME = 'CC_ALV' "container name whcih we have created
.
**Create Object for ALV Grid
CREATE OBJECT LO_ALV
EXPORTING
I_PARENT = LO_CONT "Object of custom container
.
**Get data from MARA for user input
SELECT MATNR MTART MBRSH MATKL MEINS FROM MARA
INTO TABLE IT_MARA WHERE MATNR IN S_MATNR.

**Display ALV data using structure


CALL METHOD LO_ALV->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
* I_BUFFER_ACTIVE =
* I_BYPASSING_BUFFER =
* I_CONSISTENCY_CHECK =
* I_STRUCTURE_NAME = 'MARA'
* IS_VARIANT =
* I_SAVE =
* I_DEFAULT = 'X'
* IS_LAYOUT =
* IS_PRINT =
* IT_SPECIAL_GROUPS =
* IT_TOOLBAR_EXCLUDING =
* IT_HYPERLINK =
* IT_ALV_GRAPHICS =
* IT_EXCEPT_QINFO =
* IR_SALV_ADAPTER =
CHANGING
IT_OUTTAB = IT_MARA
IT_FIELDCATALOG = IT_FIELDCATALOG
* IT_SORT =
* IT_FILTER =
* EXCEPTIONS
* INVALID_PARAMETER_COMBINATION = 1
* PROGRAM_ERROR = 2
* TOO_MANY_LINES = 3
* OTHERS = 4
.
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF.

ENDMODULE. " STATUS_0100 OUTPUT

Final and Modularized code will be

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.

CALL METHOD LO_ALV->SET_TABLE_FOR_FIRST_DISPLAY


* EXPORTING
* I_BUFFER_ACTIVE =
* I_BYPASSING_BUFFER =
* I_CONSISTENCY_CHECK =
* I_STRUCTURE_NAME =
* IS_VARIANT =
* I_SAVE =
* I_DEFAULT = 'X'
* IS_LAYOUT =
* IS_PRINT =
* IT_SPECIAL_GROUPS =
* IT_TOOLBAR_EXCLUDING =
* IT_HYPERLINK =
* IT_ALV_GRAPHICS =
* IT_EXCEPT_QINFO =
* IR_SALV_ADAPTER =
CHANGING
IT_OUTTAB = IT_MARA
IT_FIELDCATALOG = IT_FIELDCATALOG
* IT_SORT =
* IT_FILTER =
* EXCEPTIONS
* INVALID_PARAMETER_COMBINATION = 1
* PROGRAM_ERROR = 2
* TOO_MANY_LINES = 3
* OTHERS = 4
.
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF.

ENDMODULE. " STATUS_0100 OUTPUT

MODULE USER_COMMAND_0100 INPUT.

ENDMODULE. " USER_COMMAND_0100 INPUT

Execute and test the program .

Hotspot in ALV with OOABAP


factory methods
Insert hotspot into ALV columns using factory methods, example ALV report with
hotspot factory methods

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.

Step 2: Get ALV Columns using GET_COLUMNS method.

**get ALV columns


DATA : LR_COLUMNS TYPE REF TO CL_SALV_COLUMNS_TABLE. "columns instance
CALL METHOD LR_ALV->GET_COLUMNS "get all columns
RECEIVING
VALUE = LR_COLUMNS.

Step 3: Get single column to add hotspot.


DATA : LR_COL TYPE REF TO CL_SALV_COLUMN_TABLE. "column instance
TRY.
LR_COL ?= LR_COLUMNS->GET_COLUMN( 'MATNR' ). "get MATNR columns to
insert hotspot
CATCH CX_SALV_NOT_FOUND.
ENDTRY.

Step 4:Insert hotspot into column using SET_CELL_TYPE method.

* Set the Hotspot for MATNR Column


TRY.
CALL METHOD LR_COL->SET_CELL_TYPE "set cell type hotspot
EXPORTING
VALUE = IF_SALV_C_CELL_TYPE=>HOTSPOT.
.
CATCH CX_SALV_DATA_ERROR .
ENDTRY.

Step5: Display ALV .

LR_ALV->DISPLAY( ). "display grid

Final code will be

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.

**get ALV columns


DATA : LR_COLUMNS TYPE REF TO CL_SALV_COLUMNS_TABLE. "columns instance
DATA : LR_COL TYPE REF TO CL_SALV_COLUMN_TABLE. "column instance
CALL METHOD LR_ALV->GET_COLUMNS "get all columns
RECEIVING
VALUE = LR_COLUMNS.
IF LR_COLUMNS IS NOT INITIAL.
*TRY.
* Get VBELN column
TRY.
LR_COL ?= LR_COLUMNS->GET_COLUMN( 'MATNR' ). "get MATNR columns to
insert hotspot
CATCH CX_SALV_NOT_FOUND.
ENDTRY.
*
* Set the Hotspot for MATNR Column
TRY.
CALL METHOD LR_COL->SET_CELL_TYPE "set cell type hotspot
EXPORTING
VALUE = IF_SALV_C_CELL_TYPE=>HOTSPOT.
.
CATCH CX_SALV_DATA_ERROR .
ENDTRY.
ENDIF.
LR_ALV->DISPLAY( ). "display grid

Out put of ALV using factory method

Creating Local singleton class


in OOABAP
Below is the example of local singleton class in Object Oriented ABAP Programming .

REPORT ZSAPN_SINGLETON_CLASS.

DATA : IT_MARA TYPE TABLE OF MARA,


WA_MARA TYPE MARA.
CLASS CL_SINGLETON DEFINITION CREATE PRIVATE. "create a private class
PUBLIC SECTION.
METHODS: GET_MARA. "actual method get mara
CLASS-METHODS: INSTANTIATE RETURNING VALUE(LR_INST) TYPE REF TO
CL_SINGLETON . "create a static method
PRIVATE SECTION.
CLASS-DATA: LR_INST TYPE REF TO CL_SINGLETON. "private variable
ENDCLASS.

DATA : LO_CLASS TYPE REF TO CL_SINGLETON.


LO_CLASS = CL_SINGLETON=>INSTANTIATE( ). "get instance of class
*CREATE OBJECT LO_CLASS.
LO_CLASS->GET_MARA( ). "get mara data

CLASS CL_SINGLETON IMPLEMENTATION.


METHOD GET_MARA.
SELECT * FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS.
LOOP AT IT_MARA INTO WA_MARA.
WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL.
ENDLOOP.
ENDMETHOD.
METHOD INSTANTIATE.
IF LR_INST IS INITIAL.
CREATE OBJECT LR_INST. "create object
ENDIF.
ENDMETHOD.

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
+-

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.

Interactive ALV with factory


method using events
Interactive ALV with factory method using events and event handler methods,
using events in ALV factory methods
+-

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.

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.

Step 2: Get ALV columns and insert hotspot.

**get ALV columns


DATA : LR_COLUMNS TYPE REF TO CL_SALV_COLUMNS_TABLE. "columns instance
DATA : LR_COL TYPE REF TO CL_SALV_COLUMN_TABLE. "column instance
CALL METHOD LR_ALV->GET_COLUMNS "get all columns
RECEIVING
VALUE = LR_COLUMNS.
IF LR_COLUMNS IS NOT INITIAL.
*TRY.
* Get MATNR column
TRY.
LR_COL ?= LR_COLUMNS->GET_COLUMN( 'MATNR' ). "get MATNR columns to
insert hotspot
CATCH CX_SALV_NOT_FOUND.
ENDTRY.

* Set the HotSpot for MATNR Column


TRY.
CALL METHOD LR_COL->SET_CELL_TYPE "set cell type hotspot
EXPORTING
VALUE = IF_SALV_C_CELL_TYPE=>HOTSPOT.
.
CATCH CX_SALV_DATA_ERROR .
ENDTRY.
ENDIF.

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.

 Create a local class.


 Create a event handler method in that local class to handle LINK_CLICK of
CL_SALV_EVENTS_TABLE.

CLASS LCL_HANDLE_EVENTS DEFINITION.


PUBLIC SECTION.
METHODS:
ON_LINE_CLICK FOR EVENT LINK_CLICK OF CL_SALV_EVENTS_TABLE
IMPORTING ROW COLUMN. "event handler method, imports row and
column of clicked value
ENDCLASS.

 Implement event handler method.

CLASS LCL_HANDLE_EVENTS IMPLEMENTATION.


METHOD ON_LINE_CLICK. "double click implementation
**handle double click here
READ TABLE IT_MARA INTO WA_MARA INDEX ROW.
IF SY-SUBRC EQ 0.
SET PARAMETER ID 'MAT' FIELD WA_MARA-MATNR.
CALL TRANSACTION 'MM03' AND SKIP FIRST SCREEN.
ENDIF.
ENDMETHOD. "on_link_click
ENDCLASS. "lcl_events IMPLEMENTATION

 Get events of ALV using GET_EVENT method.

*Register events
DATA: LR_EVENTS TYPE REF TO CL_SALV_EVENTS_TABLE.
LR_EVENTS = LR_ALV->GET_EVENT( ). "get event

 Register event handler method.

DATA: GR_EVENTS TYPE REF TO LCL_HANDLE_EVENTS.


CREATE OBJECT GR_EVENTS.
SET HANDLER GR_EVENTS->ON_LINE_CLICK FOR LR_EVENTS. "register event
handler method

Step 4: Add Functions to ALV.


To set functions to ALV, we use SET_SCREEN_STATUS method, to implement this create a menu
in SE41 for this program and add using above method.

In this example I am using standard ALV functions (menu).

**set standard ALV functions visible


LR_ALV->SET_SCREEN_STATUS(
PFSTATUS = 'SALV_STANDARD'
REPORT = 'SALV_TEST_FUNCTIONS'
SET_FUNCTIONS = LR_ALV->C_FUNCTIONS_ALL ).

Final Code will be

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.

**get ALV columns


DATA : LR_COLUMNS TYPE REF TO CL_SALV_COLUMNS_TABLE. "columns instance
DATA : LR_COL TYPE REF TO CL_SALV_COLUMN_TABLE. "column instance
CALL METHOD LR_ALV->GET_COLUMNS "get all columns
RECEIVING
VALUE = LR_COLUMNS.
IF LR_COLUMNS IS NOT INITIAL.
*TRY.
* Get MATNR column
TRY.
LR_COL ?= LR_COLUMNS->GET_COLUMN( 'MATNR' ). "get MATNR columns to
insert hotspot
CATCH CX_SALV_NOT_FOUND.
ENDTRY.

* Set the HotSpot for MATNR Column


TRY.
CALL METHOD LR_COL->SET_CELL_TYPE "set cell type hotspot
EXPORTING
VALUE = IF_SALV_C_CELL_TYPE=>HOTSPOT.
.
CATCH CX_SALV_DATA_ERROR .
ENDTRY.
ENDIF.

*Event handler method for ALV


DATA: GR_EVENTS TYPE REF TO LCL_HANDLE_EVENTS.
***handle hotspot click
CLASS LCL_HANDLE_EVENTS DEFINITION.
PUBLIC SECTION.
METHODS:
ON_LINE_CLICK FOR EVENT LINK_CLICK OF CL_SALV_EVENTS_TABLE
IMPORTING ROW COLUMN. "event handler method, importing row and column
of clicked value
ENDCLASS. "lcl_events DEFINITION

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

LR_ALV->DISPLAY( ). "display grid


CLASS LCL_HANDLE_EVENTS IMPLEMENTATION.
METHOD ON_LINE_CLICK. "double click implementation
**handle double click here
READ TABLE IT_MARA INTO WA_MARA INDEX ROW. "here row is index no
IF SY-SUBRC EQ 0.
SET PARAMETER ID 'MAT' FIELD WA_MARA-MATNR.
CALL TRANSACTION 'MM03' AND SKIP FIRST SCREEN.
ENDIF.
ENDMETHOD. "on_link_click
ENDCLASS. "lcl_events IMPLEMENTATION

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.

Step 2: Add totals


To add totals we need to use GET_AGGREGATIONS, once we get aggregations instance, we need
to add aggregation by passing column name and aggregation type to method
ADD_AGGREGATION.

DATA: LO_AGGRS TYPE REF TO CL_SALV_AGGREGATIONS.


LO_AGGRS = LR_ALV->GET_AGGREGATIONS( ). "get aggregations
* Add TOTAL for COLUMN NETWR
TRY.
CALL METHOD LO_AGGRS->ADD_AGGREGATION "add aggregation
EXPORTING
COLUMNNAME = 'MENGE' "aggregation column name
AGGREGATION = IF_SALV_C_AGGREGATION=>TOTAL. "aggregation type

CATCH CX_SALV_DATA_ERROR . "#EC NO_HANDLER


CATCH CX_SALV_NOT_FOUND . "#EC NO_HANDLER
CATCH CX_SALV_EXISTING . "#EC NO_HANDLER
ENDTRY.

Step 3: Add subtotals


To add subtotals, we need to add sort to the columns and then we have to use SET_SUBTOTAL
method to display subtotals.
DATA : LR_SORT TYPE REF TO CL_SALV_SORTS. "ALV sorts
CALL METHOD LR_ALV->GET_SORTS "get sorts
RECEIVING
VALUE = LR_SORT.
*TRY.
DATA : LR_SORT_COLUMN TYPE REF TO CL_SALV_SORT. "column sort
CALL METHOD LR_SORT->ADD_SORT "add column sort
EXPORTING
COLUMNNAME = 'EBELN' "sort column always keyfield
* POSITION =
* SEQUENCE = IF_SALV_C_SORT=>SORT_UP
* SUBTOTAL = IF_SALV_C_BOOL_SAP=>FALSE
* GROUP = IF_SALV_C_SORT=>GROUP_NONE
* OBLIGATORY = IF_SALV_C_BOOL_SAP=>FALSE
RECEIVING
VALUE = LR_SORT_COLUMN.

* TRY.
CALL METHOD LR_SORT_COLUMN->SET_SUBTOTAL "add subtotal
EXPORTING
VALUE = IF_SALV_C_BOOL_SAP=>TRUE.
* CATCH CX_SALV_DATA_ERROR .
* ENDTRY.

Step 4: Display ALV

LR_ALV->DISPLAY( ). "display ALV

Final code will be

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.

DATA: LO_AGGRS TYPE REF TO CL_SALV_AGGREGATIONS.


LO_AGGRS = LR_ALV->GET_AGGREGATIONS( ). "get aggregations
* Add TOTAL for COLUMN NETWR
TRY.
CALL METHOD LO_AGGRS->ADD_AGGREGATION "add aggregation
EXPORTING
COLUMNNAME = 'MENGE' "aggregation column name
AGGREGATION = IF_SALV_C_AGGREGATION=>TOTAL. "aggregation type
CATCH CX_SALV_DATA_ERROR . "#EC NO_HANDLER
CATCH CX_SALV_NOT_FOUND . "#EC NO_HANDLER
CATCH CX_SALV_EXISTING . "#EC NO_HANDLER
ENDTRY.
* Bring the total line to top
DATA : LR_SORT TYPE REF TO CL_SALV_SORTS. "ALV sorts
CALL METHOD LR_ALV->GET_SORTS "get sorts
RECEIVING
VALUE = LR_SORT.
*TRY.
DATA : LR_SORT_COLUMN TYPE REF TO CL_SALV_SORT. "column sort
CALL METHOD LR_SORT->ADD_SORT "add column sort
EXPORTING
COLUMNNAME = 'EBELN' "sort column always keyfield
* POSITION =
* SEQUENCE = IF_SALV_C_SORT=>SORT_UP
* SUBTOTAL = IF_SALV_C_BOOL_SAP=>FALSE
* GROUP = IF_SALV_C_SORT=>GROUP_NONE
* OBLIGATORY = IF_SALV_C_BOOL_SAP=>FALSE
RECEIVING
VALUE = LR_SORT_COLUMN.
* TRY.
CALL METHOD LR_SORT_COLUMN->SET_SUBTOTAL "add subtotal
EXPORTING
VALUE = IF_SALV_C_BOOL_SAP=>TRUE.
* CATCH CX_SALV_DATA_ERROR .
* ENDTRY.
* CATCH CX_SALV_NOT_FOUND .
* CATCH CX_SALV_EXISTING .
* CATCH CX_SALV_DATA_ERROR .
*ENDTRY.
LR_ALV->DISPLAY( ). "display ALV
Output

Top of page and end of page in


OOALV with factory methods
Last Updated: November 2nd 2016 by Ashok Kumar Reddy

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.

Step 2: Add totals


To add totals we need to use GET_AGGREGATIONS, once we get aggregations instance, we need
to add aggregation by passing column name and aggregation type to method
ADD_AGGREGATION.

DATA: LO_AGGRS TYPE REF TO CL_SALV_AGGREGATIONS.


LO_AGGRS = LR_ALV->GET_AGGREGATIONS( ). "get aggregations
* Add TOTAL for COLUMN NETWR
TRY.
CALL METHOD LO_AGGRS->ADD_AGGREGATION "add aggregation
EXPORTING
COLUMNNAME = 'MENGE' "aggregation column name
AGGREGATION = IF_SALV_C_AGGREGATION=>TOTAL. "aggregation type

CATCH CX_SALV_DATA_ERROR . "#EC NO_HANDLER


CATCH CX_SALV_NOT_FOUND . "#EC NO_HANDLER
CATCH CX_SALV_EXISTING . "#EC NO_HANDLER
ENDTRY.

Step 3: Add subtotals


To add subtotals, we need to add sort to the columns and then we have to use SET_SUBTOTAL
method to display subtotals.

DATA : LR_SORT TYPE REF TO CL_SALV_SORTS. "ALV sorts


CALL METHOD LR_ALV->GET_SORTS "get sorts
RECEIVING
VALUE = LR_SORT.
*TRY.
DATA : LR_SORT_COLUMN TYPE REF TO CL_SALV_SORT. "column sort
CALL METHOD LR_SORT->ADD_SORT "add column sort
EXPORTING
COLUMNNAME = 'EBELN' "sort column always keyfield
* POSITION =
* SEQUENCE = IF_SALV_C_SORT=>SORT_UP
* SUBTOTAL = IF_SALV_C_BOOL_SAP=>FALSE
* GROUP = IF_SALV_C_SORT=>GROUP_NONE
* OBLIGATORY = IF_SALV_C_BOOL_SAP=>FALSE
RECEIVING
VALUE = LR_SORT_COLUMN.

* TRY.
CALL METHOD LR_SORT_COLUMN->SET_SUBTOTAL "add subtotal
EXPORTING
VALUE = IF_SALV_C_BOOL_SAP=>TRUE.
* CATCH CX_SALV_DATA_ERROR .
* ENDTRY.

Step 4: Add top of page(top of list) and end of list.


To add top of page and end of list we need to create a layout gird and insert that grid into ALV using
method SET_TOP_OF_LIST.

Create layout grid.

DATA : LR_HEADER TYPE REF TO CL_SALV_FORM_ELEMENT.


DATA: LR_GRID_LAYOUT TYPE REF TO CL_SALV_FORM_LAYOUT_GRID,
LR_LABEL TYPE REF TO CL_SALV_FORM_LABEL,
LR_TEXT TYPE REF TO CL_SALV_FORM_TEXT,
L_TEXT TYPE STRING.
CREATE OBJECT LR_GRID_LAYOUT.
L_TEXT = 'Purchase Order Report' .
LR_GRID_LAYOUT->CREATE_HEADER_INFORMATION(
ROW = 1
COLUMN = 3
TEXT = L_TEXT
TOOLTIP = L_TEXT ).
LR_GRID_LAYOUT->ADD_ROW( ).
* LR_GRID_LAYOUT_1 = LR_GRID_LAYOUT->CREATE_GRID(
* ROW = 3
* COLUMN = 1 ).
LR_LABEL = LR_GRID_LAYOUT->CREATE_LABEL(
ROW = 2
COLUMN = 1
TEXT = 'Number of records found: '
TOOLTIP = 'Number of records found for your query' ).
LR_TEXT = LR_GRID_LAYOUT->CREATE_TEXT(
ROW = 2
COLUMN = 2
TEXT = LV_CNT
TOOLTIP = LV_CNT ).
LR_LABEL->SET_LABEL_FOR( LR_TEXT ).
LR_LABEL = LR_GRID_LAYOUT->CREATE_LABEL(
ROW = 3
COLUMN = 1
TEXT = 'Date : '
TOOLTIP = 'Date' ).
L_TEXT = SY-DATUM.
LR_TEXT = LR_GRID_LAYOUT->CREATE_TEXT(
ROW = 3
COLUMN = 2
TEXT = L_TEXT
TOOLTIP = L_TEXT ).
LR_LABEL->SET_LABEL_FOR( LR_TEXT ).
LR_HEADER = LR_GRID_LAYOUT.

Create top of list.

CALL METHOD LR_ALV->SET_TOP_OF_LIST


EXPORTING
VALUE = LR_HEADER.

Step 5: Add End Of List


End of list can be created using method SET_END_OF_LIST.

DATA: LR_FOOTER TYPE REF TO CL_SALV_FORM_HEADER_INFO.


CLEAR L_TEXT.
L_TEXT = 'End of List as Footer'.
CREATE OBJECT LR_FOOTER
EXPORTING
TEXT = L_TEXT
TOOLTIP = L_TEXT.
LR_ALV->SET_END_OF_LIST( LR_FOOTER ).

Step 6: Display ALV

LR_ALV->DISPLAY( ). "display ALV

Final code will be

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.

DATA: LO_AGGRS TYPE REF TO CL_SALV_AGGREGATIONS.


LO_AGGRS = LR_ALV->GET_AGGREGATIONS( ). "get aggregations
* Add TOTAL for COLUMN NETWR
TRY.
CALL METHOD LO_AGGRS->ADD_AGGREGATION "add aggregation
EXPORTING
COLUMNNAME = 'MENGE' "aggregation column name
AGGREGATION = IF_SALV_C_AGGREGATION=>TOTAL. "aggregation type

CATCH CX_SALV_DATA_ERROR . "#EC NO_HANDLER


CATCH CX_SALV_NOT_FOUND . "#EC NO_HANDLER
CATCH CX_SALV_EXISTING . "#EC NO_HANDLER
ENDTRY.
* Bring the total line to top
DATA : LR_SORT TYPE REF TO CL_SALV_SORTS. "ALV sorts
CALL METHOD LR_ALV->GET_SORTS "get sorts
RECEIVING
VALUE = LR_SORT.
*TRY.
DATA : LR_SORT_COLUMN TYPE REF TO CL_SALV_SORT. "column sort
CALL METHOD LR_SORT->ADD_SORT "add column sort
EXPORTING
COLUMNNAME = 'EBELN' "sort column always key field
* POSITION =
* SEQUENCE = IF_SALV_C_SORT=>SORT_UP
* SUBTOTAL = IF_SALV_C_BOOL_SAP=>FALSE
* GROUP = IF_SALV_C_SORT=>GROUP_NONE
* OBLIGATORY = IF_SALV_C_BOOL_SAP=>FALSE
RECEIVING
VALUE = LR_SORT_COLUMN.
* TRY.
CALL METHOD LR_SORT_COLUMN->SET_SUBTOTAL "add subtotal
EXPORTING
VALUE = IF_SALV_C_BOOL_SAP=>TRUE.
* CATCH CX_SALV_DATA_ERROR .
* ENDTRY.
* CATCH CX_SALV_NOT_FOUND .
* CATCH CX_SALV_EXISTING .
* CATCH CX_SALV_DATA_ERROR .
*ENDTRY.
***Top of Page
DATA : LR_HEADER TYPE REF TO CL_SALV_FORM_ELEMENT.
DATA: LR_GRID_LAYOUT TYPE REF TO CL_SALV_FORM_LAYOUT_GRID,
LR_LABEL TYPE REF TO CL_SALV_FORM_LABEL,
LR_TEXT TYPE REF TO CL_SALV_FORM_TEXT,
L_TEXT TYPE STRING.
CREATE OBJECT LR_GRID_LAYOUT.
L_TEXT = 'Purchase Order Report' .
LR_GRID_LAYOUT->CREATE_HEADER_INFORMATION( "create header for gird
ROW = 1
COLUMN = 3
TEXT = L_TEXT
TOOLTIP = L_TEXT ).
LR_GRID_LAYOUT->ADD_ROW( ). "add row
LR_LABEL = LR_GRID_LAYOUT->CREATE_LABEL(
ROW = 2
COLUMN = 1
TEXT = 'Number of records found: '
TOOLTIP = 'Number of records found for your query' ).
LR_TEXT = LR_GRID_LAYOUT->CREATE_TEXT(
ROW = 2
COLUMN = 2
TEXT = LV_CNT
TOOLTIP = LV_CNT ).
LR_LABEL->SET_LABEL_FOR( LR_TEXT ).
LR_LABEL = LR_GRID_LAYOUT->CREATE_LABEL(
ROW = 3
COLUMN = 1
TEXT = 'Date : '
TOOLTIP = 'Date' ).
L_TEXT = SY-DATUM.
LR_TEXT = LR_GRID_LAYOUT->CREATE_TEXT(
ROW = 3
COLUMN = 2
TEXT = L_TEXT
TOOLTIP = L_TEXT ).
LR_LABEL->SET_LABEL_FOR( LR_TEXT ).
LR_HEADER = LR_GRID_LAYOUT.
CALL METHOD LR_ALV->SET_TOP_OF_LIST "set top of list
EXPORTING
VALUE = LR_HEADER.
***end of list
DATA: LR_FOOTER TYPE REF TO CL_SALV_FORM_HEADER_INFO.
CLEAR L_TEXT.
L_TEXT = 'End of List as Footer'.
CREATE OBJECT LR_FOOTER
EXPORTING
TEXT = L_TEXT
TOOLTIP = L_TEXT.
LR_ALV->SET_END_OF_LIST( LR_FOOTER ). "set end of list

LR_ALV->DISPLAY( ). "display ALV

Output

You might also like