0% found this document useful (0 votes)
14 views324 pages

Oops Notes

The document outlines programming paradigms, focusing on procedural and object-oriented programming, particularly in ABAP. It details features of object-oriented programming such as encapsulation, inheritance, and polymorphism, along with procedures for developing ABAP reports and creating classes. Additionally, it discusses constructors, method parameters, and the concepts of inheritance and polymorphism in depth, including method overloading and overriding.

Uploaded by

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

Oops Notes

The document outlines programming paradigms, focusing on procedural and object-oriented programming, particularly in ABAP. It details features of object-oriented programming such as encapsulation, inheritance, and polymorphism, along with procedures for developing ABAP reports and creating classes. Additionally, it discusses constructors, method parameters, and the concepts of inheritance and polymorphism in depth, including method overloading and overriding.

Uploaded by

Balagangadhar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Introduction:

Procedure oriented / structured programming / modular programming languages ---> C,


PASCAL, COBOL

--> Importance is given to Functions

Object oriented --> ABAP, C++, JAVA, .Net

---> Importance is given to securing the data

Features of OOPS Oriented Programming Languages

1. Encapsulation --> binding data and member functions into single unit (class)

2. Data abstraction / Data hiding --> hiding the implementation details

3. Inheritance ---> reusability (inheriting the components of parent class into


child class)

4. Polymorphism--> many forms behavior

Procedure followed for Basic ABAP Report development:

1. Declare variables, work areas, internal tables...

2. Generate selection screen for reading user input (parameters/select-options)

3. Validate the user input

4. Execute select queries to read data from db into work area / internal table

5. Process the work area/ internal table to show the content (data) to the user

Types Declaration Vs class declaration

types : begin of <type name>, (ty_emp) similar to -------> class declaration

field 1, empno components

field 2, ename

----
end of <type name>. endclass.

ty_emp-empno = 1. (invalid)

data wa_emp1 type ty_emp. similar to --> object1 based on class

wa_emp1-empno = 1.

wa_emp1-ename = 'Raju'.

data wa_emp2 type ty_emp. --> object2 based on class

wa_emp2-empno = 4.

wa_emp2-ename = 'Ramesh'.

Class ---> user defined data type which is a collection of components

c++ class ---> data members + member functions

java class --> instance variables + methods

abap class ---> attributes + methods + events + interfaces + aliases + macros

ABAP ---> 2 types of classes

1. Local class--> local to an object (program) ---> ABAP editor (se38)

2. Global class --> global to all objects ----> class builder tool (se24)

---> stored inside class pool

Procedure for creating local class:

1. Definition of class

Syntax:

class <class name> definition [public] [protected] [private]

[deferred] [final] [load] [abstract].

declaration of components.
endclass.

Note: if class definition contains method declarations, then we need to implement the class

2. Implementation of class

Syntax :

class <class name> implementation.

implementation of methods.

endclass.

Note: class definition and class implementation doesn't allocate any memory, it only
provides template of the class

Instantiate the class ---> creating the object for the class--> memory will be
allocated

Object creation --> 2 steps

1. Declare reference (alias) for the class

Syntax:

data <ref.name> type ref to <class name>.

Note: reference doesn't allocate any memory, it only provides alias.

2. Create object based on reference

Syntax:

create object <ref.name>. --> Memory will be allocated based on


components of class

Attribute: is like a variable which can be used to store the data.

Instance Attributes:

In local classes they are declared by using the keyword “Data”.

They are specific to object i.e. for every object, separate memory will be allocated for each
instance attribute.
They can be accessed only by using the Object.

Static Attributes:

In local classes they are declared by using the keyword “class-data”.

They are also called as “Class Variables / Class attributes”.

They are not specific to any object.

The memory for static attributes will be allocated whenever the class is loaded in to the
memory i.e. memory will be allocated only once which will be shared by all the objects of the
class.

They can be accessed either by using the object or by using the class name.

Constant Attributes:

In Local classes, they are declared by using the keyword ‘constants’. They must be initialized at
the time of declaration itself. The value of the constant attribute remains same throughout the
program.

They are not specific to any object.

The memory for them will be allocated whenever the class is loaded in to the memory i.e.
memory will be allocated only once which will be shared by all the objects of the class.

They can be accessed either by using the object or by using the class name.

Note: We go for instance attributes whenever we need to maintain unique (different) values for
each object. We go for static attributes, whenever we need to maintain a common (same) value
for all the objects of the class. We go for constant attributes, whenever we need to maintain a
fixed value for all the objects of the class.

Types Attributes

In Local classes, they are declared by using the keyword ‘Types’. Types attribute declaration
only provides template, but doesn’t allocated any memory. To make use of Types attributes, we
need to create references based on types attribute declaration. These references can be either
a normal data attribute / internal table / work area.

Methods:
- A method is a set of statements which is implemented only once and can be called any
no. of times.

- It is similar to subroutine / function module.

Subroutines: - 2 sections ---> definition ---> form...endform

Calling ---> perform <subroutine>.

Subroutine Parameters à keywords used à using, changing, tables

F.M's: 2 sections ---> definition --> function..endfunction (source code tab)

calling ---> call function <f.m>

Function Module Parameters à keywords used à importing, exporting, changing, tables

Local class methods: 3 steps

Declaration (Method prototype declaration) --> Inside Class definition

Implementation --> Inside Class Implementation

Calling --> outside class / inside other method implementation

Method Parameters:

Types of parameters: importing, exporting, changing and returning.

By Default, Importing parameters are obligatory.

We can use the keyword ‘optional’ as part of local class method parameters to declare
it as optional parameter and in case of global class methods, select the checkbox
‘optional’

Exporting parameters are always optional.

Procedure for using Local class methods:

1. Declaration

syntax:

methods/class-methods <method name> [parameters list].


2. Implementation

syntax:

method <method name>.

statements.

endmethod.

3. Calling

syntax 1:

call method <method name> [parameters list]

syntax 2:

<method name>( [parameters list] ).

Method Returning Values:

- In case of ABAP a method can return any no. of values. The no. of return values depends
on no of Exporting/changing Parameters.

Returning Parameters:

- In general, a method can return any no. of parameters.

- To restrict a method to return exactly one value, we must use returning parameter.

- If a method contains returning parameter it cannot contain exporting /changing


parameters at the same time or vice versa (prior EHP7)

- A method can contain only one returning parameter

- Returning parameter is always passed by value.

- Returning parameters are always optional.

Instance methods:

In local classes they are declared by using the keyword “Methods”.

They can be accessed only by using the object.


They can access any kind of components of the class (instance / static / constant /
types).

Static methods:

In local classes they are declared by using the keyword “class-Methods”.

They can be accessed either by using the object or class name.

They can access only static components, constant attributes and types attributes. i.e
they cannot access instance components.

Note: In Local classes, the sequence of visibility sections (access specifiers) for the class
components should be in the order of public, protected, private sections.

Note: It is recommended to define and implement the local class at the beginning of
executable program and explicitly handle the event ‘start-of-selection’ after the class
implementation to indicate the starting point of program execution.

Me keyword:

“Me” keyword refers to current object in execution i.e. as part of every instance method
execution (runtime), an implicitly created object (created by SAP) will be available and it
refers to the object which is currently executing the method.

Note: If a class contains both attributes and methods, it is recommended to declare


attributes under protected/private sections and methods under public sections.

Since we cannot access protected/private components outside the class, we need to


access them through one of the public method and call the public method outside the
class.

Friend keyword:

In General outside the class, A Object can access only public components of the class
directly. i.e protected and private components of the class cannot be accessed outside
the class using an object of a class.

In order to enable the objects of the class to access all components of the class outside
the class irrespective of the visibility, then we can go for Friend classes.

In local classes “Friend” is a keyword used as part of class definition to consider another
class as friend.
Deferred Keyword:

As part of declaring local friend classes, we need to forward declare the friend classes by
using “Deferred” keyword. This deferred keyword indicates that the class definition is
provided somewhere else in the program and not at the beginning of the program.

Note: In Global classes, we declare friend classes as part of ‘FRIENDS’ tab.

Load keyword:

It is used to load the global classes explicitly in the executable program. This is
mandatory before the release 6.20 for accessing static components of the global class
before instantiating them.

From 6.20, it is not required, but in case, if we get any compilation error while accessing
the static components / any other components of global class, then we can load the
global class explicitly from the class library by using load keyword.

Constructor:

- It is special method used for initializing the attributes of the class i.e. whenever an
object is created, the attributes should be initialized with some initial values.

- It is special because it is executed/called automatically whenever an object is created


(instance const) or whenever a class is loaded in the memory (static const).

- They are always declared in public section.

- They never return any value.

- There are two types of constructors

1. Instance constructor.

2. Static constructor.

Instance constructor

- They are declared by using the keyword “Constructor”.

- They can contain only importing parameters and exceptions.

- It is specific to object i.e. whenever a new object is created SAP executes “Instance
constructor”.

- Instance constructor is executed only once in the life time of every object.

Static constructor
- It is declared by using keyword “class_constructor”.

- It cannot contain any parameters or exceptions.

- It is not specific to any object.

- It is executed only once in the life time of every class. I.e. it is executed in either of the
following cases.

- Case 1: When we access any static components of the class before creating any objects
for the class. (or)

- Case 2: when we create first object of the class before accessing any static components
of the class.

Note:

If a class contains static and instance Constructor and if we instantiate first object before
accessing any static components, than SAP first executes Static Constructor and then the
Instance Constructor will be executed on behalf of that first object, from the second
object onwards only instance constructor will be executed.

If an instance constructor contains any mandatory importing parameters, we must pass


the values to those parameters while creating objects itself.

INHERITANCE

It is a process of using the properties (components) of one class inside other class.

The main aim of Inheritance is Reusability of the components i.e. a component declared
in one class can be accessed by other class.

In Inheritance, two classes are involved (super class/base class and sub class/derived
class).

The class which gives properties is called as super class / base class.

The class which takes the properties is called as sub-class/derived class.

Only Public and Protected components can be inherited i.e. Private Components cannot
be inherited.

Types of inheritance:
1. SINGLE INHERITANCE: A Class acquiring properties from only one super class.

2. MULTIPLE INHERITANCE: A class acquiring properties from more than one entity
(interface).

Note: In ABAP, Multiple inheritance can be implemented using the combination of class
and interfaces.

3. MULTILEVEL INHERITANCE: A class acquiring the properties from another sub-class.

Inheriting from – is a keyword used as part of local class definition for inheriting another
class.

A class declared as Final cannot be inherited i.e. it cannot have subclass. In case of local
classes, we use the keyword ‘final’ as part of class definition to declare a final class.

Polymorphism

Poly -> many,

Morph-> forms,

Ism-> behaviour

It is a process of making an entity behaving in multiple forms.

In this case, the entity is a method

Example:

Method overloading, Method Overriding.

Method Overloading:

It is similar to function overloading in C++.

It is a process of overloading the same method by passing different Number and


different types of parameters.

Eg: methods m1.

Methods m1 importing x type i.

Methods m1 importing x type c.


Methods m1 importing x type I y type i.

ABAP does not support method overloading.

Method Overriding:

- If a subclass redefines a super class method it is called as Method Overriding.

- Whenever a local subclass wants to redefine the super class method, the sub class as to
re-declare super class method by using “redefinition” keyword.

- Whenever the super class method is redefined in subclasses, we cannot change the
visibility / category.

- Only public and protected instance methods can be redefined.

- Whenever a subclass redefines super class method, it is always recommended to call the
super class method implementation in the subclass and this is done by using “super”
keyword.

- “Super” keyword is always used in subclass method implementations (inside redefined


super class method) to refer to super class method implementation.

- A class declared as final cannot be inherited.

- Static methods cannot be redefined

- Instance public / Instance protected methods declared as final can be inherited but
cannot be redefined.

- A static public/static protected methods cannot be declared as final because by default,


static methods cannot be redefined.

Hierarchy of constructor execution-scenario 1

- If a super class contains static and instance constructor and subclass without any
constructors, and if we instantiate first object of super class/sub class before accessing
any static components of super/sub class ,then SAP first executes static constructor of
super class and then instance constructor of super class and from second object of super
class/sub class only instance constructor of super class will be executed.

Hierarchy of constructor execution-scenario 2

- If a super class contains static and instance constructor and subclass only with static
constructor, and if we instantiate first object of sub class before accessing any static
components of super/sub class and also before creating any objects for super class, then
SAP first executes static constructors from super class to sub class and then instance
constructor of super class and from second object onwards, only instance constructor of
super class will be executed.

Hierarchy of constructor execution-scenario 3

- If a super class contains static and instance Constructor and subclass also with static and
instance constructor, then it is mandatory for sub class instance constructor to call the
super class instance constructor explicitly by using super keyword.

- In this case, if we instantiate first object of sub class before accessing any static
components of super/sub class and before creating any objects for super class, then SAP
first executes static constructors from super class to sub class (top to bottom) and then
instance constructors from sub class to super class (bottom to top) and from second
object of sub class onwards, only instance constructors will be executed from sub class
to super class.

Hierarchy of constructor execution-scenario 4

If a super class contains instance constructor with mandatory parameters and sub class
also contains instance constructor with mandatory parameters and whenever we
instantiate the sub class, make sure that you pass values for the parameters of sub class
instance constructors and from sub class instance constructor implementation we need
to call super class instance constructor by passing values for parameters of super class
instance constructor.

Hierarchy of constructor execution-scenario 5

If a super class contains instance constructor with mandatory parameters and sub class
without any instance constructor and whenever we instantiate the sub class, make sure
that you pass values for the parameters of super class instance constructors while
instantiating sub class object.

Visibility at Class Level - Public (default), protected, private and abstract

Public classes:

- The default visibility at class level is public.

- Public class can be instantiated anywhere (within same class/ inside subclasses /outside
classes).

- Public class can be inherited.

- The subclass inheriting the super public class is also created as public by default.

- The sub class inheriting the super public class can be created as explicit
protected/private.
Protected classes:

- Create protected is the keyword used as part of local class definition to create the
protected classes.

- Protected class can be inherited.

- Protected classes cannot be instantiated outside the classes but they can be instantiated
inside same classes as well as inside subclass methods.

- The subclass inheriting protecting class is also created as protected by default.

- The subclass inheriting protected class can be created as explicit public class (or) private
class.

- Private classes:

- Create private is keyword used for creating the private classes.

- Private class can be inherited.

- Private class cannot be instantiated outside the classes and inside subclasses, but they
can be instantiated within the same class. In order to instantiate private classes inside
subclasses or inside any other independent class, the private super class should consider
sub class / independent class as friend.

- The subclass inheriting the private class is also created as private by default.

- By default, the subclass inheriting the private class cannot be created as explicit public
class / explicit protected class. This can be made possible if the super private class
considers subclass as friend.

Friend keyword:

In local classes “Friend” is a keyword used as part of class definition to consider another
class as friend.

In this case friend class should be forward declared by using “Deferred” keyword. This
deferred keyword indicates that the class is defined somewhere else in the program and
not at the beginning of the program.

ABSTRACT CLASS:
Abstract is a class which contains one or more Abstract methods.

- An abstract method is a method which is just declared but not implemented.

- We declare a method as abstract when we are not sure about the implementation.

- The implementation for the abstract methods can be provided as part of subclasses.

- In local classes, abstract methods are declared by using a keyword “abstract”.

- If a class contains an abstract method, the class also should be declared as abstract.

- Abstract methods are declared only in public and protected sections.

- Abstract methods are also called as non-concrete methods.

- Static methods cannot be declared as abstract as they cannot be redefined inside


subclasses

- Constructors cannot be declared as abstract as they cannot be redefined inside


subclasses

- The subclass whichever is inheriting the abstract class must implement all the abstract
methods of the abstract super class otherwise the subclass also should be declared as
abstract.

- We cannot create the objects for abstract classes because they are not implemented
completely. But once abstract methods are implemented inside subclass, we can
instantiate the subclass and assign subclass object to abstract super class reference
which is called as narrow casting.

- Whenever narrow casting is done, by using super class object we can access methods of
super class only, in order to access direct methods of subclass using super class object,
we need to call the subclass method using dynamic calling approach.

INTERFACES

- An interface is a pure abstract class i.e. by default all the methods of interface are
abstract.

- Interface components doesn’t contain any explicit visibility section, by default all the
components are public.

- Interfaces cannot contain method implementations, it contains only method


declarations.

- A local class whichever want to implement an interface(local/global) , the class must


declare the interface in the public section by using ‘interfaces’ keyword and the class
must implement all the interface methods, Otherwise the class should be declared as
“abstract” and also the methods which are not implemented should be flagged
(declared) as abstract. In these cases, we must consider another class inheriting the
abstract class and provide the implementation for the abstract methods.

- The class which ever implements the interface is called as implementation class.

- Interface is always implemented in public section of local class.

- By using interfaces we can implement the concept of multiple inheritances.

- I.e. a class can implement any number of interfaces.

- Whenever we refer to interface components outside the interface, the component of


the interface must be prefixed with the name of the interface.

- Interfaces cannot contain constructors

- If a class implements any interface and if that implemented interface contains any
included interfaces, then class must also implement all the methods of the included
interfaces.

- If a global class has to implement one or more global interfaces, then global class must
declare those interfaces in the interfaces tab.

Aliases

Aliases are the alternative names provided for interface components .i.e. whenever we
refer to the interface components outside the interface, it must be prefixed with the
name of the interface. This lengthy naming representation can be avoided by declaring
“aliases” for interface components.

- Aliases can be declared in any of the visibility sections inside implementation class

- Aliases declared inside interfaces are always public

- Aliases can be declared either in interfaces / in the implementation class.

- If the aliases as to be declared in the interface it can be declared only for included
interfaces components and that alias will be created as component of interface.

Note: The included interfaces can be referred directly in the implementation class .i.e.
the interface which is included in other interface can be directly referred in the
implementation class.

Note: As Part of global classes, If a global class Includes any interface in the interfaces
tab and the checkbox ‘final’ is selected, it indicates that these interface methods cannot
be further implemented (re-defined) in other classes.
Example: Instance Vs Static Attributes

REPORT Z730OOPS1.

class lcl_abc DEFINITION.


public SECTION.
data x type i. "instance attr
class-data y type i. "static attr
endclass.

*x = 10. "syntax error


*y = 20. "syntax error
*lcl_abc->x = 10. "syntax error
*lcl_abc->y = 20. "syntax error
write :/ 'Static attr y is ',lcl_abc=>y.

lcl_abc=>y = 20.

write :/ 'Static attr y is ',lcl_abc=>y.


format color 3.
write :/ 'First Object ob1...'.
data ob1 type ref to lcl_abc. "reference / alias for class
create object ob1.
write :/ ob1->x,ob1->y.
ob1->x = 15.
ob1->y = 25.
write :/ ob1->x,ob1->y,lcl_abc=>y.
uline.
format color 7.
write :/ 'Second Object ob2...'.

data ob2 type ref to lcl_abc. "reference / alias for class


create object ob2.
write :/ ob2->x,ob2->y.

Example: Local class with Attributes and Methods, ME Keyword


REPORT Z730OOPS2.

class lcl_emp DEFINITION.


public SECTION.
methods : set, "instance method
display.
PROTECTED SECTION.
data : empno type i, "instance attr
ename(20) type c.
endclass.

class lcl_emp IMPLEMENTATION.


method display.
write :/ empno,ename. "(or)
* write :/ me->empno,me->ename.
endmethod.

method set.
* empno = 4. "(or)
me->empno = 4.
* ename = 'Ravi'. "(or)
me->ename = 'Ravi'.
endmethod.
endclass.

data ob1 type ref to lcl_emp.


START-OF-SELECTION.
create object ob1.

*write :/ ob1->empno,ob1->ename. "syntax error

*call method display. "syntax error


call method ob1->display.
ob1->display( ).

call method ob1->set.


call method ob1->display.

uline.
data ob2 type ref to lcl_emp.
create object ob2.
write :/ 'Values of object ob2...'.
call method ob2->display.
Example: Calling a Protected method from a Public Method

REPORT Z730OOPS3.

class lcl_emp DEFINITION.


public SECTION.
methods set.
PROTECTED SECTION.
data : empno type i,
ename(20) type c.
methods display.
endclass.

class lcl_emp IMPLEMENTATION.


method display.
write :/ empno,ename.
endmethod.

method set.
empno = 4.
ename = 'Ravi'.
* call method ob1->display. "syntax error
* call method display. "(or)
call method me->display.
endmethod.
endclass.

data ob1 type ref to lcl_emp.


START-OF-SELECTION.
create object ob1.

*call method ob1->display. "syntax error


call method ob1->set.

Example: Local class Methods with Importing Parameters

REPORT Z730OOPS4.

class lcl_emp DEFINITION.


public SECTION.
methods set IMPORTING i_empno type i i_ename type c OPTIONAL.
PROTECTED SECTION.
data : empno type i,
ename(20) type c.
methods display.
endclass.

class lcl_emp IMPLEMENTATION.


method display.
write :/ empno,ename.
endmethod.

method set.
empno = i_empno.
ename = i_ename.
call method display.
endmethod.
endclass.

data ob1 type ref to lcl_emp.


START-OF-SELECTION.
create object ob1.

PARAMETERS : p_empno type i,


p_ename(20) type c.

*call method ob1->set. "syntax error

call method ob1->set


EXPORTING
i_empno = p_empno.

uline.
call method ob1->set
EXPORTING
i_empno = p_empno
i_ename = p_ename.

uline.
ob1->set( EXPORTING i_empno = p_empno i_ename = p_ename ).

uline.
ob1->set( i_empno = p_empno i_ename = p_ename ).

Example: Local class Methods with Importing and Exporting Parameters

REPORT Z730OOPS5.
class lcl_customer DEFINITION.
PUBLIC SECTION.
methods getcustomer IMPORTING i_kunnr type kna1-kunnr
exporting e_name1 type kna1-name1
e_ort01 type kna1-ort01.
endclass.

class lcl_customer IMPLEMENTATION.


method getcustomer.
select single name1 ort01
from kna1
into (e_name1,e_ort01)
where kunnr = i_kunnr.
endmethod.
endclass.

START-OF-SELECTION.
data ob type ref to lcl_customer.
create object ob.

PARAMETERS p_kunnr type kna1-kunnr.

data : v_name1 type kna1-name1,


v_ort01 type kna1-ort01.

call method ob->getcustomer


EXPORTING
i_kunnr = p_kunnr.

format color 3.
write :/ 'Customer name :',v_name1,
/ 'Customer city :',v_ort01.
format color off.

uline.
call method ob->getcustomer
EXPORTING
i_kunnr = p_kunnr
IMPORTING
e_name1 = v_name1.

format color 7.
write :/ 'Customer name :',v_name1,
/ 'Customer city :',v_ort01.
format color off.

uline.
call method ob->getcustomer
EXPORTING
i_kunnr = p_kunnr
IMPORTING
e_name1 = v_name1
e_ort01 = v_ort01.

format color 1.
write :/ 'Customer name :',v_name1,
/ 'Customer city :',v_ort01.
format color off.

uline.
clear : v_name1,
v_ort01.
ob->getcustomer( EXPORTING i_kunnr = '0000001001'
IMPORTING e_name1 = v_name1
e_ort01 = v_ort01 ).

format color 1.
write :/ 'Customer name :',v_name1,
/ 'Customer city :',v_ort01.
format color off.

Example: Instance Vs Static Methods

REPORT Z730OOPS6.

class lcl_abc DEFINITION.


public SECTION.
methods m1. "instance method
class-METHODS m2. "static method
PROTECTED SECTION.
data x type i. "instance attr
class-data y type i. "static attr
constants z type i value 10. "const attr
types k type i.
endclass.

class lcl_abc IMPLEMENTATION.


method m1.
format color 3.
write :/ 'inside instance method m1...'.
data v_k type k.
write :/ x,y,z,v_k.
format color off.
endmethod.

method m2.
format color 7.
write :/ 'inside static method m2...'.
data v_k1 type k.
write :/ y,z,v_k1.
format color off.
endmethod.
endclass.

START-OF-SELECTION.
* call method lcl_abc=>m1. "syntax error
call method lcl_abc=>m2.

data ob type ref to lcl_abc.


create object ob.
write :/ 'Accessed using Object ob...'.
call method : ob->m1,
ob->m2.

Example: Returning Parameters

REPORT Z730OOPS7.

class lcl_customer DEFINITION.


PUBLIC SECTION.
methods getcustomer IMPORTING i_kunnr type kna1-kunnr
RETURNING value(e_name1) type kna1-name1.
endclass.

class lcl_customer IMPLEMENTATION.


method getcustomer.
select single name1 from kna1
into e_name1
where kunnr = i_kunnr.
endmethod.
endclass.
START-OF-SELECTION.
data ob type ref to lcl_customer.
create object ob.

PARAMETERS p_kunnr type kna1-kunnr.

data v_name1 type kna1-name1.

call method ob->getcustomer


EXPORTING
i_kunnr = p_kunnr.

write :/ 'Customer name :',v_name1.

clear v_name1.
call method ob->getcustomer
EXPORTING
i_kunnr = p_kunnr
RECEIVING
e_name1 = v_name1.

write :/ 'Customer name :',v_name1.

uline.
clear v_name1.
v_name1 = ob->getcustomer( EXPORTING i_kunnr = p_kunnr ).
write :/ 'Customer name :',v_name1.

Example: Friend classes (Local classes)

REPORT Z730OOPS8.

class lcl_pqr DEFINITION DEFERRED.

class lcl_abc DEFINITION FRIENDS lcl_pqr.


public SECTION.
methods m1.
PROTECTED SECTION.
methods m2.
PRIVATE SECTION.
methods m3.
endclass.
class lcl_abc IMPLEMENTATION.

method m1.
write :/ 'Inside public method m1 of lcl_abc...'.
endmethod.

method m2.
write :/ 'Inside protected method m2 of lcl_abc...'.
endmethod.

method m3.
write :/ 'Inside private method m3 of lcl_abc...'.
endmethod.

endclass.

class lcl_pqr DEFINITION.


public SECTION.
methods m4.
endclass.

class lcl_pqr IMPLEMENTATION.


method m4.
write :/ 'inside public method m4 of lcl_pqr...'.
data k type ref to lcl_abc.
create object k.
call method k->m1.
call method k->m2.
call method k->m3.
endmethod.
endclass.

START-OF-SELECTION.
data ob type ref to lcl_pqr.
create object ob.

call method ob->m4.

Example: Global classes (Types, Attributes, Methods)


Click on => (direct type entry), provide the following declaration in the editor

types : begin of TY_VBAK,


vbeln type vbak-vbeln,
erdat type vbak-erdat,
erzet type vbak-erzet,
ernam type vbak-ernam,
end of ty_vbak.

data t_vbak type table of ty_vbak.

Save and comeback

Because of above data declaration, SAP creates instance attribute in the attributes tab
Methods:
Parameters for method ‘Getsalesorders’
Implementation of methods (Double click on each method):

method GETSALESORDERS.
refresh t_vbak.
select vbeln erdat erzet ernam
from vbak
into table t_vbak
where kunnr = i_kunnr.
if sy-subrc eq 0.
call method display.
else.
message 'No Sales Orders for given customer' type 'I'.
endif.
endmethod.

method DISPLAY.
data wa_vbak type ty_vbak.
loop at t_vbak into wa_vbak.
write :/ wa_vbak-vbeln,
wa_vbak-erdat,
wa_vbak-erzet,
wa_vbak-ernam.
endloop.
endmethod.

Save, syntax check and activate global class

Executable Program: Accessing above global class

REPORT Z730OOPS9.

data ob type ref to z730class1.


CREATE OBJECT OB.

PARAMETERS p_kunnr type kna1-kunnr.

CALL METHOD OB->GETSALESORDERS


EXPORTING
I_KUNNR = p_kunnr.

Example: LOAD Keyword


Implementation of method ‘M1’:

method M1.
write :/ 'inside static method ...'.
x = 10.
write :/ x.
endmethod.

Save, activate global class

Executable Program:

REPORT Z730OOPS10.

class z730class2 DEFINITION load.

write :/ z730class2=>x.
call method z730class2=>m1.

Example: Global Friend classes

Class 1:
Implementations:

method M1.
write :/ 'inside instance public method m1 of class3...'.
endmethod.

method M2.
write :/ 'inside instance protected method m2 of class3...'.
endmethod.

method M3.
write :/ 'inside instance private method m3 of class3...'.
endmethod.

Class 2:
Activate class 2 and go back to class 1 for adding this class 2 as friend to class 1
Save, activate class 3

Go back to class 4 method ‘m4’ for implementation:

method M4.
write :/ 'inside instance public method m4 of class4...'.
data ob type ref to z739class3.
create object ob.
call method ob->m1.
call method ob->m2.
call method ob->m3.
endmethod.

Executable program:

REPORT Z730OOPS11.

data ob type ref to z739class4.


create object ob.

call method ob->m4.


Example: Instance Constructor initializing attributes of class

REPORT Z730OOPS12.

class lcl_emp DEFINITION.


PUBLIC SECTION.
methods display.
methods constructor. "instance constructor
PROTECTED SECTION.
data : empno type i,
ename(20) type c.
endclass.

class lcl_emp IMPLEMENTATION.


method display.
write :/ empno,ename.
ENDMETHOD.

method constructor.
empno = 4.
ename = 'Ravi'.
endmethod.
endclass.

START-OF-SELECTION.
data ob1 type ref to lcl_emp.
create object ob1.

call method ob1->display.

*call method ob1->constructor. "syntax error

Example: Parameters to Instance Constructors

REPORT Z730OOPS13.
class lcl_emp DEFINITION.
PUBLIC SECTION.
methods display.
methods constructor IMPORTING i_empno type i i_ename type c. "instance constructor
PROTECTED SECTION.
data : empno type i,
ename(20) type c.
endclass.

class lcl_emp IMPLEMENTATION.


method display.
write :/ empno,ename.
ENDMETHOD.

method constructor.
empno = i_empno.
ename = i_ename.
endmethod.
endclass.

START-OF-SELECTION.
data ob1 type ref to lcl_emp.
*create object ob1. "syntax error as mandatory parameter values are not passed

PARAMETERS : p_empno type i,


p_ename(20) type c.

create object ob1


EXPORTING
i_empno = p_empno
i_ename = p_ename.

call method ob1->display.

Example: Instance Vs Static Constructor Execution

REPORT Z730OOPS14.

class lcl_abc DEFINITION.


PUBLIC SECTION.
methods constructor. "instance const
class-methods class_constructor. "static const
class-data x type i. "static attr
endclass.
class lcl_abc IMPLEMENTATION.

method constructor.
write :/ 'Inside instance constructor...'.
endmethod.

method class_constructor.
write :/ 'Inside static constructor...'.
endmethod.

endclass.

START-OF-SELECTION.
*lcl_abc=>x = 10. "static const.gets executed

format color 3.
write :/ 'First object ob1...'.
data ob1 type ref to lcl_abc.
create object ob1.

uline.
format color 7.
write :/ 'Second object ob2...'.
data ob2 type ref to lcl_abc.
create object ob2.

uline.
format color 1.
write :/ 'Third object ob3...'.
data ob3 type ref to lcl_abc.
create object ob3.

Example: Inheritance – Local Classes

REPORT Z730OOPS15.

*class lcl_vehicle DEFINITION final. "cannot be inherited


class lcl_vehicle DEFINITION.
public SECTION.
methods display.
protected SECTION.
data : wheels type i,
brakes type i.
endclass.

class lcl_vehicle IMPLEMENTATION.


method display.
write :/ wheels,brakes.
endmethod.
endclass.

class lcl_cycle DEFINITION INHERITING FROM lcl_vehicle.


PUBLIC SECTION.
methods setcycle.
PROTECTED SECTION.
data color type string.
endclass.

class lcl_cycle IMPLEMENTATION.


method setcycle.
wheels = 2.
brakes = 2.
color = 'Green'.
write :/ 'Color of cycle is ',color.
endmethod.
endclass.

class lcl_bike DEFINITION INHERITING FROM lcl_cycle.


PUBLIC SECTION.
methods setbike.
PROTECTED SECTION.
data gears type i.
endclass.

class lcl_bike IMPLEMENTATION.


method setbike.
wheels = 2.
brakes = 1.
color = 'Red'.
gears = 5.
write :/ 'color of bike :',color,
/ 'Gears in bike :',gears.
endmethod.
endclass.

START-OF-SELECTION.
data ob1 type ref to lcl_cycle.
create object ob1.

format color 3.
call method : ob1->setcycle,
ob1->display.

uline.
format color 7.
data ob2 type ref to lcl_bike.
create object ob2.

call method : ob2->setbike,


ob2->display.

Example: Inheritance – Global Classes

Global class: ZCL_VEHICLE


Implementation for Display method:

method DISPLAY.
write :/ wheels,brakes.
endmethod.

Global class: ZCL_CYCLE inheriting ZCL_VEHICLE


Implementation of SETCYCLE Method:

method SETCYCLE.
wheels = 2.
brakes = 2.
color = 'Green'.
write :/ 'Color of cycle is ',color.

endmethod.

Global class: ZCL_BIKE inheriting from ZCL_CYCLE


Implementation for SETBIKE method:

method SETBIKE.
wheels = 2.
brakes = 1.
color = 'Red'.
gears = 5.
write :/ 'color of bike :',color,
/ 'Gears in bike :',gears.
endmethod.

Executable Program: For accessing above Global Inherited classes

REPORT Z730OOPS16.

data ob1 type ref to zcl_cycle.


create object ob1.

format color 3.
call method : ob1->setcycle,
ob1->display.

uline.
format color 7.
data ob2 type ref to zcl_bike.
create object ob2.

call method : ob2->setbike,


ob2->display.

Example: Method Overloading (Not Supported)

REPORT Z730OOPS17.

class lcl_abc DEFINITION.


public SECTION.
methods m1.
methods m1 IMPORTING x type i.
methods m1 importing x type c.
methods m1 IMPORTING x type i y type c.
endclass.

Example: Method Overriding (Local Classes)

REPORT Z730OOPS18.

class lcl_abc DEFINITION.


PUBLIC SECTION.
methods m1.
methods m2 final.
class-methods m3.
endclass.

class lcl_abc IMPLEMENTATION.


method m1.
write :/ 'inside instance method m1 of super class abc...'.
endmethod.

method m2.
write :/ 'inside instance final method m2 of super class abc...'.
endmethod.

method m3.
write :/ 'inside static method m3 of super class abc...'.
endmethod.

endclass.

class lcl_pqr DEFINITION INHERITING FROM lcl_abc.


public SECTION.
methods m1 REDEFINITION.
* methods m2 REDEFINITION. "syntax error
* class-methods m3 redefinition. "syntax error
endclass.

class lcl_pqr IMPLEMENTATION.


method m1.
write :/ 'inside instance method m1 of sub class pqr...'.
* call method m1. "recursion
call method super->m1.
endmethod.
endclass.

class lcl_xyz DEFINITION INHERITING FROM lcl_pqr.


public SECTION.
methods m1 REDEFINITION.
endclass.

class lcl_xyz IMPLEMENTATION.


method m1.
write :/ 'inside instance method m1 of sub class xyz...'.
call method super->m1.
endmethod.
endclass.

START-OF-SELECTION.
data ob type ref to lcl_xyz.
create object ob.

call method ob->m1.


call method ob->m2.
Example: Method Overriding (Global Classes)

Global class 1: ZCL_ABC


Declare method ‘M2’ as final (select the method attributes view button  select the
checkbox ‘final’)
Implementation for methods:

method M1.
write :/ 'inside instance method m1 of super class abc...'.
endmethod.

method M2.
write :/ 'inside instance final method m2 of super class abc...'.
endmethod.

method M3.
write :/ 'inside static method m3 of super class abc...'.
endmethod.

Global class 2: ZCL_PQR Inheriting Global class ZCL_ABC


In methods tab, redefine the method ‘M1’ (select the method, click on ‘redefine’ button)
method M1.
write :/ 'inside instance method m1 of sub class pqr...'.
CALL METHOD SUPER->M1.
endmethod.

Global class 3: ZCL_XYZ Inheriting Global class ZCL_PQR


In methods tab, redefine the method ‘M1’ (select the method, click on ‘redefine’ button)
method M1.
write :/ 'inside instance method m1 of sub class xyz...'.
CALL METHOD SUPER->M1.

endmethod.

Executable Program:

REPORT Z730OOPS19.

data ob type ref to zcl_xyz.


create object ob.

call method ob->m1.


uline.
call method ob->m2.
uline.
call method ob->m3.
Example: Hierarchy of constructor execution-scenario 1

REPORT Z730OOPS20.

class lcl_super DEFINITION.


public SECTION.
methods constructor.
class-methods class_constructor.
endclass.

class lcl_super IMPLEMENTATION.


method constructor.
write :/ 'inside instance const. of super class...'.
ENDMETHOD.

method class_constructor.
write :/ 'inside static const. of super class...'.
ENDMETHOD.
endclass.

class lcl_sub DEFINITION INHERITING FROM lcl_super.

endclass.

START-OF-SELECTION.
*format color 2.
*write :/ 'First object of super class...'.
*data ob type ref to lcl_super.
*create object ob.

format color 3.
write :/ 'First object of sub class...'.
data ob1 type ref to lcl_sub.
create object ob1.

uline.
format color 7.
write :/ 'Second object of sub class...'.
data ob2 type ref to lcl_sub.
create object ob2.

uline.
format color 1.
write :/ 'Third object of sub class...'.
data ob3 type ref to lcl_sub.
create object ob3.

Example: Hierarchy of constructor execution-scenario 2

REPORT Z730OOPS21.

class lcl_super DEFINITION.


public SECTION.
methods constructor.
class-methods class_constructor.
endclass.

class lcl_super IMPLEMENTATION.


method constructor.
write :/ 'inside instance const. of super class...'.
ENDMETHOD.

method class_constructor.
write :/ 'inside static const. of super class...'.
ENDMETHOD.
endclass.

class lcl_sub DEFINITION INHERITING FROM lcl_super.


PUBLIC SECTION.
class-methods class_constructor.
endclass.

class lcl_sub IMPLEMENTATION.


method class_constructor.
write :/ 'inside static const. of sub class...'.
endmethod.
endclass.

START-OF-SELECTION.
*format color 2.
*write :/ 'First object of super class...'.
*data ob type ref to lcl_super.
*create object ob.

format color 3.
write :/ 'First object of sub class...'.
data ob1 type ref to lcl_sub.
create object ob1.
uline.
format color 7.
write :/ 'Second object of sub class...'.
data ob2 type ref to lcl_sub.
create object ob2.

uline.
format color 1.
write :/ 'Third object of sub class...'.
data ob3 type ref to lcl_sub.
create object ob3.

Example: Hierarchy of constructor execution-scenario 3

REPORT Z730OOPS22.

class lcl_super DEFINITION.


public SECTION.
methods constructor.
class-methods class_constructor.
endclass.

class lcl_super IMPLEMENTATION.


method constructor.
write :/ 'inside instance const. of super class...'.
ENDMETHOD.

method class_constructor.
write :/ 'inside static const. of super class...'.
ENDMETHOD.
endclass.

class lcl_sub DEFINITION INHERITING FROM lcl_super.


PUBLIC SECTION.
methods constructor.
class-methods class_constructor.
endclass.
class lcl_sub IMPLEMENTATION.

method constructor.
write :/ 'inside instance const. of sub class...'.
call method super->constructor.
ENDMETHOD.

method class_constructor.
write :/ 'inside static const. of sub class...'.
endmethod.
endclass.

START-OF-SELECTION.
*format color 2.
*write :/ 'First object of super class...'.
*data ob type ref to lcl_super.
*create object ob.

format color 3.
write :/ 'First object of sub class...'.
data ob1 type ref to lcl_sub.
create object ob1.

uline.
format color 7.
write :/ 'Second object of sub class...'.
data ob2 type ref to lcl_sub.
create object ob2.

uline.
format color 1.
write :/ 'Third object of sub class...'.
data ob3 type ref to lcl_sub.
create object ob3.

Example: Hierarchy of constructor execution-scenario 4

REPORT Z730OOPS23.

class lcl_super DEFINITION.


public SECTION.
methods constructor IMPORTING x type i.
endclass.
class lcl_super IMPLEMENTATION.
method constructor.
write :/ 'inside instance const. of super class...'.
write :/ 'Parameter received for super class instance const is ',x.
ENDMETHOD.

endclass.

class lcl_sub DEFINITION INHERITING FROM lcl_super.


PUBLIC SECTION.
methods constructor importing x type c.
endclass.

class lcl_sub IMPLEMENTATION.

method constructor.
write :/ 'inside instance const. of sub class...'.
write :/ 'Parameter received for sub class instance const is ',x.
* call method super->constructor. "syntax error
call method super->constructor
exporting
x = 10.
ENDMETHOD.

endclass.

START-OF-SELECTION.

format color 3.
write :/ 'First object of sub class...'.
data ob1 type ref to lcl_sub.
*create object ob1. "syntax error
create object ob1
EXPORTING
x = 'ABAP'.

Example: Hierarchy of constructor execution-scenario 5


REPORT Z730OOPS24.

class lcl_super DEFINITION.


public SECTION.
methods constructor IMPORTING x type i.
endclass.

class lcl_super IMPLEMENTATION.


method constructor.
write :/ 'inside instance const. of super class...'.
write :/ 'Parameter received for super class instance const is ',x.
ENDMETHOD.

endclass.

class lcl_sub DEFINITION INHERITING FROM lcl_super.

endclass.

START-OF-SELECTION.
PARAMETERS p_x type i.

format color 3.
write :/ 'First object of sub class...'.
data ob1 type ref to lcl_sub.
create object ob1
EXPORTING
x = p_x.

Example: Polymorphism Scenario

REPORT Z730OOPS25.

class lcl_enquiry DEFINITION.


PUBLIC SECTION.
methods : calc_disc IMPORTING i_fees type i,
display_fees.
PROTECTED SECTION.
data totalfees type i.
endclass.

class lcl_enquiry IMPLEMENTATION.

method calc_disc.
totalfees = ( i_fees - i_fees * 5 / 100 ).
endmethod.

method display_fees.
write :/ 'Total Fees after discount :',totalfees.
ENDMETHOD.

endclass.

class lcl_enquiry_r DEFINITION INHERITING FROM lcl_enquiry.


PUBLIC SECTION.
methods calc_disc REDEFINITION.
endclass.

class lcl_enquiry_r IMPLEMENTATION.

method calc_disc.
totalfees = ( i_fees - i_fees * 10 / 100 ).
ENDMETHOD.

endclass.

class lcl_enquiry_v DEFINITION INHERITING FROM lcl_enquiry.


PUBLIC SECTION.
methods calc_disc REDEFINITION.
endclass.

class lcl_enquiry_v IMPLEMENTATION.

method calc_disc.
totalfees = ( i_fees - i_fees * 15 / 100 ).
ENDMETHOD.

endclass.
data ob type ref to lcl_enquiry.

START-OF-SELECTION.
PARAMETERS : p_fees type i,
p_no_enq type i.

if p_no_enq > 0.
case p_no_enq.
when 1.
create object ob type lcl_enquiry.
call method ob->calc_disc
exporting
i_fees = p_fees.
when 2 or 3.
create object ob type lcl_enquiry_r.
call method ob->calc_disc
exporting
i_fees = p_fees.
when others.
create object ob type lcl_enquiry_v.
call method ob->calc_disc
exporting
i_fees = p_fees.
endcase.
call method ob->display_fees.
else.
message 'Please enter Positive values for no.of enquiries' type 'I'.
endif.

Example: Public Classes

REPORT Z730OOPS26.

*class lcl_abc DEFINITION. "(or)


class lcl_abc DEFINITION create public.
PUBLIC SECTION.
methods m1.
endclass.

class lcl_abc IMPLEMENTATION.


method m1.
data k type ref to lcl_abc.
create object k.
endmethod.
endclass.

class lcl_pqr DEFINITION INHERITING FROM lcl_abc.


PUBLIC SECTION.
methods m2.
endclass.

class lcl_pqr IMPLEMENTATION.


method m2.
data k type ref to lcl_abc.
create object k.
endmethod.
endclass.

class lcl_xyz DEFINITION create protected INHERITING FROM lcl_abc.


endclass.

class lcl_mnr DEFINITION create private INHERITING FROM lcl_abc.


endclass.

START-OF-SELECTION.
data ob type ref to lcl_abc.
create object ob.

data ob1 type ref to lcl_pqr.


create object ob1.

Example: Protected Classes

REPORT Z730OOPS27.

class lcl_abc DEFINITION create protected.


PUBLIC SECTION.
methods m1.
endclass.

class lcl_abc IMPLEMENTATION.


method m1.
data k type ref to lcl_abc.
create object k.
endmethod.
endclass.

class lcl_pqr DEFINITION INHERITING FROM lcl_abc.


PUBLIC SECTION.
methods m2.
endclass.

class lcl_pqr IMPLEMENTATION.


method m2.
data k type ref to lcl_abc.
create object k.
endmethod.
endclass.

class lcl_xyz DEFINITION create public INHERITING FROM lcl_abc.


endclass.

class lcl_mnr DEFINITION create private INHERITING FROM lcl_abc.


endclass.

START-OF-SELECTION.
data ob type ref to lcl_abc.
*create object ob. "syntax error
*
data ob1 type ref to lcl_pqr.
*create object ob1. "syntax error

Example: Private classes

REPORT Z730OOPS28.

class lcl_xyz DEFINITION DEFERRED. "forward declaration


class lcl_mnr4 DEFINITION DEFERRED. "forward declaration
class lcl_mnr5 DEFINITION DEFERRED. "forward declaration

class lcl_abc DEFINITION create private FRIENDS lcl_xyz lcl_mnr4 lcl_mnr5.


PUBLIC SECTION.
methods m1.
endclass.

class lcl_abc IMPLEMENTATION.


method m1.
data k type ref to lcl_abc.
create object k.
endmethod.
endclass.
*
class lcl_pqr DEFINITION INHERITING FROM lcl_abc.
PUBLIC SECTION.
methods m2.
endclass.

class lcl_pqr IMPLEMENTATION.


method m2.
data k type ref to lcl_abc.
* create object k. "syntax error
endmethod.
endclass.

class lcl_xyz DEFINITION INHERITING FROM lcl_abc.


public SECTION.
methods m3.
endclass.

class lcl_xyz IMPLEMENTATION.


method m3.
data k type ref to lcl_abc.
create object k.
ENDMETHOD.
endclass.

class lcl_mnr1 DEFINITION INHERITING FROM lcl_abc.


endclass.

*class lcl_mnr2 DEFINITION create public INHERITING FROM lcl_abc. "syntax error
*endclass.

*class lcl_mnr3 DEFINITION create protected INHERITING FROM lcl_abc. "syntax error
*endclass.

class lcl_mnr4 DEFINITION create public INHERITING FROM lcl_abc.


endclass.

class lcl_mnr5 DEFINITION create protected INHERITING FROM lcl_abc.


endclass.

*
*START-OF-SELECTION.
data ob type ref to lcl_abc.
*create object ob. "syntax error
**
*data ob1 type ref to lcl_pqr.
**create object ob1. "syntax error

Example: Local Abstract classes

REPORT Z730OOPS29.

class lcl_restaurant DEFINITION ABSTRACT.


public SECTION.
methods : store,
display,
payment abstract.
PROTECTED SECTION.
data : tableno type i,
steward type string.
endclass.

class lcl_restaurant IMPLEMENTATION.


method store.
tableno = 3.
steward = 'ABC'.
endmethod.

method display.
write :/ tableno,steward.
endmethod.
endclass.

class lcl_cheque DEFINITION INHERITING FROM lcl_restaurant.


PUBLIC SECTION.
methods payment REDEFINITION.
methods m1.
PROTECTED SECTION.
data : chequeno type i,
chequedate type d,
bankname type string,
amt type i.
endclass.

class lcl_cheque IMPLEMENTATION.


method payment.
chequeno = 123.
chequedate = sy-datum.
bankname = 'RBS'.
amt = 12000.
write :/ chequeno,chequedate,bankname,amt.
endmethod.

method m1.
write :/ 'inside method m1 of cheque class..'.
endmethod.
endclass.

class lcl_creditcard DEFINITION INHERITING FROM lcl_restaurant.


PUBLIC SECTION.
methods payment REDEFINITION.
PROTECTED SECTION.
data : ccno type i,
bname type string,
amt type i.
endclass.

class lcl_creditcard IMPLEMENTATION.


method payment.
ccno = 32123.
bname = 'ICICI'.
amt = 11000.
write :/ ccno,bname,amt.
endmethod.
endclass.

START-OF-SELECTION.
data r type ref to lcl_restaurant.
*create object r. "syntax error

format color 3.
write :/ 'Cheque class object ....'.
data cq type ref to lcl_cheque.
create object cq.

call method : cq->store,


cq->display,
cq->payment,
cq->m1.

uline.
format color 7.
write :/ 'Cheque class object --> Abstract class restaurant reference..'.
r = cq. "narrow casting
call method : r->store,
r->display,
r->payment,
* r->m1. "syntax error
r->('M1'). "dynamic call

uline.
format color 3.
write :/ 'Credit card class object ....'.
data cc type ref to lcl_creditcard.
create object cc.

call method : cc->store,


cc->display,
cc->payment.

Example: Global Abstract Classes

Class: ZCL_RESTAURANT (Abstract class)


Method ‘Payment’ declared as abstract (details view button)
Implementations for other 2 methods:

method STORE.
tableno = 1.
steward = 'ABC'.

endmethod.

method DISPLAY.
write :/ tableno,steward.
endmethod.

Class: ZCL_CHEQUE Inheriting ZCL_RESTAURANT


Method Payment Redefined:

method PAYMENT.
chequeno = 123.
chequedate = sy-datum.
bankname = 'RBS'.
amt = 12000.
endmethod.

Method Display Redefined:

method DISPLAY.
CALL METHOD SUPER->DISPLAY.
write :/ chequeno,chequedate,bankname,amt.
endmethod.

Class: ZCL_CREDITCARD Inheriting ZCL_RESTAURANT


Method Payment Redefined:

method PAYMENT.
ccno = 32123.
bname = 'ICICI'.
amt = 11000.
endmethod.

Method Display Redefined:

method DISPLAY.
CALL METHOD SUPER->DISPLAY.
write :/ ccno,bname,amt.
endmethod.

Executable Program:
REPORT Z730OOPS30.

data r type ref to zcl_restaurant.


*create object r. "syntax error

format color 3.
write :/ 'Cheque class object ....'.
data cq type ref to zcl_cheque.
create object cq.

call method : cq->store,


cq->payment,
cq->display.

uline.
format color 7.
write :/ 'Cheque class object --> Abstract class restaurant reference..'.
r = cq. "narrow casting
call method : r->store,
r->payment,
r->display.

uline.
format color 3.
write :/ 'Credit card class object ....'.
data cc type ref to zcl_creditcard.
create object cc.

call method : cc->store,


cc->payment,
cc->display.

Example: Local Interfaces (Multiple Inheritance Implementation)

REPORT Z730OOPS31.

interface rectangle.
constants : length type i value 10,
breadth type i value 5.
methods : area,
perimeter.
endinterface.

interface square.
constants side type i value 10.
methods : area,
perimeter.
endinterface.

class lcl_abc DEFINITION. "implementation class


Public SECTION.
interfaces : rectangle,
square.
PROTECTED SECTION.
data res type i.
endclass.

class lcl_abc IMPLEMENTATION.


method rectangle~area.
clear res.
res = rectangle~length * rectangle~breadth.
write :/ 'Area of rectangle is ',res.
endmethod.

method rectangle~perimeter.
clear res.
res = 2 * ( rectangle~length + rectangle~breadth ).
write :/ 'Perimeter of rectangle is ',res.
endmethod.

method square~area.
clear res.
res = square~side * square~side.
write :/ 'Area of square is ',res.
endmethod.

method square~perimeter.
clear res.
res = 4 * square~side.
write :/ 'Perimeter of square is ',res.
endmethod.

endclass.
START-OF-SELECTION.

data ob type ref to lcl_abc.


create object ob.

call method : ob->rectangle~area,


ob->rectangle~perimeter,
ob->square~area,
ob->square~perimeter.

uline.
data r type ref to rectangle.
*create object r. "syntax error
r = ob. "narrow casting
call method : r->area,
r->perimeter.

uline.
data s type ref to square.
*create object s. "syntax error
s = ob. "narrow casting
call method : s->area,
s->perimeter.

Example: Implementing Global Interfaces

Interface 1: ZIF_RECTANGLE
Interface 2: ZIF_SQUARE
Implementation class: Implementing above 2 interfaces
Implementations for interface methods:

method ZIF_RECTANGLE~AREA.
clear res.
res = zif_rectangle~length * zif_rectangle~breadth.
write :/ 'Area of rectangle is ',res.
endmethod.

method ZIF_RECTANGLE~PERIMETER.
clear res.
res = 2 * ( zif_rectangle~length + zif_rectangle~breadth ).
write :/ 'Perimeter of rectangle is ',res.
endmethod.

method ZIF_SQUARE~AREA.
clear res.
res = zif_square~side * zif_square~side.
write :/ 'Area of square is ',res.
endmethod.
method ZIF_SQUARE~PERIMETER.
clear res.
res = 4 * zif_square~side.
write :/ 'Perimeter of square is ',res.
endmethod.

Executable Program: Accessing above interface components through Implementation class

REPORT Z730OOPS32.

data ob type ref to zcl_im_rectangle_square.


create object ob.

call method : ob->zif_rectangle~area,


ob->zif_rectangle~perimeter,
ob->zif_square~area,
ob->zif_square~perimeter.

uline.
data r type ref to zif_rectangle.
*create object r. "syntax error
r = ob. "narrow casting
call method : r->area,
r->perimeter.

uline.
data s type ref to zif_square.
*create object s. "syntax error
s = ob. "narrow casting
call method : s->area,
s->perimeter.

Example: Implementing Local Interfaces Partially

REPORT Z730OOPS33.

interface abc.
methods : m1,
m2,
m3.
endinterface.
class lcl_impl1 DEFINITION ABSTRACT.
public SECTION.
interfaces abc ABSTRACT METHODS m2 m3.
endclass.

class lcl_impl1 IMPLEMENTATION.


method abc~m1.
write :/ 'Inside interface method m1 of implementation class impl1....'.
ENDMETHOD.
endclass.

class lcl_impl2 DEFINITION INHERITING FROM lcl_impl1.


PUBLIC SECTION.
methods : abc~m2 REDEFINITION,
abc~m3 REDEFINITION.
endclass.

class lcl_impl2 IMPLEMENTATION.


method abc~m2.
write :/ 'Inside interface method m2 of implementation class impl2....'.
ENDMETHOD.

method abc~m3.
write :/ 'Inside interface method m3 of implementation class impl2....'.
ENDMETHOD.
endclass.

START-OF-SELECTION.
data ob type ref to lcl_impl2.
create object ob.

call method : ob->abc~m1,


ob->abc~m2,
ob->abc~m3.

Example: Including a Interface inside other Interface

REPORT Z730OOPS34.

interface intf1.
methods : m1,
m2.
endinterface.
interface intf2.
methods m3.
interfaces intf1.
endinterface.

class lcl_impl DEFINITION.


PUBLIC SECTION.
* interfaces intf1 ALL METHODS ABSTRACT.
interfaces intf2.
endclass.

class lcl_impl IMPLEMENTATION.


method intf2~m3.
write :/ 'inside method m3 of interface intf2...'.
endmethod.

method intf1~m1.
write :/ 'inside method m1 of interface intf1...'.
endmethod.

method intf1~m2.
write :/ 'inside method m2 of interface intf1...'.
endmethod.

endclass.

START-OF-SELECTION.
data ob type REF TO lcl_impl.
create object ob.

call method : ob->intf1~m1,


ob->intf1~m2,
ob->intf2~m3.

Example: Aliases for Local Interface Components

REPORT Z730OOPS35.

interface intf1.
methods : m1,
m2.
ENDINTERFACE.

class lcl_impl DEFINITION.


PUBLIC SECTION.
interfaces intf1.
ALIASES a1 for intf1~m1.
PROTECTED SECTION.
aliases a2 for intf1~m2.
endclass.

class lcl_impl IMPLEMENTATION.

* method intf1~m1. "(or)


method a1.
write :/ 'inside m1'.
call method a2.
ENDMETHOD.

* method intf1~m2. "(or)


method a2.
write :/ 'inside m2'.
ENDMETHOD.

endclass.

START-OF-SELECTION.
data ob type ref to lcl_impl.
create object ob.

*call method : ob->intf1~m1,


* ob->intf1~m2.

uline.
call method ob->a1.
*call method ob->a2. "syntax error

Example: Aliases for Global Interface Components

Global Interface:
Global Implementation class implementing above interface:
Executable Program: Accessing above Global aliases

REPORT Z730OOPS36.

data ob type ref to zcl_im_intf1.


create object ob.

CALL METHOD OB->ZIF_INTF1~M1


.

call method ob->a2.

Example: Custom Event Handling – Scenario 1


REPORT Z730OOPS37.

class lcl_abc DEFINITION.


public SECTION.
events e1. "instance event declaration
methods : m1 for event e1 of lcl_abc, "instance event handler method
m2. "normal instance method
endclass.

class lcl_abc IMPLEMENTATION.


method m1.
write :/ 'inside instance event handler method m1'.
endmethod.

method m2.
write :/ 'inside instance normal method m2-about to raise event'.
raise event e1.
endmethod.
endclass.

START-OF-SELECTION.
data ob type ref to lcl_abc.
create object ob.

*call method ob->m1. "should not be called explcitly

call method ob->m2.

Example: Custom Event Handling – Scenario 2

REPORT Z730OOPS38.

class lcl_abc DEFINITION.


public SECTION.
events e1. "instance event declaration
methods : m1 for event e1 of lcl_abc, "instance event handler method
m2. "normal instance method
endclass.

class lcl_abc IMPLEMENTATION.


method m1.
write :/ 'inside instance event handler method m1'.
endmethod.
method m2.
write :/ 'inside instance normal method m2-about to raise event'.
raise event e1.
endmethod.
endclass.

START-OF-SELECTION.
data ob type ref to lcl_abc.
create object ob.

*call method ob->m1. "should not be called explcitly

call method ob->m2.

set handler ob->m1 for ob.

Example: Custom Event Handling – Scenario 3

REPORT Z730OOPS39.

class lcl_abc DEFINITION.


public SECTION.
events e1. "instance event declaration
methods : m1 for event e1 of lcl_abc, "instance event handler method
m2. "normal instance method
endclass.

class lcl_abc IMPLEMENTATION.


method m1.
write :/ 'inside instance event handler method m1'.
endmethod.

method m2.
write :/ 'inside instance normal method m2-about to raise event'.
raise event e1.
endmethod.
endclass.

START-OF-SELECTION.
data ob1 type ref to lcl_abc.
create object ob1.
data ob2 type ref to lcl_abc.
create object ob2.

set handler ob1->m1 for ob2.


call method ob1->m2.

Example: Custom Event Handling – Scenario 4

REPORT Z730OOPS40.

class lcl_abc DEFINITION.


public SECTION.
events e1. "instance event declaration
methods : m1 for event e1 of lcl_abc, "instance event handler method
m2. "normal instance method
endclass.

class lcl_abc IMPLEMENTATION.


method m1.
write :/ 'inside instance event handler method m1'.
endmethod.

method m2.
write :/ 'inside instance normal method m2-about to raise event'.
raise event e1.
endmethod.
endclass.

START-OF-SELECTION.
data ob1 type ref to lcl_abc.
create object ob1.

data ob2 type ref to lcl_abc.


create object ob2.

*set handler ob1->m1 for ob1. "(or)


set handler ob2->m1 for ob1.
call method ob1->m2.

Example: Custom Event Handling – Scenario 5

REPORT Z730OOPS41.
class lcl_abc DEFINITION.
public SECTION.
events e1. "instance event declaration
methods : m1 for event e1 of lcl_abc, "instance event handler method
m2. "normal instance method
endclass.

class lcl_abc IMPLEMENTATION.


method m1.
write :/ 'inside instance event handler method m1'.
endmethod.

method m2.
write :/ 'inside instance normal method m2-about to raise event'.
set handler me->m1 for me.
raise event e1.
endmethod.
endclass.

START-OF-SELECTION.
data ob1 type ref to lcl_abc.
create object ob1.

data ob2 type ref to lcl_abc.


create object ob2.

call method ob1->m2.


call method ob2->m2.

Example: Custom Event handling – Scenario 6

REPORT Z730OOPS42.

class lcl_abc DEFINITION.


public SECTION.
events e1.
methods : m1 for event e1 of lcl_abc,
m2.
endclass.

class lcl_abc IMPLEMENTATION.


method m1.
write :/ 'inside instance event handler method m1'.
endmethod.

method m2.
write :/ 'inside instance normal method m2-about to raise event'.
raise event e1.
endmethod.
endclass.

START-OF-SELECTION.
data ob1 type ref to lcl_abc.
create object ob1.

data ob2 type ref to lcl_abc.


create object ob2.

data ob3 type ref to lcl_abc.


create object ob3.

*set handler ob1->m1 for ob1.


*set handler ob1->m1 for ob2.
*set handler ob1->m1 for ob3. "(or)

set handler ob1->m1 for ALL INSTANCES.

call method ob1->m2.


uline.
call method ob2->m2.
uline.
call method ob3->m2.

Example: Custom Event handling – Scenario 7

REPORT Z730OOPS43.

class lcl_eventreceiver1 DEFINITION.


public section.
events e1.
methods m1 for event e1 of lcl_eventreceiver1. "instance event handler method
class-methods m2 for event e1 of lcl_eventreceiver1. "static event handler method
methods m3. "normal method
endclass.

class lcl_eventreceiver1 IMPLEMENTATION.

method m1.
format color 3.
write :/ 'inside instance event handler method m1 of class lcl_eventreceiver1...'.
endmethod.

method m2.
write :/ 'inside static event handler method m2 of class lcl_eventreceiver1...'.
endmethod.

method m3.
write :/ 'inside instance normal method m3 of class lcl_eventreceiver1..about to raise event..'
..
raise event e1.
endmethod.

endclass.

class lcl_eventreceiver2 DEFINITION.


public SECTION.
methods m4 for event e1 of lcl_eventreceiver1.
endclass.

class lcl_eventreceiver2 IMPLEMENTATION.

method m4.
write :/ 'inside instance event handler method m4 of class lcl_eventreceiver2...'.
endmethod.

endclass.

START-OF-SELECTION.
data ob1 type ref to lcl_eventreceiver1.
create object ob1.

data ob2 type ref to lcl_eventreceiver2.


create object ob2.

set handler ob1->m1 for ob1.


* set handler ob1->m4 for ob1. "syntax error
set handler ob2->m4 for ob1.
*set handler ob1->m2 for ob1. "(or)
set handler lcl_eventreceiver1=>m2 for ob1.
format color 7.
call method ob1->m3.

Example: Custom Event handling – Scenario 8

REPORT Z730OOPS44.

class lcl_eventreceiver DEFINITION.


PUBLIC SECTION.
events e1 EXPORTING value(e_kunnr) type kna1-kunnr.
methods : handle_e1 for event e1 of lcl_eventreceiver
IMPORTING e_kunnr,
raise_e1 IMPORTING i_kunnr type kna1-kunnr.
PROTECTED SECTION.
methods display_salesorders.

types : begin of ty_sales,


vbeln type vbak-vbeln,
ernam type vbak-ernam,
netwr type vbak-netwr,
end of ty_sales.

data t_sales type table of ty_sales.


endclass.

class lcl_eventreceiver IMPLEMENTATION.

method handle_e1.
format color 7.
select vbeln ernam netwr
from vbak
into table t_sales
where kunnr = e_kunnr.
if sy-subrc eq 0.
write :/ 'No of sales orders :',sy-dbcnt.
call method display_salesorders.
else.
message 'No Sales orders for given customer' type 'I'.
endif.
endmethod.

method raise_e1.
format color 3.
write :/ 'Inside Normal method ..about to raise event e1..'.
raise event e1
EXPORTING
* e_kunnr = '0000001010'.
e_kunnr = i_kunnr.
endmethod.

method display_salesorders.
format color 2.
data wa_sales type ty_sales.
loop at t_sales into wa_sales.
write :/ wa_sales-vbeln,
wa_sales-ernam,
wa_sales-netwr.
endloop.
endmethod.
endclass.

START-OF-SELECTION.

PARAMETERS p_kunnr type kna1-kunnr.

data ob type ref to lcl_eventreceiver.


create object ob.

set handler ob->handle_e1 for ob.


call method ob->raise_e1
EXPORTING
i_kunnr = p_kunnr.
Example: Custom Event handling (Global events with parameters)
method HANDLE_E1.
format color 7.
select vbeln ernam netwr
from vbak
into table t_sales
where kunnr = e_kunnr.
if sy-subrc eq 0.
write :/ 'No of sales orders :',sy-dbcnt.
call method display_salesorders.
else.
message 'No Sales orders for given customer' type 'I'.
endif.
endmethod.

method RAISE_E1.
format color 3.
write :/ 'Inside Normal method ..about to raise event e1..'.
raise event e1
EXPORTING
* e_kunnr = '0000001010'.
e_kunnr = i_kunnr.
endmethod.

method DISPLAY_SALESORDERS.
format color 2.
data wa_sales type ty_sales.
loop at t_sales into wa_sales.
write :/ wa_sales-vbeln,
wa_sales-ernam,
wa_sales-netwr.
endloop.
endmethod.

Executable program:

REPORT Z730OOPS45.

PARAMETERS p_kunnr type kna1-kunnr.

data ob type ref to zcl_eventreceiver.


create object ob.

set handler ob->handle_e1 for ob.


call method ob->raise_e1
EXPORTING
i_kunnr = p_kunnr.

Example: Instance Vs static Events

REPORT Z730OOPS46.

class lcl_eventreceiver DEFINITION.


PUBLIC SECTION.
events e1. "instance event
class-events e2. "static event
methods m1 for event e1 of lcl_eventreceiver.
methods m2 for event e2 of lcl_eventreceiver.
methods m3.
class-methods m4.
endclass.
class lcl_eventreceiver IMPLEMENTATION.
method m1.
write :/ 'Inside instance event handler method m1'.
endmethod.

method m2.
uline.
write :/ 'Inside instance event handler method m2'.
endmethod.

method m3.
write :/ 'Inside instance normal method m3-raising events e1 and e2'.
raise event e1.
raise event e2.
endmethod.

method m4.
write :/ 'Inside static normal method m4'.
* raise event e2. "valid
* raise event e1. "invalid
endmethod.

endclass.

START-OF-SELECTION.
data ob1 type ref to lcl_eventreceiver.
create object ob1.

data ob2 type ref to lcl_eventreceiver.


create object ob2.

* handlers for executing event handler methods for instance events


set handler ob1->m1 for ob1.
set handler ob1->m1 for ob2.

* handlers for executing event handler methods for static event


* set handler ob1->m2 for ob1. "syntax error
* set handler ob1->m2 for ob2. "syntax error

set handler ob1->m2.

call method ob1->m3.


call method ob2->m3.
Example: Events in Interfaces

REPORT Z730OOPS47.

interface intf1.
events e1.
methods m1 for event e1 of intf1.
endinterface.

class lcl_abc DEFINITION.


PUBLIC SECTION.
interfaces intf1.
methods m2 for event e1 of intf1.
methods m3.
endclass.

class lcl_abc IMPLEMENTATION.


method intf1~m1.
write :/ 'inside interface event handler method m1..'.
endmethod.

method m2.
write :/ 'inside interface event handler method m2 ..'.
endmethod.

method m3.
write :/ 'inside normal method m3..'.
raise event intf1~e1.
endmethod.
endclass.

START-OF-SELECTION.
data ob type ref to lcl_abc.
create object ob.

set handler ob->intf1~m1 for ob.


set handler ob->m2 for ob.
call method ob->m3.

Example: Standard Exception Handling

REPORT Z730OOPS48.

class lcl_abc DEFINITION.


PUBLIC SECTION.
methods m1 IMPORTING i_x type i
i_y type i
EXPORTING e_z type i.
endclass.

class lcl_abc IMPLEMENTATION.


method m1.
format color 3.
write :/ 'inside begin of method m1'.
try.
e_z = i_x / i_y.
* catch cx_sy_zerodivide.
catch cx_root.
format color 2.
write :/ 'inside catch block...'.
write :/ 'Cannot divide by zero...'.
format color off.
endtry.
write :/ 'inside end of method m1'.
format color off.
endmethod.
endclass.

START-OF-SELECTION.
PARAMETERS : p_x type i,
p_y type i.

data v_z type i.

data ob type ref to lcl_abc.


create object ob.

call method ob->m1


EXPORTING
i_x = p_x
i_y = p_y
IMPORTING
e_z = v_z.

format color 7.
write :/ 'Division is ',v_z.
write :/ 'End of program...'.
format color off.
Example: Capturing Standard Exception Messages

REPORT Z730OOPS49.

*data k type ref to cx_sy_zerodivide.


data k type ref to cx_root.
data v_str type string.

data : v_pname type sy-repid,


v_iname type sy-repid,
v_pos type i.

class lcl_abc DEFINITION.


PUBLIC SECTION.
methods m1 IMPORTING i_x type i
i_y type i
EXPORTING e_z type i.
endclass.

class lcl_abc IMPLEMENTATION.


method m1.
format color 3.
write :/ 'inside begin of method m1'.
try.
e_z = i_x / i_y.
* catch cx_sy_zerodivide. "(or)
* catch cx_sy_zerodivide into k. "(or)
catch cx_root into k.
format color 2.
CALL METHOD K->IF_MESSAGE~GET_TEXT
RECEIVING
RESULT = v_str.

write :/ 'Short exception message is ',v_str.


uline.
clear v_str.
CALL METHOD K->IF_MESSAGE~GET_LONGTEXT
RECEIVING
RESULT = v_str.

write :/ 'Long exception message is ',v_str.


uline.
CALL METHOD K->GET_SOURCE_POSITION
IMPORTING
PROGRAM_NAME = v_pname
INCLUDE_NAME = v_iname
SOURCE_LINE = v_pos.

write :/ 'Program name :',v_pname,


/ 'Include name :',v_iname,
/ 'Line no :',v_pos.

format color off.


endtry.
write :/ 'inside end of method m1'.
format color off.
endmethod.
endclass.

START-OF-SELECTION.
PARAMETERS : p_x type i,
p_y type i.

data v_z type i.

data ob type ref to lcl_abc.


create object ob.

call method ob->m1


EXPORTING
i_x = p_x
i_y = p_y
IMPORTING
e_z = v_z.

format color 7.
write :/ 'Division is ',v_z.
write :/ 'End of program...'.
format color off.

Example: Raising Keyword

REPORT Z730OOPS50.

class lcl_abc DEFINITION.


PUBLIC SECTION.
methods m1 IMPORTING i_x type i
i_y type i
EXPORTING e_z type i
RAISING cx_sy_zerodivide.
endclass.

class lcl_abc IMPLEMENTATION.


method m1.
format color 3.
write :/ 'inside begin of method m1'.
e_z = i_x / i_y.
write :/ 'inside end of method m1'.
format color off.
endmethod.
endclass.

START-OF-SELECTION.

data v_str type string.

PARAMETERS : p_x type i,


p_y type i.

data v_z type i.

data ob type ref to lcl_abc.


create object ob.

try.
call method ob->m1
EXPORTING
i_x = p_x
i_y = p_y
IMPORTING
e_z = v_z.
catch cx_sy_zerodivide.
format color 3.
write :/ 'Inside catch block...'.
write :/ 'Cannot divide by zero...'.
endtry.

format color 7.
write :/ 'Division is ',v_z.
format color off.

uline.
uline.
data k type ref to cx_sy_zerodivide.
try.
call method ob->m1
EXPORTING
i_x = p_x
i_y = p_y
IMPORTING
e_z = v_z.
catch cx_sy_zerodivide into k.
format color 3.
CALL METHOD K->IF_MESSAGE~GET_TEXT
RECEIVING
RESULT = v_str.

write :/ 'Short exception message is ',v_str.


endtry.

Example: Cleanup block

REPORT Z730OOPS52.

class lcl_abc DEFINITION.


PUBLIC SECTION.
methods m1 IMPORTING i_x type i
i_y type i
EXPORTING e_z type i.
endclass.

class lcl_abc IMPLEMENTATION.


method m1.
format color 3.
try.
try.
write :/ 'inside begin of method m1'.
e_z = i_x / i_y.
format color off.
CLEANUP. "finally block
format color 6.
write :/ 'Begin of cleanup block...'.
write :/ 'inside end of method m1'.
write :/ 'Begin of end of cleanup block...'.
format color off.
endtry.
catch cx_root.
format color 2.
write :/ 'inside catch block...'.
write :/ 'Cannot divide by zero...'.
write :/ 'inside end of catch block...'.
format color off.
endtry.
endmethod.
endclass.

START-OF-SELECTION.
PARAMETERS : p_x type i,
p_y type i.

data v_z type i.

data ob type ref to lcl_abc.


create object ob.

call method ob->m1


EXPORTING
i_x = p_x
i_y = p_y
IMPORTING
e_z = v_z.

format color 7.
write :/ 'Division is ',v_z.
write :/ 'End of program...'.
format color off.
Example: Custom Exception class Referring to OTR
Create Exception ids id1 and id2 in texts tab
Attributes id1 and id2 gets created automatically:

Associate exception messages for id1(short and long) and id2 (only short)
Activate the exception class:

Executable program:

REPORT Z730OOPS53.

data : ob type ref to zcx_myexceptions,


v_str type string.

write :/ 'Before exception is raised....'.


*if condition is met, raise exception
try.
format color 3.
RAISE EXCEPTION TYPE ZCX_MYEXCEPTIONS
EXPORTING
TEXTID = zcx_myexceptions=>id1.
catch zcx_myexceptions into ob.
CALL METHOD OB->IF_MESSAGE~GET_TEXT
RECEIVING
RESULT = v_str.

write :/ 'Short exception msg is :',v_str.

clear v_str.

CALL METHOD OB->IF_MESSAGE~GET_LONGTEXT


RECEIVING
RESULT = v_str.

write :/ 'Long exception msg is :',v_str.

endtry.
format color off.
uline.
try.
format color 7.
RAISE EXCEPTION TYPE ZCX_MYEXCEPTIONS
EXPORTING
TEXTID = zcx_myexceptions=>id2.

catch zcx_myexceptions into ob.


clear v_str.
CALL METHOD OB->IF_MESSAGE~GET_TEXT
RECEIVING
RESULT = v_str.

write :/ 'Short exception msg is :',v_str.

clear v_str.

CALL METHOD OB->IF_MESSAGE~GET_LONGTEXT


RECEIVING
RESULT = v_str.

write :/ 'Long exception msg is :',v_str.

endtry.

Example: Custom Exception class referring to Message class

Create Message class with static and dynamic messages (SE91)


Create Exception ids id1 and id2 in texts tab
Create 4 attributes attr1, attr2, attr3, attr4 in attributes tab
In Texts tab, Map exception id id1 with static message ‘001’ of message class by clicking on
message text button
In Texts tab, Map exception id id2 with dynamic message ‘002’ of message class by clicking on
message text button and also map 4 place holders of message class msg with corresponding
attributes (attr1..atrr4)
Executable Program:

REPORT Z730OOPS54.

data : ob type ref to zcx_myexceptions_msgclass,


v_str type string.

write :/ 'Before exception is raised....'.


*if condition is met, raise exception
try.
format color 3.
RAISE EXCEPTION TYPE ZCX_MYEXCEPTIONS_MSGCLASS
EXPORTING
TEXTID = zcx_myexceptions_msgclass=>id1.

catch zcx_myexceptions_msgclass into ob.


CALL METHOD OB->IF_MESSAGE~GET_TEXT
RECEIVING
RESULT = v_str.
write :/ 'Short exception msg is :',v_str.

clear v_str.

CALL METHOD OB->IF_MESSAGE~GET_LONGTEXT


RECEIVING
RESULT = v_str.

write :/ 'Long exception msg is :',v_str.

endtry.
format color off.
uline.
try.
format color 7.
RAISE EXCEPTION TYPE ZCX_MYEXCEPTIONS_MSGCLASS
EXPORTING
TEXTID = zcx_myexceptions_msgclass=>id2
ATTR1 = 'Gensoft Technologies'
ATTR2 = 'SAP Technical Training'
ATTR3 = 'Ameerpt'
ATTR4 = 'ABAP HANA'.
catch zcx_myexceptions_msgclass into ob.
clear v_str.
CALL METHOD OB->IF_MESSAGE~GET_TEXT
RECEIVING
RESULT = v_str.

write :/ 'Short exception msg is :',v_str.

clear v_str.

CALL METHOD OB->IF_MESSAGE~GET_LONGTEXT


RECEIVING
RESULT = v_str.

write :/ 'Long exception msg is :',v_str.

endtry.
Example: User defined exceptions in Local classes

REPORT Z730OOPS55.

class lcl_abc DEFINITION.


public SECTION.
methods m1 IMPORTING i_x type i
i_y type i
EXPORTING e_z type i
EXCEPTIONS myexception.
endclass.

class lcl_abc IMPLEMENTATION.


method m1.
if i_y eq 0.
raise myexception.
else.
e_z = i_x / i_y.
endif.
endmethod.
endclass.

START-OF-SELECTION.
data ob type ref to lcl_abc.
create object ob.

PARAMETERS : p_x type i,


p_y type i.

data v_z type i.

call method ob->m1


EXPORTING
i_x = p_x
i_y = p_y
IMPORTING
e_z = v_z
EXCEPTIONS
myexception = 1
OTHERS = 2.

if sy-subrc eq 0.
write :/ 'Division is ',v_z.
elseif sy-subrc eq 1.
write :/ 'Cannot divide by zero'.
elseif sy-subrc eq 2.
write :/ 'Unknown error'.
endif.

Example: User defined exceptions in Global classes


Method ‘M1’ Implementation:

method M1.
if i_y eq 0.
raise myexception.
else.
e_z = i_x / i_y.
endif.
endmethod.

Executable program:

REPORT Z730OOPS56.

data ob type ref to zcl_exception.


create object ob.

PARAMETERS : p_x type i,


p_y type i.
data v_z type i.

CALL METHOD OB->M1


EXPORTING
I_X = p_x
I_Y = p_y
IMPORTING
E_Z = v_z
EXCEPTIONS
MYEXCEPTION = 13
others = 21.

if sy-subrc eq 0.
write :/ 'Division is ',v_z.
elseif sy-subrc eq 13.
write :/ 'Cannot divide by zero'.
elseif sy-subrc eq 21.
write :/ 'Unknown error'.
endif.

Example: Multiple Catch blocks

REPORT Z730OOPS57.

class lcl_abc DEFINITION.


public SECTION.
methods m1 IMPORTING i_x type i
i_y type i
EXPORTING e_z type i.

endclass.

class lcl_abc IMPLEMENTATION.


method m1.
try.
e_z = i_x / i_y.
catch cx_root.
write :/ 'Inside catch block of root class'.
write :/ 'Cannot divide by zero'.
catch cx_sy_zerodivide.
write :/ 'Inside catch block of zerodivide class'.
write :/ 'Cannot divide by zero'.
endtry.
endmethod.
endclass.

START-OF-SELECTION.
data ob type ref to lcl_abc.
create object ob.

PARAMETERS : p_x type i,


p_y type i.

data v_z type i.

call method ob->m1


EXPORTING
i_x = p_x
i_y = p_y
IMPORTING
e_z = v_z.

write :/ 'Division is ',v_z.

Example: Macros

REPORT Z730OOPS58.

define abc.
&3 = &1 + &2.
&4 = &1 - &2.
&5 = &1 * &2.
end-OF-DEFINITION.

data : v_r1 type i,


v_r2 type i,
v_r3 type i.

*abc. "syntax errors


* abc 30 20. "syntax errors
abc 30 20 v_r1 v_r2 v_r3.
write :/ v_r1,v_r2,v_r3.

uline.
abc 20 10 v_r1 v_r2 v_r3.
write :/ v_r1,v_r2,v_r3.
Event Handling in Classes

- An event is an action used for providing dynamic features to applications.

- As part of class there are many events provided by SAP which can be used in ALV
reporting, Webdynpro, UI5, workflow etc. which are called as standard events and they
exist as part of STD.classes.

- These standard events are triggered (raised) by SAP itself.

- As a developer we are only responsible for handling these standard events.

- We can also provide custom events as part of custom classes.

- These custom events are declared, raised and handled by developer itself.

- Events are raised inside the methods

- One event can have any no. of event handler methods.

- The events and the event handler methods can belong to same class or different class.

- As part of this event handler method we need to implement the business logic for the
event.

- Whenever an event is raised, SAP will execute all the event handler methods one after
the other by using registered handlers.

- The handler can be object in case of instance event handler method or a object/class
name in case of static event handler methods.

- The sequence of execution of event handler methods depends upon the sequence of
handler registrations.

- Event can contain only exporting parameters which can be imported by Event Handler
Method.

- Event parameters are always exported by value, the parameter Name in the event and
in the event handler method must be same.

- Whenever we deal with custom events in custom classes, we need to have minimum 2
methods. One method for raising the event and the other method for handling the
event.

Events can be classified into two types:


- Instance Event.

- Static Event.

Instance Event Static Event


Instance event are specific to object Static event are not specific to any object
Instance events require object registration at Since static events are not specific to any
the time of registering the handlers. object, we should not register any object at
the time of handler registration
Instance events can be raised only in instance Whereas static events can be raised in either
methods instance/static methods.

The event handler method can be instance


method /it can be a static methods.

Procedure for interacting with custom events in custom classes:

1. Declare the event in the class definition

Syntax:

Events/class-events <event name>[exporting parameter].

2. Declare the event handler method in the class definition.

Syntax:

Method/class-methods <method name> for event <event name> of <class name>/<interface


name> [importing parameters].

3. Implement the event handler method in the class implementation.

Syntax:

Method <method name> .

Statements---

Endmethod.

4. Raise the event in one of the method implementation.

Syntax:

Raise event <event name>[parameter list].


5. Register the handlers:

Syntax 1:

Set handler <handler> for <instance>.  instance event

Syntax 2:

Set handler <handler> for all instances.  instance event

Syntax 3:

Set handler <handler>.  static event

Note:

1. Event handler method should not be called explicitly; they are automatically called
whenever the event is raised.

2. If the event is raised and there is no handler registration, SAP will not execute the event
handler methods. Handler will instruct SAP using what objects or class name the
event handler method should be executed.

3. The handler registration should be made before raising the events .i.e. SAP should know
with what handler it has to execute event handler method whenever event is raised.

4. For all instances, while registering the handler for instance events we must specify the
object name as part of “FOR” keyword in the “set handler statements”. This
indicates that, this object which is responsible for raising the events. If all the objects
of the class as to be registered there should be a separate “set handler” for each
object. Instead for registering each object separately we can use “for all instances”
statements as part of set handler.

5. “For all instances” is similar to static event registration.

6. If an event contains any mandatory export parameters then those parameter values
must be exported at the time of raising the event, if the parameters are declared as
optional, then we need not export the values. These exported parameters can be
imported by event handler methods based on which we can implement the business
logic. Event parameters are always passed by value.

Scenario 1: In this, event ‘E1’ is raised in the method ‘M2’, but Method ‘M1’ will not be
executed because SAP Doesn’t know by using what handler(object/class) it should call the
event handler method ‘M1’.
Scenario 2: In this, event ‘E1’ is raised in the method ‘M2’, but Method ‘M1’ will not be
executed because SAP doesn’t know about the handler (object/class) at the time of raising the
event. I.e.in this case handler is registered after raising the event.

Scenario 3: In this, event ‘E1’ is raised in the method ‘M2’ by the object OB1, and we also
registered the handler, but the event handler method ‘M1’ will not be executed by SAP,
because we have registered the object OB2 as part of ‘for statement’ in ‘set handler statement’,
but the event is actually raised by the object OB1.

Scenario 4 and Scenario 5: In this, event ‘E1’ is raised in the method ‘M2’ and then the event
handler method ‘M1’ will be executed by SAP by using registered handler ‘OB’ (in case if the
handler is registered outside – Scenario 4 ) (or) ‘ME’ (in case if the handler is registered with in
the method ‘m2’ implementation – Scenario 5).

Scenario 6: In this, event ‘E1’ is raised in the method ‘M2’ three times, first using the object
‘OB1’ and then using the object ‘OB2’ and then using OB3. In all these cases, SAP executes
event handler method ‘M1’ as we have registered all the objects of the class at a time by using
the addition ‘FOR ALL INSTANCES’ as part of set handler statement.

Scenario 7: In this, whenever event ‘E1’ is raised in the method ‘M3’, SAP executes event
handler methods ‘M1’, ‘M2’, of ‘LCL_EVENTRECEIVER1’ class and also method ‘M4’ of
‘LCL_EVENTRECEIVER2’ class. Here ‘M2’ method is static, so handler can be either object / class.
The sequence of execution of event handler methods depends on sequence of handler
registrations. It is also understood that event and the event handler methods can be part of
same class / different class. In case of STD. events, event belongs to STD. class and then the
event handler method should be declared and implemented as part of Custom class.

Scenario 8: Event can contain only exporting parameters which can be imported by Event
Handler Method. Event parameters are always exported by value, the parameter Name in the
event and in the event handler method must be same. If an event contains any mandatory
export parameters then those parameter values must be exported at the time of raising the
event. If the parameters are declared as optional, then we need not export the values. These
exported parameters can be imported by event handler methods based on which we can
implement the business logic. In this case, we exported the customer no (e_kunnr) while raising
the event ‘E1’ in the method ‘RAISE_E1’. This parameter is imported in one of the event handler
method ‘’HANDLE_E1’ based on which we retrieved the sales orders of the customer.

Scenario 9: Instance events can be raised only in Instance methods whereas Static events can
be raised either in Instance / Static methods. Since static events are not specific to any object,
there should not be any object registration at the time of registering the handlers. I.e. In this
case, Objects Ob1 and Ob2 are raising the static event ‘E1’ in the method m3, but none of the
objects are registered.
Note: We can also declare events as part of interfaces

Exception Handling in Methods

- An exception is a runtime error which is raised during the program execution.

- If the exception is not handled, the program will be terminated.

- Exception handling is a process of handling the Runtime Error’s and continue the
program execution without termination.

- The exceptions which are raised by SAP are called as Standard Exceptions.

- These standard exceptions are raised from standard exception classes which start with
Naming convention “CX___”.

- We can handle these exceptions by using “TRY” and “CATCH” blocks.

- Inside the “TRY” Block we need to keep those statements where the possible exceptions
might be raised.

- The ‘CATCH’ block is placed immediately after the Try Block; it is responsible for
handling the exceptions by providing appropriate exception handling statements which
can be system defined exception message / user-defined exception message.

- “CX_ROOT” is common for any exceptions i.e it is super class for all exception classes.

- As part of the catch block we need to provide the exception class name which is
responsible for raising the exception.

- If we are not sure about the exception class, we can give the exception class name as
“CX_ROOT”.

- “CX_ROOT” is super class for all the exception classes.

Note:

Whenever an exception is raised in a “TRY” block, SAP creates the object of the exception
class which is raising the exception and the control will jump to catch block.

As part of catch block, we need to receive the exception class object into our local reference
of exception class and using this referenced object we can access the appropriate methods
of Exception class to capture standard exception messages.
Whenever an exception is raised in try block, SAP ignores the rest of the statements in the
try block and the control will jump to catch block.

Standard Exceptions:

Declared, Raised  By SAP

Handling  Developer (using try and catch block)

Cleanup block:

Cleanup block is provided as part of try block to execute certain statements mandatorily
whenever an exception is raised in the try block. In general, whenever an exception is raised
in the try block, the control jumps to catch block ignoring the rest of the statements in the
try block. In these cases, there might be certain statements which we need to execute
whenever an exception is raised, these statements we can provide as part of cleanup block.
If a try block contains cleanup block, we cannot have catch block, so in order to handle the
exception, catch block should be provided as part of outer try block. As part of cleanup
block, we can perform cleanup activities. i.e clearing the variables, free internal tables, close
the files…….

We should not abruptly terminate the execution of cleanup block using the statements
stop, submit and call transaction statements.

User-defined exceptions in methods:

These exceptions are declared, raised and handled by developer.

Procedure

1. Declare the exception in the method declaration by using “exceptions” keyword

Syntax: exceptions <exception name>.

2. Raise the exception in the method implementation at the appropriate place.

Syntax: raise <exception name>.

3. Handle the exception while calling the method by checking the sy-subrc status.

Note: We can use ‘raise’ keyword to raise any exceptions i.e system defined or user-defined.

Raising keyword:
Raising is a keyword used as part of local class method declaration to indicate that a
method might raise the exception but cannot handle the exception.

In these cases, the caller of the method should take the responsibility of handling the
exception.

The advantage of this approach is since a method can be called any no. of times at different
places in the object, at each place, we can handle the exception in different way (custom
defined exception messages in one place, std.exception messages in another place..)

Custom Exception classes

We create custom exception classes to store the repository of custom exception messages

Global custom exception classes starts with ‘ZCX_....’ / ‘YCX_....’ and it must be subclass of
‘CX_STATIC_CHECK’ / ‘CX_DYNAMIC_CHECK’ / ‘CX_NO_CHECK’.

These exception classes can be created by referring to

1. OTR (Online text repository) /


2. Message class

Global Exception classes are associated with ‘Texts’ tab.

As part of ‘Texts’ tab we need to create exception id’s.

Whenever exception id is created, SAP creates corresponding constant attribute with the same
name. This attribute needs to be accessed while raising the exception from these exception
classes.

Each Exception id must be associated either with OTR Text / with message of a message class.

In case of Exception classes using OTR, each exception id that we create must be associated
with short text / long text / both.

In case of Exception classes using message class, each exception id that we create must be
associated with message id of a message class which can be static / dynamic message.

In case of Dynamic message, we need to create corresponding attributes for mapping to the
place holder of dynamic message.

The values for these attributes associated with placeholders should be passed at the time of
raising the exception.
Note: A Try block can be associated with ‘n’ of catch blocks, but the catch block exception
classes should be in the ascending order of exception classes i.e from sub class to super classes.

Example: ALV Reporting using classes (ABORT ERROR - Field Catalog Not Found)

REPORT Z730ALV1.

*tables vbak.
*SELECT-OPTIONS so_vbeln for vbak-vbeln.

data v_vbeln type vbak-vbeln.


select-OPTIONS so_vbeln for v_vbeln DEFAULT '4980' to '4985'.

data : o_cust_cont type ref to cl_gui_custom_container,


o_grid type ref to cl_gui_alv_grid.

types : begin of ty_sales,


vbeln type vbak-vbeln,
erdat type vbak-erdat,
erzet type vbak-erzet,
ernam type vbak-ernam,
end of ty_sales.

data t_sales type table of ty_sales.

START-OF-SELECTION.
call screen 100.

MODULE STATUS_0100 OUTPUT.


SET PF-STATUS 'ABC'.
* SET TITLEBAR 'xxx'.

* link custom container with custom control


CREATE OBJECT O_CUST_CONT
EXPORTING
CONTAINER_NAME = 'CUSTCTRL'.

* link alv grid with custom container


CREATE OBJECT O_GRID
EXPORTING
I_PARENT = o_cust_cont.

* get sales orders


perform getsalesorders.
if t_sales is not INITIAL.
* display sales orders
CALL METHOD O_GRID->SET_TABLE_FOR_FIRST_DISPLAY
CHANGING
IT_OUTTAB = t_sales.
endif.

ENDMODULE. " STATUS_0100 OUTPUT

MODULE USER_COMMAND_0100 INPUT.


CASE sy-ucomm.
when 'FC1'.
leave PROGRAM.
endcase.
ENDMODULE. " USER_COMMAND_0100 INPUT

FORM GETSALESORDERS .
select vbeln erdat erzet ernam
from vbak
into table t_sales
where vbeln in so_vbeln.
ENDFORM. " GETSALESORDERS

Flowlogic:

PROCESS BEFORE OUTPUT.


MODULE STATUS_0100.
*
PROCESS AFTER INPUT.
MODULE USER_COMMAND_0100.

Module pool screen 100:


PF-STATUS ‘ABC’:

------
-----
Example: ALV Reporting using classes (Field Catalog information provided by considering
parameter ‘I_STRUCTURE_NAME’ as part of method ‘SET_TABLE_FOR_FIRST_DISPLAY’)

REPORT Z730ALV2.

data v_vbeln type vbak-vbeln.


select-OPTIONS so_vbeln for v_vbeln DEFAULT '4980' to '4985'.

data : o_cust_cont type ref to cl_gui_custom_container,


o_grid type ref to cl_gui_alv_grid.

types : begin of ty_sales,


vbeln type vbak-vbeln,
erdat type vbak-erdat,
erzet type vbak-erzet,
ernam type vbak-ernam,
end of ty_sales.

data t_sales type table of ty_sales.

START-OF-SELECTION.
call screen 100.

MODULE STATUS_0100 OUTPUT.


SET PF-STATUS 'ABC'.
* SET TITLEBAR 'xxx'.

* link custom container with custom control


CREATE OBJECT O_CUST_CONT
EXPORTING
CONTAINER_NAME = 'CUSTCTRL'.

* link alv grid with custom container


CREATE OBJECT O_GRID
EXPORTING
I_PARENT = o_cust_cont.

* get sales orders


perform getsalesorders.
if t_sales is not INITIAL.
* display sales orders
perform displaysalesorders.
endif.
ENDMODULE. " STATUS_0100 OUTPUT
FORM GETSALESORDERS .
select vbeln erdat erzet ernam
from vbak
into table t_sales
where vbeln in so_vbeln.
ENDFORM. " GETSALESORDERS

FORM DISPLAYSALESORDERS .
CALL METHOD O_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
* I_STRUCTURE_NAME = 'VBAK' "runtime error
I_STRUCTURE_NAME = 'ZCVBAK'
CHANGING
IT_OUTTAB = t_sales.

ENDFORM. " DISPLAYSALESORDERS

MODULE USER_COMMAND_0100 INPUT.


case sy-ucomm.
when 'FC1'.
leave PROGRAM.
endcase.
ENDMODULE. " USER_COMMAND_0100 INPUT

Flowlogic:

PROCESS BEFORE OUTPUT.


MODULE STATUS_0100.
*
PROCESS AFTER INPUT.
MODULE USER_COMMAND_0100.

Module pool screen 100:


PF-STATUS ‘ABC’:

Note: In the above example ‘ZCVBAK’ is the dictionary structure which needs to be created
Example: ALV Reporting using classes (Field Catalog information generated using the function
module ‘LVC_FIELDCATALOG_MERGE’, Modifying Field catalog)

REPORT Z730ALV3.

data v_vbeln type vbak-vbeln.


select-OPTIONS so_vbeln for v_vbeln DEFAULT '4980' to '4985'.

data : o_cust_cont type ref to cl_gui_custom_container,


o_grid type ref to cl_gui_alv_grid.

types : begin of ty_sales,


vbeln type vbak-vbeln,
erdat type vbak-erdat,
erzet type vbak-erzet,
ernam type vbak-ernam,
end of ty_sales.

data t_sales type table of ty_sales.


data : t_fcat type lvc_t_fcat,
wa_fcat type lvc_s_fcat. "normal work area

FIELD-SYMBOLS <abc> like line of t_fcat. "field symbol work area

START-OF-SELECTION.
call screen 100.

MODULE STATUS_0100 OUTPUT.


SET PF-STATUS 'ABC'.
* SET TITLEBAR 'xxx'.

* link custom container with custom control


CREATE OBJECT O_CUST_CONT
EXPORTING
CONTAINER_NAME = 'CUSTCTRL'.

* link alv grid with custom container


CREATE OBJECT O_GRID
EXPORTING
I_PARENT = o_cust_cont.

* get sales orders


perform getsalesorders.
if t_sales is not INITIAL.
* generate fieldcatalog
perform fldcat.
* display sales orders
perform displaysalesorders.
endif.
ENDMODULE. " STATUS_0100 OUTPUT

FORM GETSALESORDERS .
select vbeln erdat erzet ernam
from vbak
into table t_sales
where vbeln in so_vbeln.
ENDFORM. " GETSALESORDERS

FORM DISPLAYSALESORDERS .
CALL METHOD O_GRID->SET_TABLE_FOR_FIRST_DISPLAY
CHANGING
IT_OUTTAB = t_sales
IT_FIELDCATALOG = t_fcat.

ENDFORM. " DISPLAYSALESORDERS

MODULE USER_COMMAND_0100 INPUT.


case sy-ucomm.
when 'FC1'.
leave PROGRAM.
endcase.
ENDMODULE. " USER_COMMAND_0100 INPUT

FORM FLDCAT .
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
I_STRUCTURE_NAME = 'ZCVBAK'
CHANGING
CT_FIELDCAT = t_fcat.

if t_fcat is not INITIAL.


* loop at t_fcat into wa_fcat.
* if wa_fcat-fieldname = 'ERDAT'.
* wa_fcat-col_pos = 3.
* modify t_fcat from wa_fcat TRANSPORTING col_pos.
* elseif wa_fcat-fieldname = 'ERZET'.
* wa_fcat-col_pos = 2.
* modify t_fcat from wa_fcat TRANSPORTING col_pos.
* elseif wa_fcat-fieldname = 'ERNAM'.
* wa_fcat-coltext = 'Created Person'.
* modify t_fcat from wa_fcat TRANSPORTING coltext.
* endif.
* endloop. "performance issue-modify inside loop

loop at t_fcat ASSIGNING <abc>.


if <abc>-fieldname = 'ERDAT'.
<abc>-col_pos = 3.
elseif <abc>-fieldname = 'ERZET'.
<abc>-col_pos = 2.
elseif <abc>-fieldname = 'ERNAM'.
<abc>-coltext = 'Created Person'.
endif.
endloop.
endif.
ENDFORM. " FLDCAT
Flowlogic:

PROCESS BEFORE OUTPUT.


MODULE STATUS_0100.
*
PROCESS AFTER INPUT.
MODULE USER_COMMAND_0100.

Example: ALV Reporting using classes (Field Catalog generated manually)

REPORT Z730ALV4.

data v_vbeln type vbak-vbeln.


select-OPTIONS so_vbeln for v_vbeln DEFAULT '4980' to '4985'.

data : o_cust_cont type ref to cl_gui_custom_container,


o_grid type ref to cl_gui_alv_grid.

types : begin of ty_sales,


vbeln type vbak-vbeln,
erdat type vbak-erdat,
erzet type vbak-erzet,
ernam type vbak-ernam,
end of ty_sales.

data t_sales type table of ty_sales.

data : t_fcat type lvc_t_fcat,


wa_fcat type lvc_s_fcat. "normal work area

FIELD-SYMBOLS <abc> like line of t_fcat. "field symbol work area

START-OF-SELECTION.
call screen 100.

MODULE STATUS_0100 OUTPUT.


SET PF-STATUS 'ABC'.

* link custom container with custom control


CREATE OBJECT O_CUST_CONT
EXPORTING
CONTAINER_NAME = 'CUSTCTRL'.

* link alv grid with custom container


CREATE OBJECT O_GRID
EXPORTING
I_PARENT = o_cust_cont.

* get sales orders


perform getsalesorders.
if t_sales is not INITIAL.
* generate fieldcatalog
perform fldcat.
* display sales orders
perform displaysalesorders.
endif.
ENDMODULE. " STATUS_0100 OUTPUT

FORM GETSALESORDERS .
select vbeln erdat erzet ernam
from vbak
into table t_sales
where vbeln in so_vbeln.
ENDFORM. " GETSALESORDERS

FORM DISPLAYSALESORDERS .
CALL METHOD O_GRID->SET_TABLE_FOR_FIRST_DISPLAY
CHANGING
IT_OUTTAB = t_sales
IT_FIELDCATALOG = t_fcat.

ENDFORM. " DISPLAYSALESORDERS

MODULE USER_COMMAND_0100 INPUT.


case sy-ucomm.
when 'FC1'.
leave PROGRAM.
endcase.
ENDMODULE. " USER_COMMAND_0100 INPUT
FORM FLDCAT .
clear wa_fcat.
wa_fcat-fieldname = 'VBELN'.
wa_fcat-col_pos = 1.
wa_fcat-coltext = 'Sales Document'.
wa_fcat-outputlen = 15.
wa_fcat-tooltip = 'Sales Document Number'.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERDAT'.
wa_fcat-col_pos = 2.
wa_fcat-coltext = 'Creation Date'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERZET'.
wa_fcat-col_pos = 3.
wa_fcat-coltext = 'Creation Time'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERNAM'.
wa_fcat-col_pos = 4.
wa_fcat-coltext = 'Name of Person Created Sales Doc'.
wa_fcat-outputlen = 40.
append wa_fcat to t_fcat.
ENDFORM. " FLDCAT

Example: ALV Row coloring


REPORT Z730ALV6.

data v_vbeln type vbak-vbeln.


select-OPTIONS so_vbeln for v_vbeln DEFAULT '4980' to '5020'.

data : o_cust_cont type ref to cl_gui_custom_container,


o_grid type ref to cl_gui_alv_grid.

types : begin of ty_temp_sales.


include type zcvbak.
types : netwr type vbak-netwr,
end of ty_temp_sales.

data : t_temp_sales type table of ty_temp_sales,


wa_temp_sales type ty_temp_sales.

types : begin of ty_final_sales.


include type zcvbak.
types : netwr type vbak-netwr,
rowcolor(4) type c,
end of ty_final_sales.

data : t_final_sales type table of ty_final_sales,


wa_final_sales type ty_final_sales.

field-symbols <wa_final> like line of t_final_sales.

data wa_layo type lvc_s_layo.

data : t_fcat type lvc_t_fcat,


wa_fcat type lvc_s_fcat. "normal work area

FIELD-SYMBOLS <abc> like line of t_fcat. "field symbol work area

START-OF-SELECTION.
call screen 100.

MODULE STATUS_0100 OUTPUT.


SET PF-STATUS 'ABC'.

* link custom container with custom control


CREATE OBJECT O_CUST_CONT
EXPORTING
CONTAINER_NAME = 'CUSTCTRL'.
* link alv grid with custom container
CREATE OBJECT O_GRID
EXPORTING
I_PARENT = o_cust_cont.

* get sales orders


perform getsalesorders.
if t_final_sales is not INITIAL.
* generate fieldcatalog
perform fldcat.
* row coloring
perform rowcoloring.
* layout
perform layout.
* display sales orders
perform displaysalesorders.
endif.
ENDMODULE. " STATUS_0100 OUTPUT

FORM GETSALESORDERS .
select vbeln erdat erzet ernam netwr
from vbak
into table t_temp_sales
where vbeln in so_vbeln.
if sy-subrc eq 0.
* append lines of t_temp_sales to t_final_sales.
loop at t_temp_sales into wa_temp_sales.
clear wa_final_sales.
wa_final_sales-vbeln = wa_temp_sales-vbeln.
wa_final_sales-erdat = wa_temp_sales-erdat.
wa_final_sales-erzet = wa_temp_sales-erzet.
wa_final_sales-ernam = wa_temp_sales-ernam.
wa_final_sales-netwr = wa_temp_sales-netwr.
append wa_final_sales to t_final_sales.
endloop.
endif.
ENDFORM. " GETSALESORDERS

FORM DISPLAYSALESORDERS .
CALL METHOD O_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
IS_LAYOUT = wa_layo
CHANGING
IT_OUTTAB = t_final_sales
IT_FIELDCATALOG = t_fcat.

ENDFORM. " DISPLAYSALESORDERS

MODULE USER_COMMAND_0100 INPUT.


case sy-ucomm.
when 'FC1'.
leave PROGRAM.
endcase.
ENDMODULE. " USER_COMMAND_0100 INPUT

FORM FLDCAT .
clear wa_fcat.
wa_fcat-fieldname = 'VBELN'.
wa_fcat-col_pos = 1.
wa_fcat-coltext = 'Sales Document'.
wa_fcat-outputlen = 15.
wa_fcat-tooltip = 'Sales Document Number'.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERDAT'.
wa_fcat-col_pos = 2.
wa_fcat-coltext = 'Creation Date'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERZET'.
wa_fcat-col_pos = 3.
wa_fcat-coltext = 'Creation Time'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERNAM'.
wa_fcat-col_pos = 4.
wa_fcat-coltext = 'Name of Person'.
wa_fcat-outputlen = 30.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'NETWR'.
wa_fcat-col_pos = 5.
wa_fcat-coltext = 'Net Value'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.
ENDFORM. " FLDCAT

FORM ROWCOLORING .
loop at t_final_sales ASSIGNING <wa_final>.
if <wa_final>-erdat = '19970121'.
<wa_final>-rowcolor = 'C510'.
elseif <wa_final>-erdat = '19970122'.
<wa_final>-rowcolor = 'C310'.
elseif <wa_final>-erdat = '19970123'.
<wa_final>-rowcolor = 'C710'.
else.
<wa_final>-rowcolor = 'C210'.
endif.
endloop.
ENDFORM. " ROWCOLORING

FORM LAYOUT .
clear wa_layo.
wa_layo-info_fname = 'ROWCOLOR'. "row coloring
ENDFORM. " LAYOUT

Example: ALV Cell coloring

REPORT Z730ALV7.

data v_vbeln type vbak-vbeln.


select-OPTIONS so_vbeln for v_vbeln DEFAULT '4980' to '5020'.

data : o_cust_cont type ref to cl_gui_custom_container,


o_grid type ref to cl_gui_alv_grid.

types : begin of ty_temp_sales.


include type zcvbak.
types : netwr type vbak-netwr,
end of ty_temp_sales.

data : t_temp_sales type table of ty_temp_sales,


wa_temp_sales type ty_temp_sales.

types : begin of ty_final_sales.


include type zcvbak.
types : netwr type vbak-netwr,
cellcolor type lvc_t_scol,
end of ty_final_sales.

data : t_final_sales type table of ty_final_sales,


wa_final_sales type ty_final_sales.

field-symbols <wa_final> like line of t_final_sales.

data wa_cellcolor type LVC_S_SCOL.

data wa_layo type lvc_s_layo.

data : t_fcat type lvc_t_fcat,


wa_fcat type lvc_s_fcat. "normal work area

FIELD-SYMBOLS <abc> like line of t_fcat. "field symbol work area

START-OF-SELECTION.
call screen 100.

MODULE STATUS_0100 OUTPUT.


SET PF-STATUS 'ABC'.

* link custom container with custom control


CREATE OBJECT O_CUST_CONT
EXPORTING
CONTAINER_NAME = 'CUSTCTRL'.

* link alv grid with custom container


CREATE OBJECT O_GRID
EXPORTING
I_PARENT = o_cust_cont.

* get sales orders


perform getsalesorders.
if t_final_sales is not INITIAL.
* generate fieldcatalog
perform fldcat.
* cell coloring
perform cellcoloring.
* layout
perform layout.
* display sales orders
perform displaysalesorders.
endif.
ENDMODULE. " STATUS_0100 OUTPUT

FORM GETSALESORDERS .
select vbeln erdat erzet ernam netwr
from vbak
into table t_temp_sales
where vbeln in so_vbeln.
if sy-subrc eq 0.
* append lines of t_temp_sales to t_final_sales.
loop at t_temp_sales into wa_temp_sales.
clear wa_final_sales.
wa_final_sales-vbeln = wa_temp_sales-vbeln.
wa_final_sales-erdat = wa_temp_sales-erdat.
wa_final_sales-erzet = wa_temp_sales-erzet.
wa_final_sales-ernam = wa_temp_sales-ernam.
wa_final_sales-netwr = wa_temp_sales-netwr.
append wa_final_sales to t_final_sales.
endloop.
endif.
ENDFORM. " GETSALESORDERS

FORM DISPLAYSALESORDERS .
CALL METHOD O_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
IS_LAYOUT = wa_layo
CHANGING
IT_OUTTAB = t_final_sales
IT_FIELDCATALOG = t_fcat.

ENDFORM. " DISPLAYSALESORDERS

MODULE USER_COMMAND_0100 INPUT.


case sy-ucomm.
when 'FC1'.
leave PROGRAM.
endcase.
ENDMODULE. " USER_COMMAND_0100 INPUT

FORM FLDCAT .
clear wa_fcat.
wa_fcat-fieldname = 'VBELN'.
wa_fcat-col_pos = 1.
wa_fcat-coltext = 'Sales Document'.
wa_fcat-outputlen = 15.
wa_fcat-tooltip = 'Sales Document Number'.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERDAT'.
wa_fcat-col_pos = 2.
wa_fcat-coltext = 'Creation Date'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERZET'.
wa_fcat-col_pos = 3.
wa_fcat-coltext = 'Creation Time'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERNAM'.
wa_fcat-col_pos = 4.
wa_fcat-coltext = 'Name of Person'.
wa_fcat-outputlen = 30.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'NETWR'.
wa_fcat-col_pos = 5.
wa_fcat-coltext = 'Net Value'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.
ENDFORM. " FLDCAT

FORM LAYOUT .
clear wa_layo.
wa_layo-ctab_fname = 'CELLCOLOR'. "cell coloring
ENDFORM. " LAYOUT

FORM CELLCOLORING .
loop at t_final_sales assigning <wa_final>.
if <wa_final>-netwr < 10000.
clear wa_cellcolor.
wa_cellcolor-fname = 'VBELN'.
wa_cellcolor-color-col = 3.
wa_cellcolor-color-int = 1.
wa_cellcolor-color-inv = 0.
append wa_cellcolor to <wa_final>-cellcolor.
elseif <wa_final>-netwr >= 10000 and
<wa_final>-netwr < 20000.
clear wa_cellcolor.
wa_cellcolor-fname = 'VBELN'.
wa_cellcolor-color-col = 7.
wa_cellcolor-color-int = 1.
wa_cellcolor-color-inv = 0.
append wa_cellcolor to <wa_final>-cellcolor.
elseif <wa_final>-netwr > 20000 and
<wa_final>-netwr <= 40000.
clear wa_cellcolor.
wa_cellcolor-fname = 'VBELN'.
wa_cellcolor-color-col = 2.
wa_cellcolor-color-int = 1.
wa_cellcolor-color-inv = 0.
append wa_cellcolor to <wa_final>-cellcolor.
elseif <wa_final>-netwr > 40000.
clear wa_cellcolor.
wa_cellcolor-fname = 'VBELN'.
wa_cellcolor-color-col = 6.
wa_cellcolor-color-int = 1.
wa_cellcolor-color-inv = 0.
append wa_cellcolor to <wa_final>-cellcolor.
endif.
endloop.
ENDFORM. " CELLCOLORING

Example: ALV Traffic Light Column (First Position)

REPORT Z730ALV8.

data v_vbeln type vbak-vbeln.


select-OPTIONS so_vbeln for v_vbeln DEFAULT '4980' to '5020'.

data : o_cust_cont type ref to cl_gui_custom_container,


o_grid type ref to cl_gui_alv_grid.
types : begin of ty_temp_sales.
include type zcvbak.
types : netwr type vbak-netwr,
end of ty_temp_sales.

data : t_temp_sales type table of ty_temp_sales,


wa_temp_sales type ty_temp_sales.

types : begin of ty_final_sales.


include type zcvbak.
types : netwr type vbak-netwr,
lights type c,
end of ty_final_sales.

data : t_final_sales type table of ty_final_sales,


wa_final_sales type ty_final_sales.

field-symbols <wa_final> like line of t_final_sales.

data wa_layo type lvc_s_layo.

data : t_fcat type lvc_t_fcat,


wa_fcat type lvc_s_fcat. "normal work area

FIELD-SYMBOLS <abc> like line of t_fcat. "field symbol work area

START-OF-SELECTION.
call screen 100.

MODULE STATUS_0100 OUTPUT.


SET PF-STATUS 'ABC'.

* link custom container with custom control


CREATE OBJECT O_CUST_CONT
EXPORTING
CONTAINER_NAME = 'CUSTCTRL'.

* link alv grid with custom container


CREATE OBJECT O_GRID
EXPORTING
I_PARENT = o_cust_cont.

* get sales orders


perform getsalesorders.
if t_final_sales is not INITIAL.
* generate fieldcatalog
perform fldcat.
* Traffic lights
perform trafficlights.
* layout
perform layout.
* display sales orders
perform displaysalesorders.
endif.
ENDMODULE. " STATUS_0100 OUTPUT

FORM GETSALESORDERS .
select vbeln erdat erzet ernam netwr
from vbak
into table t_temp_sales
where vbeln in so_vbeln.
if sy-subrc eq 0.
append lines of t_temp_sales to t_final_sales.
endif.
ENDFORM. " GETSALESORDERS

FORM DISPLAYSALESORDERS .
CALL METHOD O_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
IS_LAYOUT = wa_layo
CHANGING
IT_OUTTAB = t_final_sales
IT_FIELDCATALOG = t_fcat.

ENDFORM. " DISPLAYSALESORDERS

MODULE USER_COMMAND_0100 INPUT.


case sy-ucomm.
when 'FC1'.
leave PROGRAM.
endcase.
ENDMODULE. " USER_COMMAND_0100 INPUT

FORM FLDCAT .
clear wa_fcat.
wa_fcat-fieldname = 'VBELN'.
wa_fcat-col_pos = 1.
wa_fcat-coltext = 'Sales Document'.
wa_fcat-outputlen = 15.
wa_fcat-tooltip = 'Sales Document Number'.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERDAT'.
wa_fcat-col_pos = 2.
wa_fcat-coltext = 'Creation Date'.
wa_fcat-outputlen = 13.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERZET'.
wa_fcat-col_pos = 3.
wa_fcat-coltext = 'Creation Time'.
wa_fcat-outputlen = 13.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERNAM'.
wa_fcat-col_pos = 4.
wa_fcat-coltext = 'Name of Person'.
wa_fcat-outputlen = 25.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'LIGHTS'.
wa_fcat-col_pos = 5.
wa_fcat-coltext = 'Status'.
wa_fcat-outputlen = 7.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'NETWR'.
wa_fcat-col_pos = 6.
wa_fcat-coltext = 'Net Value'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.
ENDFORM. " FLDCAT

FORM LAYOUT .
clear wa_layo.
wa_layo-grid_title = 'Traffic Lights'.
wa_layo-excp_fname = 'LIGHTS'.
ENDFORM. " LAYOUT

FORM TRAFFICLIGHTS .
loop at t_final_sales ASSIGNING <wa_final>.
if <wa_final>-netwr < 10000.
<wa_final>-lights = '1'. "red color
elseif <wa_final>-netwr >= 10000 and <wa_final>-netwr <= 30000.
<wa_final>-lights = '2'. "yellow color
else.
<wa_final>-lights = '3'. "green color
endif.
endloop.
ENDFORM. " TRAFFICLIGHTS

Flowlogic:

PROCESS BEFORE OUTPUT.


MODULE STATUS_0100.
*
PROCESS AFTER INPUT.
MODULE USER_COMMAND_0100.

Screen 100 Layout:


Example: ALV Traffic Light Column (Specific Position)

REPORT Z730ALV9.

data v_vbeln type vbak-vbeln.


select-OPTIONS so_vbeln for v_vbeln DEFAULT '4980' to '5020'.

data : o_cust_cont type ref to cl_gui_custom_container,


o_grid type ref to cl_gui_alv_grid.

types : begin of ty_temp_sales.


include type zcvbak.
types : netwr type vbak-netwr,
end of ty_temp_sales.

data : t_temp_sales type table of ty_temp_sales,


wa_temp_sales type ty_temp_sales.

types : begin of ty_final_sales.


include type zcvbak.
types : netwr type vbak-netwr,
lights(20) type c,
end of ty_final_sales.

data : t_final_sales type table of ty_final_sales,


wa_final_sales type ty_final_sales.

field-symbols <wa_final> like line of t_final_sales.

data wa_layo type lvc_s_layo.

data : t_fcat type lvc_t_fcat,


wa_fcat type lvc_s_fcat. "normal work area

FIELD-SYMBOLS <abc> like line of t_fcat. "field symbol work area

START-OF-SELECTION.
call screen 100.

MODULE STATUS_0100 OUTPUT.


SET PF-STATUS 'ABC'.

* link custom container with custom control


CREATE OBJECT O_CUST_CONT
EXPORTING
CONTAINER_NAME = 'CUSTCTRL'.

* link alv grid with custom container


CREATE OBJECT O_GRID
EXPORTING
I_PARENT = o_cust_cont.

* get sales orders


perform getsalesorders.
if t_final_sales is not INITIAL.
* generate fieldcatalog
perform fldcat.
* Traffic lights
perform trafficlights.
* layout
perform layout.
* display sales orders
perform displaysalesorders.
endif.
ENDMODULE. " STATUS_0100 OUTPUT

FORM GETSALESORDERS .
select vbeln erdat erzet ernam netwr
from vbak
into table t_temp_sales
where vbeln in so_vbeln.
if sy-subrc eq 0.
append lines of t_temp_sales to t_final_sales.
endif.
ENDFORM. " GETSALESORDERS

FORM DISPLAYSALESORDERS .
CALL METHOD O_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
IS_LAYOUT = wa_layo
CHANGING
IT_OUTTAB = t_final_sales
IT_FIELDCATALOG = t_fcat.

ENDFORM. " DISPLAYSALESORDERS

MODULE USER_COMMAND_0100 INPUT.


case sy-ucomm.
when 'FC1'.
leave PROGRAM.
endcase.
ENDMODULE. " USER_COMMAND_0100 INPUT

FORM FLDCAT .
clear wa_fcat.
wa_fcat-fieldname = 'VBELN'.
wa_fcat-col_pos = 1.
wa_fcat-coltext = 'Sales Document'.
wa_fcat-outputlen = 15.
wa_fcat-tooltip = 'Sales Document Number'.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERDAT'.
wa_fcat-col_pos = 2.
wa_fcat-coltext = 'Creation Date'.
wa_fcat-outputlen = 13.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERZET'.
wa_fcat-col_pos = 3.
wa_fcat-coltext = 'Creation Time'.
wa_fcat-outputlen = 13.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERNAM'.
wa_fcat-col_pos = 4.
wa_fcat-coltext = 'Name of Person'.
wa_fcat-outputlen = 20.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'LIGHTS'.
wa_fcat-col_pos = 5.
wa_fcat-coltext = 'Status'.
wa_fcat-outputlen = 10.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'NETWR'.
wa_fcat-col_pos = 6.
wa_fcat-coltext = 'Net Value'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.
ENDFORM. " FLDCAT

FORM LAYOUT .
clear wa_layo.
wa_layo-grid_title = 'Traffic Lights'.
ENDFORM. " LAYOUT

FORM TRAFFICLIGHTS .
loop at t_final_sales ASSIGNING <wa_final>.
if <wa_final>-netwr < 10000.
<wa_final>-lights = '@0A@'. "red color
* <wa_final>-lights = '1'. "red color
CONCATENATE <wa_final>-lights 'Red' into <wa_final>-lights SEPARATED BY space.
elseif <wa_final>-netwr >= 10000 and <wa_final>-netwr <= 30000.
<wa_final>-lights = '@09@'. "yellow color
CONCATENATE <wa_final>-lights 'Yellow' into <wa_final>-lights SEPARATED BY space.
else.
<wa_final>-lights = '@08@'. "green color
CONCATENATE <wa_final>-lights 'Green' into <wa_final>-lights SEPARATED BY space.
endif.
endloop.
ENDFORM. " TRAFFICLIGHTS

Flowlogic:

PROCESS BEFORE OUTPUT.


MODULE STATUS_0100.
*
PROCESS AFTER INPUT.
MODULE USER_COMMAND_0100.

Screen 100 Layout:

Example: Splitter Control, Tree Control, Picture Control


REPORT Z730ALV10.

data : o_cust_cont type ref to cl_gui_custom_container,


o_split type ref to cl_gui_splitter_container,
o_cont1 type ref to cl_gui_container,
o_cont2 type ref to cl_gui_container,
o_tree type ref to cl_gui_simple_tree,
o_pic type ref to cl_gui_picture.

data : t_nodes type table of ztreestr,


wa_nodes like line of t_nodes.

call screen 100.

MODULE STATUS_0100 OUTPUT.


if o_cust_cont is INITIAL.
SET PF-STATUS 'ABC'.

* link custom control with custom container


CREATE OBJECT O_CUST_CONT
EXPORTING
CONTAINER_NAME = 'CUSTCTRL'.

* link splitter container with custom container


CREATE OBJECT O_SPLIT
EXPORTING
PARENT = o_cust_cont
ROWS =1
COLUMNS = 2.

* Specify widths for each column


CALL METHOD O_SPLIT->SET_COLUMN_WIDTH
EXPORTING
ID =1
WIDTH = 10.

CALL METHOD O_SPLIT->SET_COLUMN_WIDTH


EXPORTING
ID =2
WIDTH = 16.

* Associate containers for each pane


CALL METHOD O_SPLIT->GET_CONTAINER
EXPORTING
ROW =1
COLUMN = 1
RECEIVING
CONTAINER = o_cont1.

CALL METHOD O_SPLIT->GET_CONTAINER


EXPORTING
ROW =1
COLUMN = 2
RECEIVING
CONTAINER = o_cont2.

* logic for tree control


perform tree.

* logic for picture control


perform picture.
endif.

ENDMODULE. " STATUS_0100 OUTPUT

MODULE USER_COMMAND_0100 INPUT.


case sy-ucomm.
when 'FC1'.
leave PROGRAM.
endcase.
ENDMODULE. " USER_COMMAND_0100 INPUT

FORM TREE .
* link tree control with container (o_cont1)
CREATE OBJECT O_TREE
EXPORTING
PARENT = o_cont1
NODE_SELECTION_MODE = cl_gui_simple_tree=>node_sel_mode_single.

* construct nodes
clear wa_nodes.
wa_nodes-node_key = 'RT'.
wa_nodes-isfolder = 'X'.
wa_nodes-expander = 'X'.
wa_nodes-text = 'Transactions'.
append wa_nodes to t_nodes.
clear wa_nodes.
wa_nodes-node_key = 'SO'.
wa_nodes-relatkey = 'RT'.
wa_nodes-isfolder = 'X'.
wa_nodes-expander = 'X'.
wa_nodes-text = 'Sales Orders'.
append wa_nodes to t_nodes.

clear wa_nodes.
wa_nodes-node_key = 'CSO'.
wa_nodes-relatkey = 'SO'.
wa_nodes-n_image = '@15@'.
wa_nodes-text = 'Create Sales Orders'.
append wa_nodes to t_nodes.

clear wa_nodes.
wa_nodes-node_key = 'CHSO'.
wa_nodes-relatkey = 'SO'.
wa_nodes-n_image = '@15@'.
wa_nodes-text = 'Change Sales Orders'.
append wa_nodes to t_nodes.

clear wa_nodes.
wa_nodes-node_key = 'DSO'.
wa_nodes-relatkey = 'SO'.
wa_nodes-n_image = '@15@'.
wa_nodes-text = 'Display Sales Orders'.
append wa_nodes to t_nodes.

clear wa_nodes.
wa_nodes-node_key = 'PO'.
wa_nodes-relatkey = 'RT'.
wa_nodes-isfolder = 'X'.
wa_nodes-expander = 'X'.
wa_nodes-text = 'Purchase Orders'.
append wa_nodes to t_nodes.

clear wa_nodes.
wa_nodes-node_key = 'CPO'.
wa_nodes-relatkey = 'PO'.
wa_nodes-n_image = '@16@'.
wa_nodes-text = 'Create Purchase Order'.
append wa_nodes to t_nodes.
clear wa_nodes.
wa_nodes-node_key = 'CHPO'.
wa_nodes-relatkey = 'PO'.
wa_nodes-n_image = '@16@'.
wa_nodes-text = 'Change Purchase Order'.
append wa_nodes to t_nodes.

clear wa_nodes.
wa_nodes-node_key = 'DPO'.
wa_nodes-relatkey = 'PO'.
wa_nodes-n_image = '@16@'.
wa_nodes-text = 'Display Purchase Orders'.
append wa_nodes to t_nodes.

* add nodes to tree control


CALL METHOD O_TREE->ADD_NODES
EXPORTING
TABLE_STRUCTURE_NAME = 'ZTREESTR'
NODE_TABLE = t_nodes.

ENDFORM. " TREE

FORM PICTURE .
* link picture control with container (o_cont2)
CREATE OBJECT O_PIC
EXPORTING
PARENT = o_cont2.

* get the URL of the picture


data lv_pic_url type CNDP_URL.
CALL FUNCTION 'DP_PUBLISH_WWW_URL'
EXPORTING
OBJID = 'Z730SUNFLOWER'
LIFETIME = 'X'
IMPORTING
URL = lv_pic_url.

* display the picture


CALL METHOD O_PIC->LOAD_PICTURE_FROM_URL
EXPORTING
URL = lv_pic_url.
ENDFORM. " PICTURE

Flowlogic:
PROCESS BEFORE OUTPUT.
MODULE STATUS_0100.
*
PROCESS AFTER INPUT.
MODULE USER_COMMAND_0100.

Screen 100 Layout:

Example: Managing ALV Toolbar (Adding Custom Buttons, Enabling / disabling standard ALV
toolbar buttons)

REPORT Z730ALV11.

data v_vbeln type vbak-vbeln.


select-OPTIONS so_vbeln for v_vbeln DEFAULT '4980' to '4985'.

data : o_cust_cont type ref to cl_gui_custom_container,


o_grid type ref to cl_gui_alv_grid.

types : begin of ty_sales,


vbeln type vbak-vbeln,
erdat type vbak-erdat,
erzet type vbak-erzet,
ernam type vbak-ernam,
end of ty_sales.

data t_sales type table of ty_sales.

data wa_layo type lvc_s_layo.

data : t_fcat type lvc_t_fcat,


wa_fcat type lvc_s_fcat. "normal work area

FIELD-SYMBOLS <abc> like line of t_fcat. "field symbol work area

class lcl_eventreceiver DEFINITION.


PUBLIC SECTION.
methods handle_toolbar
for event toolbar of cl_gui_alv_grid
importing e_object.
endclass.

class lcl_eventreceiver IMPLEMENTATION.


method handle_toolbar.
* message 'Toolbar event' type 'I'.
data wa_button type STB_BUTTON.

* logic for disabling/enabling std.alv toolbar buttons


clear wa_button.
wa_button-disabled = 'X'.
modify e_object->mt_toolbar from wa_button
TRANSPORTING disabled
where function = cl_gui_alv_grid=>mc_fc_find.

* logic for adding custom buttons


* prepare work area for adding custom buttons
clear wa_button.
wa_button-function = 'F1'.
wa_button-icon = '@15@'.
wa_button-quickinfo = 'Display Sales Items'.
wa_button-butn_type = 0. "normal button
wa_button-text = 'Sales Items'.
append wa_button to e_object->mt_toolbar.

clear wa_button.
wa_button-butn_type = 3. "separator
append wa_button to e_object->mt_toolbar.

clear wa_button.
wa_button-function = 'F2'.
wa_button-icon = '@16@'.
wa_button-quickinfo = 'Navigate to Transactions'.
wa_button-butn_type = 2. "menu button
wa_button-text = 'Transactions'.
append wa_button to e_object->mt_toolbar.
endmethod.
endclass.

data ob type ref to lcl_eventreceiver.

START-OF-SELECTION.
call screen 100.

MODULE STATUS_0100 OUTPUT.


SET PF-STATUS 'ABC'.

* link custom container with custom control


CREATE OBJECT O_CUST_CONT
EXPORTING
CONTAINER_NAME = 'CUSTCTRL'.

* link alv grid with custom container


CREATE OBJECT O_GRID
EXPORTING
I_PARENT = o_cust_cont.

* get sales orders


perform getsalesorders.
if t_sales is not INITIAL.
* generate fieldcatalog
perform fldcat.
* generate layout
perform layout.
* register handlers
perform reghandlers.
* display sales orders
perform displaysalesorders.
endif.
ENDMODULE. " STATUS_0100 OUTPUT

FORM GETSALESORDERS .
select vbeln erdat erzet ernam
from vbak
into table t_sales
where vbeln in so_vbeln.
ENDFORM. " GETSALESORDERS

FORM DISPLAYSALESORDERS .
CALL METHOD O_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
IS_LAYOUT = wa_layo
CHANGING
IT_OUTTAB = t_sales
IT_FIELDCATALOG = t_fcat.

ENDFORM. " DISPLAYSALESORDERS

MODULE USER_COMMAND_0100 INPUT.


case sy-ucomm.
when 'FC1'.
leave PROGRAM.
endcase.
ENDMODULE. " USER_COMMAND_0100 INPUT

FORM FLDCAT .
clear wa_fcat.
wa_fcat-fieldname = 'VBELN'.
wa_fcat-col_pos = 1.
wa_fcat-coltext = 'Sales Document'.
wa_fcat-outputlen = 15.
wa_fcat-tooltip = 'Sales Document Number'.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERDAT'.
wa_fcat-col_pos = 2.
wa_fcat-coltext = 'Creation Date'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.
clear wa_fcat.
wa_fcat-fieldname = 'ERZET'.
wa_fcat-col_pos = 3.
wa_fcat-coltext = 'Creation Time'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERNAM'.
wa_fcat-col_pos = 4.
wa_fcat-coltext = 'Name of Person Created Sales Doc'.
wa_fcat-outputlen = 40.
append wa_fcat to t_fcat.
ENDFORM. " FLDCAT

FORM REGHANDLERS .
create object ob.
set handler ob->handle_toolbar for o_grid.
ENDFORM. " REGHANDLERS

FORM LAYOUT .
clear wa_layo.
wa_layo-sel_mode = 'A'. "multiple selection
ENDFORM. " LAYOUT

Example: MENU_BUTTON Event (Associating Menu Items with Menu Button),


USER_COMMAND Event (Actions on Normal Button and Menu Items of Menu Button)

REPORT Z730ALV12.

data v_vbeln type vbak-vbeln.


select-OPTIONS so_vbeln for v_vbeln DEFAULT '4980' to '4985'.

data : o_cust_cont type ref to cl_gui_custom_container,


o_grid type ref to cl_gui_alv_grid.

data : o_cust_cont2 type ref to cl_gui_custom_container,


o_grid2 type ref to cl_gui_alv_grid.

types : begin of ty_sales,


vbeln type vbak-vbeln,
erdat type vbak-erdat,
erzet type vbak-erzet,
ernam type vbak-ernam,
end of ty_sales.

data : t_sales type table of ty_sales,


lt_sales type table of ty_sales,
wa_sales type ty_sales.

types : begin of ty_sales_items.


include type zcvbap.
types end of ty_sales_items.

data : t_sales_items type table of ty_sales_items,


wa_sales_items type ty_sales_items.

data wa_layo type lvc_s_layo.

data : t_fcat type lvc_t_fcat,


wa_fcat type lvc_s_fcat. "normal work area

FIELD-SYMBOLS <abc> like line of t_fcat. "field symbol work area

class lcl_eventreceiver DEFINITION.


PUBLIC SECTION.
methods handle_toolbar
for event toolbar of cl_gui_alv_grid
importing e_object.
methods handle_menu_button
for event menu_button of cl_gui_alv_grid
IMPORTING e_object e_ucomm.
methods handle_user_command
for event user_command of cl_gui_alv_grid
IMPORTING e_ucomm.
endclass.

class lcl_eventreceiver IMPLEMENTATION.


method handle_toolbar.
* message 'Toolbar event' type 'I'.
data wa_button type STB_BUTTON.

* logic for disabling/enabling std.alv toolbar buttons


clear wa_button.
wa_button-disabled = 'X'.
modify e_object->mt_toolbar from wa_button
TRANSPORTING disabled
where function = cl_gui_alv_grid=>mc_fc_find.

* logic for adding custom buttons


* prepare work area for adding custom buttons
clear wa_button.
wa_button-function = 'F1'.
wa_button-icon = '@15@'.
wa_button-quickinfo = 'Display Sales Items'.
wa_button-butn_type = 0. "normal button
wa_button-text = 'Sales Items'.
append wa_button to e_object->mt_toolbar.

clear wa_button.
wa_button-butn_type = 3. "separator
append wa_button to e_object->mt_toolbar.

clear wa_button.
wa_button-function = 'F2'.
wa_button-icon = '@16@'.
wa_button-quickinfo = 'Navigate to Transactions'.
wa_button-butn_type = 2. "menu button
wa_button-text = 'Transactions'.
append wa_button to e_object->mt_toolbar.
endmethod.

method handle_menu_button.
* message 'menu button event' type 'I'.
case e_ucomm.
when 'F2'.
CALL METHOD E_OBJECT->ADD_FUNCTION
EXPORTING
FCODE = 'MI1'
TEXT = 'ABAP EDITOR'.

CALL METHOD E_OBJECT->ADD_FUNCTION


EXPORTING
FCODE = 'MI2'
TEXT = 'FUNCTION BUILDER'.

CALL METHOD E_OBJECT->ADD_FUNCTION


EXPORTING
FCODE = 'MI3'
TEXT = 'CLASS BUILDER'.
CALL METHOD E_OBJECT->ADD_SEPARATOR.

CALL METHOD E_OBJECT->ADD_FUNCTION


EXPORTING
FCODE = 'MI4'
TEXT = 'LOGICAL DATABASE BUILDER'.

data k type ref to cl_ctmenu.


create object k.

CALL METHOD k->ADD_FUNCTION


EXPORTING
FCODE = 'MI5'
TEXT = 'Define Background Job'.

CALL METHOD k->ADD_FUNCTION


EXPORTING
FCODE = 'MI6'
TEXT = 'Background job overview'.

CALL METHOD E_OBJECT->ADD_MENU


EXPORTING
MENU = k.

data ob type ref to cl_ctmenu.


create object ob.

CALL METHOD ob->ADD_FUNCTION


EXPORTING
FCODE = 'MI7'
TEXT = 'Form Painter'.

CALL METHOD ob->ADD_FUNCTION


EXPORTING
FCODE = 'MI8'
TEXT = 'Smart Forms'.

CALL METHOD ob->ADD_FUNCTION


EXPORTING
FCODE = 'MI9'
TEXT = 'Form Builder'.

* add submenu
CALL METHOD E_OBJECT->ADD_SUBMENU
EXPORTING
MENU = ob
TEXT = 'Forms'.
endcase.
endmethod.

method handle_user_command.
case e_ucomm.
when 'F1'. "normal button function code
* message 'Normal button' type 'I'.
* get indexes of selected rows
data : t_rows type lvc_t_row,
wa_row type lvc_s_row.
CALL METHOD O_GRID->GET_SELECTED_ROWS
IMPORTING
ET_INDEX_ROWS = t_rows.

if t_rows is INITIAL .
message 'Please select atleast one row' type 'I'.
else.
refresh lt_sales.
loop at t_rows into wa_row.
clear wa_sales.
read table t_sales into wa_sales index wa_row-index
TRANSPORTING vbeln.
if sy-subrc eq 0.
append wa_sales to lt_sales.
endif.
endloop.
if lt_sales is not INITIAL.
* get sales items for selected sales orders
perform getsalesitems.
if t_sales_items is not initial.
call screen 200.
else.
message 'No sales items for selected sales orders' type 'I'.
endif.
endif.
endif.
when 'MI1'.
call transaction 'SE38'.
when 'MI2'.
call transaction 'SE37'.
when 'MI3'.
call transaction 'SE24'.
when 'MI4'.
call transaction 'SE36'.
when 'MI5'.
call transaction 'SM36'.
when 'MI6'.
call transaction 'SM37'.
when 'MI7'.
call transaction 'SE71'.
when 'MI8'.
call transaction 'SMARTFORMS'.
when 'MI9'.
call transaction 'SFP'.
endcase.
endmethod.
endclass.

data ob type ref to lcl_eventreceiver.

START-OF-SELECTION.
call screen 100.

MODULE STATUS_0100 OUTPUT.


SET PF-STATUS 'ABC'.

* link custom container with custom control


CREATE OBJECT O_CUST_CONT
EXPORTING
CONTAINER_NAME = 'CUSTCTRL'.

* link alv grid with custom container


CREATE OBJECT O_GRID
EXPORTING
I_PARENT = o_cust_cont.

* get sales orders


perform getsalesorders.
if t_sales is not INITIAL.
* generate fieldcatalog
perform fldcat.
* generate layout
perform layout.
* register handlers
perform reghandlers.
* display sales orders
perform displaysalesorders.
endif.
ENDMODULE. " STATUS_0100 OUTPUT

FORM GETSALESORDERS .
select vbeln erdat erzet ernam
from vbak
into table t_sales
where vbeln in so_vbeln.
ENDFORM. " GETSALESORDERS

FORM DISPLAYSALESORDERS .
CALL METHOD O_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
IS_LAYOUT = wa_layo
CHANGING
IT_OUTTAB = t_sales
IT_FIELDCATALOG = t_fcat.

ENDFORM. " DISPLAYSALESORDERS

MODULE USER_COMMAND_0100 INPUT.


case sy-ucomm.
when 'FC1'.
leave PROGRAM.
endcase.
ENDMODULE. " USER_COMMAND_0100 INPUT

FORM FLDCAT .
clear wa_fcat.
wa_fcat-fieldname = 'VBELN'.
wa_fcat-col_pos = 1.
wa_fcat-coltext = 'Sales Document'.
wa_fcat-outputlen = 15.
wa_fcat-tooltip = 'Sales Document Number'.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERDAT'.
wa_fcat-col_pos = 2.
wa_fcat-coltext = 'Creation Date'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERZET'.
wa_fcat-col_pos = 3.
wa_fcat-coltext = 'Creation Time'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERNAM'.
wa_fcat-col_pos = 4.
wa_fcat-coltext = 'Name of Person Created Sales Doc'.
wa_fcat-outputlen = 40.
append wa_fcat to t_fcat.
ENDFORM. " FLDCAT

FORM REGHANDLERS .
create object ob.
set handler ob->handle_toolbar for o_grid.
set handler ob->handle_menu_button for o_grid.
set handler ob->handle_user_command for o_grid.
ENDFORM. " REGHANDLERS

FORM LAYOUT .
clear wa_layo.
wa_layo-sel_mode = 'A'. "multiple selection
ENDFORM. " LAYOUT

FORM GETSALESITEMS .
select vbeln posnr matnr netwr
from vbap
into table t_sales_items
for ALL ENTRIES IN lt_sales
where vbeln = lt_sales-vbeln.
ENDFORM. " GETSALESITEMS

MODULE STATUS_0200 OUTPUT.


SET PF-STATUS 'PQR'.
* link custom container with custom control
CREATE OBJECT O_CUST_CONT2
EXPORTING
CONTAINER_NAME = 'CUSTCTRL2'.
* link alv grid with custom container
CREATE OBJECT O_GRID2
EXPORTING
I_PARENT = o_cust_cont2.

* generate fieldcatalog for sales items fields


perform fldcat_items.
* display sales items
perform display_items.
ENDMODULE. " STATUS_0200 OUTPUT

FORM FLDCAT_ITEMS .
refresh t_fcat.
clear wa_fcat.
wa_fcat-fieldname = 'VBELN'.
wa_fcat-col_pos = 1.
wa_fcat-coltext = 'Sales Document'.
wa_fcat-outputlen = 15.
wa_fcat-tooltip = 'Sales Document Number'.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'POSNR'.
wa_fcat-col_pos = 2.
wa_fcat-coltext = 'Item Number'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'MATNR'.
wa_fcat-col_pos = 3.
wa_fcat-coltext = 'Material No'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'NETWR'.
wa_fcat-col_pos = 4.
wa_fcat-coltext = 'Netvalue'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.
ENDFORM. " FLDCAT_ITEMS

FORM DISPLAY_ITEMS .
CALL METHOD O_GRID2->SET_TABLE_FOR_FIRST_DISPLAY
CHANGING
IT_OUTTAB = t_sales_items
IT_FIELDCATALOG = t_fcat.

ENDFORM. " DISPLAY_ITEMS

MODULE USER_COMMAND_0200 INPUT.


case sy-ucomm.
when 'BACK'.
leave to screen 100.
endcase.
ENDMODULE. " USER_COMMAND_0200 INPUT

Flowlogic of screen 100:

PROCESS BEFORE OUTPUT.


MODULE STATUS_0100.
*
PROCESS AFTER INPUT.
MODULE USER_COMMAND_0100.

Flowlogic of screen 200:

PROCESS BEFORE OUTPUT.


MODULE STATUS_0200.
*
PROCESS AFTER INPUT.
MODULE USER_COMMAND_0200.

PF-STATUS ‘ABC’ of screen 100:


PF-STATUS ‘ABC’ of screen 200:

Screen 100 Layout:


Screen 200 Layout:
Example: Identifying Selected Rows on ALV grid and displaying corresponding data in the next
screen ALV grid, Refreshing ALV Grid

REPORT Z730ALV13.

data v_vbeln type vbak-vbeln.


select-OPTIONS so_vbeln for v_vbeln DEFAULT '4980' to '4985'.

data : o_cust_cont type ref to cl_gui_custom_container,


o_grid type ref to cl_gui_alv_grid.

data : o_cust_cont2 type ref to cl_gui_custom_container,


o_grid2 type ref to cl_gui_alv_grid.

types : begin of ty_sales,


vbeln type vbak-vbeln,
erdat type vbak-erdat,
erzet type vbak-erzet,
ernam type vbak-ernam,
end of ty_sales.

data : t_sales type table of ty_sales,


lt_sales type table of ty_sales,
wa_sales type ty_sales.

types : begin of ty_sales_items.


include type zcvbap.
types end of ty_sales_items.

data : t_sales_items type table of ty_sales_items,


wa_sales_items type ty_sales_items.

data wa_layo type lvc_s_layo.

data : t_fcat type lvc_t_fcat,


wa_fcat type lvc_s_fcat. "normal work area

FIELD-SYMBOLS <abc> like line of t_fcat. "field symbol work area

class lcl_eventreceiver DEFINITION.


PUBLIC SECTION.
methods handle_toolbar
for event toolbar of cl_gui_alv_grid
importing e_object.
methods handle_menu_button
for event menu_button of cl_gui_alv_grid
IMPORTING e_object e_ucomm.
methods handle_user_command
for event user_command of cl_gui_alv_grid
IMPORTING e_ucomm.
endclass.

class lcl_eventreceiver IMPLEMENTATION.


method handle_toolbar.
* message 'Toolbar event' type 'I'.
data wa_button type STB_BUTTON.

* logic for disabling/enabling std.alv toolbar buttons


clear wa_button.
wa_button-disabled = 'X'.
modify e_object->mt_toolbar from wa_button
TRANSPORTING disabled
where function = cl_gui_alv_grid=>mc_fc_find.

* logic for adding custom buttons


* prepare work area for adding custom buttons
clear wa_button.
wa_button-function = 'F1'.
wa_button-icon = '@15@'.
wa_button-quickinfo = 'Display Sales Items'.
wa_button-butn_type = 0. "normal button
wa_button-text = 'Sales Items'.
append wa_button to e_object->mt_toolbar.

clear wa_button.
wa_button-butn_type = 3. "separator
append wa_button to e_object->mt_toolbar.

clear wa_button.
wa_button-function = 'F2'.
wa_button-icon = '@16@'.
wa_button-quickinfo = 'Navigate to Transactions'.
wa_button-butn_type = 2. "menu button
wa_button-text = 'Transactions'.
append wa_button to e_object->mt_toolbar.
endmethod.
method handle_menu_button.
* message 'menu button event' type 'I'.
case e_ucomm.
when 'F2'.
CALL METHOD E_OBJECT->ADD_FUNCTION
EXPORTING
FCODE = 'MI1'
TEXT = 'ABAP EDITOR'.

CALL METHOD E_OBJECT->ADD_FUNCTION


EXPORTING
FCODE = 'MI2'
TEXT = 'FUNCTION BUILDER'.

CALL METHOD E_OBJECT->ADD_FUNCTION


EXPORTING
FCODE = 'MI3'
TEXT = 'CLASS BUILDER'.

CALL METHOD E_OBJECT->ADD_SEPARATOR.

CALL METHOD E_OBJECT->ADD_FUNCTION


EXPORTING
FCODE = 'MI4'
TEXT = 'LOGICAL DATABASE BUILDER'.

data k type ref to cl_ctmenu.


create object k.

CALL METHOD k->ADD_FUNCTION


EXPORTING
FCODE = 'MI5'
TEXT = 'Define Background Job'.

CALL METHOD k->ADD_FUNCTION


EXPORTING
FCODE = 'MI6'
TEXT = 'Background job overview'.

CALL METHOD E_OBJECT->ADD_MENU


EXPORTING
MENU = k.

data ob type ref to cl_ctmenu.


create object ob.

CALL METHOD ob->ADD_FUNCTION


EXPORTING
FCODE = 'MI7'
TEXT = 'Form Painter'.

CALL METHOD ob->ADD_FUNCTION


EXPORTING
FCODE = 'MI8'
TEXT = 'Smart Forms'.

CALL METHOD ob->ADD_FUNCTION


EXPORTING
FCODE = 'MI9'
TEXT = 'Form Builder'.

* add submenu
CALL METHOD E_OBJECT->ADD_SUBMENU
EXPORTING
MENU = ob
TEXT = 'Forms'.
endcase.
endmethod.

method handle_user_command.
case e_ucomm.
when 'F1'. "normal button function code
* message 'Normal button' type 'I'.
* get indexes of selected rows
data : t_rows type lvc_t_row,
wa_row type lvc_s_row.
CALL METHOD O_GRID->GET_SELECTED_ROWS
IMPORTING
ET_INDEX_ROWS = t_rows.

if t_rows is INITIAL .
message 'Please select atleast one row' type 'I'.
else.
refresh lt_sales.
loop at t_rows into wa_row.
clear wa_sales.
read table t_sales into wa_sales index wa_row-index
TRANSPORTING vbeln.
if sy-subrc eq 0.
append wa_sales to lt_sales.
endif.
endloop.
if lt_sales is not INITIAL.
* get sales items for selected sales orders
perform getsalesitems.
if t_sales_items is not initial.
call screen 200.
else.
message 'No sales items for selected sales orders' type 'I'.
endif.
endif.
endif.
when 'MI1'.
call transaction 'SE38'.
when 'MI2'.
call transaction 'SE37'.
when 'MI3'.
call transaction 'SE24'.
when 'MI4'.
call transaction 'SE36'.
when 'MI5'.
call transaction 'SM36'.
when 'MI6'.
call transaction 'SM37'.
when 'MI7'.
call transaction 'SE71'.
when 'MI8'.
call transaction 'SMARTFORMS'.
when 'MI9'.
call transaction 'SFP'.
endcase.
endmethod.
endclass.

data ob type ref to lcl_eventreceiver.

START-OF-SELECTION.
call screen 100.

MODULE STATUS_0100 OUTPUT.


if o_cust_cont is INITIAL.
SET PF-STATUS 'ABC'.
* link custom container with custom control
CREATE OBJECT O_CUST_CONT
EXPORTING
CONTAINER_NAME = 'CUSTCTRL'.

* link alv grid with custom container


CREATE OBJECT O_GRID
EXPORTING
I_PARENT = o_cust_cont.

* get sales orders


perform getsalesorders.
if t_sales is not INITIAL.
* generate fieldcatalog
perform fldcat.
* generate layout
perform layout.
* register handlers
perform reghandlers.
* display sales orders
perform displaysalesorders.
endif.
else. "second visit to screen 100
* refresh ALV Grid
CALL METHOD O_GRID->REFRESH_TABLE_DISPLAY.
endif.
ENDMODULE. " STATUS_0100 OUTPUT

FORM GETSALESORDERS .
select vbeln erdat erzet ernam
from vbak
into table t_sales
where vbeln in so_vbeln.
ENDFORM. " GETSALESORDERS

FORM DISPLAYSALESORDERS .
CALL METHOD O_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
IS_LAYOUT = wa_layo
CHANGING
IT_OUTTAB = t_sales
IT_FIELDCATALOG = t_fcat.
ENDFORM. " DISPLAYSALESORDERS

MODULE USER_COMMAND_0100 INPUT.


case sy-ucomm.
when 'FC1'.
leave PROGRAM.
endcase.
ENDMODULE. " USER_COMMAND_0100 INPUT

FORM FLDCAT .
clear wa_fcat.
wa_fcat-fieldname = 'VBELN'.
wa_fcat-col_pos = 1.
wa_fcat-coltext = 'Sales Document'.
wa_fcat-outputlen = 15.
wa_fcat-tooltip = 'Sales Document Number'.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERDAT'.
wa_fcat-col_pos = 2.
wa_fcat-coltext = 'Creation Date'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERZET'.
wa_fcat-col_pos = 3.
wa_fcat-coltext = 'Creation Time'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERNAM'.
wa_fcat-col_pos = 4.
wa_fcat-coltext = 'Name of Person Created Sales Doc'.
wa_fcat-outputlen = 40.
append wa_fcat to t_fcat.
ENDFORM. " FLDCAT

FORM REGHANDLERS .
create object ob.
set handler ob->handle_toolbar for o_grid.
set handler ob->handle_menu_button for o_grid.
set handler ob->handle_user_command for o_grid.
ENDFORM. " REGHANDLERS

FORM LAYOUT .
clear wa_layo.
wa_layo-sel_mode = 'A'. "multiple selection
ENDFORM. " LAYOUT

FORM GETSALESITEMS .
select vbeln posnr matnr netwr
from vbap
into table t_sales_items
for ALL ENTRIES IN lt_sales
where vbeln = lt_sales-vbeln.
ENDFORM. " GETSALESITEMS

MODULE STATUS_0200 OUTPUT.


if o_cust_cont2 is INITIAL.
SET PF-STATUS 'PQR'.
* link custom container with custom control
CREATE OBJECT O_CUST_CONT2
EXPORTING
CONTAINER_NAME = 'CUSTCTRL2'.

* link alv grid with custom container


CREATE OBJECT O_GRID2
EXPORTING
I_PARENT = o_cust_cont2.

* generate fieldcatalog for sales items fields


perform fldcat_items.
* display sales items
perform display_items.
else. "second visit to screen 200
* refresh ALV grid
CALL METHOD O_GRID2->REFRESH_TABLE_DISPLAY.
endif.
ENDMODULE. " STATUS_0200 OUTPUT

FORM FLDCAT_ITEMS .
refresh t_fcat.
clear wa_fcat.
wa_fcat-fieldname = 'VBELN'.
wa_fcat-col_pos = 1.
wa_fcat-coltext = 'Sales Document'.
wa_fcat-outputlen = 15.
wa_fcat-tooltip = 'Sales Document Number'.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'POSNR'.
wa_fcat-col_pos = 2.
wa_fcat-coltext = 'Item Number'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'MATNR'.
wa_fcat-col_pos = 3.
wa_fcat-coltext = 'Material No'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'NETWR'.
wa_fcat-col_pos = 4.
wa_fcat-coltext = 'Netvalue'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.
ENDFORM. " FLDCAT_ITEMS

FORM DISPLAY_ITEMS .
CALL METHOD O_GRID2->SET_TABLE_FOR_FIRST_DISPLAY
CHANGING
IT_OUTTAB = t_sales_items
IT_FIELDCATALOG = t_fcat.

ENDFORM. " DISPLAY_ITEMS

MODULE USER_COMMAND_0200 INPUT.


case sy-ucomm.
when 'BACK'.
* clear the selection in alv grid screen 100
CALL METHOD O_GRID->SET_SELECTED_ROWS
EXPORTING
IS_KEEP_OTHER_SELECTIONS = ' '.

leave to screen 100.


endcase.
ENDMODULE. " USER_COMMAND_0200 INPUT

Flowlogic of screen 100:

PROCESS BEFORE OUTPUT.


MODULE STATUS_0100.
*
PROCESS AFTER INPUT.
MODULE USER_COMMAND_0100.

Flowlogic of screen 200:

PROCESS BEFORE OUTPUT.


MODULE STATUS_0200.
*
PROCESS AFTER INPUT.
MODULE USER_COMMAND_0200.

PF-STATUS ‘ABC’ of screen 100:


PF-STATUS ‘ABC’ of screen 200:

Screen 100 Layout:


Screen 200 Layout:

Example: Interactive ALV Reporting (double_click event)


REPORT Z730ALV14.

include z730alv14_inc.

START-OF-SELECTION.
call screen 100.

Include Program:

data gv_kunnr type kna1-kunnr.


SELECT-OPTIONS so_kunnr for gv_kunnr DEFAULT '1000' to '1010'.

data : o_kna1_cont type ref to cl_gui_custom_container,


o_kna1_grid type ref to cl_gui_alv_grid,
o_vbak_cont type ref to cl_gui_custom_container,
o_vbak_grid type ref to cl_gui_alv_grid,
o_vbap_cont type ref to cl_gui_custom_container,
o_vbap_grid type ref to cl_gui_alv_grid.

types : begin of ty_kna1,


kunnr type kna1-kunnr,
land1 type kna1-land1,
name1 type kna1-name1,
end of ty_kna1.

data : t_kna1 type table of ty_kna1,


wa_kna1 type ty_kna1.

types : begin of ty_vbak.


include type zcvbak.
types end of ty_vbak.

data : t_vbak type table of ty_vbak,


wa_vbak type ty_vbak.

types : begin of ty_vbap.


include type zcvbap.
types end of ty_vbap.

data : t_vbap type table of ty_vbap,


wa_vbap type ty_vbap.

data : t_fcat type lvc_t_fcat,


wa_fcat type lvc_s_fcat.
data wa_layo type lvc_s_layo.

class lcl_eventreceiver DEFINITION.


public SECTION.
methods handle_double_click
for event double_click of cl_gui_alv_grid
IMPORTING e_row e_column.
endclass.

class lcl_eventreceiver IMPLEMENTATION.


method handle_double_click.
case e_column-fieldname.
when 'KUNNR'.
* get the content of the selected row
clear wa_kna1.
read table t_kna1 into wa_kna1
index e_row-index
TRANSPORTING kunnr.
if sy-subrc eq 0.
* get sales orders for selected customer
perform getsalesorders.
if t_vbak is not initial. "sales orders found
call screen 200.
else. "no sales orders
message 'No sales orders' type 'I'.
endif.
endif.
when others.
message 'Please double click on customer no only' type 'I'.
endcase.
endmethod.
endclass.

data ob type ref to lcl_eventreceiver.

MODULE STATUS_0100 OUTPUT.


if o_kna1_cont is INITIAL.
SET PF-STATUS 'ABC'.
* link custom container with alv grid
CREATE OBJECT O_KNA1_CONT
EXPORTING
CONTAINER_NAME = 'CUSTCTRL1'.
* link alv grid with custom container
CREATE OBJECT O_KNA1_GRID
EXPORTING
I_PARENT = o_kna1_cont.

* get customer data


perform getcustomers.
if t_kna1 is not INITIAL.
* generate fieldcatalog for customer fields
perform fldcatkna1.
* generate layout for customer grid
perform layoutkna1.
* register handlers
perform reghandlers.
* display customer data
perform displaykna1.
endif.
else.
CALL METHOD O_KNA1_GRID->REFRESH_TABLE_DISPLAY.
endif.
ENDMODULE. " STATUS_0100 OUTPUT

FORM GETCUSTOMERS .
select kunnr land1 name1
from kna1
into table t_kna1
where kunnr in so_kunnr.
ENDFORM. " GETCUSTOMERS

FORM FLDCATVBAK .
refresh t_fcat.
clear wa_fcat.
wa_fcat-fieldname = 'VBELN'.
wa_fcat-col_pos = 1.
wa_fcat-coltext = 'Sales Document'.
wa_fcat-outputlen = 15.
wa_fcat-style = cl_gui_alv_grid=>mc_style_button.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERDAT'.
wa_fcat-col_pos = 2.
wa_fcat-coltext = 'Creation Date'.
wa_fcat-outputlen = 12.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERZET'.
wa_fcat-col_pos = 3.
wa_fcat-coltext = 'Creation Time'.
wa_fcat-outputlen = 12.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERNAM'.
wa_fcat-col_pos = 4.
wa_fcat-coltext = 'Created By'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.
ENDFORM. " FLDCATVBAK

FORM LAYOUTKNA1 .
clear wa_layo.
wa_layo-grid_title = 'CUSTOMER MASTER DATA'.
ENDFORM. " LAYOUTKNA1

FORM DISPLAYKNA1 .
CALL METHOD O_KNA1_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
IS_LAYOUT = wa_layo
CHANGING
IT_OUTTAB = t_kna1
IT_FIELDCATALOG = t_fcat.
ENDFORM. " DISPLAYKNA1

MODULE USER_COMMAND_0100 INPUT.


case sy-ucomm.
when 'F1'.
leave program.
endcase.
ENDMODULE. " USER_COMMAND_0100 INPUT

FORM REGHANDLERS .
create object ob.
set handler ob->handle_double_click for o_kna1_grid.
ENDFORM. " REGHANDLERS

FORM GETSALESORDERS .
select vbeln erdat erzet ernam
from vbak
into table t_vbak
where kunnr = wa_kna1-kunnr.
ENDFORM. " GETSALESORDERS

MODULE STATUS_0200 OUTPUT.


if o_vbak_cont is INITIAL.
SET PF-STATUS 'PQR'.
* link custom container with alv grid
CREATE OBJECT O_VBAK_CONT
EXPORTING
CONTAINER_NAME = 'CUSTCTRL2'.

* link alv grid with custom container


CREATE OBJECT O_VBAK_GRID
EXPORTING
I_PARENT = o_VBAK_cont.

* generate fieldcatalog for sales order fields


perform fldcatvbak.
* generate layout for Sales order grid
perform layoutvbak.
* register handlers
* perform reghandlers.
* display Sales orders data
perform displayvbak.
else.
CALL METHOD O_VBAK_GRID->REFRESH_TABLE_DISPLAY.
endif.
ENDMODULE. " STATUS_0200 OUTPUT

FORM LAYOUTVBAK .
clear wa_layo.
wa_layo-grid_title = 'SALES ORDERS'.
ENDFORM. " LAYOUTVBAK

FORM DISPLAYVBAK .
CALL METHOD O_VBAK_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
IS_LAYOUT = wa_layo
CHANGING
IT_OUTTAB = t_vbak
IT_FIELDCATALOG = t_fcat.
ENDFORM. " DISPLAYVBAK

FORM FLDCATKNA1 .
refresh t_fcat.
clear wa_fcat.
wa_fcat-fieldname = 'KUNNR'.
wa_fcat-col_pos = 1.
wa_fcat-coltext = 'Customer Number'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'LAND1'.
wa_fcat-col_pos = 2.
wa_fcat-coltext = 'Country Key'.
wa_fcat-outputlen = 12.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'NAME1'.
wa_fcat-col_pos = 3.
wa_fcat-coltext = 'Customer Name'.
wa_fcat-outputlen = 30.
append wa_fcat to t_fcat.
ENDFORM. " FLDCATKNA1

MODULE USER_COMMAND_0200 INPUT.


case sy-ucomm.
when 'F2'.
leave to screen 100.
endcase.
ENDMODULE. " USER_COMMAND_0200 INPUT

Flowlogic of screen 100:

PROCESS BEFORE OUTPUT.


MODULE STATUS_0100.
*
PROCESS AFTER INPUT.
MODULE USER_COMMAND_0100.

Layout of Screen 100:

GUI status of screen 100:


Example: Interactive ALV Reporting (double_click event, button_click event, hotspot_click
event) – End to End

REPORT Z730ALV15.

INCLUDE Z730ALV15_INC.

START-OF-SELECTION.
call screen 100.

Include Program:

data gv_kunnr type kna1-kunnr.


SELECT-OPTIONS so_kunnr for gv_kunnr DEFAULT '1000' to '1010'.

data : o_kna1_cont type ref to cl_gui_custom_container,


o_kna1_grid type ref to cl_gui_alv_grid,
o_vbak_cont type ref to cl_gui_custom_container,
o_vbak_grid type ref to cl_gui_alv_grid,
o_vbap_cont type ref to cl_gui_custom_container,
o_vbap_grid type ref to cl_gui_alv_grid.

types : begin of ty_kna1,


kunnr type kna1-kunnr,
land1 type kna1-land1,
name1 type kna1-name1,
end of ty_kna1.

data : t_kna1 type table of ty_kna1,


wa_kna1 type ty_kna1.

types : begin of ty_vbak.


include type zcvbak.
types end of ty_vbak.

data : t_vbak type table of ty_vbak,


wa_vbak type ty_vbak.

types : begin of ty_vbap.


include type zcvbap.
types end of ty_vbap.

data : t_vbap type table of ty_vbap,


wa_vbap type ty_vbap.

data : t_fcat type lvc_t_fcat,


wa_fcat type lvc_s_fcat.

data wa_layo type lvc_s_layo.

class lcl_eventreceiver DEFINITION.


public SECTION.
methods handle_double_click
for event double_click of cl_gui_alv_grid
IMPORTING e_row e_column.
methods handle_button_click
for event button_click of cl_gui_alv_grid
IMPORTING es_row_no.
methods handle_hotspot_click
for event hotspot_click of cl_gui_alv_grid
importing e_row_id.
endclass.

class lcl_eventreceiver IMPLEMENTATION.


method handle_double_click.
case e_column-fieldname.
when 'KUNNR'.
* get the content of the selected row
clear wa_kna1.
read table t_kna1 into wa_kna1
index e_row-index
TRANSPORTING kunnr.
if sy-subrc eq 0.
* get sales orders for selected customer
perform getsalesorders.
if t_vbak is not initial. "sales orders found
call screen 200.
else. "no sales orders
message 'No sales orders' type 'I'.
endif.
endif.
when others.
message 'Please double click on customer no only' type 'I'.
endcase.
endmethod.

method handle_button_click.
* message 'button click' type 'I'.
clear wa_vbak.
read table t_vbak into wa_vbak
index es_row_no-row_id TRANSPORTING vbeln.
if sy-subrc eq 0.
* get sales items for selected sales order
perform getsalesitems.
if t_vbap is not INITIAL.
call screen 300.
else.
message 'No sales items for selected sales order' type 'I'.
endif.
endif.
endmethod.

method handle_hotspot_click.
* message 'hotspot' type 'I'.
clear wa_vbap.
read table t_vbap into wa_vbap
index e_row_id-index TRANSPORTING matnr.
if sy-subrc eq 0.
set PARAMETER ID 'MAT' field wa_vbap-matnr.
call transaction 'MM03'.
endif.
endmethod.
endclass.

data ob type ref to lcl_eventreceiver.

MODULE STATUS_0100 OUTPUT.


if o_kna1_cont is INITIAL.
SET PF-STATUS 'ABC'.
* link custom container with alv grid
CREATE OBJECT O_KNA1_CONT
EXPORTING
CONTAINER_NAME = 'CUSTCTRL1'.

* link alv grid with custom container


CREATE OBJECT O_KNA1_GRID
EXPORTING
I_PARENT = o_kna1_cont.

* get customer data


perform getcustomers.
if t_kna1 is not INITIAL.
* generate fieldcatalog for customer fields
perform fldcatkna1.
* generate layout for customer grid
perform layoutkna1.
* register handlers
perform reghandlers.
* display customer data
perform displaykna1.
endif.
else.
CALL METHOD O_KNA1_GRID->REFRESH_TABLE_DISPLAY.
endif.
ENDMODULE. " STATUS_0100 OUTPUT

FORM GETCUSTOMERS .
select kunnr land1 name1
from kna1
into table t_kna1
where kunnr in so_kunnr.
ENDFORM. " GETCUSTOMERS

FORM FLDCATVBAK .
refresh t_fcat.
clear wa_fcat.
wa_fcat-fieldname = 'VBELN'.
wa_fcat-col_pos = 1.
wa_fcat-coltext = 'Sales Document'.
wa_fcat-outputlen = 15.
wa_fcat-style = cl_gui_alv_grid=>mc_style_button.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERDAT'.
wa_fcat-col_pos = 2.
wa_fcat-coltext = 'Creation Date'.
wa_fcat-outputlen = 12.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERZET'.
wa_fcat-col_pos = 3.
wa_fcat-coltext = 'Creation Time'.
wa_fcat-outputlen = 12.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERNAM'.
wa_fcat-col_pos = 4.
wa_fcat-coltext = 'Created By'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.
ENDFORM. " FLDCATVBAK

FORM LAYOUTKNA1 .
clear wa_layo.
wa_layo-grid_title = 'CUSTOMER MASTER DATA'.
ENDFORM. " LAYOUTKNA1

FORM DISPLAYKNA1 .
CALL METHOD O_KNA1_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
IS_LAYOUT = wa_layo
CHANGING
IT_OUTTAB = t_kna1
IT_FIELDCATALOG = t_fcat.
ENDFORM. " DISPLAYKNA1

MODULE USER_COMMAND_0100 INPUT.


case sy-ucomm.
when 'F1'.
leave program.
endcase.
ENDMODULE. " USER_COMMAND_0100 INPUT

FORM REGHANDLERS .
create object ob.
set handler ob->handle_double_click for o_kna1_grid.
ENDFORM. " REGHANDLERS

FORM GETSALESORDERS .
select vbeln erdat erzet ernam
from vbak
into table t_vbak
where kunnr = wa_kna1-kunnr.
ENDFORM. " GETSALESORDERS

MODULE STATUS_0200 OUTPUT.


if o_vbak_cont is INITIAL.
SET PF-STATUS 'PQR'.
* link custom container with alv grid
CREATE OBJECT O_VBAK_CONT
EXPORTING
CONTAINER_NAME = 'CUSTCTRL2'.

* link alv grid with custom container


CREATE OBJECT O_VBAK_GRID
EXPORTING
I_PARENT = o_VBAK_cont.

* generate fieldcatalog for sales order fields


perform fldcatvbak.
* generate layout for Sales order grid
perform layoutvbak.
* register handlers for alv grid screen 200
perform reg_handlers.
* display Sales orders data
perform displayvbak.
else.
CALL METHOD O_VBAK_GRID->REFRESH_TABLE_DISPLAY.
endif.
ENDMODULE. " STATUS_0200 OUTPUT

FORM LAYOUTVBAK .
clear wa_layo.
wa_layo-grid_title = 'SALES ORDERS'.
ENDFORM. " LAYOUTVBAK

FORM DISPLAYVBAK .
CALL METHOD O_VBAK_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
IS_LAYOUT = wa_layo
CHANGING
IT_OUTTAB = t_vbak
IT_FIELDCATALOG = t_fcat.
ENDFORM. " DISPLAYVBAK

FORM FLDCATKNA1 .
refresh t_fcat.
clear wa_fcat.
wa_fcat-fieldname = 'KUNNR'.
wa_fcat-col_pos = 1.
wa_fcat-coltext = 'Customer Number'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'LAND1'.
wa_fcat-col_pos = 2.
wa_fcat-coltext = 'Country Key'.
wa_fcat-outputlen = 12.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'NAME1'.
wa_fcat-col_pos = 3.
wa_fcat-coltext = 'Customer Name'.
wa_fcat-outputlen = 30.
append wa_fcat to t_fcat.
ENDFORM. " FLDCATKNA1
MODULE USER_COMMAND_0200 INPUT.
case sy-ucomm.
when 'F2'.
leave to screen 100.
endcase.
ENDMODULE. " USER_COMMAND_0200 INPUT

FORM REG_HANDLERS .
create object ob.
set handler ob->handle_button_click for o_vbak_grid.
ENDFORM. " REG_HANDLERS

FORM GETSALESITEMS .
select vbeln posnr matnr netwr
from vbap
into table t_vbap
where vbeln = wa_vbak-vbeln.
ENDFORM. " GETSALESITEMS

MODULE STATUS_0300 OUTPUT.


if o_vbap_cont is INITIAL.
SET PF-STATUS 'MNR'.
* link custom container with alv grid
CREATE OBJECT O_VBAP_CONT
EXPORTING
CONTAINER_NAME = 'CUSTCTRL3'.

* link alv grid with custom container


CREATE OBJECT O_VBAP_GRID
EXPORTING
I_PARENT = o_VBAP_cont.

* generate fieldcatalog for sales items grid


perform fldcat_vbap.

* generate layout
perform layout_vbap.

* register handlers for alv grid 300


perform handlers_reg.

* display alv grid with sales items


perform display_vbap.
else.
* refresh alv grid
CALL METHOD O_VBAP_GRID->REFRESH_TABLE_DISPLAY.
endif.
ENDMODULE. " STATUS_0300 OUTPUT

FORM FLDCAT_VBAP .
refresh t_fcat.
clear wa_fcat.
wa_fcat-fieldname = 'VBELN'.
wa_fcat-col_pos = 1.
wa_fcat-coltext = 'Sales Document'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'POSNR'.
wa_fcat-col_pos = 2.
wa_fcat-coltext = 'Item Number'.
wa_fcat-outputlen = 12.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'MATNR'.
wa_fcat-col_pos = 3.
wa_fcat-coltext = 'Material No'.
wa_fcat-outputlen = 12.
wa_fcat-hotspot = 'X'.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'NETWR'.
wa_fcat-col_pos = 4.
wa_fcat-coltext = 'Net Value'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.

ENDFORM. " FLDCAT_VBAP

FORM LAYOUT_VBAP .
clear wa_layo.
wa_layo-grid_title = 'SALES ITEMS'.
ENDFORM. " LAYOUT_VBAP
FORM DISPLAY_VBAP .
CALL METHOD O_VBAP_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
IS_LAYOUT = wa_layo
CHANGING
IT_OUTTAB = t_vbap
IT_FIELDCATALOG = t_fcat.

ENDFORM. " DISPLAY_VBAP

MODULE USER_COMMAND_0300 INPUT.


case sy-ucomm.
when 'F5'.
leave to screen 200.
endcase.
ENDMODULE. " USER_COMMAND_0300 INPUT

FORM HANDLERS_REG .
create object ob.
set handler ob->handle_hotspot_click for o_vbap_grid.
ENDFORM. " HANDLERS_REG

Flowlogic of screen 100:

PROCESS BEFORE OUTPUT.


MODULE STATUS_0100.
*
PROCESS AFTER INPUT.
MODULE USER_COMMAND_0100.

Layout of Screen 100:


GUI status of screen 100:
Flowlogic of screen 200:

PROCESS BEFORE OUTPUT.


MODULE STATUS_0200.
*
PROCESS AFTER INPUT.
MODULE USER_COMMAND_0200.

Screen Layout 200:


GUI Status of screen 200:
Flowlogic of screen 300:

PROCESS BEFORE OUTPUT.


MODULE STATUS_0300.
*
PROCESS AFTER INPUT.
MODULE USER_COMMAND_0300.

Screen Layout 300:


GUI Status of screen 300:
Example: Splitter Within Splitter

REPORT Z730ALV16.

include z730alv16_inc.

START-OF-SELECTION.
call screen 100.

Include Program:

data : o_cust_cont type ref to cl_gui_custom_container,


o_split1 type ref to cl_gui_splitter_container,
o_split2 type ref to cl_gui_splitter_container,
o_vbak_grid type ref to cl_gui_alv_grid,
o_vbap_grid type ref to cl_gui_alv_grid,
o_top_cont type ref to cl_gui_container,
o_sub_cont type ref to cl_gui_container,
o_vbak_cont type ref to cl_gui_container,
o_vbap_cont type ref to cl_gui_container.

MODULE STATUS_0100 OUTPUT.


if o_cust_cont is INITIAL.
SET PF-STATUS 'ABC'.

* link custom container with custom control


CREATE OBJECT O_CUST_CONT
EXPORTING
CONTAINER_NAME = 'CUSTCTRL'.

* link splitter container with custom container


CREATE OBJECT O_SPLIT1
EXPORTING
PARENT = o_cust_cont
ROWS =2
COLUMNS = 1.

* set heights for each row


CALL METHOD O_SPLIT1->SET_ROW_HEIGHT
EXPORTING
ID =1
HEIGHT = 3.

CALL METHOD O_SPLIT1->SET_ROW_HEIGHT


EXPORTING
ID =2
HEIGHT = 8.

* associate containers for each pane


CALL METHOD O_SPLIT1->GET_CONTAINER
EXPORTING
ROW =1
COLUMN = 1
RECEIVING
CONTAINER = o_top_cont.

CALL METHOD O_SPLIT1->GET_CONTAINER


EXPORTING
ROW =2
COLUMN = 1
RECEIVING
CONTAINER = o_sub_cont.
* split o_sub_cont into further panes

CREATE OBJECT O_SPLIT2


EXPORTING
PARENT = o_sub_cont
ROWS =1
COLUMNS = 2.

* set widths for columns in the sub panes


CALL METHOD O_SPLIT2->SET_COLUMN_WIDTH
EXPORTING
ID =1
WIDTH = 8.

CALL METHOD O_SPLIT2->SET_COLUMN_WIDTH


EXPORTING
ID =2
WIDTH = 5.

* associate containers for sub panes


CALL METHOD O_SPLIT2->GET_CONTAINER
EXPORTING
ROW =1
COLUMN = 1
RECEIVING
CONTAINER = o_vbak_cont.

CALL METHOD O_SPLIT2->GET_CONTAINER


EXPORTING
ROW =1
COLUMN = 2
RECEIVING
CONTAINER = o_vbap_cont.

endif.

ENDMODULE. " STATUS_0100 OUTPUT

MODULE USER_COMMAND_0100 INPUT.


case sy-ucomm.
when 'F1'.
leave PROGRAM.
endcase.
ENDMODULE. " USER_COMMAND_0100 INPUT
Flowlogic of screen 100:

PROCESS BEFORE OUTPUT.


MODULE STATUS_0100.
*
PROCESS AFTER INPUT.
MODULE USER_COMMAND_0100.

Screen Layout 100:

GUI Status of screen 100:


Example: Splitter Within Splitter, Data_changed event, Checkbox ALV column

REPORT Z730ALV17.

INCLUDE Z730ALV17_INC.

START-OF-SELECTION.
call screen 100.

Include Program:

data : o_cust_cont type ref to cl_gui_custom_container,


o_split1 type ref to cl_gui_splitter_container,
o_split2 type ref to cl_gui_splitter_container,
o_vbak_grid type ref to cl_gui_alv_grid,
o_vbap_grid type ref to cl_gui_alv_grid,
o_top_cont type ref to cl_gui_container,
o_sub_cont type ref to cl_gui_container,
o_vbak_cont type ref to cl_gui_container,
o_vbap_cont type ref to cl_gui_container.

types : begin of ty_final_vbak.


include type zcvbak.
types : sel type c,
end of ty_final_vbak.

data : t_final_vbak type table of ty_final_vbak,


lt_final_vbak type table of ty_final_vbak,
wa_final_vbak type ty_final_vbak.

types : begin of ty_temp_vbak.


include type zcvbak.
types : end of ty_temp_vbak.

data : t_temp_vbak type table of ty_temp_vbak,


wa_temp_vbak type ty_temp_vbak.

types : begin of ty_vbap.


include type zcvbap.
types end of ty_vbap.

data t_vbap type table of ty_vbap.

data : t_fcat type lvc_t_fcat,


wa_fcat type lvc_s_fcat.

data wa_layo type lvc_s_layo.

class lcl_eventreceiver DEFINITION.


public SECTION.
methods handle_toolbar
for event toolbar of cl_gui_alv_grid
IMPORTING e_object.
methods handle_data_changed
for event data_changed of cl_gui_alv_grid
IMPORTING ER_DATA_CHANGED.
methods handle_user_command
for event user_command of cl_gui_alv_grid
IMPORTING e_ucomm.
endclass.

class lcl_eventreceiver IMPLEMENTATION.


method handle_toolbar.
* prepare work area containing info.about additional custom button
data wa_button type stb_button.
wa_button-function = 'FC1'.
wa_button-icon = '@15@'.
wa_button-quickinfo = 'Display Sales Items'.
wa_button-butn_type = 0. "normal button
wa_button-text = 'Sales Items'.
append wa_button to e_object->mt_toolbar.
endmethod.

method handle_data_changed.
* message 'checkbox selected or deselected' type 'I'.
data wa_modi type lvc_s_modi.
read table er_data_changed->mt_mod_cells into wa_modi
index 1.
if sy-subrc eq 0.
* read final internal table row where the interaction is made
clear wa_final_vbak.
read table t_final_vbak into wa_final_vbak
index wa_modi-row_id
TRANSPORTING sel.
if sy-subrc eq 0.
* update the internal table corresponding row
wa_final_vbak-sel = wa_modi-value.
modify t_final_vbak from wa_final_vbak
index wa_modi-row_id TRANSPORTING sel.
endif.
endif.
endmethod.

method handle_user_command.
case e_ucomm.
when 'FC1'.
refresh lt_final_vbak.
append lines of t_final_vbak to lt_final_vbak.
delete lt_final_vbak where sel = ' '.
* get sales items for selected sales orders
if lt_final_vbak is not INITIAL.
perform getsalesitems.
if t_vbap is not INITIAL.
* logic for vbap grid
if o_vbap_grid is INITIAL.
* link vbap container with vbap grid
CREATE OBJECT O_VBAP_GRID
EXPORTING
I_PARENT = o_vbap_cont.
* generate field catalog for sales items fields
perform fldcatvbap.
* generate layout for sales items grid
perform layoutvbap.
* display sales items grid
perform displayvbap.
else.
* refresh sales items grid
CALL METHOD O_VBAP_GRID->REFRESH_TABLE_DISPLAY.
endif.
else.
message 'No Sales items' type 'I'.
endif.
else.
message 'Please select atleast one row' type 'I'.
if o_vbap_grid is not INITIAL.
refresh t_vbap.
CALL METHOD O_VBAP_GRID->REFRESH_TABLE_DISPLAY.
endif.
endif.
endcase.
endmethod.
endclass.

data ob type ref to lcl_eventreceiver.


MODULE STATUS_0100 OUTPUT.
if o_cust_cont is INITIAL.
SET PF-STATUS 'ABC'.

* link custom container with custom control


CREATE OBJECT O_CUST_CONT
EXPORTING
CONTAINER_NAME = 'CUSTCTRL'.

* link splitter container with custom container


CREATE OBJECT O_SPLIT1
EXPORTING
PARENT = o_cust_cont
ROWS =2
COLUMNS = 1.

* set heights for each row


CALL METHOD O_SPLIT1->SET_ROW_HEIGHT
EXPORTING
ID =1
HEIGHT = 3.

CALL METHOD O_SPLIT1->SET_ROW_HEIGHT


EXPORTING
ID =2
HEIGHT = 8.

* associate containers for each pane


CALL METHOD O_SPLIT1->GET_CONTAINER
EXPORTING
ROW =1
COLUMN = 1
RECEIVING
CONTAINER = o_top_cont.

CALL METHOD O_SPLIT1->GET_CONTAINER


EXPORTING
ROW =2
COLUMN = 1
RECEIVING
CONTAINER = o_sub_cont.

* split o_sub_cont into further panes

CREATE OBJECT O_SPLIT2


EXPORTING
PARENT = o_sub_cont
ROWS =1
COLUMNS = 2.

* set widths for columns in the sub panes


CALL METHOD O_SPLIT2->SET_COLUMN_WIDTH
EXPORTING
ID =1
WIDTH = 8.
CALL METHOD O_SPLIT2->SET_COLUMN_WIDTH
EXPORTING
ID =2
WIDTH = 5.

* associate containers for sub panes


CALL METHOD O_SPLIT2->GET_CONTAINER
EXPORTING
ROW =1
COLUMN = 1
RECEIVING
CONTAINER = o_vbak_cont.

CALL METHOD O_SPLIT2->GET_CONTAINER


EXPORTING
ROW =1
COLUMN = 2
RECEIVING
CONTAINER = o_vbap_cont.

* logic for vbak grid


* link alv grid(o_vbak_grid) with the parent container (o_vbak_cont)
CREATE OBJECT O_VBAK_GRID
EXPORTING
I_PARENT = o_vbak_cont.

* get sales orders


perform getsalesorders.
if t_final_vbak is not INITIAL.
* generate fieldcatalog for salesorders fields
perform fldcatvbak.
* generate layout
perform layoutvbak.
* register handlers
perform reghandlers.
* display sales orders
perform displayvbak.
endif.
endif.

ENDMODULE. " STATUS_0100 OUTPUT

MODULE USER_COMMAND_0100 INPUT.


case sy-ucomm.
when 'F1'.
leave PROGRAM.
endcase.
ENDMODULE. " USER_COMMAND_0100 INPUT

FORM GETSALESORDERS .
select vbeln erdat erzet ernam
from vbak
into table t_temp_vbak
where vbeln >= '0000004980' and vbeln <= '0000004985'.
if sy-subrc eq 0.
* copy data to final int.table
append lines of t_temp_vbak to t_final_vbak.
endif.
ENDFORM. " GETSALESORDERS

FORM FLDCATVBAK .
refresh t_fcat.
clear wa_fcat.
wa_fcat-fieldname = 'SEL'.
wa_fcat-col_pos = 1.
wa_fcat-coltext = 'Select'.
wa_fcat-outputlen = 8.
wa_fcat-checkbox = 'X'.
wa_fcat-edit = 'X'.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'VBELN'.
wa_fcat-col_pos = 2.
wa_fcat-coltext = 'Sales Document'.
wa_fcat-outputlen = 12.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERDAT'.
wa_fcat-col_pos = 3.
wa_fcat-coltext = 'Creation Date'.
wa_fcat-outputlen = 12.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERZET'.
wa_fcat-col_pos = 4.
wa_fcat-coltext = 'Creation Time'.
wa_fcat-outputlen = 12.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERNAM'.
wa_fcat-col_pos = 5.
wa_fcat-coltext = 'Created By'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.

ENDFORM. " FLDCATVBAK

FORM LAYOUTVBAK .
clear wa_layo.
wa_layo-grid_title = 'SALES ORDERS'.
ENDFORM. " LAYOUTVBAK

FORM DISPLAYVBAK .
CALL METHOD O_VBAK_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
IS_LAYOUT = wa_layo
CHANGING
IT_OUTTAB = t_final_vbak
IT_FIELDCATALOG = t_fcat.
ENDFORM. " DISPLAYVBAK

FORM REGHANDLERS .
create object ob.
set handler ob->handle_toolbar for o_vbak_grid.
set handler ob->handle_data_changed for o_vbak_grid.
set handler ob->handle_user_command for o_vbak_grid.

* register data_changed event explictly


CALL METHOD O_VBAK_GRID->REGISTER_EDIT_EVENT
EXPORTING
I_EVENT_ID = cl_gui_alv_grid=>mc_evt_modified.
ENDFORM. " REGHANDLERS

FORM GETSALESITEMS .
select vbeln posnr matnr netwr
from vbap
into table t_vbap
for ALL ENTRIES IN lt_final_vbak
where vbeln = lt_final_vbak-vbeln.
ENDFORM. " GETSALESITEMS

FORM FLDCATVBAP .
refresh t_fcat.
clear wa_fcat.
wa_fcat-fieldname = 'VBELN'.
wa_fcat-col_pos = 1.
wa_fcat-coltext = 'Sales Document'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'POSNR'.
wa_fcat-col_pos = 2.
wa_fcat-coltext = 'Item Number'.
wa_fcat-outputlen = 12.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'MATNR'.
wa_fcat-col_pos = 3.
wa_fcat-coltext = 'Material No'.
wa_fcat-outputlen = 12.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'NETWR'.
wa_fcat-col_pos = 4.
wa_fcat-coltext = 'Net Value'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.

ENDFORM. " FLDCATVBAP

FORM LAYOUTVBAP .
clear wa_layo.
wa_layo-grid_title = 'SALES ITEMS'.
ENDFORM. " LAYOUTVBAP

FORM DISPLAYVBAP .
CALL METHOD O_VBAP_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
IS_LAYOUT = wa_layo
CHANGING
IT_OUTTAB = t_vbap
IT_FIELDCATALOG = t_fcat.
ENDFORM. " DISPLAYVBAP

Flowlogic of screen 100:

PROCESS BEFORE OUTPUT.


MODULE STATUS_0100.
*
PROCESS AFTER INPUT.
MODULE USER_COMMAND_0100.

Screen Layout 100:

GUI Status of screen 100:


Example: Splitter within Splitter, data_changed event, Checkbox ALV column, TOP_OF_PAGE
event (Consolidated example)

REPORT Z730ALV17.

INCLUDE Z730ALV17_INC.

START-OF-SELECTION.
call screen 100.

Include Program:
data : o_cust_cont type ref to cl_gui_custom_container,
o_split1 type ref to cl_gui_splitter_container,
o_split2 type ref to cl_gui_splitter_container,
o_vbak_grid type ref to cl_gui_alv_grid,
o_vbap_grid type ref to cl_gui_alv_grid,
o_top_cont type ref to cl_gui_container,
o_sub_cont type ref to cl_gui_container,
o_vbak_cont type ref to cl_gui_container,
o_vbap_cont type ref to cl_gui_container.

types : begin of ty_final_vbak.


include type zcvbak.
types : sel type c,
end of ty_final_vbak.

data : t_final_vbak type table of ty_final_vbak,


lt_final_vbak type table of ty_final_vbak,
wa_final_vbak type ty_final_vbak.

types : begin of ty_temp_vbak.


include type zcvbak.
types : end of ty_temp_vbak.

data : t_temp_vbak type table of ty_temp_vbak,


wa_temp_vbak type ty_temp_vbak.

types : begin of ty_vbap.


include type zcvbap.
types end of ty_vbap.

data t_vbap type table of ty_vbap.

data : t_fcat type lvc_t_fcat,


wa_fcat type lvc_s_fcat.

data wa_layo type lvc_s_layo.

class lcl_eventreceiver DEFINITION.


public SECTION.
methods handle_toolbar
for event toolbar of cl_gui_alv_grid
IMPORTING e_object.
methods handle_data_changed
for event data_changed of cl_gui_alv_grid
IMPORTING ER_DATA_CHANGED.
methods handle_user_command
for event user_command of cl_gui_alv_grid
IMPORTING e_ucomm.
methods handle_top_of_page
for event top_of_page of cl_gui_alv_grid
importing e_dyndoc_id.
endclass.

class lcl_eventreceiver IMPLEMENTATION.


method handle_toolbar.
* prepare work area containing info.about additional custom button
data wa_button type stb_button.
wa_button-function = 'FC1'.
wa_button-icon = '@15@'.
wa_button-quickinfo = 'Display Sales Items'.
wa_button-butn_type = 0. "normal button
wa_button-text = 'Sales Items'.
append wa_button to e_object->mt_toolbar.
endmethod.

method handle_data_changed.
* message 'checkbox selected or deselected' type 'I'.
data wa_modi type lvc_s_modi.
read table er_data_changed->mt_mod_cells into wa_modi
index 1.
if sy-subrc eq 0.
* read final internal table row where the interaction is made
clear wa_final_vbak.
read table t_final_vbak into wa_final_vbak
index wa_modi-row_id
TRANSPORTING sel.
if sy-subrc eq 0.
* update the internal table corresponding row
wa_final_vbak-sel = wa_modi-value.
modify t_final_vbak from wa_final_vbak
index wa_modi-row_id TRANSPORTING sel.
endif.
endif.
endmethod.

method handle_user_command.
case e_ucomm.
when 'FC1'.
refresh lt_final_vbak.
append lines of t_final_vbak to lt_final_vbak.
delete lt_final_vbak where sel = ' '.
* get sales items for selected sales orders
if lt_final_vbak is not INITIAL.
perform getsalesitems.
if t_vbap is not INITIAL.
* logic for vbap grid
if o_vbap_grid is INITIAL.
* link vbap container with vbap grid
CREATE OBJECT O_VBAP_GRID
EXPORTING
I_PARENT = o_vbap_cont.
* generate field catalog for sales items fields
perform fldcatvbap.
* generate layout for sales items grid
perform layoutvbap.
* register handlers for vbap grid
perform handlers_reg.
* display sales items grid
perform displayvbap.
else.
* refresh sales items grid
CALL METHOD O_VBAP_GRID->REFRESH_TABLE_DISPLAY.
endif.
else.
message 'No Sales items' type 'I'.
endif.
else.
message 'Please select atleast one row' type 'I'.
if o_vbap_grid is not INITIAL.
refresh t_vbap.
CALL METHOD O_VBAP_GRID->REFRESH_TABLE_DISPLAY.
endif.
endif.
endcase.
endmethod.

method handle_top_of_page.
* message 'top' type 'I'.
CALL METHOD E_DYNDOC_ID->ADD_TEXT
EXPORTING
TEXT = text-001
SAP_STYLE = cl_dd_document=>heading
SAP_COLOR = cl_dd_document=>LIST_HEADING_INT
SAP_FONTSIZE = cl_dd_document=>LARGE
SAP_FONTSTYLE = cl_dd_document=>SANS_SERIF
SAP_EMPHASIS = cl_dd_document=>STRONG.

CALL METHOD E_DYNDOC_ID->ADD_GAP


EXPORTING
WIDTH = 6.

CALL METHOD E_DYNDOC_ID->ADD_TEXT


EXPORTING
TEXT = text-002.

CALL METHOD E_DYNDOC_ID->NEW_LINE


EXPORTING
REPEAT = 1.

CALL METHOD E_DYNDOC_ID->ADD_TEXT


EXPORTING
TEXT = text-003.

* Associate container for top of page


CALL METHOD E_DYNDOC_ID->DISPLAY_DOCUMENT
EXPORTING
PARENT = o_top_cont.

endmethod.
endclass.

data ob type ref to lcl_eventreceiver.


MODULE STATUS_0100 OUTPUT.
if o_cust_cont is INITIAL.
SET PF-STATUS 'ABC'.

* link custom container with custom control


CREATE OBJECT O_CUST_CONT
EXPORTING
CONTAINER_NAME = 'CUSTCTRL'.

* link splitter container with custom container


CREATE OBJECT O_SPLIT1
EXPORTING
PARENT = o_cust_cont
ROWS =2
COLUMNS = 1.

* set heights for each row


CALL METHOD O_SPLIT1->SET_ROW_HEIGHT
EXPORTING
ID =1
HEIGHT = 3.

CALL METHOD O_SPLIT1->SET_ROW_HEIGHT


EXPORTING
ID =2
HEIGHT = 8.

* associate containers for each pane


CALL METHOD O_SPLIT1->GET_CONTAINER
EXPORTING
ROW =1
COLUMN = 1
RECEIVING
CONTAINER = o_top_cont.

CALL METHOD O_SPLIT1->GET_CONTAINER


EXPORTING
ROW =2
COLUMN = 1
RECEIVING
CONTAINER = o_sub_cont.

* split o_sub_cont into further panes

CREATE OBJECT O_SPLIT2


EXPORTING
PARENT = o_sub_cont
ROWS =1
COLUMNS = 2.

* set widths for columns in the sub panes


CALL METHOD O_SPLIT2->SET_COLUMN_WIDTH
EXPORTING
ID =1
WIDTH = 8.
CALL METHOD O_SPLIT2->SET_COLUMN_WIDTH
EXPORTING
ID =2
WIDTH = 5.

* associate containers for sub panes


CALL METHOD O_SPLIT2->GET_CONTAINER
EXPORTING
ROW =1
COLUMN = 1
RECEIVING
CONTAINER = o_vbak_cont.

CALL METHOD O_SPLIT2->GET_CONTAINER


EXPORTING
ROW =1
COLUMN = 2
RECEIVING
CONTAINER = o_vbap_cont.

* logic for vbak grid


* link alv grid(o_vbak_grid) with the parent container (o_vbak_cont)
CREATE OBJECT O_VBAK_GRID
EXPORTING
I_PARENT = o_vbak_cont.

* get sales orders


perform getsalesorders.
if t_final_vbak is not INITIAL.
* generate fieldcatalog for salesorders fields
perform fldcatvbak.
* generate layout
perform layoutvbak.
* register handlers
perform reghandlers.
* display sales orders
perform displayvbak.
endif.
endif.

ENDMODULE. " STATUS_0100 OUTPUT

MODULE USER_COMMAND_0100 INPUT.


case sy-ucomm.
when 'F1'.
leave PROGRAM.
endcase.
ENDMODULE. " USER_COMMAND_0100 INPUT

FORM GETSALESORDERS .
select vbeln erdat erzet ernam
from vbak
into table t_temp_vbak
where vbeln >= '0000004980' and vbeln <= '0000004985'.
if sy-subrc eq 0.
* copy data to final int.table
append lines of t_temp_vbak to t_final_vbak.
endif.
ENDFORM. " GETSALESORDERS

FORM FLDCATVBAK .
refresh t_fcat.
clear wa_fcat.
wa_fcat-fieldname = 'SEL'.
wa_fcat-col_pos = 1.
wa_fcat-coltext = 'Select'.
wa_fcat-outputlen = 8.
wa_fcat-checkbox = 'X'.
wa_fcat-edit = 'X'.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'VBELN'.
wa_fcat-col_pos = 2.
wa_fcat-coltext = 'Sales Document'.
wa_fcat-outputlen = 12.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERDAT'.
wa_fcat-col_pos = 3.
wa_fcat-coltext = 'Creation Date'.
wa_fcat-outputlen = 12.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERZET'.
wa_fcat-col_pos = 4.
wa_fcat-coltext = 'Creation Time'.
wa_fcat-outputlen = 12.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERNAM'.
wa_fcat-col_pos = 5.
wa_fcat-coltext = 'Created By'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.

ENDFORM. " FLDCATVBAK

FORM LAYOUTVBAK .
clear wa_layo.
wa_layo-grid_title = 'SALES ORDERS'.
ENDFORM. " LAYOUTVBAK

FORM DISPLAYVBAK .
CALL METHOD O_VBAK_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
IS_LAYOUT = wa_layo
CHANGING
IT_OUTTAB = t_final_vbak
IT_FIELDCATALOG = t_fcat.
ENDFORM. " DISPLAYVBAK

FORM REGHANDLERS .
create object ob.
set handler ob->handle_toolbar for o_vbak_grid.
set handler ob->handle_data_changed for o_vbak_grid.
set handler ob->handle_user_command for o_vbak_grid.

* register data_changed event explictly


CALL METHOD O_VBAK_GRID->REGISTER_EDIT_EVENT
EXPORTING
I_EVENT_ID = cl_gui_alv_grid=>mc_evt_modified.
ENDFORM. " REGHANDLERS

FORM GETSALESITEMS .
select vbeln posnr matnr netwr
from vbap
into table t_vbap
for ALL ENTRIES IN lt_final_vbak
where vbeln = lt_final_vbak-vbeln.
ENDFORM. " GETSALESITEMS

FORM FLDCATVBAP .
refresh t_fcat.
clear wa_fcat.
wa_fcat-fieldname = 'VBELN'.
wa_fcat-col_pos = 1.
wa_fcat-coltext = 'Sales Document'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'POSNR'.
wa_fcat-col_pos = 2.
wa_fcat-coltext = 'Item Number'.
wa_fcat-outputlen = 12.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'MATNR'.
wa_fcat-col_pos = 3.
wa_fcat-coltext = 'Material No'.
wa_fcat-outputlen = 12.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'NETWR'.
wa_fcat-col_pos = 4.
wa_fcat-coltext = 'Net Value'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.

ENDFORM. " FLDCATVBAP

FORM LAYOUTVBAP .
clear wa_layo.
wa_layo-grid_title = 'SALES ITEMS'.
ENDFORM. " LAYOUTVBAP

FORM DISPLAYVBAP .
CALL METHOD O_VBAP_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
IS_LAYOUT = wa_layo
CHANGING
IT_OUTTAB = t_vbap
IT_FIELDCATALOG = t_fcat.
ENDFORM. " DISPLAYVBAP

FORM HANDLERS_REG .
create object ob.
set handler ob->handle_top_of_page for o_vbap_grid.
* register the event top_of_page explictly
data k type ref to cl_dd_document.
create object k.

CALL METHOD O_VBAP_GRID->LIST_PROCESSING_EVENTS


EXPORTING
I_EVENT_NAME = 'TOP_OF_PAGE'
I_DYNDOC_ID = k.
ENDFORM. " HANDLERS_REG

Flowlogic of screen 100:

PROCESS BEFORE OUTPUT.


MODULE STATUS_0100.
*
PROCESS AFTER INPUT.
MODULE USER_COMMAND_0100.

Screen Layout 100:


GUI Status of screen 100:
Example: Editing ALV Cells in Runtime (data_changed event – example 2)

REPORT Z730ALV18.

data v_empno type z9amemp-empno.


select-OPTIONS so_empno for v_empno DEFAULT 1 to 500.

data : o_cust_cont type ref to cl_gui_custom_container,


o_grid type ref to cl_gui_alv_grid.

types : begin of ty_emp.


include type z9amemp.
types end of ty_emp.

data : t_emp type table of ty_emp,


wa_emp type ty_emp.

data : t_fcat type lvc_t_fcat,


wa_fcat type lvc_s_fcat. "normal work area
FIELD-SYMBOLS <abc> like line of t_fcat. "field symbol work area

class lcl_eventreceiver DEFINITION.


public SECTION.
methods handle_data_changed
for event data_changed of cl_gui_alv_grid
importing er_data_changed.
endclass.

class lcl_eventreceiver IMPLEMENTATION.


method handle_data_changed.
* message 'data changed' type 'I'.
data wa_modi type lvc_s_modi.
loop at er_data_changed->mt_mod_cells into wa_modi.
clear wa_emp.
read table t_emp into wa_emp index wa_modi-row_id.
if sy-subrc eq 0.
wa_emp-ename = wa_modi-value. "update work area
modify t_emp from wa_emp index wa_modi-row_id
TRANSPORTING ename.
endif.
endloop.
CALL METHOD O_GRID->REFRESH_TABLE_DISPLAY. "refreshes grid
modify z9amemp from table t_emp.
endmethod.
endclass.

data ob type ref to lcl_eventreceiver.

START-OF-SELECTION.
call screen 100.

MODULE STATUS_0100 OUTPUT.


SET PF-STATUS 'ABC'.

* link custom container with custom control


CREATE OBJECT O_CUST_CONT
EXPORTING
CONTAINER_NAME = 'CUSTCTRL'.

* link alv grid with custom container


CREATE OBJECT O_GRID
EXPORTING
I_PARENT = o_cust_cont.

* get employees
perform getemployees.
if t_emp is not INITIAL.
* generate fieldcatalog
perform fldcat.
* register handlers.
perform reghandlers.
* display employees
perform displayemployees.
endif.
ENDMODULE. " STATUS_0100 OUTPUT

FORM GETemployees .
select *
from z9amemp
into table t_emp
where empno in so_empno.
ENDFORM. " GETEMPLOYEES

FORM DISPLAYEMPLOYEES.
CALL METHOD O_GRID->SET_TABLE_FOR_FIRST_DISPLAY
CHANGING
IT_OUTTAB = t_emp
IT_FIELDCATALOG = t_fcat.

ENDFORM. " DISPLAYEMPLOYEES

MODULE USER_COMMAND_0100 INPUT.


case sy-ucomm.
when 'FC1'.
leave PROGRAM.
endcase.
ENDMODULE. " USER_COMMAND_0100 INPUT

FORM FLDCAT .
clear wa_fcat.
wa_fcat-fieldname = 'EMPNO'.
wa_fcat-col_pos = 1.
wa_fcat-coltext = 'Employee No'.
wa_fcat-outputlen = 10.
wa_fcat-tooltip = 'Empno'.
append wa_fcat to t_fcat.
clear wa_fcat.
wa_fcat-fieldname = 'ENAME'.
wa_fcat-col_pos = 2.
wa_fcat-coltext = 'Employee Name'.
wa_fcat-edit = 'X'.
wa_fcat-outputlen = 20.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'EMPDESIG'.
wa_fcat-col_pos = 3.
wa_fcat-coltext = 'Designation'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'DEPTNO'.
wa_fcat-col_pos = 4.
wa_fcat-coltext = 'Department No'.
wa_fcat-outputlen = 12.
append wa_fcat to t_fcat.
ENDFORM. " FLDCAT

FORM REGHANDLERS .
create object ob.
set handler ob->handle_data_changed for o_grid.

* register data_changed event explictly


CALL METHOD O_GRID->REGISTER_EDIT_EVENT
EXPORTING
* I_EVENT_ID = cl_gui_alv_grid=>MC_EVT_MODIFIED. "single cell modification
I_EVENT_ID = cl_gui_alv_grid=>MC_EVT_ENTER. "Multi cell modification

ENDFORM. " REGHANDLERS

Layout of screen 100:


Flowlogic of screen 100:

PROCESS BEFORE OUTPUT.


MODULE STATUS_0100.
*
PROCESS AFTER INPUT.
MODULE USER_COMMAND_0100.

Example: CONTEXT_MENU_REQUEST event and USER_COMMAND event

REPORT Z730ALV19.

data v_vbeln type vbak-vbeln.


select-OPTIONS so_vbeln for v_vbeln DEFAULT '4980' to '4985'.

data : o_cust_cont type ref to cl_gui_custom_container,


o_grid type ref to cl_gui_alv_grid.

types : begin of ty_sales,


vbeln type vbak-vbeln,
erdat type vbak-erdat,
erzet type vbak-erzet,
ernam type vbak-ernam,
end of ty_sales.

data t_sales type table of ty_sales.

data : t_fcat type lvc_t_fcat,


wa_fcat type lvc_s_fcat. "normal work area

FIELD-SYMBOLS <abc> like line of t_fcat. "field symbol work area

class lcl_eventreceiver DEFINITION.


public SECTION.
methods handle_context_menu_request
for event context_menu_request of cl_gui_alv_grid
IMPORTING e_object.
methods handle_user_command
for event user_command of cl_gui_alv_grid
IMPORTING e_ucomm.

endclass.

class lcl_eventreceiver IMPLEMENTATION.


method handle_context_menu_request.
* message 'Context' type 'I'.
* suppress standard context menu
CALL METHOD E_OBJECT->CLEAR.

* Associate custom context menu items


CALL METHOD E_OBJECT->ADD_FUNCTION
EXPORTING
FCODE = 'MI1'
TEXT = 'ABAP EDITOR'.

CALL METHOD E_OBJECT->ADD_FUNCTION


EXPORTING
FCODE = 'MI2'
TEXT = 'Function Builder'.

CALL METHOD E_OBJECT->ADD_SEPARATOR.

CALL METHOD E_OBJECT->ADD_FUNCTION


EXPORTING
FCODE = 'MI3'
TEXT = 'Class Builder'.
endmethod.

method handle_user_command.
case e_ucomm.
when 'MI1'.
call transaction 'SE38'.
when 'MI2'.
call transaction 'SE37'.
when 'MI3'.
call transaction 'SE24'.
endcase.
endmethod.
endclass.

data ob type ref to lcl_eventreceiver.

START-OF-SELECTION.
call screen 100.

MODULE STATUS_0100 OUTPUT.


SET PF-STATUS 'ABC'.

* link custom container with custom control


CREATE OBJECT O_CUST_CONT
EXPORTING
CONTAINER_NAME = 'CUSTCTRL'.

* link alv grid with custom container


CREATE OBJECT O_GRID
EXPORTING
I_PARENT = o_cust_cont.

* get sales orders


perform getsalesorders.
if t_sales is not INITIAL.
* generate fieldcatalog
perform fldcat.
* register handlers
perform reghandlers.
* display sales orders
perform displaysalesorders.
endif.
ENDMODULE. " STATUS_0100 OUTPUT

FORM GETSALESORDERS .
select vbeln erdat erzet ernam
from vbak
into table t_sales
where vbeln in so_vbeln.
ENDFORM. " GETSALESORDERS

FORM DISPLAYSALESORDERS .
CALL METHOD O_GRID->SET_TABLE_FOR_FIRST_DISPLAY
CHANGING
IT_OUTTAB = t_sales
IT_FIELDCATALOG = t_fcat.

ENDFORM. " DISPLAYSALESORDERS

MODULE USER_COMMAND_0100 INPUT.


case sy-ucomm.
when 'FC1'.
leave PROGRAM.
endcase.
ENDMODULE. " USER_COMMAND_0100 INPUT

FORM FLDCAT .
clear wa_fcat.
wa_fcat-fieldname = 'VBELN'.
wa_fcat-col_pos = 1.
wa_fcat-coltext = 'Sales Document'.
wa_fcat-outputlen = 15.
wa_fcat-tooltip = 'Sales Document Number'.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERDAT'.
wa_fcat-col_pos = 2.
wa_fcat-coltext = 'Creation Date'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERZET'.
wa_fcat-col_pos = 3.
wa_fcat-coltext = 'Creation Time'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERNAM'.
wa_fcat-col_pos = 4.
wa_fcat-coltext = 'Name of Person Created Sales Doc'.
wa_fcat-outputlen = 40.
append wa_fcat to t_fcat.
ENDFORM. " FLDCAT

FORM REGHANDLERS .
create object ob.
set handler ob->handle_context_menu_request for o_grid.
set handler ob->handle_user_command for o_grid.
ENDFORM. " REGHANDLERS

Flowlogic:

PROCESS BEFORE OUTPUT.


MODULE STATUS_0100.
*
PROCESS AFTER INPUT.
MODULE USER_COMMAND_0100.

Screen Layout:
Example: Scheduling ALV Report in Background

REPORT Z730ALV20.

data v_vbeln type vbak-vbeln.


select-OPTIONS so_vbeln for v_vbeln DEFAULT '4980' to '4985'.

data : o_cust_cont type ref to cl_gui_custom_container,


o_grid type ref to cl_gui_alv_grid,
o_dock type ref to cl_gui_docking_container.

types : begin of ty_sales,


vbeln type vbak-vbeln,
erdat type vbak-erdat,
erzet type vbak-erzet,
ernam type vbak-ernam,
end of ty_sales.

data t_sales type table of ty_sales.

data : t_fcat type lvc_t_fcat,


wa_fcat type lvc_s_fcat. "normal work area
FIELD-SYMBOLS <abc> like line of t_fcat. "field symbol work area

data v_offline type i.

START-OF-SELECTION.
call screen 100.

MODULE STATUS_0100 OUTPUT.


CALL METHOD CL_GUI_ALV_GRID=>OFFLINE
RECEIVING
E_OFFLINE = v_offline.

if v_offline eq 0. "foreground execution


SET PF-STATUS 'ABC'.

* link custom container with custom control


CREATE OBJECT O_CUST_CONT
EXPORTING
CONTAINER_NAME = 'CUSTCTRL'.

* link alv grid with custom container


CREATE OBJECT O_GRID
EXPORTING
I_PARENT = o_cust_cont.
else. "background execution

* link alv grid with docking container reference


CREATE OBJECT O_GRID
EXPORTING
I_PARENT = o_dock.
endif.

* get sales orders


perform getsalesorders.
if t_sales is not INITIAL.
* generate fieldcatalog
perform fldcat.
* display sales orders
perform displaysalesorders.
endif.
ENDMODULE. " STATUS_0100 OUTPUT

FORM GETSALESORDERS .
select vbeln erdat erzet ernam
from vbak
into table t_sales
where vbeln in so_vbeln.
ENDFORM. " GETSALESORDERS

FORM DISPLAYSALESORDERS .
CALL METHOD O_GRID->SET_TABLE_FOR_FIRST_DISPLAY
CHANGING
IT_OUTTAB = t_sales
IT_FIELDCATALOG = t_fcat.

ENDFORM. " DISPLAYSALESORDERS

MODULE USER_COMMAND_0100 INPUT.


case sy-ucomm.
when 'FC1'.
leave PROGRAM.
endcase.
ENDMODULE. " USER_COMMAND_0100 INPUT

FORM FLDCAT .
clear wa_fcat.
wa_fcat-fieldname = 'VBELN'.
wa_fcat-col_pos = 1.
wa_fcat-coltext = 'Sales Document'.
wa_fcat-outputlen = 15.
wa_fcat-tooltip = 'Sales Document Number'.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERDAT'.
wa_fcat-col_pos = 2.
wa_fcat-coltext = 'Creation Date'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERZET'.
wa_fcat-col_pos = 3.
wa_fcat-coltext = 'Creation Time'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERNAM'.
wa_fcat-col_pos = 4.
wa_fcat-coltext = 'Name of Person Created Sales Doc'.
wa_fcat-outputlen = 40.
append wa_fcat to t_fcat.
ENDFORM. " FLDCAT

Flowlogic:

PROCESS BEFORE OUTPUT.


MODULE STATUS_0100.
*
PROCESS AFTER INPUT.
MODULE USER_COMMAND_0100.

Screen Layout:
Example: ALV Drop down columns

REPORT Z730ALV21.

data v_empno type z9amemp-empno.


select-OPTIONS so_empno for v_empno DEFAULT 1 to 500.

data : o_cust_cont type ref to cl_gui_custom_container,


o_grid type ref to cl_gui_alv_grid.

types : begin of ty_emp.


include type z9amemp.
types : dname(20) type c,
end of ty_emp.

data : t_emp type table of ty_emp,


wa_emp type ty_emp.

data : t_fcat type lvc_t_fcat,


wa_fcat type lvc_s_fcat. "normal work area

FIELD-SYMBOLS <abc> like line of t_fcat. "field symbol work area


data : t_drop type lvc_t_drop,
wa_drop type lvc_s_drop.

START-OF-SELECTION.
call screen 100.

MODULE STATUS_0100 OUTPUT.


SET PF-STATUS 'ABC'.

* link custom container with custom control


CREATE OBJECT O_CUST_CONT
EXPORTING
CONTAINER_NAME = 'CUSTCTRL'.

* link alv grid with custom container


CREATE OBJECT O_GRID
EXPORTING
I_PARENT = o_cust_cont.

* get employees
perform getemployees.
if t_emp is not INITIAL.
* generate fieldcatalog
perform fldcat.
* prepare dropdown values
perform dropdown_values.
* display employees
perform displayemployees.
endif.
ENDMODULE. " STATUS_0100 OUTPUT

FORM GETemployees .
select *
from z9amemp
into table t_emp
where empno in so_empno.
ENDFORM. " GETEMPLOYEES

FORM DISPLAYEMPLOYEES.
CALL METHOD O_GRID->SET_TABLE_FOR_FIRST_DISPLAY
CHANGING
IT_OUTTAB = t_emp
IT_FIELDCATALOG = t_fcat.
ENDFORM. " DISPLAYEMPLOYEES

MODULE USER_COMMAND_0100 INPUT.


case sy-ucomm.
when 'FC1'.
leave PROGRAM.
endcase.
ENDMODULE. " USER_COMMAND_0100 INPUT

FORM FLDCAT .
clear wa_fcat.
wa_fcat-fieldname = 'EMPNO'.
wa_fcat-col_pos = 1.
wa_fcat-coltext = 'Employee No'.
wa_fcat-outputlen = 10.
wa_fcat-tooltip = 'Empno'.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ENAME'.
wa_fcat-col_pos = 2.
wa_fcat-coltext = 'Employee Name'.
wa_fcat-outputlen = 20.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'EMPDESIG'.
wa_fcat-col_pos = 3.
wa_fcat-coltext = 'Designation'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'DEPTNO'.
wa_fcat-col_pos = 4.
wa_fcat-coltext = 'Department No'.
wa_fcat-outputlen = 12.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'DNAME'.
wa_fcat-col_pos = 5.
wa_fcat-coltext = 'Department Name'.
wa_fcat-outputlen = 15.
wa_fcat-drdn_hndl = 25.
wa_fcat-edit = 'X'.
append wa_fcat to t_fcat.

ENDFORM. " FLDCAT

FORM DROPDOWN_VALUES .
refresh t_drop.
clear wa_drop.
wa_drop-handle = 25.
wa_drop-value = 'SAP'.
append wa_drop to t_drop.

clear wa_drop.
wa_drop-handle = 25.
wa_drop-value = 'ORACLE'.
append wa_drop to t_drop.

clear wa_drop.
wa_drop-handle = 25.
wa_drop-value = 'JAVA'.
append wa_drop to t_drop.

clear wa_drop.
wa_drop-handle = 25.
wa_drop-value = '.Net'.
append wa_drop to t_drop.

CALL METHOD O_GRID->SET_DROP_DOWN_TABLE


EXPORTING
IT_DROP_DOWN = t_drop.

ENDFORM. " DROPDOWN_VALUES

Flowlogic:

PROCESS BEFORE OUTPUT.


MODULE STATUS_0100.
*
PROCESS AFTER INPUT.
MODULE USER_COMMAND_0100.
Screen Layout:

Example: Tree Control events

REPORT Z730ALV22.

data : o_cust_cont type ref to cl_gui_custom_container,


o_split type ref to cl_gui_splitter_container,
o_cont1 type ref to cl_gui_container,
o_cont2 type ref to cl_gui_container,
o_tree type ref to cl_gui_simple_tree,
o_pic type ref to cl_gui_picture.

data : t_nodes type table of ztreestr,


wa_nodes like line of t_nodes.

class lcl_eventreceiver DEFINITION.


public SECTION.
methods handle_node_context_menu_req
for event node_context_menu_request
of cl_gui_simple_tree
importing node_key menu.
methods handle_node_context_menu_sel
for event node_context_menu_select
of cl_gui_simple_tree
importing node_key fcode.
methods handle_node_double_click
for event node_double_click
of cl_gui_simple_tree
importing node_key.
methods handle_node_keypress
for event node_keypress
of cl_gui_simple_tree
importing node_key key.
endclass.

class lcl_eventreceiver IMPLEMENTATION.


method handle_node_context_menu_req.
* message 'context menu' type 'I'.
case node_key.
when 'SO'.
CALL METHOD MENU->ADD_FUNCTION
EXPORTING
FCODE = 'MI1'
TEXT = 'Create Sales Order'.

CALL METHOD MENU->ADD_FUNCTION


EXPORTING
FCODE = 'MI2'
TEXT = 'Change Sales Order'.

CALL METHOD MENU->ADD_FUNCTION


EXPORTING
FCODE = 'MI3'
TEXT = 'Display Sales Order'.

when 'PO'.

CALL METHOD MENU->ADD_FUNCTION


EXPORTING
FCODE = 'MI4'
TEXT = 'Create Purchase Order'.

CALL METHOD MENU->ADD_FUNCTION


EXPORTING
FCODE = 'MI5'
TEXT = 'Change Purchase Order'.

CALL METHOD MENU->ADD_FUNCTION


EXPORTING
FCODE = 'MI6'
TEXT = 'Display Purchase Order'.
endcase.
endmethod.

method handle_node_context_menu_sel.
case node_key.
when 'SO'.
if fcode = 'MI1'.
call transaction 'VA01'.
elseif fcode = 'MI2'.
call transaction 'VA02'.
elseif fcode = 'MI3'.
call transaction 'VA03'.
endif.
when 'PO'.
if fcode = 'MI4'.
call transaction 'ME21'.
elseif fcode = 'MI5'.
call transaction 'ME22'.
elseif fcode = 'MI6'.
call transaction 'ME23'.
endif.
endcase.
endmethod.

method handle_node_double_click.
* message 'Double' type 'I'.
case node_key.
when 'CSO'.
call transaction 'VA01'.
when 'CHSO'.
call transaction 'VA02'.
when 'DSO'.
call transaction 'VA03'.
when 'CPO'.
call transaction 'ME21'.
when 'CHPO'.
call transaction 'ME22'.
when 'DPO'.
call transaction 'ME23'.
endcase.
endmethod.

method handle_node_keypress.
case node_key.
when 'CSO'.
if key = cl_gui_simple_tree=>key_enter.
message 'Pressed enter key' type 'I'.
elseif key = cl_gui_simple_tree=>key_delete.
message 'Pressed delete key' type 'I'.
endif.
endcase.
endmethod.
endclass.

data ob type ref to lcl_eventreceiver.

START-OF-SELECTION.
call screen 100.

MODULE STATUS_0100 OUTPUT.


if o_cust_cont is INITIAL.
SET PF-STATUS 'ABC'.

* link custom control with custom container


CREATE OBJECT O_CUST_CONT
EXPORTING
CONTAINER_NAME = 'CUSTCTRL'.

* link splitter container with custom container


CREATE OBJECT O_SPLIT
EXPORTING
PARENT = o_cust_cont
ROWS =1
COLUMNS = 2.

* Specify widths for each column


CALL METHOD O_SPLIT->SET_COLUMN_WIDTH
EXPORTING
ID =1
WIDTH = 10.

CALL METHOD O_SPLIT->SET_COLUMN_WIDTH


EXPORTING
ID =2
WIDTH = 16.

* Associate containers for each pane


CALL METHOD O_SPLIT->GET_CONTAINER
EXPORTING
ROW =1
COLUMN = 1
RECEIVING
CONTAINER = o_cont1.

CALL METHOD O_SPLIT->GET_CONTAINER


EXPORTING
ROW =1
COLUMN = 2
RECEIVING
CONTAINER = o_cont2.

* logic for tree control


perform tree.

* logic for picture control


perform picture.
endif.

ENDMODULE. " STATUS_0100 OUTPUT

MODULE USER_COMMAND_0100 INPUT.


case sy-ucomm.
when 'FC1'.
leave PROGRAM.
endcase.
ENDMODULE. " USER_COMMAND_0100 INPUT

FORM TREE .
* link tree control with container (o_cont1)
CREATE OBJECT O_TREE
EXPORTING
PARENT = o_cont1
NODE_SELECTION_MODE = cl_gui_simple_tree=>node_sel_mode_single.

* construct nodes
clear wa_nodes.
wa_nodes-node_key = 'RT'.
wa_nodes-isfolder = 'X'.
wa_nodes-expander = 'X'.
wa_nodes-text = 'Transactions'.
append wa_nodes to t_nodes.

clear wa_nodes.
wa_nodes-node_key = 'SO'.
wa_nodes-relatkey = 'RT'.
wa_nodes-isfolder = 'X'.
wa_nodes-expander = 'X'.
wa_nodes-text = 'Sales Orders'.
append wa_nodes to t_nodes.

clear wa_nodes.
wa_nodes-node_key = 'CSO'.
wa_nodes-relatkey = 'SO'.
wa_nodes-n_image = '@15@'.
wa_nodes-text = 'Create Sales Orders'.
append wa_nodes to t_nodes.

clear wa_nodes.
wa_nodes-node_key = 'CHSO'.
wa_nodes-relatkey = 'SO'.
wa_nodes-n_image = '@15@'.
wa_nodes-text = 'Change Sales Orders'.
append wa_nodes to t_nodes.

clear wa_nodes.
wa_nodes-node_key = 'DSO'.
wa_nodes-relatkey = 'SO'.
wa_nodes-n_image = '@15@'.
wa_nodes-text = 'Display Sales Orders'.
append wa_nodes to t_nodes.

clear wa_nodes.
wa_nodes-node_key = 'PO'.
wa_nodes-relatkey = 'RT'.
wa_nodes-isfolder = 'X'.
wa_nodes-expander = 'X'.
wa_nodes-text = 'Purchase Orders'.
append wa_nodes to t_nodes.

clear wa_nodes.
wa_nodes-node_key = 'CPO'.
wa_nodes-relatkey = 'PO'.
wa_nodes-n_image = '@16@'.
wa_nodes-text = 'Create Purchase Order'.
append wa_nodes to t_nodes.

clear wa_nodes.
wa_nodes-node_key = 'CHPO'.
wa_nodes-relatkey = 'PO'.
wa_nodes-n_image = '@16@'.
wa_nodes-text = 'Change Purchase Order'.
append wa_nodes to t_nodes.

clear wa_nodes.
wa_nodes-node_key = 'DPO'.
wa_nodes-relatkey = 'PO'.
wa_nodes-n_image = '@16@'.
wa_nodes-text = 'Display Purchase Orders'.
append wa_nodes to t_nodes.

* register handlers for tree control events


perform reg_handlers.

* add nodes to tree control


CALL METHOD O_TREE->ADD_NODES
EXPORTING
TABLE_STRUCTURE_NAME = 'ZTREESTR'
NODE_TABLE = t_nodes.

ENDFORM. " TREE


FORM PICTURE .
* link picture control with container (o_cont2)
CREATE OBJECT O_PIC
EXPORTING
PARENT = o_cont2.

* get the URL of the picture


data lv_pic_url type CNDP_URL.
CALL FUNCTION 'DP_PUBLISH_WWW_URL'
EXPORTING
OBJID = 'Z730SUNFLOWER'
LIFETIME = 'X'
IMPORTING
URL = lv_pic_url.

* display the picture


CALL METHOD O_PIC->LOAD_PICTURE_FROM_URL
EXPORTING
URL = lv_pic_url.
ENDFORM. " PICTURE

FORM REG_HANDLERS .
create object ob.
set handler ob->handle_node_context_menu_req for o_tree.
set handler ob->handle_node_context_menu_sel for o_tree.
set handler ob->handle_node_double_click for o_tree.
set handler ob->handle_node_keypress for o_tree.

data : t_events type CNTL_SIMPLE_EVENTS,


wa_events like line of t_events.

* register node_context_menu_request event explictly


clear wa_events.
wa_events-eventid = cl_gui_simple_tree=>EVENTID_NODE_CONTEXT_MENU_REQ.
append wa_events to t_events.

* register node_double_click event explictly


clear wa_events.
wa_events-eventid = cl_gui_simple_tree=>EVENTID_NODE_DOUBLE_CLICK.
append wa_events to t_events.

* register the keyboard keys for node_keypress event


CALL METHOD O_TREE->ADD_KEY_STROKE
EXPORTING
KEY = cl_gui_simple_tree=>key_enter.

CALL METHOD O_TREE->ADD_KEY_STROKE


EXPORTING
KEY = cl_gui_simple_tree=>key_delete.

* register node_keypress event explictly


clear wa_events.
wa_events-eventid = cl_gui_simple_tree=>EVENTID_NODE_KEYPRESS.
append wa_events to t_events.

CALL METHOD O_TREE->SET_REGISTERED_EVENTS


EXPORTING
EVENTS = t_events.
ENDFORM. " REG_HANDLERS

Example: Custom F4 Help (onf4 event) and Custom F1 help (onf1 event) for ALV columns

REPORT Z730ALV23.

data v_empno type z9amemp-empno.


select-OPTIONS so_empno for v_empno DEFAULT 1 to 500.

data : o_cust_cont type ref to cl_gui_custom_container,


o_grid type ref to cl_gui_alv_grid.

types : begin of ty_emp.


include type z9amemp.
types end of ty_emp.

data : t_emp type table of ty_emp,


wa_emp type ty_emp.

data : t_fcat type lvc_t_fcat,


wa_fcat type lvc_s_fcat. "normal work area

FIELD-SYMBOLS <abc> like line of t_fcat. "field symbol work area

data : t_f4 type lvc_t_f4,


wa_f4 like line of t_f4.

types : begin of ty_f4values,


empno type z9amemp-empno,
empdesig type z9amemp-empdesig,
end of ty_f4values.

data : t_f4values type table of ty_f4values,


wa_f4values type ty_f4values.

data : t_retvalues type table of DDSHRETVAL,


wa_retvalues like line of t_retvalues.

class lcl_eventreceiver DEFINITION.


PUBLIC SECTION.
methods handle_onf4 for event onf4
of cl_gui_alv_grid
importing e_fieldname e_fieldvalue es_row_no.
methods handle_onf1 for event onf1
of cl_gui_alv_grid
importing e_fieldname.
endclass.

class lcl_eventreceiver IMPLEMENTATION.


method handle_onf4.
* message 'onf4 event' type 'I'.
case e_fieldname.
when 'EMPDESIG'.
select empno empdesig
from z9amemp
into table t_f4values.
if sy-subrc eq 0.
CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
EXPORTING
RETFIELD = 'EMPDESIG' "internal table field
DYNPPROG = sy-repid
DYNPNR = sy-dynnr
DYNPROFIELD = 'EMPDESIG' "alv column name
VALUE_ORG = 'S'
TABLES
VALUE_TAB = t_f4values
RETURN_TAB = t_retvalues.

if t_retvalues is not INITIAL.


clear wa_emp.
read table t_emp into wa_emp
index es_row_no-row_id
TRANSPORTING empdesig.
if sy-subrc eq 0.
clear wa_retvalues.
read table t_retvalues into wa_retvalues index 1.
if sy-subrc eq 0.
wa_emp-empdesig = wa_retvalues-fieldval.
modify t_emp from wa_emp index es_row_no-row_id
TRANSPORTING empdesig.
CALL METHOD O_GRID->REFRESH_TABLE_DISPLAY.
endif.
endif.
endif.
endif.
when 'DEPTNO'.
* message 'onf4 event for deptno' type 'I'.
endcase.
endmethod.

method handle_onf1.
case e_fieldname.
when 'EMPNO'.
CALL FUNCTION 'POPUP_TO_INFORM'
EXPORTING
TITEL = 'Custom F1 help'
TXT1 = 'Employee Number'
TXT2 = 'Table:Z9AMEMP Field:EMPNO'.
endcase.
endmethod.
endclass.

data ob type ref to lcl_eventreceiver.

START-OF-SELECTION.
call screen 100.

MODULE STATUS_0100 OUTPUT.


SET PF-STATUS 'ABC'.

* link custom container with custom control


CREATE OBJECT O_CUST_CONT
EXPORTING
CONTAINER_NAME = 'CUSTCTRL'.

* link alv grid with custom container


CREATE OBJECT O_GRID
EXPORTING
I_PARENT = o_cust_cont.

* get employees
perform getemployees.
if t_emp is not INITIAL.
* generate fieldcatalog
perform fldcat.
* register handlers
perform reghandlers.
* display employees
perform displayemployees.
endif.
ENDMODULE. " STATUS_0100 OUTPUT

FORM GETemployees .
select *
from z9amemp
into table t_emp
where empno in so_empno.
ENDFORM. " GETEMPLOYEES

FORM DISPLAYEMPLOYEES.
CALL METHOD O_GRID->SET_TABLE_FOR_FIRST_DISPLAY
CHANGING
IT_OUTTAB = t_emp
IT_FIELDCATALOG = t_fcat.

ENDFORM. " DISPLAYEMPLOYEES

MODULE USER_COMMAND_0100 INPUT.


case sy-ucomm.
when 'FC1'.
leave PROGRAM.
endcase.
ENDMODULE. " USER_COMMAND_0100 INPUT

FORM FLDCAT .
clear wa_fcat.
wa_fcat-fieldname = 'EMPNO'.
wa_fcat-col_pos = 1.
wa_fcat-coltext = 'Employee No'.
wa_fcat-outputlen = 10.
wa_fcat-tooltip = 'Empno'.
append wa_fcat to t_fcat.
clear wa_fcat.
wa_fcat-fieldname = 'ENAME'.
wa_fcat-col_pos = 2.
wa_fcat-coltext = 'Employee Name'.
wa_fcat-outputlen = 20.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'EMPDESIG'.
wa_fcat-col_pos = 3.
wa_fcat-coltext = 'Designation'.
wa_fcat-outputlen = 15.
wa_fcat-f4availabl = 'X'.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'DEPTNO'.
wa_fcat-col_pos = 4.
wa_fcat-coltext = 'Department No'.
wa_fcat-outputlen = 12.
wa_fcat-tooltip = 'Deptno'.
wa_fcat-f4availabl = 'X'.
append wa_fcat to t_fcat.

ENDFORM. " FLDCAT

FORM REGHANDLERS .
create object ob.
set handler ob->handle_onf4 for o_grid.
set handler ob->handle_onf1 for o_grid.

* register fieldnames for f4 event


clear wa_f4.
wa_f4-fieldname = 'EMPDESIG'.
wa_f4-register = 'X'.
append wa_f4 to t_f4.

clear wa_f4.
wa_f4-fieldname = 'DEPTNO'.
wa_f4-register = 'X'.
* append wa_f4 to t_f4. "runtime error
insert wa_f4 into table t_f4.
CALL METHOD O_GRID->REGISTER_F4_FOR_FIELDS
EXPORTING
IT_F4 = t_f4.

ENDFORM. " REGHANDLERS

Flowlogic:

PROCESS BEFORE OUTPUT.


MODULE STATUS_0100.
*
PROCESS AFTER INPUT.
MODULE USER_COMMAND_0100.

Screen Layout:

Example: Custom F4 Help (onf4 event) Vs Standard F4 help


REPORT Z730ALV24.

data v_vbeln type vbak-vbeln.


select-OPTIONS so_vbeln for v_vbeln DEFAULT '4980' to '4985'.

data : o_cust_cont type ref to cl_gui_custom_container,


o_grid type ref to cl_gui_alv_grid.

types : begin of ty_sales,


vbeln type vbak-vbeln,
erdat type vbak-erdat,
erzet type vbak-erzet,
ernam type vbak-ernam,
end of ty_sales.

data t_sales type table of ty_sales.

data : t_fcat type lvc_t_fcat,


wa_fcat type lvc_s_fcat. "normal work area

FIELD-SYMBOLS <abc> like line of t_fcat. "field symbol work area

class lcl_eventreceiver DEFINITION.


PUBLIC SECTION.
methods handle_onf4 for event onf4
of cl_gui_alv_grid
importing e_fieldname e_fieldvalue es_row_no er_event_data.
endclass.

class lcl_eventreceiver IMPLEMENTATION.


method handle_onf4.
case e_fieldname.
when 'VBELN'.
* suppress standard F4 help for VBELN
er_event_data->m_event_handled = 'X'.
message 'onf4 event for vbeln' type 'I'.
endcase.
endmethod.
endclass.

data ob type ref to lcl_eventreceiver.

START-OF-SELECTION.
call screen 100.
MODULE STATUS_0100 OUTPUT.
SET PF-STATUS 'ABC'.

* link custom container with custom control


CREATE OBJECT O_CUST_CONT
EXPORTING
CONTAINER_NAME = 'CUSTCTRL'.

* link alv grid with custom container


CREATE OBJECT O_GRID
EXPORTING
I_PARENT = o_cust_cont.

* get sales orders


perform getsalesorders.
if t_sales is not INITIAL.
* generate fieldcatalog
perform fldcat.
* register handlers
perform reghandlers.
* display sales orders
perform displaysalesorders.
endif.
ENDMODULE. " STATUS_0100 OUTPUT

FORM GETSALESORDERS .
select vbeln erdat erzet ernam
from vbak
into table t_sales
where vbeln in so_vbeln.
ENDFORM. " GETSALESORDERS

FORM DISPLAYSALESORDERS .
CALL METHOD O_GRID->SET_TABLE_FOR_FIRST_DISPLAY
CHANGING
IT_OUTTAB = t_sales
IT_FIELDCATALOG = t_fcat.

ENDFORM. " DISPLAYSALESORDERS

MODULE USER_COMMAND_0100 INPUT.


case sy-ucomm.
when 'FC1'.
leave PROGRAM.
endcase.
ENDMODULE. " USER_COMMAND_0100 INPUT

FORM FLDCAT .
clear wa_fcat.
wa_fcat-fieldname = 'VBELN'.
wa_fcat-col_pos = 1.
wa_fcat-coltext = 'Sales Document'.
wa_fcat-outputlen = 15.
wa_fcat-tooltip = 'Sales Document Number'.
wa_fcat-ref_table = 'VBAK'. "for standard f4 and standard f1 helps
wa_fcat-ref_field = 'VBELN'.
wa_fcat-f4availabl = 'X'. "custom f4 help
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERDAT'.
wa_fcat-col_pos = 2.
wa_fcat-coltext = 'Creation Date'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERZET'.
wa_fcat-col_pos = 3.
wa_fcat-coltext = 'Creation Time'.
wa_fcat-outputlen = 15.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERNAM'.
wa_fcat-col_pos = 4.
wa_fcat-coltext = 'Name of Person Created Sales Doc'.
wa_fcat-outputlen = 40.
append wa_fcat to t_fcat.
ENDFORM. " FLDCAT

FORM REGHANDLERS .
create object ob.
set handler ob->handle_onf4 for o_grid.

* register field names for custom f4 help (onf4 event)


data : t_f4 type lvc_t_f4,
wa_f4 type lvc_s_f4.

clear wa_f4.
wa_f4-fieldname = 'VBELN'.
wa_f4-register = 'X'.
append wa_f4 to t_f4.

CALL METHOD O_GRID->REGISTER_F4_FOR_FIELDS


EXPORTING
IT_F4 = t_f4.

ENDFORM. " REGHANDLERS

Flowlogic:

PROCESS BEFORE OUTPUT.


MODULE STATUS_0100.
*
PROCESS AFTER INPUT.
MODULE USER_COMMAND_0100.

Screen Layout:
Procedure for Classical Reporting:

1. Generate a selection screen for reading user input

2. Retrieve data from database tables based on user input

3. Process the internal table for display (write)

--> displays the output in L.P.S

Procedure for developing ALV reports using classes:

1. Create a Module pool screen

2. Place custom control component on the module pool screen

3. Create the object for container class 'CL_GUI_CUSTOM_CONTAINER’ linking with


custom control (physical control on module pool screen toolbox)
4. Create the object for grid class 'CL_GUI_ALV_GRID' linking with
container object

5. Retrieve the data to be displayed in the ALV grid

6. Generate the field catalog for the fields of ALV grid

7. Generate the layout for the ALV grid (optional)

8. Display the data in the ALV grid using the method 'SET_TABLE_FOR_FIRST_DISPLAY'
of the grid class 'cl_gui_alv_grid'

Field catalog Generation:

Whenever we display the data in the form of ALV report, we need to provide field catalog
information; otherwise the report execution leads to ABORT error. In case of ALV reporting
using classes, we can generate field catalog in 2 ways.

1. Using the function module ‘LVC_FIELDCATALOG_MERGE’: As part of this F.M call, we


need to provide the dictionary structure containing the required fields (fields provided
as part of internal table) , based on this dictionary structure fields SAP constructs the
fieldcatalog internal table and returns which is of type ‘LVC_T_FCAT’.

2. Manually
Note: In either of the above 2 process, once the fieldcatalog is generated, we need to pass it as
a value to the parameter ‘IT_FIELDCATALOG’ as part of the method call
‘SET_TABLE_FOR_FIRST_DISPLAY’.

Excluding the standard ALV toolbar pushbuttons:

For this, we need to identify the function codes of the ALV toolbar buttons and prepare an
internal table with those function codes and pass the internal table as a value to the parameter
“IT_TOOLBAR_EXCLUDING” of the method “SET_TABLE_FOR_FIRST_DISPLAY” of the class
‘CL_GUI_ALV_GRID’. All the ALV toolbar buttons function codes exist in the form of constant
public attributes with the naming standard ‘MC_FC…’ of the class ‘CL_GUI_ALV_GRID’.

Note: For excluding entire ALV toolbar in the Report, set the field ‘NO_TOOLBAR’ to ‘X’ as part
of Layout.

ALV Column as pushbutton:

For displaying the entire ALV column as pushbutton, set the field ‘STYLE’ as part of field catalog.
All the styles exist in the form of constant attributes with the naming standard ‘MC_STYL’ as
part of the class ‘CL_GUI_ALV_GRID’.
Displaying ALV cells as pushbutton:

Procedure:

Step1:

Take an additional column in the final internal table which is of table type “LVC_T_STYL”.

Step 2:

Before displaying the final data in the ALV grid, loop the final internal table and set the
button style (MC_STYLE_BUTTON) to ALV cells based on the condition.

As part of layout generation, set the field “stylefname” whose value is name of the
additional field which holds the style.

ALV Column Coloring:

For coloring the entire ALV column, set the field ‘EMPHASIZE’ to a color coding as part of field
catalog.

ALV Row Coloring:

Procedure:

Step1:

Take an additional column in the final internal table which is of type char4.

Step 2:

Before displaying the final data in the ALV grid, loop the final internal table and set the color
coding for the additional field based on the condition.

As part of layout generation, set the field “info_fname” whose value is name of the
additional field which holds the row color coding.

ALV Cell Coloring:

Procedure:

Step1:

Take an additional column in the final internal table which is of type ‘LVC_T_SCOL’.
Step 2:

Before displaying the final data in the ALV grid, loop the final internal table and set the color
coding for the additional field based on the condition.

For this, we need to prepare work area of type ‘LVC_S_SCOL’ and append the same to the
additional field and update the internal table.

As part of layout generation, set the field “ctab_fname” whose value is name of the
additional field which holds the cell color coding.

ALV Traffic Light columns:

- Traffic light columns acts as indicators in the ALV reports.

- These traffic lights by default are displayed as First column of the ALV report.

- Traffic light codes are 3 types (1-Red, 2-Yellow, 3-Green) and also we can refer to the
corresponding constant values in the ICON type-group.

Procedure:

Step 1:

Take an additional column in the final internal table which is of type character of “One”.

Step 2:

Before displaying the final data in the ALV GRID, loop the internal table and set the traffic
light code for an additional column based on a condition.

Step 3:

As part of layout, set the field “EXCP_FNAME”, the value of the field should be “Name of the
additional column” containing traffic light code.

Note: By default, Traffic light column is displayed in first column position. To display traffic
light in a specific column position, declare an additional column of type character with some
length, generate field catalog for this column including required column position and assign
traffic light code to its column value along with static text. In this case, we should not set
the field ‘EXCP_FNAME’ in layout.

Splitter control
It is used for splitting the container in to “N” no of partitions.

Each partition is called as a “PANE”.

Each “PANE” should be associated with a container to hold an object. The object can be ALV
Grid, Image and Tree.

Procedure for displaying images in ALV Reporting using classes

Step 1:

Upload the picture by using t-code ‘SMW0’.

In SMW0, choose the radio button ‘binary data for webrfc applications’, click on ‘FIND’
(appl.toolbar)  provide package name(any package), object name (any name, unique id)
description(…), click on execute  click on ‘create’ button (appl.toolbar),provide object name
(ZBT), description(…), click on ‘import button’ (status bar of dialog box)

Step 2:

Call the function module “dp_publish_www_url”.

This function module takes an image object id as input and returns the “URL” of picture which is
of type “cndp_url”.

Note:

“cndp_url” is a types declaration declared in the type group “CNDP”.

Step 3: Create the object of picture class ‘CL_GUI_PICTURE’ linking with a container.

Step 4: call the instance method “load_picture_from_url” of the class “cl_gui_picture”. This
method takes URL of the picture as input and displays the picture in the container.

Procedure for tree control:

A tree control is collection of nodes.

A node can be a folder / it can be an action item.

We can add the nodes to the tree control by using the instance method “add_nodes”
of the class “cl_gui_simple_tree”.

This method contains two mandatory parameters.


1. Structure representing the nodes.

2. Internal table containing the nodes information.

Note: “ABDEMONODE” is one of the standard structure given by SAP which represents simple
tree (or) we can create custom dictionary structure by including std.structure ‘TREEV_NODE’
and a character field ‘text’ of any length

Displaying ALV columns as Dropdown:

Generally we display ALV column as drop down when we have fixed set of values to be shown
to the user as a drop down so that the user can select the value in the runtime.

Procedure:

Step 1:

Take an additional column in the final internal table, this additional column needs to be
displayed as dropdown.

Step 2:

Generate the field catalog for the additional column.

As part of the field catalog, set the field “DRDN_HNDL” to some Numeric value and also set
the edit field to ‘X’ as the column should be editable, so that user can choose value from the
drop down in the runtime.

Step 3:

Prepare an internal table of type ‘LVC_T_DROP’ with drop down handle and dropdown
values and pass this internal table as a parameter to the method
‘SET_DROP_DOWN_TABLE’ of the class ‘CL_GUI_ALV_GRID’.

Custom event handling process:

1. Needs to be declared in custom class definition


2. Event Business logic needs to be implemented as part of custom class
implementation(inside event handler method)

3. Raised in custom class methods


4. Register the handlers for execution of event handler methods
Standard Event handling process in ALV Reports using classes:
1. They are declared by SAP itself as part of standard SAP classes

2. Event Business logic needs to implemented as part of custom class implementation (inside
event handler method)

3. Raised by SAP itself depending on user actions (from. The std. methods)

4. Register the handlers for execution of event handler methods

Note:

1. To Display ALV column values as link, we need to set the field ‘HOTSPOT’ as part of field
catalog for that particular field.

2. ‘HOTSPOT_CLICK’ is the instance event of the class ‘CL_GUI_ALV_GRID’ which is


triggered by SAP whenever the user clicks on ALV Cell value in the runtime.

3. ‘BUTTON_CLICK’ is the instance event of the class ‘CL_GUI_ALV_GRID’ which is triggered


by SAP whenever the user clicks on ALV Cell displayed as pushbutton

4. ‘DOUBLE_CLICK’ is the instance event of the class ‘CL_GUI_ALV_GRID’ which is triggered


by SAP whenever the user double clicks on ALV Cell value in the runtime.

5. As part of interactive ALV reporting using classes, when the user interacts and navigates
from one screen to another screen, we need to refresh the grid with the corresponding
internal table data using the method ‘REFRESH_TABLE_DISPLAY’ of the class
‘CL_GUI_ALV_GRID’.

TOOLBAR Event: is the instance event of the class ‘CL_GUI_ALV_GRID’ which is triggered when
the ALV grid is displayed. This event can be used to manage the ALV Toolbar for
Enabling/Disabling standard ALV Toolbar buttons, Adding custom buttons.

USER_COMMAND event: is the instance event of the class ‘CL_GUI_ALV_GRID’ which is


triggered when the user clicks on custom normal buttons on ALV toolbar. Before this event, SAP
Triggers ‘BEFORE_USER_COMMAND’ and then ‘USER_COMMAND’ and after this it triggers
‘AFTER_USER_COMMAND’. These events are also triggered when the user clicks on Menu
items of Menu Buttons of ALV toolbar.

MENU_BUTTON event: is the instance event of the class ‘CL_GUI_ALV_GRID’ which is triggered
when the user clicks on custom MENU buttons on ALV toolbar.

Note:

1. To enable multiple selection of rows on ALV grid, we need to set the field ‘SEL_MODE’
TO ‘A’ as part of layout
2. To identify the selected rows on the ALV grid, we need to use the instance method
‘GET_SELECTED_ROWS’ of the class ‘CL_GUI_ALV_GRID’. This method returns the
internal tables containing the indexes of selected rows.

Editing ALV Cells in runtime and updating to database:

Procedure:

1. For the ALV column to be editable, set the field edit to ‘X’ As part of field catalog,
2. Handle the event ‘DATA_CHANGED’ of the class ‘CL_GUI_ALV_GRID’. This event is not
triggered by default as it is a system event, it requires explicit registration and it is done
by calling the instance method ‘REGISTER_EDIT_EVENT’ of the class
‘CL_GUI_ALV_GRID’. This method takes event id as a mandatory input parameter.
These event ids exist in the form of constant attributes of the grid class and we can use
any of the following attribute event ids.
a) Mc_evt_modified  Allows only single cell editing, in this case the event
‘DATA_CHANGED’ is triggered when the user shifts the focus from the first modified
cell or when the user presses enter key in the first modified cell.
b) Mc_evt_enter -> Allows multi cell editing, in this case the event is triggered when
the user presses enter key in the last modified cell.
3. As part of the event handler method, we need to import the event parameter
‘ER_DATA_CHANGED’ and using this event parameter (object) access the instance
attribute (internal table) ‘MT_MODIFIED_CELLS’ which keeps track about the
information of the modified cells which includes row_id and modified value. Based on
this, update the grid internal table as well as corresponding database table.

Tree Control Events:

1. ‘NODE_DOUBLE_CLICK’: is the instance event of the class ‘CL_GUI_SIMPLE_TREE’


which is triggered when the user double clicks on the tree node of the tree control.
This event is not triggered by default; it needs to be registered explicitly by using the
instance method ‘SET_REGISTERED_EVENTS’ of the class ‘CL_GUI_SIMPLE_TREE’.

2. ‘NODE_CONTEXT_MENU_REQUEST’: is the instance event of the class


‘CL_GUI_SIMPLE_TREE’ which is triggered when the user right clicks on the tree
node of the tree control. This event can be handled to associate the context menu
with the tree node. This event is not triggered by default; it needs to be registered
explicitly by using the instance method ‘SET_REGISTERED_EVENTS’ of the class
‘CL_GUI_SIMPLE_TREE’.

3. ‘NODE_KEYPRESS’: is the instance event of the class ‘CL_GUI_SIMPLE_TREE’ which


is triggered when the user presses a key on the tree node of the tree control. For
this, we need to register the keys for triggering this event. The keys can be
registered by calling the method ‘ADD_KEY_STROKE’ of the class
‘CL_GUI_SIMPLE_TREE’. All the keys exist in the form of constant attributes with the
naming standard ‘KEY_....’ of the class ‘CL_GUI_SIMPLE_TREE’. This event is not
triggered by default; it needs to be registered explicitly by using the instance
method ‘SET_REGISTERED_EVENTS’ of the class ‘CL_GUI_SIMPLE_TREE’.

4. ‘NODE_CONTEXT_MENU_SELECT’: is the instance event of the class


‘CL_GUI_SIMPLE_TREE’ which is triggered when the user selects a context menu
item from the context menu of the node.

Note: For the method ‘SET_REGISTERED_EVENTS’, we need to pass event id as a


parameter. All the event ids exist in the form of constant attributes with the naming
standard ‘EVENTID_.....’ of the class ‘CL_GUI_SIMPLE_TREE’.

Generating TOP OF PAGE content for ALV Grid:

For this, we need to handle the event ‘TOP_OF_PAGE’. It is the instance event of the class
‘CL_GUI_ALV_GRID’ used for generating content in the TOP OF PAGE area of ALV Grid. This
event is not triggered by default; it should be registered explicitly by calling the instance
method ‘LIST_PROCESSING_EVENTS’ of the class ‘CL_GUI_ALV_GRID’.

Example: 1. ALV Interactive Reporting using Function Modules

2. Displaying Images in ALV TOP_OF_PAGE Area using F.M

3. ALV Blocked List

Report Program:

REPORT Z730ALV25.

data gv_vbeln type vbak-vbeln.


SELECT-OPTIONS so_vbeln for gv_vbeln DEFAULT '4980' to '4985'.

types : begin of ty_vbak.


include type zcvbak.
types end of ty_vbak.

data : t_vbak type table of ty_vbak,


wa_vbak type ty_vbak.

types : begin of ty_vbap.


include type zcvbap.
types end of ty_vbap.

data : t_vbap type table of ty_vbap,


wa_vbap type ty_vbap.

types : begin of ty_marc,


matnr type marc-matnr,
werks type marc-werks,
ekgrp type marc-ekgrp,
end of ty_marc.

data t_marc type table of ty_marc.

types : begin of ty_mard,


matnr type mard-matnr,
werks type mard-werks,
lgort type mard-lgort,
end of ty_mard.

data t_mard type table of ty_mard.

data : t_fcat type SLIS_T_FIELDCAT_ALV,


wa_fcat like line of t_fcat.

data wa_layo type SLIS_LAYOUT_ALV.

data : t_events type SLIS_T_EVENT,


wa_events like line of t_events.

start-OF-SELECTION.
* get sales orders
perform getsalesorders.
if t_vbak is not INITIAL.
* generate fieldcatalog for sales orders fields
perform fldcatvbak.
* register events
perform regevents.
* display sales orders grid
perform displayvbak.
endif.

FORM GETSALESORDERS .
select vbeln erdat erzet ernam
from vbak
into table t_vbak
where vbeln in so_vbeln.
ENDFORM. " GETSALESORDERS

FORM FLDCATVBAK .
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
I_STRUCTURE_NAME = 'ZCVBAK'
CHANGING
CT_FIELDCAT = t_fcat.

ENDFORM. " FLDCATVBAK

FORM DISPLAYVBAK .
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = SY-REPID
I_GRID_TITLE = 'SALES ORDERS'
IT_FIELDCAT = t_fcat
IT_EVENTS = t_events
TABLES
T_OUTTAB = t_vbak.
ENDFORM. " DISPLAYVBAK

FORM REGEVENTS .
clear wa_events.
wa_events-name = 'USER_COMMAND'.
wa_events-form = 'SALESITEMS'.
append wa_events to t_events.
ENDFORM. " REGEVENTS

*form salesitems. "GENERATES runtime error


form salesitems using ucomm type sy-ucomm
sel type slis_selfield.
case sel-fieldname.
when 'VBELN'.
* message 'user command' type 'I'.
clear wa_vbak.
read table t_vbak into wa_vbak
index sel-tabindex TRANSPORTING vbeln.
if sy-subrc eq 0.
* get corresponding sales items
perform getsalesitems.
if t_vbap is not INITIAL.
* generate fieldcatalog for sales items fields
perform fldcatvbap.
* register events for sales items grid
perform eventsreg.
* display sales items
perform displayvbap.
else.
message 'No sales items' type 'I'.
endif.
endif.
when others.
message 'Please click on sales doc' type 'I'.
endcase.
endform.

FORM GETSALESITEMS .
select vbeln posnr matnr netwr
from vbap
into table t_vbap
where vbeln = wa_vbak-vbeln.
ENDFORM. " GETSALESITEMS

FORM FLDCATVBAP .
refresh t_fcat.
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
I_STRUCTURE_NAME = 'ZCVBAP'
CHANGING
CT_FIELDCAT = t_fcat.
ENDFORM. " FLDCATVBAP

FORM DISPLAYVBAP .
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = sy-repid
I_GRID_TITLE = 'SALES ITEMS'
IT_FIELDCAT = t_fcat
IT_EVENTS = t_events
TABLES
T_OUTTAB = t_vbap.

ENDFORM. " DISPLAYVBAP


FORM EVENTSREG .
refresh t_events.
clear wa_events.
wa_events-name = 'TOP_OF_PAGE'.
wa_events-form = 'HEADING'.
append wa_events to t_events.

clear wa_events.
wa_events-name = 'USER_COMMAND'.
wa_events-form = 'MATERIALDATA'.
append wa_events to t_events.
ENDFORM. " EVENTSREG

form heading.
* message 'top' type 'I'.
data : t_header type SLIS_T_LISTHEADER,
wa_header like line of t_header.

clear wa_header.
wa_header-typ = 'H'.
wa_header-info = 'Gensoft Technologies'.
append wa_header to t_header.

clear wa_header.
wa_header-typ = 'S'.
wa_header-info = 'SAP Technical Training'.
append wa_header to t_header.

CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'


EXPORTING
IT_LIST_COMMENTARY = t_header
I_LOGO = 'ZBUTTERFLY'.
endform.

form materialdata using ucomm type sy-ucomm


sel type slis_selfield.
* message 'user' type 'I'.
case sel-fieldname.
when 'MATNR'.
clear wa_vbap.
read table t_vbap into wa_vbap index sel-tabindex
transporting matnr.
if sy-subrc eq 0.
perform materialblocks.
endif.
when others.
message 'Please select material' type 'I'.
endcase.
endform.

FORM MATERIALBLOCKS .
* Initialize ALV Blocked list
CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_INIT'
EXPORTING
I_CALLBACK_PROGRAM = SY-REPID.

* get material plant data


perform getmaterialplants.
if t_marc is not initial.
* generate fieldcatalog for block 1 (material plants)
perform fldcatmarc.
* append material plant data to ALV block
perform appendmarc.
endif.

* get material storage data


perform getmaterialstoragelocs.
if t_mard is not initial.
* generate fieldcatalog for block 2 (material storage locations)
perform fldcatmard.
* append material storage locations to ALV block
perform appendmard.
endif.

* display ALV blocked list


CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_DISPLAY'.

ENDFORM. " MATERIALBLOCKS

FORM FLDCATMARC .
refresh t_fcat.
clear wa_fcat.
wa_fcat-fieldname = 'MATNR'.
wa_fcat-col_pos = 1.
wa_fcat-outputlen = 15.
wa_fcat-seltext_m = 'Material No'.
append wa_fcat to t_fcat.
clear wa_fcat.
wa_fcat-fieldname = 'WERKS'.
wa_fcat-col_pos = 2.
wa_fcat-outputlen = 10.
wa_fcat-seltext_m = 'Plant'.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'EKGRP'.
wa_fcat-col_pos = 3.
wa_fcat-outputlen = 15.
wa_fcat-seltext_m = 'Purchase Group'.
append wa_fcat to t_fcat.
ENDFORM. " FLDCATMARC

FORM GETMATERIALPLANTS .
select matnr werks ekgrp
from marc
into table t_marc
where matnr = wa_vbap-matnr.
ENDFORM. " GETMATERIALPLANTS

FORM APPENDMARC .
refresh t_events.
CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_APPEND'
EXPORTING
IS_LAYOUT = wa_layo
IT_FIELDCAT = t_fcat
I_TABNAME = 'T_MARC'
IT_EVENTS = t_events
TABLES
T_OUTTAB = t_marc.

ENDFORM. " APPENDMARC

FORM GETMATERIALSTORAGELOCS .
select matnr werks lgort
from mard
into table t_mard
where matnr = wa_vbap-matnr.
ENDFORM. " GETMATERIALSTORAGELOCS

FORM FLDCATMARD .
refresh t_fcat.
clear wa_fcat.
wa_fcat-fieldname = 'MATNR'.
wa_fcat-col_pos = 1.
wa_fcat-outputlen = 15.
wa_fcat-seltext_m = 'Material No'.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'WERKS'.
wa_fcat-col_pos = 2.
wa_fcat-outputlen = 10.
wa_fcat-seltext_m = 'Plant'.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'LGORT'.
wa_fcat-col_pos = 3.
wa_fcat-outputlen = 15.
wa_fcat-seltext_m = 'Storage Locations'.
append wa_fcat to t_fcat.

ENDFORM. " FLDCATMARD

FORM APPENDMARD .
refresh t_events.
CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_APPEND'
EXPORTING
IS_LAYOUT = wa_layo
IT_FIELDCAT = t_fcat
I_TABNAME = 'T_MARD'
IT_EVENTS = t_events
TABLES
T_OUTTAB = t_mard.
ENDFORM. " APPENDMARD

Example: Hierarchical Sequential Report using ALV Function Modules (Field catalog
generated using Function Module)

REPORT Z730ALV26.

types : begin of ty_vbak.


include type zcvbak.
types end of ty_vbak.

data t_vbak type table of ty_vbak.

types : begin of ty_vbap.


include type zcvbap.
types end of ty_vbap.

data t_vbap type table of ty_vbap.

data t_fcat type SLIS_T_FIELDCAT_ALV.

data wa_keyinfo type SLIS_KEYINFO_ALV.

data gv_vbeln type vbak-vbeln.


SELECT-OPTIONS so_vbeln for gv_vbeln DEFAULT '4980' to '4985'.

START-OF-SELECTION.
* get sales orders header data
perform getsalesorders.
if t_vbak is NOT INITIAL.
* get sales orders item data
perform getsalesitems.
if t_vbap is not INITIAL.
* generate fieldcatalog for salesorder header and item tables
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
I_INTERNAL_TABNAME = 'T_VBAK'
I_STRUCTURE_NAME = 'ZCVBAK'
CHANGING
CT_FIELDCAT = t_fcat.

CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'


EXPORTING
I_INTERNAL_TABNAME = 'T_VBAP'
I_STRUCTURE_NAME = 'ZCVBAP'
CHANGING
CT_FIELDCAT = t_fcat.

* register key fields


perform regkeyfields.
* display hierarachial report
perform display.
endif.
endif.

FORM GETSALESORDERS .
select vbeln erdat erzet ernam
from vbak
into table t_vbak
where vbeln in so_vbeln.
ENDFORM. " GETSALESORDERS

FORM GETSALESITEMS .
select vbeln posnr matnr netwr
from vbap
into table t_vbap
for ALL ENTRIES IN t_vbak
where vbeln = t_vbak-vbeln.
ENDFORM. " GETSALESITEMS

FORM REGKEYFIELDS .
clear wa_keyinfo.
wa_keyinfo-header01 = 'VBELN'.
wa_keyinfo-item01 = 'VBELN'.
wa_keyinfo-header02 = space.
wa_keyinfo-item02 = 'POSNR'.
ENDFORM. " REGKEYFIELDS

FORM DISPLAY .
CALL FUNCTION 'REUSE_ALV_HIERSEQ_LIST_DISPLAY'
EXPORTING
IT_FIELDCAT = t_fcat
I_TABNAME_HEADER = 'T_VBAK'
I_TABNAME_ITEM = 'T_VBAP'
IS_KEYINFO = wa_keyinfo
TABLES
T_OUTTAB_HEADER = t_vbak
T_OUTTAB_ITEM = t_vbap.
ENDFORM. " DISPLAY

Example: Hierarchical Sequential Report using ALV Function Modules (Field catalog
generated manually)

REPORT Z730ALV27.
types : begin of ty_vbak.
include type zcvbak.
types end of ty_vbak.

data t_vbak type table of ty_vbak.

types : begin of ty_vbap.


include type zcvbap.
types end of ty_vbap.

data t_vbap type table of ty_vbap.

data : t_fcat type SLIS_T_FIELDCAT_ALV,


wa_fcat like line of t_fcat.

data wa_keyinfo type SLIS_KEYINFO_ALV.

data gv_vbeln type vbak-vbeln.


SELECT-OPTIONS so_vbeln for gv_vbeln DEFAULT '4980' to '4985'.

START-OF-SELECTION.
* get sales orders header data
perform getsalesorders.
if t_vbak is NOT INITIAL.
* get sales orders item data
perform getsalesitems.
if t_vbap is not INITIAL.
* generate fieldcatalog for salesorder header and item tables
perform fldcat.

* register key fields


perform regkeyfields.
* display hierarachial report
perform display.
endif.
endif.

FORM GETSALESORDERS .
select vbeln erdat erzet ernam
from vbak
into table t_vbak
where vbeln in so_vbeln.
ENDFORM. " GETSALESORDERS
FORM GETSALESITEMS .
select vbeln posnr matnr netwr
from vbap
into table t_vbap
for ALL ENTRIES IN t_vbak
where vbeln = t_vbak-vbeln.
ENDFORM. " GETSALESITEMS

FORM REGKEYFIELDS .
clear wa_keyinfo.
wa_keyinfo-header01 = 'VBELN'.
wa_keyinfo-item01 = 'VBELN'.
wa_keyinfo-header02 = space.
wa_keyinfo-item02 = 'POSNR'.
ENDFORM. " REGKEYFIELDS

FORM DISPLAY .
CALL FUNCTION 'REUSE_ALV_HIERSEQ_LIST_DISPLAY'
EXPORTING
IT_FIELDCAT = t_fcat
I_TABNAME_HEADER = 'T_VBAK'
I_TABNAME_ITEM = 'T_VBAP'
IS_KEYINFO = wa_keyinfo
TABLES
T_OUTTAB_HEADER = t_vbak
T_OUTTAB_ITEM = t_vbap.
ENDFORM. " DISPLAY

FORM FLDCAT .
refresh t_fcat.
clear wa_fcat.
wa_fcat-fieldname = 'VBELN'.
wa_fcat-col_pos = 1.
wa_fcat-seltext_m = 'Sales Doc'.
wa_fcat-outputlen = 12.
wa_fcat-tabname = 'T_VBAK'.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERDAT'.
wa_fcat-col_pos = 2.
wa_fcat-seltext_m = 'Date'.
wa_fcat-outputlen = 12.
wa_fcat-tabname = 'T_VBAK'.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERZET'.
wa_fcat-col_pos = 3.
wa_fcat-seltext_m = 'Time'.
wa_fcat-outputlen = 12.
wa_fcat-tabname = 'T_VBAK'.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERNAM'.
wa_fcat-col_pos = 4.
wa_fcat-seltext_m = 'Person'.
wa_fcat-outputlen = 15.
wa_fcat-tabname = 'T_VBAK'.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'VBELN'.
wa_fcat-col_pos = 5.
wa_fcat-seltext_m = 'Sales Doc'.
wa_fcat-outputlen = 12.
wa_fcat-tabname = 'T_VBAP'.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'POSNR'.
wa_fcat-col_pos = 6.
wa_fcat-seltext_m = 'Item No'.
wa_fcat-outputlen = 12.
wa_fcat-tabname = 'T_VBAP'.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'MATNR'.
wa_fcat-col_pos = 7.
wa_fcat-seltext_m = 'Material'.
wa_fcat-outputlen = 12.
wa_fcat-tabname = 'T_VBAP'.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'NETWR'.
wa_fcat-col_pos = 8.
wa_fcat-seltext_m = 'Net value'.
wa_fcat-outputlen = 12.
wa_fcat-tabname = 'T_VBAP'.
append wa_fcat to t_fcat.

ENDFORM. " FLDCAT

Example: ALV Object Model (Simple Table Display)

REPORT Z730ALV28.

data lv_vbeln type vbak-vbeln.


select-options so_vbeln for lv_vbeln DEFAULT '4980' to '4990'.

types : begin of ty_vbak.


include type zcvbak.
types end of ty_vbak.

data t_vbak type table of ty_vbak.

data o_alv type ref to CL_SALV_TABLE.

START-OF-SELECTION.
* get data
perform getdata.
if t_vbak is not INITIAL.
* get instance of SALV table class
data o_salv type ref to CL_SALV_TABLE.
TRY.
CALL METHOD CL_SALV_TABLE=>FACTORY
IMPORTING
R_SALV_TABLE = o_salv
CHANGING
T_TABLE = t_vbak.
CATCH CX_SALV_MSG .
message 'Exception in creating SALV object' type 'I'.
ENDTRY.

if o_salv is not INITIAL.


* display the ALV grid
call method o_salv->display.
endif.
endif.

FORM GETDATA .
select vbeln erdat erzet ernam
from vbak
into table t_vbak
where vbeln in so_vbeln.
ENDFORM. " GETDATA

Example: Interactive ALV Reporting in ALV Object Model (SALV classes)

REPORT Z730ALV29.

data gv_kunnr type kna1-kunnr.


select-OPTIONS so_kunnr for gv_kunnr DEFAULT '1000' to '1010'.

types : begin of ty_kna1.


include type zckna1.
types end of ty_kna1.

data : t_kna1 type table of ty_kna1,


wa_kna1 type ty_kna1.

types : begin of ty_vbak.


include type zcvbak.
types end of ty_vbak.

data t_vbak type table of ty_vbak.

class lcl_eventreceiver DEFINITION.


PUBLIC SECTION.
methods handle_link_click
for event link_click of CL_SALV_EVENTS_TABLE
importing row.
endclass.

class lcl_eventreceiver IMPLEMENTATION.


method handle_link_click.
* message 'hotspot click' type 'I'.
* read the customer no selected
clear wa_kna1.
read table t_kna1 into wa_kna1 index row TRANSPORTING kunnr.
if sy-subrc eq 0.
* get the sales orders of selected customer
perform getsalesorders.
if t_vbak is not INITIAL.
* get the ALV object for sales orders data
data o_vbak_alv type ref to CL_SALV_TABLE.
TRY.
CALL METHOD CL_SALV_TABLE=>FACTORY
IMPORTING
R_SALV_TABLE = o_vbak_alv
CHANGING
T_TABLE = t_vbak.
CATCH CX_SALV_MSG .
message 'Exception in creating ALV object for VBAK grid' type 'I'.
ENDTRY.
if o_vbak_alv is bound.
* get the ALV toolbar functions
data o_functions type ref to CL_SALV_FUNCTIONS_LIST.
CALL METHOD O_VBAK_ALV->GET_FUNCTIONS
RECEIVING
VALUE = o_functions.
if o_functions is bound.
* show the alv toolbar functions
call method o_functions->set_all.
endif.
* display the sales orders grid
call method o_vbak_alv->display.
endif.
else.
message 'No Sales orders for selected customer' type 'I'.
endif.
endif.
ENDMETHOD.
endclass.

data ob type ref to lcl_eventreceiver.

START-OF-SELECTION.
* get customer data
perform getcustomers.
if t_kna1 is not INITIAL.
* get the ALV object for customer data
data o_kna1_alv type ref to CL_SALV_TABLE.
TRY.
CALL METHOD CL_SALV_TABLE=>FACTORY
IMPORTING
R_SALV_TABLE = o_kna1_alv
CHANGING
T_TABLE = t_kna1.
CATCH CX_SALV_MSG .
message 'Exception in creating ALV object for KNA1 grid' type 'I'.
ENDTRY.
if o_kna1_alv is BOUND. "same as not initial
* get the reference of ALL columns
data o_columns type ref to CL_SALV_COLUMNS_TABLE.
CALL METHOD O_KNA1_ALV->GET_COLUMNS
RECEIVING
VALUE = o_columns.
if o_columns is bound.
* get the reference of a specific column (KUNNR)
data o_column type ref to CL_SALV_COLUMN.
TRY.
CALL METHOD O_COLUMNS->GET_COLUMN
EXPORTING
COLUMNNAME = 'KUNNR'
RECEIVING
VALUE = o_column.
if o_column is bound.
* type cast the object to sub class
data o_col type ref to CL_SALV_COLUMN_TABLE.
o_col ?= o_column.
if o_col is bound.
* set the cell type to HOTSPOT for Kunnr column
CALL METHOD O_COL->SET_CELL_TYPE
EXPORTING
VALUE = IF_SALV_C_CELL_TYPE=>HOTSPOT.
endif.
endif.
CATCH CX_SALV_NOT_FOUND .
message 'Exception in getting the column reference' type 'I'.
ENDTRY.
endif.
endif.
* register handlers
perform reghandlers.
* display the ALV grid containing customer data
CALL METHOD O_KNA1_ALV->DISPLAY.

endif.
FORM GETCUSTOMERS .
SELECT kunnr land1 name1
from kna1
into table t_kna1
where kunnr in so_kunnr.
ENDFORM. " GETCUSTOMERS

FORM REGHANDLERS .
create object ob.
* set handler ob->handle_link_click for o_kna1_alv. "syntax error
* get the object of event table class
data o_events_table type ref to cl_salv_events_table.
CALL METHOD O_KNA1_ALV->GET_EVENT
RECEIVING
VALUE = o_events_table.

set handler ob->handle_link_click for o_events_table.


ENDFORM. " REGHANDLERS

FORM GETSALESORDERS .
select vbeln erdat erzet ernam
from vbak
into table t_vbak
where kunnr = wa_kna1-kunnr.
ENDFORM. " GETSALESORDERS

Introduction:

The CL_SALV_TABLE class is part of the ALV Object Model which was introduced in
NetWeaver 2004. Basically it is an encapsulation of the pre-existing ALV tools. For example the
class CL_SALV_TABLE actually wraps around the CL_GUI_ALV_GRID class for container
implementation, as well as the REUSE_ALV_GRID_DISPLAY and REUSE_ALV_LIST_DISPLAY
function modules for full screen display.

It was designed to be a single point of entry when using a specific ALV tool such as the
ALV table display, ALV hierarchical sequential list, and tree ALV. All of these individual ALV tools
have their own base class, for table it is the CL_SALV_TABLE, but all have a common look. A lot
of the methods are the same, with only some differences in the parameters of the methods
depending on the actual tool you are using.

So to summarize, the ALV Object Model was delivered to give a more collective interface
to the ALV tools. There are limitations in the ALV Object Model, for example, you can NOT
color a line or a cell, but you can color a column. Also, you can NOT have an editable ALV using
the Object Model.

But for basic lists, it is a very powerful tool.

Following are the restrictions with CL_SALV_TABLE (ALV Object Model):

The number of columns is restricted to 90.

The output length of a column is restricted to 128 characters.

For sorting and subtotals, you use a total of nine levels or columns.

For columns that can be aggregated, note that the internal length of the column is large
enough for both single values and the result.

The output is column-oriented. You are only able to display flat, structured tables. You are not
able to display nested tables.

Tables displayed with ALV are not ready for input.

If you insert the table as grid in container, you are not able to use batch mode (background)

Event handling in ALV Object Model:

For this, we need to handle the events defined in the class ‘CL_SALV_EVENTS_TABLE’.

Example: TOP_OF_PAGE, PRINT Events in OOPS ALV

Report Program: Z8AMALV22


REPORT Z8AMALV22.

include z8amalv22_inc.

start-of-SELECTION.
call screen 100.

Include Program: Z8AMALV22_INC


data gv_kunnr type kna1-kunnr.
SELECT-OPTIONS so_kunnr for gv_kunnr DEFAULT '1000' to '1500'.
types : begin of ty_kna1,
kunnr type kna1-kunnr,
land1 type kna1-land1,
name1 type kna1-name1,
end of ty_kna1.

data : t_kna1 type table of ty_kna1,


wa_kna1 type ty_kna1.

data : t_fcat type lvc_t_fcat,


wa_fcat type lvc_s_fcat.

data : o_split type ref to cl_gui_splitter_container,


cust_cont type ref to cl_gui_custom_container,
o_cont1 type ref to cl_gui_container,
o_cont2 type ref to cl_gui_container,
kna1_grid type ref to cl_gui_alv_grid.

class lcl_eventreceiver DEFINITION.


public SECTION.
methods handle_top_of_page for event
top_of_page of cl_gui_alv_grid
IMPORTING e_dyndoc_id.
methods handle_print_top_of_page for event
print_top_of_page of cl_gui_alv_grid.
methods handle_print_top_of_list for event
print_top_of_list of cl_gui_alv_grid.
methods handle_print_end_of_page for event
print_end_of_page of cl_gui_alv_grid.
methods handle_print_end_of_list for event
print_end_of_list of cl_gui_alv_grid.
endclass.

class lcl_eventreceiver IMPLEMENTATION.


method handle_top_of_page.
CALL METHOD E_DYNDOC_ID->ADD_TEXT
EXPORTING
TEXT = text-001.

CALL METHOD E_DYNDOC_ID->NEW_LINE


EXPORTING
REPEAT = 1.

CALL METHOD E_DYNDOC_ID->ADD_TEXT


EXPORTING
TEXT = text-002
SAP_COLOR = cl_dd_document=>LIST_POSITIVE
SAP_FONTSIZE = cl_dd_document=>strong.

CALL METHOD E_DYNDOC_ID->NEW_LINE


EXPORTING
REPEAT = 1.

CALL METHOD E_DYNDOC_ID->ADD_TEXT


EXPORTING
TEXT = text-003
SAP_COLOR = cl_dd_document=>LIST_TOTAL_INT
SAP_FONTSIZE = cl_dd_document=>MEDIUM.

CALL METHOD E_DYNDOC_ID->ADD_GAP


EXPORTING
WIDTH = 1.

CALL METHOD E_DYNDOC_ID->ADD_TEXT


EXPORTING
TEXT = text-004
SAP_COLOR = cl_dd_document=>LIST_NORMAL_INT
SAP_FONTSIZE = cl_dd_document=>MEDIUM.

* Associate top_of_page text with container o_cont1


CALL METHOD E_DYNDOC_ID->DISPLAY_DOCUMENT
EXPORTING
PARENT = O_CONT1.

endmethod.

method handle_print_top_of_page.
write :/ 'PRINT TOP OF PAGE EVENT' color 3,'Page No :' color
4,sy-pagno color 5.
endmethod.

method handle_print_end_of_page.
write :/ 'PRINT END OF PAGE EVENT' color 1.
endmethod.

method handle_print_top_of_list.
write :/ 'PRINT TOP OF LIST EVENT' color 5.
endmethod.

method handle_print_end_of_list.
write :/ 'PRINT END OF LIST EVENT' color 7.
endmethod.
endclass.
data ob type ref to lcl_eventreceiver.

MODULE USER_COMMAND_0100 INPUT.


case sy-ucomm.
when 'FC1'.
leave PROGRAM.
endcase.
ENDMODULE. " USER_COMMAND_0100 INPUT

MODULE STATUS_0100 OUTPUT.


if cust_cont is INITIAL.
CREATE OBJECT CUST_CONT
EXPORTING
CONTAINER_NAME = 'CST'.

CREATE OBJECT O_SPLIT


EXPORTING
PARENT = cust_cont
ROWS = 2
COLUMNS = 1.

CALL METHOD O_SPLIT->SET_ROW_HEIGHT


EXPORTING
ID = 1
HEIGHT = 5.

CALL METHOD O_SPLIT->SET_ROW_HEIGHT


EXPORTING
ID = 2
HEIGHT = 8.

CALL METHOD O_SPLIT->GET_CONTAINER


EXPORTING
ROW = 1
COLUMN = 1
RECEIVING
CONTAINER = o_cont1.

CALL METHOD O_SPLIT->GET_CONTAINER


EXPORTING
ROW = 2
COLUMN = 1
RECEIVING
CONTAINER = o_cont2.

* get customer data


perform getcustomers.
if t_kna1 is not INITIAL.
* field catalog for customer data
perform fldcatkna1.
* display customer data
perform displaycustomers.
endif.
endif.
ENDMODULE. " STATUS_0100 OUTPUT

FORM GETCUSTOMERS .
select kunnr land1 name1
from kna1
into table t_kna1
where kunnr in so_kunnr.

ENDFORM. " GETCUSTOMERS

FORM FLDCATKNA1 .
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
I_STRUCTURE_NAME = 'ZCSTKNA1'
CHANGING
CT_FIELDCAT = t_fcat.

ENDFORM. " FLDCATKNA1

FORM DISPLAYCUSTOMERS .
if kna1_grid is INITIAL.
CREATE OBJECT KNA1_GRID
EXPORTING
I_PARENT = o_cont2.

perform reghandlers.

CALL METHOD KNA1_GRID->SET_TABLE_FOR_FIRST_DISPLAY


CHANGING
IT_OUTTAB = t_kna1
IT_FIELDCATALOG = t_fcat.
endif.

ENDFORM. " DISPLAYCUSTOMERS

FORM REGHANDLERS .
create object ob.
set handler ob->handle_top_of_page for kna1_grid.
set handler ob->handle_print_top_of_page for kna1_grid.
set handler ob->handle_print_end_of_page for kna1_grid.
set handler ob->handle_print_top_of_list for kna1_grid.
set handler ob->handle_print_end_of_list for kna1_grid.

* register top_of_page event explictly


data k type ref to cl_dd_document.
create object k.
CALL METHOD KNA1_GRID->LIST_PROCESSING_EVENTS
EXPORTING
I_EVENT_NAME = 'TOP_OF_PAGE'
I_DYNDOC_ID = k.
ENDFORM. " REGHANDLERS

Note: To Test Print events, execute the ALV report and in the runtime, click on ‘print’ button so
that report is sent to captured as spool request. Now, go to ‘SP01’ and view the spool.

Example: ALV Grand Total and Grand Total Text

Report Program: Z8AMALV23


REPORT Z8AMALV23.

include z8amalv23_inc.

start-of-SELECTION.
call screen 100.

Include Program: Z8AMALV23_INC


data gv_vbeln type vbak-vbeln.
select-OPTIONS so_vbeln for gv_vbeln DEFAULT '4980' to '4995'.

types : begin of ty_vbak.


include type zcstvbak.
types : netwr type vbak-netwr,
tottext type string,
end of ty_vbak.

data : t_vbak type table of ty_vbak,


wa_vbak type ty_vbak.

data : t_fcat type lvc_t_fcat,


wa_fcat like line of t_fcat.

data : lt_sort type lvc_t_sort,


ls_sort type lvc_s_sort.

data wa_layo type lvc_s_layo.


data : vbak_cont type ref to cl_gui_custom_container,
vbak_grid type ref to cl_gui_alv_grid.

MODULE USER_COMMAND_0100 INPUT.


case sy-ucomm.
when 'FC1'.
leave PROGRAM.
endcase.
ENDMODULE. " USER_COMMAND_0100 INPUT

MODULE STATUS_0100 OUTPUT.


if vbak_cont is INITIAL.
CREATE OBJECT VBAK_CONT
EXPORTING
CONTAINER_NAME = 'CST'.

CREATE OBJECT VBAK_GRID


EXPORTING
I_PARENT = vbak_cont.

perform getsalesorders.
if t_vbak is not INITIAL.
perform fldcatvbak.
perform sortcriteria.
perform layoutvbak.
perform displaysalesorders.
endif.
endif.
ENDMODULE. " STATUS_0100 OUTPUT

FORM GETSALESORDERS .
select vbeln erdat erzet ernam netwr
from vbak
into table t_vbak
where vbeln in so_vbeln.
ENDFORM. " GETSALESORDERS

FORM FLDCATVBAK .
refresh t_fcat.
clear wa_fcat.
wa_fcat-fieldname = 'VBELN'.
wa_fcat-col_pos = 1.
wa_fcat-coltext = 'Sales Doc'.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERDAT'.
wa_fcat-col_pos = 2.
wa_fcat-coltext = 'Date'.
wa_fcat-outputlen = 12.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERZET'.
wa_fcat-col_pos = 3.
wa_fcat-coltext = 'Time'.
wa_fcat-outputlen = 12.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERNAM'.
wa_fcat-col_pos = 4.
wa_fcat-coltext = 'Created By'.
wa_fcat-outputlen = 22.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'NETWR'.
wa_fcat-col_pos = 5.
wa_fcat-coltext = 'Net Value'.
wa_fcat-outputlen = 12.
wa_fcat-do_sum = 'X'.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'TOTTEXT'.
wa_fcat-coltext = 'Total Net Value'.
wa_fcat-no_out = 'X'.
append wa_fcat to t_fcat.
ENDFORM. " FLDCATVBAK

FORM DISPLAYSALESORDERS .
CALL METHOD VBAK_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
IS_LAYOUT = wa_layo
CHANGING
IT_OUTTAB = t_vbak
IT_FIELDCATALOG = t_fcat
IT_SORT = lt_sort.

ENDFORM. " DISPLAYSALESORDERS

FORM SORTCRITERIA .
clear ls_sort.
ls_sort-fieldname = 'TOTTEXT'.
ls_sort-subtot = 'X'.
append ls_sort to lt_sort.
ENDFORM. " SORTCRITERIA

FORM LAYOUTVBAK .
clear wa_layo.
wa_layo-no_totline = 'X'. "Suppress output total line
ENDFORM. " LAYOUTVBAK

Example: ALV Subtotals and Subtotal text (event subtotal_text)

Report Program: Z8AMALV24


REPORT Z8AMALV24.

INCLUDE Z8AMALV24_INC.

start-of-SELECTION.
call screen 100.

Include Program: Z8AMALV24_INC


data gv_vbeln type vbak-vbeln.
select-OPTIONS so_vbeln for gv_vbeln DEFAULT '4980' to '4995'.

types : begin of ty_vbap.


include type zcstvbap.
types : ttext type string,
end of ty_vbap.

data : t_vbap type table of ty_vbap,


wa_vbap type ty_vbap.

data : t_fcat type lvc_t_fcat,


wa_fcat like line of t_fcat.

data : lt_sort type lvc_t_sort,


ls_sort type lvc_s_sort.

data wa_layo type lvc_s_layo.

data : vbap_cont type ref to cl_gui_custom_container,


vbap_grid type ref to cl_gui_alv_grid.

class lcl_eventreceiver DEFINITION.


public SECTION.
methods handle_subtotal_text for event
subtotal_text of cl_gui_alv_grid
IMPORTING ep_subtot_line e_event_data.
endclass.

class lcl_eventreceiver IMPLEMENTATION.


method handle_subtotal_text.
e_event_data->M_EVENT_HANDLED = 'X'.
FIELD-SYMBOLS <abc> type any.
assign e_event_data->m_data->* to <abc>.
if sy-subrc eq 0.
clear <abc>.
<abc> = text-001.
endif.
endmethod.
endclass.

data ob type ref to lcl_eventreceiver.

MODULE USER_COMMAND_0100 INPUT.


case sy-ucomm.
when 'FC1'.
leave PROGRAM.
endcase.
ENDMODULE. " USER_COMMAND_0100 INPUT

MODULE STATUS_0100 OUTPUT.


if vbap_cont is INITIAL.
CREATE OBJECT VBAP_CONT
EXPORTING
CONTAINER_NAME = 'CST'.

CREATE OBJECT VBAP_GRID


EXPORTING
I_PARENT = vbap_cont.

perform getsalesitems.
if t_vbap is not INITIAL.
perform fldcatvbap.
perform sortcriteria.
perform layoutvbap.
perform reghandlers.
perform displaysalesitems.
endif.
endif.
ENDMODULE. " STATUS_0100 OUTPUT

FORM GETSALESITEMS .
select vbeln posnr matnr netwr
from vbap
into table t_vbap
where vbeln in so_vbeln.
ENDFORM. " GETSALESITEMS

FORM FLDCATVBAP .
refresh t_fcat.
clear wa_fcat.
wa_fcat-fieldname = 'VBELN'.
wa_fcat-col_pos = 1.
wa_fcat-coltext = 'Sales Doc'.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'POSNR'.
wa_fcat-col_pos = 2.
wa_fcat-coltext = 'Item No'.
wa_fcat-outputlen = 12.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'MATNR'.
wa_fcat-col_pos = 3.
wa_fcat-coltext = 'Material'.
wa_fcat-outputlen = 12.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'NETWR'.
wa_fcat-col_pos = 4.
wa_fcat-coltext = 'Net Value'.
wa_fcat-outputlen = 12.
wa_fcat-do_sum = 'X'.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'TTEXT'.
wa_fcat-coltext = 'Subtotal is '.
wa_fcat-no_out = 'X'.
append wa_fcat to t_fcat.
ENDFORM. " FLDCATVBAP

FORM DISPLAYSALESITEMS .
CALL METHOD VBAP_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
IS_LAYOUT = wa_layo
CHANGING
IT_OUTTAB = t_vbap
IT_FIELDCATALOG = t_fcat
IT_SORT = lt_sort.

ENDFORM. " DISPLAYSALESITEMS

FORM SORTCRITERIA .
clear ls_sort.
ls_sort-fieldname = 'VBELN'.
ls_sort-subtot = 'X'.
append ls_sort to lt_sort.

clear ls_sort.
ls_sort-fieldname = 'TTEXT'.
ls_sort-subtot = 'X'.
append ls_sort to lt_sort.
ENDFORM. " SORTCRITERIA

FORM LAYOUTVBAP .
clear wa_layo.
* wa_layo-no_totline = 'X'. "Suppress output total line
ENDFORM. " LAYOUTVBAK

FORM REGHANDLERS .
create object ob.
set handler ob->handle_subtotal_text for vbap_grid.
ENDFORM. " REGHANDLERS

Example: OOPS ALV Layout Variants

Report Program: Z8AMALV25


REPORT Z8AMALV25.

INCLUDE Z8AMALV25_INC.
start-of-SELECTION.
call screen 100.

Include Program: z8amalv25_inc


data gv_vbeln type vbak-vbeln.
select-OPTIONS so_vbeln for gv_vbeln DEFAULT '4980' to '4995'.

types : begin of ty_vbak.


include type zcstvbak.
types : netwr type vbak-netwr,
tottext type string,
end of ty_vbak.

data : t_vbak type table of ty_vbak,


wa_vbak type ty_vbak.

data : t_fcat type lvc_t_fcat,


wa_fcat like line of t_fcat.

data : lt_sort type lvc_t_sort,


ls_sort type lvc_s_sort.

data wa_layo type lvc_s_layo.

data : vbak_cont type ref to cl_gui_custom_container,


vbak_grid type ref to cl_gui_alv_grid.

data wa_variant type DISVARIANT.

MODULE USER_COMMAND_0100 INPUT.


case sy-ucomm.
when 'FC1'.
leave PROGRAM.
endcase.
ENDMODULE. " USER_COMMAND_0100 INPUT

MODULE STATUS_0100 OUTPUT.


if vbak_cont is INITIAL.
CREATE OBJECT VBAK_CONT
EXPORTING
CONTAINER_NAME = 'CST'.

CREATE OBJECT VBAK_GRID


EXPORTING
I_PARENT = vbak_cont.

perform getsalesorders.
if t_vbak is not INITIAL.
perform fldcatvbak.
perform sortcriteria.
perform layoutvbak.
perform configvariant.
perform displaysalesorders.
endif.
endif.
ENDMODULE. " STATUS_0100 OUTPUT

FORM GETSALESORDERS .
select vbeln erdat erzet ernam netwr
from vbak
into table t_vbak
where vbeln in so_vbeln.
ENDFORM. " GETSALESORDERS

FORM FLDCATVBAK .
refresh t_fcat.
clear wa_fcat.
wa_fcat-fieldname = 'VBELN'.
wa_fcat-col_pos = 1.
wa_fcat-coltext = 'Sales Doc'.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERDAT'.
wa_fcat-col_pos = 2.
wa_fcat-coltext = 'Date'.
wa_fcat-outputlen = 12.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERZET'.
wa_fcat-col_pos = 3.
wa_fcat-coltext = 'Time'.
wa_fcat-outputlen = 12.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'ERNAM'.
wa_fcat-col_pos = 4.
wa_fcat-coltext = 'Created By'.
wa_fcat-outputlen = 22.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'NETWR'.
wa_fcat-col_pos = 5.
wa_fcat-coltext = 'Net Value'.
wa_fcat-outputlen = 12.
wa_fcat-do_sum = 'X'.
append wa_fcat to t_fcat.

clear wa_fcat.
wa_fcat-fieldname = 'TOTTEXT'.
wa_fcat-coltext = 'Total Net Value'.
wa_fcat-no_out = 'X'.
append wa_fcat to t_fcat.
ENDFORM. " FLDCATVBAK

FORM DISPLAYSALESORDERS .
CALL METHOD VBAK_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
IS_LAYOUT = wa_layo
IS_VARIANT = wa_variant
I_SAVE = 'X'
CHANGING
IT_OUTTAB = t_vbak
IT_FIELDCATALOG = t_fcat
IT_SORT = lt_sort.

ENDFORM. " DISPLAYSALESORDERS

FORM SORTCRITERIA .
clear ls_sort.
ls_sort-fieldname = 'TOTTEXT'.
ls_sort-subtot = 'X'.
append ls_sort to lt_sort.
ENDFORM. " SORTCRITERIA

FORM LAYOUTVBAK .
clear wa_layo.
wa_layo-no_totline = 'X'. "Suppress output total line
ENDFORM. " LAYOUTVBAK

FORM CONFIGVARIANT .
clear wa_variant.
wa_variant-report = sy-repid.
wa_variant-username = sy-uname.
ENDFORM. " CONFIGVARIANT

Testing the above program:


1. Execute the program and in the runtime do the following:

Case 1:

Right click on one of the column (say ERZET), and hide, now to save these changes click
on layout icon (alv toolbar), save layout, provide variant name (/v1), description and
continue.

Case 2:

Right click on one of the column (say VBELN), and sort in descending order, now to save
these changes click on layout icon (alv toolbar), save layout, provide variant name (/v2),
description and continue.

Now, in the next execution, to get back the previous layouts, click on layout icon (alv
toolbar) and choose the layout from the list.

You might also like