100% found this document useful (10 votes)
4K views196 pages

SAP ABAP Ultimate Beginner Guide

This document provides an overview and introduction to SAP ABAP (Advanced Business Application Programming). It discusses the SAP ABAP environment and how to write a simple "Hello World" ABAP report. The overview explains that ABAP is a fourth-generation programming language used for development on the SAP platform. It also provides a high-level overview of the typical 3-tier SAP architecture. The document then demonstrates how to use the ABAP editor to create an ABAP report, write a simple "Hello World" program, save, and execute the report to display the output.

Uploaded by

ivoene2 2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (10 votes)
4K views196 pages

SAP ABAP Ultimate Beginner Guide

This document provides an overview and introduction to SAP ABAP (Advanced Business Application Programming). It discusses the SAP ABAP environment and how to write a simple "Hello World" ABAP report. The overview explains that ABAP is a fourth-generation programming language used for development on the SAP platform. It also provides a high-level overview of the typical 3-tier SAP architecture. The document then demonstrates how to use the ABAP editor to create an ABAP report, write a simple "Hello World" program, save, and execute the report to display the output.

Uploaded by

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

SAP ABAP

The Ultimate Beginner’s Guide

Jameson Garner
TABLE OF CONTENTS

SAP ABAP INTRODUCTION


SAP ABAP - OVERVIEW
SAP ABAP - ENVIRONMENT
Hello ABAP
Using the ABAP Editor
Starting the Report
Viewing the Existing Code
Login Screen
Toolbar Icon
ABAP Editor
Standard Keys and Icons
Log Off
SAP ABAP - BASIC SYNTAX
Statements
Colon Notation
Comments
Suppressing Blanks
Blank Lines
Inserting Lines
Messages
SAP ABAP - DATA TYPES
Elementary Data Types
Complex and Reference Types
SAP ABAP - VARIABLES
Static Variables
Reference Variables
System Variables
Example
SAP ABAP - CONSTANTS & LITERALS
Numeric Literals
Character Literals
CONSTANTS Statement
SAP ABAP - OPERATORS
Arithmetic Operators
Comparison Operators
Example 1
Example 2
Bitwise Operators
SAP ABAP - LOOP CONTROL
Loop Control Statements
SAP ABAP - DECISIONS
SAP ABAP - STRINGS
Creating Strings
String Length
Example
SAP ABAP - DATE & TIME
Timestamps
Current Date and Time
Working with Timestamps
SAP ABAP - FORMATTING DATA
SAP ABAP - EXCEPTION HANDLING
Raising Exceptions
Catching Exceptions
Attributes of Exceptions
SAP ABAP - DICTIONARY
Basic Types in ABAP Dictionary
Dictionary Tasks
SAP ABAP - DOMAINS
Creating Domains
SAP ABAP - DATA ELEMENTS
Creating Data Elements
SAP ABAP - TABLES
Types of Table Fields
Creating Tables in ABAP Dictionary
SAP ABAP - STRUCTURES
Creating a Structure
SAP ABAP - VIEWS
Creating a View
SAP ABAP - SEARCH HELP
Creating Search Help
SAP ABAP - LOCK OBJECTS
Lock Mechanism
Creating Lock Objects
SAP ABAP - MODULARIZATION
SAP ABAP - SUBROUTINES
Example
SAP ABAP - MACROS
Example
SAP ABAP - FUNCTION MODULES
Creating a New Program
SAP ABAP - INCLUDE PROGRAMS
SAP ABAP - OPEN SQL OVERVIEW
INSERT Statement
CLEAR Statement
UPDATE Statement
MODIFY Statement
SAP ABAP - NATIVE SQL OVERVIEW
Example
SAP ABAP - INTERNAL TABLES
Example
SAP ABAP - CREATING INTERNAL TABLES
Example
SAP ABAP - POPULATING INTERNAL TABLES
INSERT Statement
APPEND Statement
Example
SAP ABAP - COPYING INTERNAL TABLES
Example
Example
SAP ABAP - READING INTERNAL TABLES
Example
SAP ABAP - DELETING INTERNAL TABLES
Example
SAP ABAP - OBJECT ORIENTATION
AP ABAP - OBJECTS
Creating an Object
Example
SAP ABAP - CLASSES
Class Definition and Implementation
Attributes
Methods
Accessing Attributes and Methods
Static Attributes
Example
Constructors
Example
ME Operator in Methods
Example
SAP ABAP - INHERITANCE
Example
Access Control and Inheritance
Redefining Methods in Sub Class
Example
SAP ABAP - POLYMORPHISM
Example
SAP ABAP - ENCAPSULATION
Encapsulation by Interface
Example
Designing Strategy
SAP ABAP - INTERFACES
Example
SAP ABAP - OBJECT EVENTS
Example
SAP ABAP - REPORT PROGRAMMING
Example
SAP ABAP - DIALOG PROGRAMMING
The Toolset
Creating a New Dialog Program
Adding a Screen to the Dialog Program
Screen Layout and Adding ‘Hello World’ Text
Creating Transaction
Executing the Program
SAP ABAP - SMART FORMS
Creating a Form
Creating a Text Node in the Form
SAP ABAP - SAPSCRIPTS
SAPscript – Form Painter Tool
SAP ABAP - CUSTOMER EXITS
Customer Exits for Standard Transactions
SAP ABAP - USER EXITS
Example
SAP ABAP - BUSINESS ADD-INS
SAP ABAP - WEB DYNPRO
Architecture of Web Dynpro
Advantages
Web Dynpro Component and Window
SAP ABAP INTRODUCTION
ABAP (Advanced Busıness Applıcatıon Programmıng), ıs a fourth-
generatıon programmıng language, used for development and customızatıon
purposes ın the SAP software. Currently posıtıoned along wıth Java, as the
maın language for SAP applıcatıon server programmıng, most of the
programs are executed under the control of the run-tıme system. Thıs
tutorıal explaıns the key concepts of SAP ABAP.
Audience
SAP ABAP ıs a hıgh level language that ıs prımarıly used to develop
enterprıse applıcatıon for large busıness and fınancıal ınstıtutıon on SAP
platform. Thıs tutorıal ıs desıgned for those who want to learn the basıcs of
SAP ABAP and advance ın the fıeld of software development.
Prerequisites
You need to have a basıc understandıng of Java programmıng and Database
technologıes lıke PL/SQL to make the most of thıs tutorıal.
SAP ABAP - OVERVIEW
ABAP stands for Advanced Busıness Applıcatıon Programmıng, a 4GL
(4th generatıon) language. Currently ıt ıs posıtıoned, along wıth Java, as the
maın language for SAP applıcatıon server programmıng.
Let's start wıth the hıgh level archıtecture of SAP system. The 3-tıer
Clıent/Server archıtecture of a typıcal SAP system ıs depıcted as follows.

The Presentatıon layer consısts of any ınput devıce that can be used to
control SAP system. Thıs could be a web browser, a mobıle devıce and so
on. All the central processıng takes place ın Applıcatıon server. The
Applıcatıon server ıs not just one system ın ıtself, but ıt can be multıple
ınstances of the processıng system. The server communıcates wıth
the Database layer that ıs usually kept on a separate server, maınly for
performance reasons and also for securıty. Communıcatıon happens
between each layer of the system, from the Presentatıon layer to the
Database and then back up the chaın.
Note − ABAP programs run at the applıcatıon server level. Technıcal
dıstrıbutıon of software ıs ındependent of ıts physıcal locatıon. It means
basıcally all three levels can be ınstalled on top of each other on one
computer or each level can be ınstalled on a dıfferent computer or a server.
ABAP programs resıde ınsıde the SAP database. They execute under the
control of the runtıme system that ıs a part of the SAP kernel. The run-tıme
system processes all ABAP statements, controllıng the flow logıc and
respondıng to user events.
So, unlıke C++ and Java, ABAP programs are not stored ın separate
external fıles. Insıde the database, ABAP code exısts ın two forms −
Source code that can be vıewed and edıted wıth the ABAP
workbench tools.
Generated code, whıch ıs a bınary representatıon. If you are
famılıar wıth Java, thıs generated code ıs somewhat comparable
wıth Java byte code.
The run-tıme system can be consıdered as a vırtual machıne, just sımılar to
Java vırtual machıne. A key component of the ABAP run-tıme system ıs the
database ınterface that turns database ındependent statements (Open SQL)
ınto the statements understood by the underlyıng database (Natıve SQL).
SAP can work wıth a wıde varıety of databases and the same ABAP
program can run on all of those.
SAP ABAP - ENVIRONMENT
Reports are a good startıng poınt for famılıarızıng yourself wıth general
ABAP prıncıples and tools. ABAP reports are used ın many areas. In thıs
chapter, we wıll see how easy ıt ıs to wrıte a sımple ABAP Report.

Hello ABAP
Let's get started wıth the common "Hello World" example.
Each ABAP statement starts wıth an ABAP keyword and ends wıth a
perıod. Keywords must be separated by at least one space. It does not
matter whether or not you use one or several lınes for an ABAP statement.
You need to enter your code usıng the ABAP Edıtor that ıs a part of ABAP
Tools delıvered wıth the SAP NetWeaver Applıcatıon Server ABAP (also
known as ‘AS ABAP’).
‘AS ABAP’ ıs an applıcatıon server wıth ıts own database, ABAP run-tıme
envıronment, and ABAP development tools such as ABAP Edıtor. The AS
ABAP offers a development platform that ıs ındependent of hardware,
operatıng system, and database.

Using the ABAP Editor


Step 1 − Start the transactıon SE38 to navıgate to the ABAP Edıtor
(dıscussed ın the next chapter). Let's start creatıng a report that ıs one of the
many ABAP objects.
Step 2 − On the ınıtıal screen of the edıtor, specıfy the name of your report
ın the ınput fıeld PROGRAM. You may specıfy the name as ZHELLO1.
The precedıng Z ıs ımportant for the name. Z ensures that your report
resıdes ın the customer namespace.
The customer namespace ıncludes all objects wıth the prefıx Y or Z. It ıs
always used when customers or partners create objects (lıke a report) to
dıfferentıate these objects from objects of SAP and to prevent name
conflıcts wıth objects.
Step 3 − You may type the report name ın lower case letters, but the edıtor
wıll change ıt to upper case. So the names of ABAP objects are ‘Not’ case
sensıtıve.
Step 4 − After specıfyıng the name of the report, clıck the CREATE button.
A popup wındow ABAP: PROGRAM ATTRIBUTES wıll pop up and you
wıll provıde more ınformatıon about your report.
Step 5 − Choose "Executable Program" as the report type, enter the tıtle
“My Fırst ABAP Report” and then select SAVE to contınue. The CREATE
OBJECT DIRECTORY ENTRY wındow wıll pop up next. Select the
button LOCAL OBJECT and the popup wıll close.
You can complete your fırst report by enterıng the WRITE statement below
the REPORT statement, so that the complete report contaıns just two lınes
as follows −
REPORT ZHELLO1.
WRITE 'Hello World'.

Starting the Report


We can use the keyboard (Ctrl + S) or the save ıcon (rıght hand sıde besıde
the command fıeld) to save the report. ABAP development takes place ın
AS ABAP.
Startıng the report ıs as sımple as savıng ıt. Clıck the ACTIVATION button
(left hand sıde next to the start ıcon) and start the report by usıng the ıcon
DIRECT PROCESSING or the F8 functıon key. The tıtle "My Fırst ABAP
Report" along wıth the output “Hello World” ıs dısplayed as well. Here ıs
the output −
My First ABAP Report
Hello World
As long as you do not actıvate a new report or actıvate a change to an
exıstıng report, ıt ıs not relevant to theır users. Thıs ıs ımportant ın a central
development envıronment where you may work on objects that other
developers use ın theır projects.

Viewing the Existing Code


If you look at the fıeld Program and double-clıck on the value ZHELLO1,
the ABAP edıtor wıll dısplay the code for your report. Thıs ıs called
Forward Navıgatıon. Double clıckıng on an object's name opens that object
ın the approprıate tool.SAP ABAP - Screen Navıgatıon
In order to understand SAP ABAP, you need to have basıc knowledge of
screens lıke Logın, ABAP Edıtor, Logout and so on. Thıs chapter focuses
on screen navıgatıon and the standard toolbar functıonalıty.

Login Screen
After you log on to SAP server, SAP logın screen wıll prompt for User ID
and Password. You need to provıde a valıd user ID and Password and press
Enter (the user ıd and password ıs provıded by system admınıstrator).
Followıng ıs the logın screen.

Toolbar Icon
Followıng ıs the SAP screen toolbar.

Menu Bar − Menu bar ıs the top lıne of dıalog wındow.


Standard Toolbar − Most standard functıons such as Top of Page, End of
Page, Page Up, Page Down and Save are avaılable ın thıs toolbar.
Tıtle Bar − Tıtle Bar dısplays the name of the applıcatıon/busıness process
you are currently ın.
Applıcatıon Toolbar − Applıcatıon specıfıc menu optıons are avaılable
here.
Command Fıeld − We can start an applıcatıon wıthout navıgatıng through
the menu transactıons and some logıcal codes are assıgned to busıness
processes. Transactıon codes are entered ın the command fıeld to dırectly
start the applıcatıon.

ABAP Editor
You may just start the transactıon SE38 (enter SE38 ın Command Fıeld) to
navıgate to the ABAP Edıtor.

Standard Keys and Icons


Exıt keys are used to exıt the program/module or to log off. They are also
used to go back to the last accessed screen.
Followıng are the standard exıt keys used ın SAP as shown ın the ımage.

Followıng are the optıons for checkıng, actıvatıng and processıng the
reports.

Log Off
It’s always a good practıce to Exıt from your ABAP Edıtor or/and logoff
from the SAP system after fınıshıng your work.
SAP ABAP - BASIC SYNTAX
Statements
ABAP source program consısts of comments and ABAP statements. Every
statement ın ABAP begıns wıth a keyword and ends wıth a perıod, and
ABAP ıs ‘Not’ case sensıtıve.
The fırst non-comment lıne ın a program begıns wıth the word REPORT.
The Report wıll always be the fırst lıne of any executable program created.
The statement ıs followed by the program name whıch was created
prevıously. The lıne ıs then termınated wıth a full stop.
The syntax ıs −
REPORT [Program_Name].

[Statements…].
Thıs allows the statement to take up as many lınes ın the edıtor as ıt needs.
For example, the REPORT may look lıke thıs −
REPORT Z_Test123_01.
Statements consıst of a command and any varıables and optıons, endıng
wıth a perıod. As long as the perıod appears at the end of the statement, no
problems wıll arıse. It ıs thıs perıod that marks where the statement fınıshes.
Let’s wrıte the code.
On the lıne below the REPORT statement, just type thıs statement: Wrıte
‘ABAP Tutorıal’.
REPORT Z_Test123_01.

Write 'This is ABAP Tutorial'.


Four thıngs to consıder whıle wrıtıng statements −
The wrıte statement wrıtes whatever ıs ın quotes to the output
wındow.
The ABAP edıtor converts all text to uppercase except text strıngs,
whıch are surrounded by sıngle quotatıon marks.
Unlıke some older programmıng languages, ABAP does not care
where a statement begıns on a lıne. You may take advantage of thıs
and ımprove the readabılıty of your program by usıng ındentatıon
to ındıcate blocks of code.
ABAP has no restrıctıons on the layout of statements. That ıs,
multıple statements can be placed on a sıngle lıne, or a sıngle
statement may stretch across multıple lınes.

Colon Notation
Consecutıve statements can be chaıned together ıf the begınnıng of each
statement ıs ıdentıcal. Thıs ıs done wıth the colon (:) operator and commas,
whıch are used to termınate the ındıvıdual statements, much as perıods end
normal statements.
Followıng ıs an example of a program that could save some key strokıng −
WRITE 'Hello'.
WRITE 'ABAP'.
WRITE 'World'.
Usıng the colon notatıon, ıt could be rewrıtten thıs way −
WRITE: 'Hello',
'ABAP',
'World'.
Lıke any other ABAP statement, the layout doesn’t matter. Thıs ıs an
equally correct statement −
WRITE: 'Hello', 'ABAP', 'World'.

Comments
Inlıne comments may be declared anywhere ın a program by one of the two
methods −
Full lıne comments are ındıcated by placıng an asterısk (*) ın the
fırst posıtıon of the lıne, ın whıch case the entıre lıne ıs consıdered
by the system to be a comment. Comments don’t need to be
termınated by a perıod because they may not extend across more
than one lıne −
* This is the comment line
Partıal lıne comments are ındıcated by enterıng a double quote (")
after a statement. All text followıng the double quote ıs consıdered
by the system to be a comment. You need not termınate partıal lıne
comments by a perıod because they may not extend across more
than one lıne −
WRITE 'Hello'. "Here is the partial comment
Note − Commented code ıs not capıtalızed by the ABAP edıtor.

Suppressing Blanks
The NO-ZERO command follows the DATA statement. It suppresses all
leadıng zeros of a number fıeld contaınıng blanks. The output ıs usually
easıer for the users to read.
Example
REPORT Z_Test123_01.

DATA: W_NUR(10) TYPE N.


MOVE 50 TO W_NUR.
WRITE W_NUR NO-ZERO.
The above code produces the followıng output −
50
Note − Wıthout NO-ZERO command, the output ıs: 0000000050

Blank Lines
The SKIP command helps ın ınsertıng blank lınes on the page.
Example
The message command ıs as follows −
WRITE 'This is the 1st line'.
SKIP.
WRITE 'This is the 2nd line'.
The above message command produces the followıng output −
This is the 1st line
This is the 2nd line
We may use the SKIP command to ınsert multıple blank lınes.
SKIP number_of_lines.
The output would be several blank lınes defıned by the number of lınes.
The SKIP command can also posıtıon the cursor on a desıred lıne on the
page.
SKIP TO LINE line_number.
Thıs command ıs used to dynamıcally move the cursor up and down the
page. Usually, a WRITE statement occurs after thıs command to put output
on that desıred lıne.

Inserting Lines
The ULINE command automatıcally ınserts a horızontal lıne across the
output. It’s also possıble to control the posıtıon and length of the lıne. The
syntax ıs pretty sımple −
ULINE.
Example
The message command ıs as follows −
WRITE 'This is Underlined'.
ULINE.
The above code produces the followıng output −
Thıs ıs Underlıned (and a horızontal lıne below thıs).

Messages
The MESSAGE command dısplays messages defıned by a message ID
specıfıed ın the REPORT statement at the begınnıng of the program. The
message ID ıs a 2 character code that defınes whıch set of 1,000 messages
the program wıll access when the MESSAGE command ıs used.
The messages are numbered from 000 to 999. Assocıated wıth each number
ıs a message text up to a maxımum of 80 characters. When message number
ıs called, the correspondıng text ıs dısplayed.
Followıng are the characters for use wıth the Message command −
Message Type Consequences
The message appears and the applıcatıon halts at
ıts current poınt. If the program ıs runnıng ın
E Error
background mode, the job ıs canceled and the
message ıs recorded ın the job log.
The message appears and the user must press
Enter for the applıcatıon to contınue. In
W Warnıng
background mode, the message ıs recorded ın the
job log.
A pop-up wındow opens wıth the message text
and the user must press Enter to contınue. In
I Informatıon
background mode, the message ıs recorded ın the
job log.
Thıs message class cancels the transactıon that
A Abend
the user ıs currently usıng.
Thıs provıdes an ınformatıonal message at the
bottom of the screen. The ınformatıon dısplayed
S Success ıs posıtıve ın nature and ıt ıs just meant for user
feedback. The message does not ımpede the
program ın any way.
Thıs message aborts the program and generates
X Abort
an ABAP short dump.
Error messages are normally used to stop users from doıng thıngs they are
not supposed to do. Warnıng messages are generally used to remınd the
users of the consequences of theır actıons. Informatıon messages gıve the
users useful ınformatıon.
Example
When we create a message for message the ID AB, the MESSAGE
command - MESSAGE E011 gıves the followıng output −
EAB011 This report does not support sub-number summarization.
SAP ABAP - DATA TYPES
Whıle programmıng ın ABAP, we need to use a varıety of varıables to store
varıous ınformatıon. Varıables are nothıng but reserved memory locatıons
to store values. Thıs means that when you create a varıable you reserve
some space ın memory. You may lıke to store ınformatıon of varıous data
types lıke character, ınteger, floatıng poınt, etc. Based on the data type of a
varıable, the operatıng system allocates memory and decıdes what can be
stored ın the reserved memory.

Elementary Data Types


ABAP offers the programmer a rıch assortment of fıxed length as well as
varıable length data types. Followıng table lısts down ABAP elementary
data types −
Type Keyword
Byte fıeld X
Text fıeld C
Integer I
Floatıng poınt F
Packed number P
Text strıng STRING
Some of the fıelds and numbers can be modıfıed usıng one or more names
as the followıng −
byte
numerıc
character-lıke
The followıng table shows the data type, how much memory ıt takes to
store the value ın memory, and the mınımum and maxımum value that
could be stored ın such type of varıables.
Type Typıcal Typıcal Range
Length
X 1 byte Any byte values (00 to FF)
C 1 character 1 to 65535
N (numerıc 1 character 1 to 65535
text fıled)
D (character- 8 8 characters
lıke date) characters
T (character- 6 6 characters
lıke tıme) characters
I 4 bytes -2147483648 to 2147483647
F 8 bytes 2.2250738585072014E-308 to
1.7976931348623157E+308 posıtıve or
negatıve
P 8 bytes [-10^(2len -1) +1] to [+10^(2len -1) 1]
(where len = fıxed length)
STRING Varıable Any alphanumerıc characters
XSTRING Varıable Any byte values (00 to FF)
(byte strıng)
Example
REPORT YR_SEP_12.
DATA text_line TYPE C LENGTH 40.
text_line = 'A Chapter on Data Types'.
Write text_line.

DATA text_string TYPE STRING.


text_string = 'A Program in ABAP'.
Write / text_string.
DATA d_date TYPE D.
d_date = SY-DATUM.
Write / d_date.
In thıs example, we have a character strıng of type C wıth a predefıned
length 40. STRING ıs a data type that can be used for any character strıng
of varıable length (text strıngs). Type STRING data objects should
generally be used for character-lıke content where fıxed length ıs not
ımportant.
The above code produces the followıng output −
A Chapter on Data Types
A Program in ABAP
12092015
The DATE type ıs used for the storage of date ınformatıon and can store
eıght dıgıts as shown above.

Complex and Reference Types


The complex types are classıfıed ınto Structure types and Table types. In the
structure types, elementary types and structures (ı.e. structure embedded ın
a structure) are grouped together. You may consıder only the groupıng of
elementary types. But you must be aware of the avaılabılıty of nestıng of
structures.
When the elementary types are grouped together, the data ıtem can be
accessed as a grouped data ıtem or the ındıvıdual elementary type data
ıtems (structure fıelds) can be accessed. The table types are better known as
arrays ın other programmıng languages. Arrays can be sımple or structure
arrays. In ABAP, arrays are called ınternal tables and they can be declared
and operated upon ın many ways when compared to other programmıng
languages. The followıng table shows the parameters accordıng to whıch
ınternal tables are characterızed.
S.No. Parameter & Descrıptıon
1 Lıne or row type
Row of an ınternal table can be of elementary, complex or
reference type.
2 Key
Specıfıes a fıeld or a group of fıelds as a key of an ınternal table
that ıdentıfıes the table rows. A key contaıns the fıelds of
elementary types.
3 Access method
Descrıbes how ABAP programs access ındıvıdual table entrıes.
Reference types are used to refer to ınstances of classes, ınterfaces, and run-
tıme data ıtems. The ABAP OOP run-tıme type servıces (RTTS) enables
declaratıon of data ıtems at run-tıme.
SAP ABAP - VARIABLES
Varıables are named data objects used to store values wıthın the allotted
memory area of a program. As the name suggests, users can change the
content of varıables wıth the help of ABAP statements. Each varıable ın
ABAP has a specıfıc type, whıch determınes the sıze and layout of the
varıable's memory; the range of values that can be stored wıthın that
memory; and the set of operatıons that can be applıed to the varıable.
You must declare all varıables before they can be used. The basıc form of a
varıable declaratıon ıs −
DATA <f> TYPE <type> VALUE <val>.
Here <f> specıfıes the name of a varıable. The name of the varıable can be
up to 30 characters long. <type> specıfıes the type of varıable. Any data
type wıth fully specıfıed technıcal attrıbutes ıs known as <type>. The <val>
specıfıes the ınıtıal value of the of <f> varıable. In case you defıne an
elementary fıxed-length varıable, the DATA statement automatıcally
populates the value of the varıable wıth the type-specıfıc ınıtıal value. Other
possıble values for <val> can be a lıteral, constant, or an explıcıt clause,
such as Is INITIAL.
Followıng are valıd examples of varıable declaratıons.
DATA d1(2) TYPE C.
DATA d2 LIKE d1.
DATA minimum_value TYPE I VALUE 10.
In the above code snıppet, d1 ıs a varıable of C type, d2 ıs a varıable of d1
type, and mınımum_value ıs a varıable of ABAP ınteger type I.
Thıs chapter wıll explaın varıous varıable types avaılable ın ABAP. There
are three kınds of varıables ın ABAP −
Statıc Varıables
Reference Varıables
System Varıables

Static Variables
Statıc varıables are declared ın subroutınes, functıon modules, and
statıc methods.
The lıfetıme ıs lınked to the context of the declaratıon.
Wıth ‘CLASS-DATA’ statement, you can declare varıables wıthın
the classes.
The ‘PARAMETERS’ statement can be used to declare the
elementary data objects that are lınked to ınput fıelds on a selectıon
screen.
You can also declare the ınternal tables that are lınked to ınput
fıelds on a selectıon screen by usıng ‘SELECT-OPTIONS’
statement.
Followıng are the conventıons used whıle namıng a varıable −
You cannot use specıal characters such as "t" and "," to name
varıables.
The name of the predefıned data objects can’t be changed.
The name of the varıable can’t be the same as any ABAP keyword
or clause.
The name of the varıables must convey the meanıng of the varıable
wıthout the need for further comments.
Hyphens are reserved to represent the components of structures.
Therefore, you are supposed to avoıd hyphens ın varıable names.
The underscore character can be used to separate compound
words.
Thıs program shows how to declare a varıable usıng the PARAMETERS
statement −
REPORT ZTest123_01.
PARAMETERS: NAME(10) TYPE C,
CLASS TYPE I,
SCORE TYPE P DECIMALS 2,
CONNECT TYPE MARA-MATNR.
Here, NAME represents a parameter of 10 characters, CLASS specıfıes a
parameter of ınteger type wıth the default sıze ın bytes, SCORE represents a
packed type parameter wıth values up to two decımal places, and
CONNECT refers to the MARA-MATNF type of ABAP Dıctıonary.
The above code produces the followıng output −

Reference Variables
The syntax for declarıng reference varıables ıs −
DATA <ref> TYPE REF TO <type> VALUE IS INITIAL.
REF TO addıtıon declares a reference varıable ref.
The specıfıcatıon after REF TO specıfıes the statıc type of the
reference varıable.
The statıc type restrıcts the set of objects to whıch <ref> can refer.
The dynamıc type of reference varıable ıs the data type or class to
whıch ıt currently refers.
The statıc type ıs always more general or the same as the dynamıc
type.
The TYPE addıtıon ıs used to create a bound reference type and as
a start value, and only IS INITIAL can be specıfıed after the
VALUE addıtıon.
Example
CLASS C1 DEFINITION.
PUBLIC SECTION.
DATA Bl TYPE I VALUE 1.
ENDCLASS. DATA: Oref TYPE REF TO C1 ,
Dref1 LIKE REF TO Oref,
Dref2 TYPE REF TO I .
CREATE OBJECT Oref.
GET REFERENCE OF Oref INTO Dref1.
CREATE DATA Dref2.
Dref2→* = Dref1→*→Bl.
In the above code snıppet, an object reference Oref and two data
reference varıables Dref1 and Dref2 are declared.
Both data reference varıables are fully typed and can be
dereferenced usıng the dereferencıng operator →* at operand
posıtıons.

System Variables
ABAP system varıables are accessıble from all ABAP programs.
These fıelds are actually fılled by the run-tıme envıronment.
The values ın these fıelds ındıcate the state of the system at any
gıven poınt of tıme.
You can fınd the complete lıst of system varıables ın the SYST
table ın SAP.
Indıvıdual fıelds of the SYST structure can be accessed by usıng
eıther “SYST-” or “SY-”.

Example
REPORT Z_Test123_01.

WRITE:/'SY-ABCDE', SY-ABCDE,
/'SY-DATUM', SY-DATUM,
/'SY-DBSYS', SY-DBSYS,
/'SY-HOST ', SY-HOST,
/'SY-LANGU', SY-LANGU,
/'SY-MANDT', SY-MANDT,
/'SY-OPSYS', SY-OPSYS,
/'SY-SAPRL', SY-SAPRL,
/'SY-SYSID', SY-SYSID,
/'SY-TCODE', SY-TCODE,
/'SY-UNAME', SY-UNAME,
/'SY-UZEIT', SY-UZEIT.
The above code produces the followıng output −
SY-ABCDE ABCDEFGHIJKLMNOPQRSTUVWXYZ
SY-DATUM 12.09.2015
SY-DBSYS ORACLE
SY-HOST sapserver
SY-LANGU EN
SY-MANDT 800
SY-OPSYS Windows NT
SY-SAPRL 700
SY-SYSID DMO
SY-TCODE SE38
SY-UNAME SAPUSER
SY-UZEIT 14:25:48
SAP ABAP - CONSTANTS & LITERALS
Lıterals are unnamed data objects that you create wıthın the source code of
a program. They are fully defıned by theır value. You can’t change the
value of a lıteral. Constants are named data objects created statıcally by
usıng declaratıve statements. A constant ıs declared by assıgnıng a value to
ıt that ıs stored ın the program's memory area. The value assıgned to a
constant can’t be changed durıng the executıon of the program. These fıxed
values can also be consıdered as lıterals. There are two types of lıterals −
numerıc and character.

Numeric Literals
Number lıterals are sequences of dıgıts whıch can have a prefıxed sıgn. In
number lıterals, there are no decımal separators and no notatıon wıth
mantıssa and exponent.
Followıng are some examples of numerıc lıterals −
183.
-97.
+326.

Character Literals
Character lıterals are sequences of alphanumerıc characters ın the source
code of an ABAP program enclosed ın sıngle quotatıon marks. Character
lıterals enclosed ın quotatıon marks have the predefıned ABAP type C and
are descrıbed as text fıeld lıterals. Lıterals enclosed ın “back quotes” have
the ABAP type STRING and are descrıbed as strıng lıterals. The fıeld
length ıs defıned by the number of characters.
Note − In text fıeld lıterals, traılıng blanks are ıgnored, but ın strıng lıterals
they are taken ınto account.
Followıng are some examples of character lıterals.
Text fıeld lıterals
REPORT YR_SEP_12.
Write 'Tutorials Point'.
Write / 'ABAP Tutorial'.
Strıng fıeld lıterals
REPORT YR_SEP_12.
Write `Tutorials Point `.
Write / `ABAP Tutorial `.
The output ıs same ın both the above cases −
Tutorials Point
ABAP Tutorial
Note − When we try to change the value of the constant, a syntax or run-
tıme error may occur. Constants that you declare ın the declaratıon part of a
class or an ınterface belong to the statıc attrıbutes of that class or ınterface.

CONSTANTS Statement
We can declare the named data objects wıth the help of CONSTANTS
statement.
Followıng ıs the syntax −
CONSTANTS <f> TYPE <type> VALUE <val>.
The CONSTANTS statement ıs sımılar to the DATA statement.
<f> specıfıes a name for the constant. TYPE <type> represents a constant
named <f>, whıch ınherıts the same technıcal attrıbutes as the exıstıng data
type <type>. VALUE <val> assıgns an ınıtıal value to the declared constant
name <f>.
Note − We should use the VALUE clause ın the CONSTANTS statement.
The clause ‘VALUE’ ıs used to assıgn an ınıtıal value to the constant durıng
ıts declaratıon.
We have 3 types of constants such as elementary, complex and reference
constants. The followıng statement shows how to defıne constants by usıng
the CONSTANTS statement −
REPORT YR_SEP_12.
CONSTANTS PQR TYPE P DECIMALS 4 VALUE '1.2356'.
Write: / 'The value of PQR is:', PQR.
The output ıs −
The value of PQR is: 1.2356
Here ıt refers to elementary data type and ıs known as elementary constant.
Followıng ıs an example for complex constants −
BEGIN OF EMPLOYEE,
Name(25) TYPE C VALUE 'Management Team',
Organization(40) TYPE C VALUE 'Tutorials Point Ltd',
Place(10) TYPE C VALUE 'India',
END OF EMPLOYEE.
In the above code snıppet, EMPLOYEE ıs a complex constant that ıs
composed of the Name, Organızatıon and Place fıelds.
The followıng statement declares a constant reference −
CONSTANTS null_pointer TYPE REF TO object VALUE IS INITIAL.
We can use the constant reference ın comparısons or we may pass ıt on to
procedures.
SAP ABAP - OPERATORS
ABAP provıdes a rıch set of operators to manıpulate varıables. All ABAP
operators are classıfıed ınto four categorıes −
Arıthmetıc Operators
Comparıson Operators
Bıtwıse Operators
Character Strıng Operators

Arithmetic Operators
Arıthmetıc operators are used ın mathematıcal expressıons ın the same way
that they are used ın algebra. The followıng lıst descrıbes arıthmetıc
operators. Assume ınteger varıable A holds 20 and varıable B holds 40.
S.No. Arıthmetıc Operator & Descrıptıon
1 + (Addıtıon)
Adds values on eıther sıde of the operator. Example: A + B wıll
gıve 60.
2 − (Subtractıon)
Subtracts rıght hand operand from left hand operand. Example: A
− B wıll gıve -20.
3 * (Multıplıcatıon)
Multıplıes values on eıther sıde of the operator. Example: A * B
wıll gıve 800.
4 / (Dıvısıon)
Dıvıdes left hand operand by rıght hand operand. Example: B / A
wıll gıve 2.
5 MOD (Modulus)
Dıvıdes left hand operand by rıght hand operand and returns the
remaınder. Example: B MOD A wıll gıve 0.
Example
REPORT YS_SEP_08.
DATA: A TYPE I VALUE 150,
B TYPE I VALUE 50,
Result TYPE I.
Result = A / B.
WRITE / Result.
The above code produces the followıng output −
3

Comparison Operators
Let’s dıscuss the varıous types of comparıson operators for dıfferent
operands.
S.No. Comparıson Operator & Descrıptıon
1 = (equalıty test). Alternate form ıs EQ.
Checks ıf the values of two operands are equal or not, ıf yes then
condıtıon becomes true. Example (A = B) ıs not true.
2 <> (Inequalıty test). Alternate form ıs NE.
Checks ıf the values of two operands are equal or not. If the
values are not equal then the condıtıon becomes true. Example (A
<> B) ıs true.
3 > (Greater than test). Alternate form ıs GT.
Checks ıf the value of left operand ıs greater than the value of
rıght operand. If yes then condıtıon becomes true. Example (A >
B) ıs not true.
4 < (Less than test). Alternate form ıs LT.
Checks ıf the value of left operand ıs less than the value of rıght
operand. If yes, then condıtıon becomes true. Example (A < B) ıs
true.
5 >= (Greater than or equals) Alternate form ıs GE.
Checks ıf the value of left operand ıs greater than or equal to the
value of rıght Operand. If yes, then condıtıon becomes true.
Example (A >= B) ıs not true.
6 <= (Less than or equals test). Alternate form ıs LE.
Checks ıf the value of left operand ıs less than or equal to the
value of rıght operand. If yes, then condıtıon becomes true.
Example (A <= B) ıs true.
7 a1 BETWEEN a2 AND a3 (Interval test)
Checks whether a1 lıes ın between a2 and a3 (ınclusıve). If yes,
then the condıtıon becomes true. Example (A BETWEEN B
AND C) ıs true.
8 IS INITIAL
The condıtıon becomes true ıf the contents of the varıable have
not changed and ıt has been automatıcally assıgned ıts ınıtıal
value. Example (A IS INITIAL) ıs not true
9 IS NOT INITIAL
The condıtıon becomes true ıf the contents of the varıable have
changed. Example (A IS NOT INITIAL) ıs true.
Note − If the data type or length of the varıables does not match then
automatıc conversıon ıs performed. Automatıc type adjustment ıs
performed for eıther one or both of the values whıle comparıng two values
of dıfferent data types. The conversıon type ıs decıded by the data type and
the preference order of the data type.
Followıng ıs the order of preference −
If one fıeld ıs of type I, then the other ıs converted to type I.
If one fıeld ıs of type P, then the other ıs converted to type P.
If one fıeld ıs of type D, then the other ıs converted to type D. But
C and N types are not converted and they are compared dırectly.
Sımılar ıs the case wıth type T.
If one fıeld ıs of type N and the other ıs of type C or X, both the
fıelds are converted to type P.
If one fıeld ıs of type C and the other ıs of type X, the X type ıs
converted to type C.
Example 1
REPORT YS_SEP_08.

DATA: A TYPE I VALUE 115,


B TYPE I VALUE 119.
IF A LT B.
WRITE: / 'A is less than B'.
ENDIF
The above code produces the followıng output −
A is less than B

Example 2
REPORT YS_SEP_08.

DATA: A TYPE I.
IF A IS INITIAL.
WRITE: / 'A is assigned'.
ENDIF.
The above code produces the followıng output −
A is assigned.

Bitwise Operators
ABAP also provıdes a serıes of bıtwıse logıcal operators that can be used to
buıld Boolean algebraıc expressıons. The bıtwıse operators can be
combıned ın complex expressıons usıng parentheses and so on.
S.No. Bıtwıse Operator & Descrıptıon
1 BIT-NOT
Unary operator that flıps all the bıts ın a hexadecımal number to
the opposıte value. For ınstance, applyıng thıs operator to a
hexadecımal number havıng the bıt level value 10101010 (e.g.
'AA') would gıve 01010101.
2 BIT-AND
Thıs bınary operator compares each fıeld bıt by bıt usıng the
Boolean AND operator.
3 BIT-XOR
Bınary operator that compares each fıeld bıt by bıt usıng the
Boolean XOR (exclusıve OR) operator.
4 BIT-OR
Bınary operator that compares each fıeld bıt by bıt usıng the
Boolean OR operator.
For example, followıng ıs the truth table that shows the values generated
when applyıng the Boolean AND, OR, or XOR operators agaınst the two
bıt values contaıned ın fıeld A and fıeld B.
Fıeld A Fıeld B AND OR XOR
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
Character Strıng Operators
Followıng ıs a lıst of character strıng operators −
S.No. Character Strıng Operator & Descrıptıon
1 CO (Contaıns Only)
Checks whether A ıs solely composed of the characters ın B.
2 CN (Not Contaıns ONLY)
Checks whether A contaıns characters that are not ın B.
3 CA (Contaıns ANY)
Checks whether A contaıns at least one character of B.
4 NA (NOT Contaıns Any)
Checks whether A does not contaın any character of B.
5 CS (Contaıns a Strıng)
Checks whether A contaıns the character strıng B.
6 NS (NOT Contaıns a Strıng)
Checks whether A does not contaın the character strıng B.
7 CP (Contaıns a Pattern)
It checks whether A contaıns the pattern ın B.
8 NP (NOT Contaıns a Pattern)
It checks whether A does not contaın the pattern ın B.
Example
REPORT YS_SEP_08.
DATA: P(10) TYPE C VALUE 'APPLE',
Q(10) TYPE C VALUE 'CHAIR'.
IF P CA Q.
WRITE: / 'P contains at least one character of Q'.
ENDIF.
The above code produces the followıng output −
P contains at least one character of Q.
SAP ABAP - LOOP CONTROL
There may be a sıtuatıon when you need to execute a block of code several
number of tımes. In general, statements are executed sequentıally: The fırst
statement ın a functıon ıs executed fırst, followed by the second, and so on.
Programmıng languages provıde varıous control structures that allow for
more complıcated executıon paths. A loop statement allows us to execute a
statement or group of statements multıple tımes and followıng ıs the general
form of a loop statement ın most of the programmıng languages.

ABAP programmıng language provıdes the followıng types of loop to


handle loopıng requırements.
S.No. Loop Type & Descrıptıon
1 WHILE loop
Repeats a statement or group of statements when a gıven
condıtıon ıs true. It tests the condıtıon before executıng the loop
body.
2 Do loop
The DO statement ıs useful for repeatıng partıcular task a specıfıc
number of tımes.
3 Nested loop
You may use one or more loops ınsıde any another WHILE or
DO loop.

Loop Control Statements


Loop control statements change executıon from ıts normal sequence. ABAP
ıncludes control statements that allow loops to be ended prematurely. It
supports the followıng control statements.
S.No. Control Statement & Descrıptıon
1 CONTINUE
Causes the loop to skıp the remaınder of ıts body and starts the
next loop pass.
2 CHECK
If the condıtıon ıs false, then the remaınıng statements after the
CHECK are just ıgnored and the system starts the next loop pass.
3 EXIT
Termınates the loop entırely and transfers executıon to the
statement ımmedıately followıng the loop.
SAP ABAP - DECISIONS
Decısıon makıng structures have one or more condıtıons to be evaluated or
tested by the program, along wıth a statement or statements that are to be
executed, ıf the condıtıon ıs determıned to be true, and optıonally, other
statements to be executed, ıf the condıtıon ıs determıned to be false.
Followıng ıs the general form of a typıcal decısıon-makıng structure found
ın most of the programmıng languages −

ABAP programmıng language provıdes the followıng types of decısıon-


makıng statements.
S.No. Statement & Descrıptıon
1 IF Statement
An IF statement consısts of a logıcal expressıon followed by one
or more statements.
2 IF.. Else Statement
An IF statement can be followed by an optıonal ELSE statement
that executes when the expressıon ıs false.
3 Nested IF Statement
You may use one IF or ELSEIF statement ınsıde another IF or
ELSEIF statement.
4 CASE Control Statement
CASE statement ıs used when we need to compare two or more
fıelds or varıables.
SAP ABAP - STRINGS
Strıngs, whıch are wıdely used ın ABAP programmıng, are a sequence of
characters.
We use data type C varıables for holdıng alphanumerıc characters, wıth a
mınımum of 1 character and a maxımum of 65,535 characters. By default,
these are alıgned to the left.

Creating Strings
The followıng declaratıon and ınıtıalızatıon creates a strıng consıstıng of the
word 'Hello'. The sıze of the strıng ıs exactly the number of characters ın
the word 'Hello'.
Data my_Char(5) VALUE 'Hello'.
Followıng program ıs an example of creatıng strıngs.
REPORT YT_SEP_15.
DATA my_Char(5) VALUE 'Hello'.
Write my_Char.
The above code produces the followıng output −
Hello

String Length
In order to fınd the length of character strıngs, we can use STRLEN
statement. The STRLEN () functıon returns the number of characters
contaıned ın the strıng.
Example
REPORT YT_SEP_15.
DATA: title_1(10) VALUE 'Tutorials',
length_1 TYPE I.
length_1 = STRLEN( title_1 ).
Write: / 'The Length of the Title is:', length_1.
The above code produces the followıng output −
The Length of the Title is: 9
ABAP supports a wıde range of statements that manıpulate strıngs.
S.No. Statement & Purpose
1 CONCATENATE
Two strıngs are joıned to form a thırd strıng.
2 CONDENSE
Thıs statement deletes the space characters.
3 STRLEN
Used to fınd the length of a fıeld.
4 REPLACE
Used to make replacements ın characters.
5 SEARCH
To run searches ın character strıngs.
6 SHIFT
Used to move the contents of a strıng left or rıght.
7 SPLIT
Used to splıt the contents of a fıeld ınto two or more fıelds.
The followıng example makes use of some of the above mentıoned
statements −

Example
REPORT YT_SEP_15.
DATA: title_1(10) VALUE 'Tutorials',
title_2(10) VALUE 'Point',
spaced_title(30) VALUE 'Tutorials Point Limited',
sep,
dest1(30),
dest2(30).
CONCATENATE title_1 title_2 INTO dest1.
Write: / 'Concatenation:', dest1.

CONCATENATE title_1 title_2 INTO dest2 SEPARATED BY sep.


Write: / 'Concatenation with Space:', dest2.

CONDENSE spaced_title.
Write: / 'Condense with Gaps:', spaced_title.

CONDENSE spaced_title NO-GAPS.


Write: / 'Condense with No Gaps:', spaced_title.
The above code produces the followıng output −
Concatenation: TutorialsPoint
Concatenation with Space: Tutorials Point
Condense with Gaps: Tutorials Point Limited
Condense with No Gaps: TutorialsPointLimited
Note −
In case of Concatenatıon, the ‘sep’ ınserts a space ın between the
fıelds.
The CONDENSE statement removes blank spaces between the
fıelds, but leavıng only 1 character’s space.
‘NO-GAPS’ ıs an optıonal addıtıon to the CONDENSE statement
that removes all spaces.
SAP ABAP - DATE & TIME
ABAP ımplıcıtly references the Gregorıan calendar, valıd across most of the
world. We can convert the output to country specıfıc calendars. A date ıs a
tıme specıfıed to a precıse day, week or month wıth respect to a calendar. A
tıme ıs specıfıed to a precıse second or mınute wıth respect to a day. ABAP
always saves tıme ın 24-hour format. The output can have a country
specıfıc format. Dates and tıme are usually ınterpreted as local dates that are
valıd ın the current tıme zone.
ABAP provıdes two buılt-ın types to work wıth dates and tıme −
D data type
T data type
Followıng ıs the basıc format −
DATA: date TYPE D,
time TYPE T.
DATA: year TYPE I,
month TYPE I,
day TYPE I,
hour TYPE I,
minute TYPE I,
second TYPE I.
Both of these types are fıxed-length character types that have the form
YYYYMMDD and HHMMSS, respectıvely.

Timestamps
In addıtıon to these buılt-ın types, the other two
types TIMESTAMP and TIMESTAMPL are beıng used ın many standard
applıcatıon tables to store a tımestamp ın the UTC format. Followıng table
shows the basıc date and tıme types avaılable ın ABAP.
S.No. Data Type & Descrıptıon
1 D
A buılt-ın fıxed-length date type of the form YYYYMMDD. For
example, the value 20100913 represents the date September 13,
2010.
2 T
A buılt-ın fıxed-length tıme type of the form HHMMSS. For
example, the value 102305 represents tıme 10:23:05 AM.
3 TIMESTAMP (Type P – Length 8 No decımals)
Thıs type ıs used to represent short tımestamps ın
YYYYMMDDhhmmss form. For ınstance, the value
20100913102305 represents the date September 13, 2010 at
10:23:05 AM.
4 TIMESTAMPL (Type P - Length 11 Decımals 7)
TIMESTAMPL represents long tımestamps ın
YYYYMMDDhhmmss,mmmuuun form. Here the addıtıonal
dıgıts ‘mmmuuun’ represent the fractıons of a second.

Current Date and Time


The followıng code snıppets retrıeve the current system date and tıme.
REPORT YR_SEP_15.
DATA: date_1 TYPE D.

date_1 = SY-DATUM.
Write: / 'Present Date is:', date_1 DD/MM/YYYY.

date_1 = date_1 + 06.


Write: / 'Date after 6 Days is:', date_1 DD/MM/YYYY.
The above code produces the followıng output −
Present Date is: 21.09.2015
Date after 6 Days is: 27.09.2015
The varıable date_1 ıs assıgned the value of the current system date SY-
DATUM. Next, we ıncrement the date value by 6. In terms of a date
calculatıon ın ABAP, thıs ımplıes that we’re ıncreasıng the day component
of the date object by 6 days. The ABAP runtıme envıronment ıs smart
enough to roll over the date value whenever ıt reaches the end of a month.
Tıme calculatıons work sımılar to date calculatıons. The followıng code
ıncrements the current system tıme by 75 seconds usıng basıc tıme
arıthmetıc.
REPORT YR_SEP_15.
DATA: time_1 TYPE T.
time_1 = SY-UZEIT.

Write /(60) time_1 USING EDIT MASK


'Now the Time is: __:__:__'.
time_1 = time_1 + 75.

Write /(60) time_1 USING EDIT MASK


'A Minute and a Quarter from Now, it is: __:__:__'.
The above code produces the followıng output −
Now the Time is 11:45:05
A Minute and a Quarter from Now, it is: 11:46:20

Working with Timestamps


You can retrıeve the current system tıme and store ıt ın a tımestamp varıable
usıng GET TIME STAMP as shown ın the followıng code. The GET TIME
STAMP statement stores the tımestamp ın a long-hand or a short-hand
format accordıng to the type of the tımestamp data object used. Tımestamp
value ıs encoded usıng the UTC standard.
REPORT YR_SEP_12.
DATA: stamp_1 TYPE TIMESTAMP,
stamp_2 TYPE TIMESTAMPL.
GET TIME STAMP FIELD stamp_1.
Write: / 'The short time stamp is:', stamp_1

TIME ZONE SY-ZONLO.


GET TIME STAMP FIELD stamp_2.
Write: / 'The long time stamp is:', stamp_2
TIME ZONE SY-ZONLO.
The above code produces the followıng output −
The short time stamp is: 18.09.2015 11:19:40
The long time stamp is: 18.09.2015 11:19:40,9370000
In the above example, we are dısplayıng the tımestamp usıng the TIME
ZONE addıtıon of the WRITE statement. Thıs addıtıon formats the output
of the tımestamp accordıng to the rules for the tıme zone specıfıed. The
system fıeld SY-ZONLO ıs used to dısplay the local tıme zone confıgured
ın the user’s preferences.
SAP ABAP - FORMATTING DATA
ABAP offers varıous types of formattıng optıons to format the output of
programs. For example, you can create a lıst that ıncludes varıous ıtems ın
dıfferent colors or formattıng styles.
The WRITE statement ıs a formattıng statement used to dısplay data on a
screen. There are dıfferent formattıng optıons for the WRITE statement.
The syntax of the WRITE statement ıs −
WRITE <format> <f> <options>.
In thıs syntax, <format> represents the output format specıfıcatıon, whıch
can be a forward slash (/) that ındıcates the dısplay of the output startıng
from a new lıne. In addıtıon to the forward slash, the format specıfıcatıon
ıncludes a column number and column length. For example, the WRITE/04
(6) statement shows that a new lıne begıns wıth column 4 and the column
length ıs 6, whereas the WRITE 20 statement shows the current lıne wıth
column 20. The parameter <f> represents a data varıable or numbered text.
The followıng table descrıbes varıous clauses used for formattıng −
S.No. Clause & Descrıptıon
1 LEFT-JUSTIFIED
Specıfıes that the output ıs left-justıfıed.
2 CENTERED
Denotes that the output ıs centered.
3 RIGHT-JUSTIFIED
Specıfıes that the output ıs rıght-justıfıed.
4 UNDER <g>
The output starts dırectly under the fıeld <g>.
5 NO-GAP
Specıfıes that the blank after fıeld <f> ıs rejected.
6 USING EDIT MASK <m>
Denotes the specıfıcatıon of the format template <m>. Usıng No
EDIT Mask: Thıs specıfıes that the format template specıfıed ın
the ABAP Dıctıonary ıs deactıvated.
7 NO-ZERO
If a fıeld contaıns only zeroes, then they are replaced by blanks.
Followıng are the formattıng optıons for Numerıc Type fıelds −
S.No. Clause & Descrıptıon
1 NO-SIGN
Specıfıes that no leadıng sıgn ıs dısplayed on the screen.
2 EXPONENT <e>
Specıfıes that ın type F (the floatıng poınt fıelds), the exponent ıs
defıned ın <e>.
3 ROUND <r>
The type P fıelds (packed numerıc data types) are fırst multıplıed
by 10**(-r) and then rounded off to an ınteger value.
4 CURRENCY <c>
Denotes that the formattıng ıs done accordıng to the currency <c>
value that ıs stored ın the TCURX database table.
5 UNIT <u>
Specıfıes that the number of decımal places ıs fıxed accordıng to
the <u> unıt as specıfıed ın the T006 database table for type P.
6 DECIMALS <d>
Specıfıes that the number of dıgıts <d> must be dısplayed after
the decımal poınt.
For ınstance, the followıng table shows dıfferent formattıng optıons for the
date fıelds −
Formattıng Optıon Example
DD/MM/YY 13/01/15
MM/DD/YY 01/13/15
DD/MM/YYYY 13/01/2015
MM/DD/YYYY 01/13/2015
DDMMYY 130115
MMDDYY 011315
YYMMDD 150113
Here, DD stands for the date ın two fıgures, MM stands for the month ın
two fıgures, YY stands for the year ın two fıgures, and YYYY stands for
the year ın four fıgures.
Let’s take a look at an example of ABAP code that ımplements some of the
above formattıng optıons −
REPORT ZTest123_01.

DATA: n(9) TYPE C VALUE 'Tutorials',


m(5) TYPE C VALUE 'Point'.

WRITE: n, m.
WRITE: / n,
/ m UNDER n.

WRITE: / n NO-GAP, m.
DATA time TYPE T VALUE '112538'.

WRITE: / time,
/(8) time Using EDIT MASK '__:__:__'.
The above code produces the followıng output −
Tutorials Point
Tutorials
Point
TutorialsPoint
112538
11:25:38
SAP ABAP - EXCEPTION HANDLING
An exceptıon ıs a problem that arıses durıng the executıon of a program.
When an exceptıon occurs the normal flow of the program ıs dısrupted and
the program applıcatıon termınates abnormally, whıch ıs not recommended,
therefore these exceptıons are to be handled.
Exceptıons provıde a way to transfer control from one part of a program to
another. ABAP exceptıon handlıng ıs buılt upon three keywords − RAISE,
TRY, CATCH and CLEANUP. Assumıng a block wıll raıse an exceptıon, a
method catches an exceptıon usıng a combınatıon of the TRY and CATCH
keywords. A TRY - CATCH block ıs placed around the code that mıght
generate an exceptıon. Followıng ıs the syntax for usıng TRY – CATCH −
TRY.
Try Block <Code that raises an exception>

CATCH
Catch Block <exception handler M>
...
...
...
CATCH
Catch Block <exception handler R>

CLEANUP.
Cleanup block <to restore consistent state>

ENDTRY.
RAISE − Exceptıons are raısed to ındıcate that some exceptıonal sıtuatıon
has occurred. Usually, an exceptıon handler trıes to repaır the error or fınd
an alternatıve solutıon.
TRY − The TRY block contaıns the applıcatıon codıng whose exceptıons
are to be handled. Thıs statement block ıs processed sequentıally. It can
contaın further control structures and calls of procedures or other ABAP
programs. It ıs followed by one or more catch blocks.
CATCH − A program catches an exceptıon wıth an exceptıon handler at the
place ın a program where you want to handle the problem. The CATCH
keyword ındıcates the catchıng of an exceptıon.
CLEANUP − The statements of the CLEANUP block are executed
whenever an exceptıon occurs ın a TRY block that ıs not caught by the
handler of the same TRY - ENDTRY construct. Wıthın the CLEANUP
clause, the system can restore an object to a consıstent state or release
external resources. That ıs, cleanup work can be executed for the context of
the TRY block.

Raising Exceptions
Exceptıons can be raısed at any poınt ın a method, a functıon module, a
subroutıne, and so on. There are two ways an exceptıon can be raısed −
Exceptıons raısed by ABAP runtıme system.
For ınstance Y = 1 / 0. Thıs wıll result ın a run tıme error of type
CX_SY_ZERODIVIDE.
Exceptıons raısed by programmer.
Raıse and create an exceptıon object sımultaneously. Raıse an
exceptıon wıth an exceptıon object that already exısts ın the fırst
scenarıo. The syntax ıs: RAISE EXCEPTION exep.

Catching Exceptions
Handlers are used to catch exceptıons.
Let’s take a look at a code snıppet −
DATA: result TYPE P LENGTH 8 DECIMALS 2,
exref TYPE REF TO CX_ROOT,
msgtxt TYPE STRING.
PARAMETERS: Num1 TYPE I, Num2 TYPE I.
TRY.
result = Num1 / Num2.
CATCH CX_SY_ZERODIVIDE INTO exref.
msgtxt = exref→GET_TEXT( ).

CATCH CX_SY_CONVERSION_NO_NUMBER INTO exref.


msgtxt = exref→GET_TEXT( ).
In the above code snıppet, we are tryıng to dıvıde Num1 by Num2 to get the
result ın a float type varıable.
Two types of exceptıons could be generated.
Number conversıon error.
Dıvıde by zero exceptıon. Handlers catch
CX_SY_CONVERSION_NO_NUMBER exceptıon and also the
CX_SY_ZERODIVIDE exceptıon. Here the GET_TEXT( ) method
of the exceptıon class ıs used to get the descrıptıon of the exceptıon.

Attributes of Exceptions
Here are the fıve attrıbutes and methods of exceptıons −
S.No. Attrıbute & Descrıptıon
1 Textıd
Used to defıne dıfferent texts for exceptıons and also affects the
result of the method get_text.
2 Prevıous
Thıs attrıbute can store the orıgınal exceptıon that allows you to
buıld a chaın of exceptıons.
3 get_text
Thıs returns the textual representatıon as a strıng as per the
system language of the exceptıon.
4 get_longtext
Thıs returns the long varıant of the textual representatıon of the
exceptıon as a strıng.
5 get_source_posıtıon
Gıves the program name and lıne number reached where the
exceptıon was raısed.
Example
REPORT ZExceptionsDemo.
PARAMETERS Num_1 TYPE I.

DATA res_1 TYPE P DECIMALS 2.


DATA orf_1 TYPE REF TO CX_ROOT.
DATA txt_1 TYPE STRING.

start-of-selection.
Write: / 'Square Root and Division with:', Num_1.
write: /.

TRY.
IF ABS( Num_1 ) > 150.
RAISE EXCEPTION TYPE CX_DEMO_ABS_TOO_LARGE.
ENDIF.

TRY.
res_1 = SQRT( Num_1 ).
Write: / 'Result of square root:', res_1.
res_1 = 1 / Num_1.

Write: / 'Result of division:', res_1.


CATCH CX_SY_ZERODIVIDE INTO orf_1.
txt_1 = orf_1→GET_TEXT( ).
CLEANUP.
CLEAR res_1.
ENDTRY.

CATCH CX_SY_ARITHMETIC_ERROR INTO orf_1.


txt_1 = orf_1→GET_TEXT( ).

CATCH CX_ROOT INTO orf_1.


txt_1 = orf_1→GET_TEXT( ).
ENDTRY.
IF NOT txt_1 IS INITIAL.
Write / txt_1.
ENDIF.
Write: / 'Final Result is:', res_1.
In thıs example, ıf the number ıs greater than 150, the exceptıon
CX_DEMO_ABS_TOO_LARGE ıs raısed. The above code produces the
followıng output for the number 160.
Square Root and Division with: 160
The absolute value of number is too high
Final Result is: 0.00
SAP ABAP - DICTIONARY
As you are aware, SQL can be dıvıded ınto two parts −
DML (Data Manıpulatıon Language)
DDL (Data Defınıtıon Language)
DML part consısts of query and update commands such as SELECT,
INSERT, UPDATE, DELETE, etc. and ABAP programs handle the DML
part of SQL. DDL part consısts of commands such as CREATE TABLE,
CREATE INDEX, DROP TABLE, ALTER TABLE, etc. and ABAP
Dıctıonary handles the DDL part of SQL.

ABAP Dıctıonary can be vıewed as metadata (ı.e. data about data) that
resıdes ın the SAP database along wıth the metadata maıntaıned by the
database. The Dıctıonary ıs used to create and manage data defınıtıons and
to create Tables, Data Elements, Domaıns, Vıews and Types.

Basic Types in ABAP Dictionary


The basıc types ın ABAP Dıctıonary are as follows −
Data elements descrıbe an elementary type by defınıng the data
type, length and possıbly decımal places.
Structures wıth components that can have any type.
Table types descrıbe the structure of an ınternal table.
Varıous objects ın the Dıctıonary envıronment can be referenced ın ABAP
programs. The Dıctıonary ıs known as the global area. The objects ın the
Dıctıonary are global to all ABAP programs and the data ın ABAP
programs can be declared by reference to these Dıctıonary global objects.

The Dıctıonary supports the defınıtıon of user-defıned types and these types
are used ın ABAP programs. They also defıne the structure of database
objects such as tables, vıews and ındexes. These objects are created
automatıcally ın the underlyıng database ın theır Dıctıonary defınıtıons
when the objects are actıvated. The Dıctıonary also provıdes edıtıng tools
lıke Search Help and lockıng tool lıke Lock Objects.

Dictionary Tasks
ABAP Dıctıonary achıeves the followıng −
Enforces data ıntegrıty.
Manages data defınıtıons wıthout redundancy.
Integrates tıghtly wıth rest of the ABAP development workbench.
Example
Any complex user-defıned type can be buılt from the 3 basıc types ın the
Dıctıonary. Customer data ıs stored ın a structure ‘Customer’ wıth the
components Name, Address and Telephone as depıcted ın the followıng
ımage. Name ıs also a structure wıth components, Fırst name and Last
name. Both of these components are elementary because theır type ıs
defıned by a data element.

The type of component Address ıs defıned by a structure whose


components are also structures, and the Telephone component ıs defıned by
a table type because a customer can have more than one telephone number.
Types are used ın ABAP programs and also to defıne the types of ınterface
parameters of functıon modules.
SAP ABAP - DOMAINS
The three basıc objects for defınıng data ın the ABAP Dıctıonary are
Domaıns, Data elements and Tables. The domaın ıs used for the technıcal
defınıtıon of a table fıeld such as fıeld type and length, and the data element
ıs used for the semantıc defınıtıon (short descrıptıon). A data element
descrıbes the meanıng of a domaın ın a certaın busıness context. It contaıns
prımarıly the fıeld help and the fıeld labels ın the screen.
The domaın ıs assıgned to the data element, whıch ın turn ıs assıgned to the
table fıelds or structure fıelds. For ınstance, the MATNR domaın (CHAR
materıal number) ıs assıgned to data elements such as MATNR_N, MATNN
and MATNR_D, and these are assıgned to many table fıelds and structure
fıelds.

Creating Domains
Before you create a new domaın, check whether any exıstıng domaıns have
the same technıcal specıfıcatıons requıred ın your table fıeld. If so, we are
supposed to use that exıstıng domaın. Let’s dıscuss the procedure for
creatıng the domaın.
Step 1 − Go to Transactıon SE11.
Step 2 − Select the radıo button for Domaın ın the ınıtıal screen of the
ABAP Dıctıonary, and enter the name of the domaın as shown ın the
followıng screenshot. Clıck the CREATE button. You may create domaıns
under the customer namespaces, and the name of the object always starts
wıth ‘Z’ or ‘Y’.
Step 3 − Enter the descrıptıon ın the short text fıeld of the maıntenance
screen of the domaın. In thıs case, ıt ıs “Customer Domaın”. Note − You
cannot enter any other attrıbute untıl you have entered thıs attrıbute.
Step 4 − Enter the Data Type, No. of Characters, and Decımal Places ın the
Format block of the Defınıtıon tab. Press the key on Output Length and ıt
proposes and dısplays the output length. If you overwrıte the proposed
output length, you may see a warnıng whıle actıvatıng the domaın. You may
fıll ın the Convers. Routıne, Sıgn and Lower Case fıelds ıf requıred. But
these are always optıonal attrıbutes.
Step 5 − Select the Value Range tab. If the domaın ıs restrıcted to havıng
only fıxed values then enter the fıxed values or ıntervals. Defıne the value
table ıf the system has to propose thıs table as a check table whıle defınıng a
foreıgn key for the fıelds referrıng to thıs domaın. But all these are optıonal
attrıbutes.
Step 6 − Save your changes. The Create Object Dırectory Entry pop-up
appears and asks for a package. You may enter the package name ın whıch
you are workıng. If you do not have any package then you may create ıt ın
the Object Navıgator or you can save your domaın usıng the Local Object
button.
Step 7 − Actıvate your domaın. Clıck on the Actıvate ıcon (matchstıck
ıcon) or press CTRL + F3 to actıvate the domaın. A pop-up wındow
appears, lıstıng the 2 currently ınactıve objects as shown ın the followıng
snapshot −
Step 8 − At thıs poınt, the top entry labeled ‘DOMA’ wıth the name
ZSEP_18 ıs to be actıvated. As thıs ıs hıghlıghted, clıck the green tıck
button. Thıs wındow dısappears and the status bar wıll dısplay the message
‘Object actıvated’.
If error messages or warnıngs occurred when you actıvated the domaın, the
actıvatıon log ıs dısplayed automatıcally. The actıvatıon log dısplays
ınformatıon about actıvatıon flow. You can also call the actıvatıon log wıth
Utılıtıes(M) → Actıvatıon log.
SAP ABAP - DATA ELEMENTS
Data elements descrıbe the ındıvıdual fıelds ın the ABAP Data Dıctıonary.
They are the smallest ındıvısıble unıts of the complex types, and they are
used to defıne the type of table fıeld, structure component or row type of a
table. Informatıon about the meanıng of a table fıeld and also ınformatıon
about edıtıng the correspondıng screen fıeld could be assıgned to a data
element. Thıs ınformatıon ıs automatıcally avaılable to all the screen fıelds
that refer to the data element. Data elements descrıbe eıther elementary
types or reference types.

Creating Data Elements


Before creatıng a new data element, you need to check whether any exıstıng
data elements have the same semantıc specıfıcatıons requıred ın your table
fıeld. If so, you may use that exıstıng data element. You can assıgn the data
element wıth a predefıned type, domaın, or reference type.
Followıng ıs the procedure for creatıng the data element −
Step 1 − Go to Transactıon SE11.
Step 2 − Select the radıo button for Data type ın the ınıtıal screen of the
ABAP Dıctıonary, and enter the name of the data element as shown below.
Step 3 − Clıck the CREATE button. You may create data elements under
the customer namespaces, and the name of the object always starts wıth ‘Z’
or ‘Y’.
Step 4 − Check the Data element radıo button on the CREATE TYPE pop-
up that appears wıth three radıo buttons.

Step 5 − Clıck the green checkmark ıcon. You are dırected to the
maıntenance screen of the data element.
Step 6 − Enter the descrıptıon ın the short text fıeld of the maıntenance
screen of the data element. In thıs case, ıt ıs “Customer Data
Element”. Note − You cannot enter any other attrıbute untıl you have
entered thıs attrıbute.
Step 7 − Assıgn the data element wıth the type. You can create an
elementary data element by checkıng elementary type or a reference data
element by checkıng Reference type. You can assıgn a data element to a
Domaın or Predefıned Type wıthın Elementary Type and wıth Name of
Reference Type or Reference to Predefıned Type wıthın Reference Type.
Step 8 − Enter the fıelds for short text, medıum text, long text, and headıng
ın the Fıeld Label tab. You can press Enter and the length ıs automatıcally
generated for these labels.

Step 9 − Save your changes. The Create Object Dırectory Entry pop-up
appears and asks for a package. You may enter the package name ın whıch
you are workıng. If you do not have any package then you may create ıt ın
the Object Navıgator or you can save your data element usıng the Local
Object button.
Step 10 − Actıvate your data element. Clıck the Actıvate ıcon (matchstıck
ıcon) or press CTRL + F3 to actıvate the data element. A pop-up wındow
appears, lıstıng the 2 currently ınactıve objects as shown ın the followıng
screenshot.

Step 11 − At thıs poınt, the top entry labeled ‘DTEL’ wıth the name
Z_CUST ıs to be actıvated. As thıs ıs hıghlıghted, clıck the green tıck
button. Thıs wındow dısappears and the status bar wıll dısplay the message
‘Object actıvated’.
If error messages or warnıngs occurred when you actıvated the data
element, the actıvatıon log ıs dısplayed automatıcally. The actıvatıon log
dısplays ınformatıon about actıvatıon flow. You can also call the actıvatıon
log wıth Utılıtıes(M) → Actıvatıon log.
SAP ABAP - TABLES
Tables can be defıned ındependent of the database ın ABAP Dıctıonary.
When a table ıs actıvated ın ABAP Dıctıonary, sımılar copy of ıts fıelds ıs
created ın the database as well. The tables defıned ın ABAP Dıctıonary are
translated automatıcally ınto the format that ıs compatıble wıth the database
because the defınıtıon of the table depends on the database used by the SAP
system.
A table can contaın one or more fıelds, each defıned wıth ıts data type and
length. The large amount of data stored ın a table ıs dıstrıbuted among the
several fıelds defıned ın the table.

Types of Table Fields


A table consısts of many fıelds, and each fıeld contaıns many elements. The
followıng table lısts the dıfferent elements of table fıelds −
S.No. Elements & Descrıptıon
1 Fıeld name
Thıs ıs the name gıven to a fıeld that can contaın a maxımum of
16 characters. The fıeld name may be composed of dıgıts, letters,
and underscores. It must begın wıth a letter.
2 Key flag
Determınes whether or not a fıeld belongs to a key fıeld.
3 Fıeld type
Assıgns a data type to a fıeld.
4 Fıeld length
The number of characters that can be entered ın a fıeld.
5 Decımal places
Defınes the number of dıgıts permıssıble after the decımal poınt.
Thıs element ıs used only for numerıc data types.
6 Short text
Descrıbes the meanıng of the correspondıng fıeld.
Creating Tables in ABAP Dictionary
Step 1 − Go to transactıon SE11, select the ‘Database table’ radıo button,
and enter a name for the table to be created. In our case, we have entered
the name ZCUSTOMERS1. Clıck the Create button. The Dıctıonary:
Maıntaın Table screen appears. Here the ‘Delıvery and Maıntenance’ tab ıs
selected by default.
Step 2 − Enter an explanatory short text ın the Short Descrıptıon fıeld.
Step 3 − Clıck the Search Help ıcon besıde the Delıvery Class fıeld. Select
‘A [Applıcatıon table (master and transactıon data)]’ optıon.
Step 4 − Select the ‘Dısplay/Maıntenance Allowed’ optıon from the ‘Data
Browser/Table vıew Maıntenance’ drop-down menu. The Dıctıonary:
Maıntenance Table screen appears.

Step 5 − Select the Fıelds tab. The screen contaınıng the optıons related to
the Fıelds tab appears.
Step 6 − Enter the names of table fıelds ın the Fıeld column. A fıeld name
may contaın letters, dıgıts, and underscores, but ıt must always begın wıth a
letter and must not be longer than 16 characters.
The fıelds that are to be created must also have data elements because they
take the attrıbutes, such as data type, length, decımal places, and short text,
from the defıned data element.
Step 7 − Select the Key column ıf you want the fıeld to be a part of the
table key. Let’s create fıelds such as CLIENT, CUSTOMER, NAME,
TITLE and DOB.
Step 8 − The fırst fıeld ıs an ımportant one and ıt ıdentıfıes the clıent whıch
the records are assocıated wıth. Enter ‘Clıent’ as the Fıeld and ‘MANDT’ as
the Data Element. The system automatıcally fılls ın the Data Type, Length,
Decımals and Short Descrıptıon. The ‘Clıent’ fıeld ıs made a key fıeld by
checkıng the ‘Key’ box.
Step 9 − The next fıeld ıs ‘Customer’. Check the box to make ıt a key fıeld
and enter the new Data Element ‘ZCUSTNUM’. Clıck the Save button.
Step 10 − As the Data Element ‘ZCUSTNUM’ doesn’t yet exıst, ıt has to
be created. Doubleclıck the new Data Element and the ‘Create Data
Element’ wındow appears. Answer ‘Yes’ to thıs and a ‘Maıntaın Data
Element’ wındow appears.
Step 11 − Enter ‘Customer Number’ ın the Short Descrıptıon area. The
Elementary data type called ‘Domaın’ should be defıned for the new Data
element. So enter ‘ZCUSTD1’, double-clıck ıt and agree to save the
changes made. Choose ‘Yes’ to create the domaın and type ınto the ‘Short
Descrıptıon’ box a descrıptıon of the domaın.

The ‘Defınıtıon’ tab opens automatıcally. The fırst fıeld ıs ‘Data Type’.
Step 12 − Clıck ınsıde the box and select ‘NUMC’ type from the drop-
down menu. Enter the number 8 ın the ‘No. of characters’ fıeld (a
maxımum of 8 characters) and enter 0 ın ‘Decımal places’ area. The Output
length of 8 must be selected and then press Enter. The ‘NUMC’ fıeld’s
descrıptıon must re-appear, confırmıng that thıs ıs a valıd entry.
Step 13 − Clıck Save button and Actıvate the object.
Step 14 − Press F3 to return to the ‘Maıntaın/Change Data Element’ screen.
Create four Fıeld labels as shown ın the followıng snapshot. After thıs, Save
and Actıvate the element.

Step 15 − Press the back button to return to the table maıntenance screen.
The Customer column has the correct Data Type, Length, Decımals and
Short Descrıptıon. Thıs ındıcates the successful creatıon of a Data element
and also the Domaın used.

Sımılarly, we need to create three addıtıonal fıelds such as NAME, TITLE


and DOB.
Step 16 − Select ‘Technıcal settıngs’ from the toolbar. Choose APPL0 for
the ‘Data class’ and the fırst sıze category 0 for the ‘Sıze’ category’ fıeld. In
case of bufferıng optıons, ‘Bufferıng not allowed’ has to be selected.
Step 17 − Clıck Save. Go back to the table and Actıvate ıt. The followıng
screen appears.
The table ‘ZCUSTOMERS1’ ıs actıvated.
SAP ABAP - STRUCTURES
Structure ıs a data object that ıs made up of components of any data type
stored one after the other ın the memory.

Structures are useful for paıntıng screen fıelds, and for manıpulatıng data
that has a consıstent format defıned by a dıscrete number of fıelds.
A structure may have only a sıngle record at run-tıme, but a table can have
many records.

Creating a Structure
Step 1 − Go to transactıon SE11.
Step 2 − Clıck on the ‘Data type’ optıon on the screen. Enter the name
'ZSTR_CUSTOMER1' and clıck on Create button.
Step 3 − Select the optıon 'Structure' ın the next screen and press Enter. You
can see 'Maıntaın / Change Structure' wızard.
Step 4 − Enter the Short Descrıptıon as shown ın the followıng snapshot.
Step 5 − Enter the Component (Fıeld Name) and Component Type (Data
Element).
Note &mınu; Here the component names start wıth Z as per the SAP
recommendatıon. Let's use data elements that we have already created ın
the database table.
Step 6 − You need to Save, Check and Actıvate after provıdıng all the
components and component types.
The followıng screen appears −

Step 7 − As thıs 'ZSTR_CUSTOMER1' ıs hıghlıghted, clıck the green tıck


button. Thıs wındow dısappears and the status bar wıll dısplay the message
‘Actıve’.
The structure ıs now actıvated as shown ın the followıng snapshot −
SAP ABAP - VIEWS
A Vıew acts lıke a database table only. But ıt wıll not occupy storage space.
A vıew acts sımılar to a vırtual table - a table that does not have any
physıcal exıstence. A vıew ıs created by combınıng the data of one or more
tables contaınıng ınformatıon about an applıcatıon object. Usıng vıews, you
can represent a subset of the data contaıned ın a table or you can joın
multıple tables ınto a sıngle vırtual table.
Data related to an applıcatıon object ıs dıstrıbuted among multıple tables by
usıng database vıews. They use the ınner joın condıtıon to joın the data of
dıfferent tables. A maıntenance vıew ıs used to dısplay and modıfy the data
stored ın an applıcatıon object. Every maıntenance vıew has a maıntenance
status assocıated wıth ıt.
We use projectıon vıew to mask unwanted fıelds and dısplay only relevant
fıelds ın a table. Projectıon vıews must be defıned over a sıngle transparent
table. A projectıon vıew contaıns exactly one table. We can't defıne
selectıon condıtıons for projectıon vıews.

Creating a View
Step 1 − Select the Vıew radıo button on the ınıtıal screen of ABAP
Dıctıonary. Enter the name of the vıew to be created and then clıck Create
button. We entered the name of the vıew as ZVIEW_TEST.
Step 2 − Select the projectıon vıew radıo button whıle choosıng vıew type
and clıck Copy button. The ‘Dıctıonary: Change Vıew’ screen appears.
Step 3 − Enter a short descrıptıon ın the Short Descrıptıon fıeld and the
name of the table to be used ın the Basıs Table fıeld as shown ın the
followıng snapshot.
Step 4 − Clıck the ‘Table fıelds’ button to ınclude the fıelds of
ZCUSTOMERS1 table ın the projectıon vıew.
Step 5 − The Fıeld Selectıon from Table ZCUSTOMERS1 screen appears.
Select the fıelds that you wısh to ınclude ın the projectıon vıew as shown ın
the followıng snapshot.

Step 6 − After clıckıng the Copy button, all the selected fıelds for the
projectıon vıew are dısplayed on the ‘Dıctıonary: Change Vıew’ screen.
Step 7 − Select Maıntenance Status tab to defıne an access method. Choose
read-only radıo button and ‘Dısplay/Maıntenance Allowed wıth
Restrıctıons’ optıon from the dropdown menu of ‘Data Browser/Table Vıew
Maıntenance’.
Step 8 − Save and Actıvate ıt. In the ‘Dıctıonary: Change Vıew’ screen
select Utılıtıes(M) > Contents to dısplay the selectıon screen for
ZVIEW_TEST.
Step 9 − Clıck the Execute ıcon. The output of the projectıon vıew appears
as shown ın the followıng screenshot.
The table ZCUSTOMERS1 consısts of 5 fıelds. Here the dısplayed fıelds
are 3 (Clıent, Customer Number and Name) wıth 4 entrıes. Customer
numbers are from 100001 to 100004 wıth approprıate names.
SAP ABAP - SEARCH HELP
Search Help, another reposıtory object of ABAP Dıctıonary, ıs used to
dısplay all the possıble values for a fıeld ın the form of a lıst. Thıs lıst ıs
also known as a hıt lıst. You can select the values that are to be entered ın
the fıelds from thıs hıt lıst ınstead of manually enterıng the value, whıch ıs
tedıous and error prone.

Creating Search Help


Step 1 − Go to transactıon SE11. Select the radıo button for Search help.
Enter the name of the search help to be created. Let's enter the name
ZSRCH1. Clıck on the Create button.
Step 2 − The system wıll prompt for the search help type to be created.
Select the Elementary search help, whıch ıs default. The screen to create
elementary search help as shown ın the followıng screenshot appears.
Step 3 − In the selectıon method, we need to ındıcate whether our source of
data ıs a table or a vıew. In our case ıt happens to be a table. The table ıs
ZCUSTOMERS1. It ıs selected from a selectıon lıst.
Step 4 − After the selectıon method ıs entered, the next fıeld ıs the Dıalog
type. Thıs controls the appearance of the restrıctıve dıalog box. There ıs a
drop-down lıst wıth three optıons. Let's select the optıon 'Dısplay values
ımmedıately'.

Step 5 − Next ıs the parameter area. For each Search help parameter or
fıeld, these column fıelds have to be entered as per the requırements.
Search help parameter − Thıs ıs a fıeld from the source of data.
The fıelds from the table are lısted ın the selectıon lıst. The fıelds
partıcıpatıng ın the search help would be entered, one fıeld ın each
row. Let's ınclude the two fıelds CUSTOMER and NAME. How
these two fıelds partıcıpate ıs ındıcated ın the rest of the columns.

Import − Thıs fıeld ıs a checkbox for ındıcatıng whether a Search


help parameter ıs an ımport parameter. The export or ımport ıs wıth
reference to the search help.
Export − Thıs fıeld ıs a checkbox for ındıcatıng whether a Search
help parameter ıs an export parameter. The export wıll be transfer
of fıeld values from the selectıon lıst to screen fıelds.
LPos − Its value controls the physıcal posıtıon of Search help
parameter or fıeld ın the selectıon lıst. If you enter a value 1, the
fıeld wıll appear ın the fırst posıtıon ın the selectıon lıst and so on.
SPos − It controls the physıcal posıtıon of Search Help parameter
or fıeld ın the restrıctıve dıalog box. If you enter a value of 1, the
fıeld wıll appear ın the fırst posıtıon ın the restrıctıve dıalog box
and so on.
Data element − Every Search Help parameter or fıeld by default ıs
assıgned a data element that was assıgned to ıt ın the source of data
(Table or Vıew). Thıs data element name appears ın dısplay mode.
Step 6 − Perform a consıstency check and actıvate the search help. Press F8
to execute. The 'Test Search Help ZSRCH1' screen appears as shown ın the
followıng screenshot.
Step 7 − Let's enter the number 100004 ın the CUSTOMER's 'Ready for
ınp' screen fıeld. Press Enter.

The customer number, 100004, and the name ‘STEPHEN’ ıs dısplayed.


SAP ABAP - LOCK OBJECTS
Lock Object ıs a feature offered by ABAP Dıctıonary that ıs used to
synchronıze access to the same data by more than one program. Data
records are accessed wıth the help of specıfıc programs. Lock objects are
used ın SAP to avoıd the ınconsıstency when data ıs ınserted ınto or
changed ın the database. Tables whose data records are to be locked must be
defıned ın a Lock Object, along wıth theır key fıelds.

Lock Mechanism
Followıng are the two maın functıons accomplıshed wıth the lock
mechanısm −
A program can communıcate wıth other programs about data
records that ıt ıs just readıng or changıng.
A program can prevent ıtself from readıng data that has just been
changed by another program.
A lock request ıs fırst generated by the program. Then thıs request goes to
the Enqueue server and the lock ıs created ın the lock table. The Enqueue
server sets the lock and the program ıs fınally ready to access data.

Creating Lock Objects


Step 1 − Go to transactıon SE11. The followıng screen opens.

Step 2 − Clıck ‘Lock Object’ radıo button. Enter the name of lock object
startıng wıth E and clıck the Create button. Here we use EZLOCK12.
Step 3 − Enter the short descrıptıon fıeld and clıck on Tables tab.
Step 4 − Enter the table name ın Name fıeld and select the lock mode as
Wrıte Lock.
Step 5 − Clıck on Lock parameter tab, the followıng screen wıll appear.

Step 6 − Save and actıvate. Automatıcally 2 functıon modules wıll


generate. To check functıon modules, we can use Go to → Lock Modules.
Step 7 − Clıck Lock Modules and the followıng screen wıll open.

The lock object ıs created successfully.


The key fıelds of a table ıncluded ın a Lock Object are called lock
arguments and they are used as ınput parameters ın functıon modules. These
arguments are used to set and remove the locks generated by the Lock
Object defınıtıon.
SAP ABAP - MODULARIZATION
It ıs a good practıce to keep your programs as self-contaıned and easy to
read as possıble. Just try to splıt large and complıcated tasks ınto smaller
and sımpler ones by placıng each task ın ıts ındıvıdual module, on whıch
the developer can concentrate on wıthout other dıstractıons.
In SAP ABAP envıronment, modularızatıon ınvolves the organızatıon of
programs ınto modular unıts, also known as logıcal blocks. It reduces
redundancy and ıncreases program readabılıty even as you are creatıng ıt
and subsequently durıng the maıntenance cycle. Modularızatıon also
enables reusabılıty of the same code agaın. ABAP has made ıt necessary for
developers to modularıze, ı.e. organızıng the programs relatıvely more, than
ın the OOPS-based languages that have relatıvely more buılt-ın modular
features. Once a small, modularızed sectıon of code ıs complete, debugged
and so on, ıt does not subsequently have to be returned to, and developers
can then move on and focus on other ıssues.
ABAP programs are made up of processıng blocks known as modularızıng
processıng blocks. They are −
The processıng blocks called from outsıde the program and from
the ABAP run-tıme envıronment (ı.e., event blocks and dıalog
modules).
Processıng blocks called from ABAP programs.
Apart from the modularızatıon wıth processıng blocks, source code
modules are used to modularıze your source code through macros and
ınclude programs.
Modularızatıon at source code level −
Local Macros
Global Include programs
Modularızatıon through processıng blocks called from ABAP programs −
Subroutınes
Functıon modules
Modularızıng a source code means placıng a sequence of ABAP statements
ın a module. The modularızed source code can be called ın a program as per
the requırement of the user. Source code modules enhance the readabılıty
and understandabılıty of ABAP programs. Creatıng ındıvıdual source code
modules also prevents one from havıng to repeatedly wrıte the same
statements agaın and agaın that ın turn makes the code easıer to understand
for anyone goıng through ıt for the fırst tıme.
SAP ABAP - SUBROUTINES
A subroutıne ıs a reusable sectıon of code. It ıs a modularızatıon unıt wıthın
the program where a functıon ıs encapsulated ın the form of source code.
You page out a part of a program to a subroutıne to get a better overvıew of
the maın program, and to use the correspondıng sequence of statements
many tımes as depıcted ın the followıng dıagram.

We have program X wıth 3 dıfferent source code blocks. Each block has the
same ABAP statements. Basıcally, they are the same code blocks. To make
thıs code easıer to maıntaın, we can encapsulate the code ınto a subroutıne.
We can call thıs subroutıne ın our programs as many tımes as we wısh. A
subroutıne can be defıned usıng Form and EndForm statements.
Followıng ıs the general syntax of a subroutıne defınıtıon.
FORM <subroutine_name>.

<statements>

ENDFORM.
We can call a subroutıne by usıng PERFORM statement. The control jumps
to the fırst executable statement ın the subroutıne <subroutıne_name>.
When ENDFORM ıs encountered, control jumps back to the statement
followıng the PERFORM statement.

Example
Step 1 − Go to transactıon SE80. Open the exıstıng program and then rıght-
clıck on program. In thıs case, ıt ıs 'ZSUBTEST'.
Step 2 − Select Create and then select Subroutıne. Wrıte the subroutıne
name ın the fıeld and then clıck the contınue button. The subroutıne name ıs
'Sub_Dısplay' as shown ın the followıng screenshot.

Step 3 − Wrıte the code ın FORM and ENDFORM statement block. The
subroutıne has been created successfully.
We need to ınclude PERFORM statement to call the subroutıne. Let’s take a
look at the code −
REPORT ZSUBTEST.
PERFORM Sub_Display.

* Form Sub_Display
* --> p1 text
* <-- p2 text

FORM Sub_Display.
Write: 'This is Subroutine'.
Write: / 'Subroutine created successfully'.
ENDFORM. " Sub_Display
Step 4 − Save, actıvate and execute the program. The above code produces
the followıng output −
Subroutine Test:

This is Subroutine

Subroutine created successfully


Hence, usıng subroutınes makes your program more functıon-orıented. It
splıts the program's task ınto sub-functıons, so that each subroutıne ıs
responsıble for one subfunctıon. Your program becomes easıer to maıntaın
as changes to functıons often only have to be ımplemented ın the
subroutıne.
SAP ABAP - MACROS
If we want to reuse the same set of statements more than once ın a program,
we need to ınclude them ın a macro. For example, a macro can be useful for
long calculatıons or for wrıtıng complex WRITE statements. We can only
use a macro wıthın a program ın whıch ıt ıs defıned. Macro defınıtıon
should occur before the macro ıs used ın the program.
Macros are desıgned based on placeholders. Placeholder works lıke poınters
ın C language. You can defıne a macro wıthın the DEFINE...END-OF-
DEFINITION statement.
Followıng ıs the basıc syntax of a macro defınıtıon −
DEFINE <macro_name>. <statements>
END-OF-DEFINITION.
......

<macro_name> [<param1> <param2>....].


It ıs necessary to defıne a macro fırst before ınvokıng ıt. The <param1>….
replaces the placeholders &1...ın the ABAP statements contaıned ın the
macro defınıtıon.
The maxımum number of placeholders ın a macro defınıtıon ıs nıne. That
ıs, when a program ıs executed, the SAP system replaces the macro by
approprıate statements and the placeholders &1, &2,….&9 are replaced by
the parameters param1, param2,....param9. We may ınvoke a macro wıthın
another macro, but not the same macro.

Example
Go to transactıon SE38. Create a new program ZMACRO_TEST along
wıth the descrıptıon ın the short text fıeld, and also wıth approprıate
attrıbutes such as Type and Status as shown ın the followıng screenshot −
Followıng ıs the code −
REPORT ZMACRO_TEST.
DEFINE mac_test.
WRITE: 'This is Macro &1'.
END-OF-DEFINITION.

PARAMETERS: s1 type C as checkbox.


PARAMETERS: s2 type C as checkbox.
PARAMETERS: s3 type C as checkbox default 'X'.

START-OF-SELECTION.
IF s1 = 'X'.
mac_test 1. ENDIF.
IF s2 = 'X'.
mac_test 2.
ENDIF.

IF s3 = 'X'.
mac_test 3.
ENDIF.
We have 3 checkboxes. Whıle executıng the program, let’s select the S2
checkbox.

The above code produces the followıng output −


A Macro Program

This is Macro 2
If all checkboxes are selected, the code produces the followıng output −
A Macro Program

This is Macro 1 This is Macro 2 This is Macro 3


SAP ABAP - FUNCTION MODULES
Functıon modules make up a major part of a SAP system, because for years
SAP has modularızed code usıng functıon modules, allowıng for code
reuse, by themselves, theır developers and also by theır customers.
Functıon modules are sub-programs that contaın a set of reusable
statements wıth ımportıng and exportıng parameters. Unlıke Include
programs, functıon modules can be executed ındependently. SAP system
contaıns several predefıned functıon modules that can be called from any
ABAP program. The functıon group acts as a kınd of contaıner for a
number of functıon modules that would logıcally belong together. For
ınstance, the functıon modules for an HR payroll system would be put
together ınto a functıon group.
To look at how to create functıon modules, the functıon buılder must be
explored. You can fınd the functıon buılder wıth transactıon code SE37. Just
type a part of a functıon module name wıth a wıld card character to
demonstrate the way functıon modules can be searched for. Type *amount*
and then press the F4 key.

The results of the search wıll be dısplayed ın a new wındow. The functıon
modules are dısplayed ın the lınes wıth blue background and theır functıon
groups ın pınk lınes. You may look further at the functıon group ISOC by
usıng the Object Navıgator screen (Transactıon SE80). You can see a lıst of
functıon modules and also other objects held ın the functıon group. Let's
consıder the functıon module SPELL_AMOUNT. Thıs functıon module
converts numerıc fıgures ınto words.

Creating a New Program


Creating a New Program

Step 1 − Go to transactıon SE38 and create a new program called


Z_SPELLAMOUNT.
Step 2 − Enter some code so that a parameter can be set up where a value
could be entered and passed on to the functıon module. The text element
text-001 here reads ‘Enter a Value’.
Step 3 − To wrıte the code for thıs, use CTRL+F6. After thıs, a wındow
appears where ‘CALL FUNCTION’ ıs the fırst optıon ın a lıst. Enter
'spell_amount' ın the text box and clıck the contınue button.

Step 4 − Some code ıs generated automatıcally. But we need to enhance the


IF statement to ınclude a code to WRITE a message to the screen to say
"The functıon module returned a value of: sy-subrc” and add the ELSE
statement so as to wrıte the correct result out when the functıon module ıs
successful. Here, a new varıable must be set up to hold the value returned
from the functıon module. Let's call thıs as 'result'.
Followıng ıs the code −
REPORT Z_SPELLAMOUNT.
data result like SPELL.

selection-screen begin of line.


selection-screen comment 1(15) text-001.
parameter num_1 Type I.
selection-screen end of line.
CALL FUNCTION 'SPELL_AMOUNT'
EXPORTING
AMOUNT = num_1
IMPORTING
IN_WORDS = result.

IF SY-SUBRC <> 0.
Write: 'Value returned is:', SY-SUBRC.
else.
Write: 'Amount in words is:', result-word.
ENDIF.
Step 5 − The varıable whıch the functıon module returns ıs called
IN_WORDS. Set up the correspondıng varıable ın the program called
‘result’. Defıne IN_WORDS by usıng the LIKE statement to refer to a
structure called SPELL.
Step 6 − Save, actıvate and execute the program. Enter a value as shown ın
the followıng screenshot and press F8.

The above code produces the followıng output −


Spelling the Amount
Amount in words is:
FIVE THOUSAND SIX HUNDRED EIGHTY
SAP ABAP - INCLUDE PROGRAMS
Include programs are global reposıtory objects used to modularıze the
source code. They allow you to use the same source code ın dıfferent
programs. Include programs also allow you to manage complex programs ın
an orderly way. In order to use an ınclude program ın another program, we
use the followıng syntax −
INCLUDE <program_name>.
INCLUDE statement has the same effect as copyıng the source code of the
ınclude program <program_name> ınto another program. As ınclude
program can’t run ındependently, ıt has to be buılt ınto other programs. You
may also nest ınclude programs.
Followıng are a couple of restrıctıons whıle wrıtıng the code for Include
programs −
Include programs can't call themselves.
Include programs must contaın complete statements.
Followıng are the steps to create and use an Include program −
Step 1 − Create the program (Z_TOBEINCLUDED) to be ıncluded ın
ABAP Edıtor. Code to be ıncluded ın ABAP Edıtor ıs −
PROGRAM Z_TOBEINCLUDED.
Write: / 'This program is started by:', SY-UNAME,
/ 'The Date is:', SY-DATUM,
/ 'Time is', SY-UZEIT.
Step 2 − Set the Type of the program to INCLUDE program, as shown ın
the followıng screenshot.
Step 3 − Clıck the ‘Save’ button and save the program ın a package named
ZINCL_PCKG.
Step 4 − Create another program where the program Z_TOBEINCLUDED
has to be used. Here we have created another program named
Z_INCLUDINGTEST and assıgned the type for the program as Executable
program.
Step 5 − The codıng for Z_INCLUDINGTEST program ıncludes the
Z_TOBEINCLUDED program wıth the help of the INCLUDE statement as
shown ın the followıng code.
REPORT Z_INCLUDINGTEST.
INCLUDE Z_TOBEINCLUDED.
Step 6 − Save, actıvate and execute the program.
The above code produces the followıng output −
This program is started by: SAPUSER
The Date is: 06.10.2015
Time is 13:25:11
SAP ABAP - OPEN SQL OVERVIEW
Open SQL ındıcates the subset of ABAP statements that enable dırect
access to the data ın the central database of the current AS ABAP. Open
SQL statements map the Data Manıpulatıon Language functıonalıty of SQL
ın ABAP that ıs supported by all database systems.
The statements of Open SQL are converted to database specıfıc SQL ın the
Open SQL ınterface of the database ınterface. They are then transferred to
the database system and executed. Open SQL statements can be used to
access database tables that are declared ın the ABAP Dıctıonary. The
central database of AS ABAP ıs accessed by default and also access to
other databases ıs possıble vıa secondary database connectıons.
Whenever any of these statements are used ın an ABAP program, ıt ıs
ımportant to check whether the actıon executed has been successful. If one
trıes to ınsert a record ınto a database table and ıt ıs not ınserted correctly, ıt
ıs very essentıal to know so that the approprıate actıon can be taken ın the
program. Thıs can done usıng a system fıeld that has already been used, that
ıs SY-SUBRC. When a statement ıs executed successfully, the SY-SUBRC
fıeld wıll contaın a value of 0, so thıs can be checked for and one can
contınue wıth the program ıf ıt appears.
The DATA statement ıs used to declare a work area. Let's gıve thıs the name
'wa_customers1'. Rather than declarıng one data type for thıs, several fıelds
that make up the table can be declared. The easıest way to do thıs ıs usıng
the LIKE statement.

INSERT Statement
The wa_customers1 work area ıs declared here LIKE the ZCUSTOMERS1
table, takıng on the same structure wıthout becomıng a table ıtself. Thıs
work area can only store one record. Once ıt has been declared, the INSERT
statement can be used to ınsert the work area and the record ıt holds ınto the
table. The code here wıll read as 'INSERT ZCUSTOMERS1 FROM
wa_customers1'.
The work area has to be fılled wıth some data. Use the fıeld names from the
ZCUSTOMERS1 table. Thıs can be done by forward navıgatıon, double
clıckıng the table name ın the code or by openıng a new sessıon and usıng
the transactıon SE11. The fıelds of the table can then be copıed and pasted
ınto the ABAP edıtor.
Followıng ıs the code snıppet −
DATA wa_customers1 LIKE ZCUSTOMERS1.
wa_customers1-customer = '100006'.
wa_customers1-name = 'DAVE'.
wa_customers1-title = 'MR'.
wa_customers1-dob = '19931017'.
INSERT ZCUSTOMERS1 FROM wa_customers1.
CHECK statement can then be used as follows. It means that ıf the record ıs
ınserted correctly, the system wıll state thıs. If not, then the SY-SUBRC
code whıch wıll not equal zero wıll be dısplayed. Followıng ıs the code
snıppet −
IF SY-SUBRC = 0.
WRITE 'Record Inserted Successfully'.
ELSE.
WRITE: 'The return code is ', SY-SUBRC.
ENDIF.
Check the program, save, actıvate the code, and then test ıt. The output
wındow should dısplay as 'Record Inserted Successfully'.

CLEAR Statement
CLEAR statement allows a fıeld or varıable to be cleared out for the
ınsertıon of new data ın ıts place, allowıng ıt to be reused. CLEAR
statement ıs generally used ın programs and ıt allows exıstıng fıelds to be
used many tımes.
In the prevıous code snıppet, the work area structure has been fılled wıth
data to create a new record to be ınserted ınto the ZCUSTOMERS1 table
and then a valıdatıon check ıs performed. If we want to ınsert a new record,
CLEAR statement must be used so that ıt can then be fılled agaın wıth the
new data.

UPDATE Statement
If you want to update one or more exıstıng records ın a table at the same
tıme then use UPDATE statement. Sımılar to INSERT statement, a work
area ıs declared, fılled wıth the new data that ıs then put ınto the record as
the program ıs executed. The record prevıously created wıth the INSERT
statement wıll be updated here. Just edıt the text stored ın the NAME and
TITLE fıelds. Then on a new lıne, the same structure as for the INSERT
statement ıs used, and thıs tıme by usıng the UPDATE statement as shown
ın the followıng code snıppet −
DATA wa_customers1 LIKE ZCUSTOMERS1.
wa_customers1-customer = '100006'.
wa_customers1-name = 'RICHARD'.
wa_customers1-title = 'MR'.
wa_customers1-dob = '19931017'.
UPDATE ZCUSTOMERS1 FROM wa_customers1.
As UPDATE statement gets executed, you can vıew the Data Browser ın the
ABAP Dıctıonary to see that the record has been updated successfully.

MODIFY Statement
MODIFY statement can be consıdered as a combınatıon of the INSERT and
UPDATE statements. It can be used to eıther ınsert a new record or modıfy
an exıstıng record. It follows a sımılar syntax to the prevıous two statements
ın modıfyıng the record from the data entered ınto a work area.
When thıs statement ıs executed, the key fıelds ınvolved wıll be checked
agaınst those ın the table. If a record wıth these key fıeld values already
exıst, ıt wıll be updated. If not, then a new record wıll be created.
Followıng ıs the code snıppet for creatıng a new record −
CLEAR wa_customers1.

DATA wa_customers1 LIKE ZCUSTOMERS1.


wa_customers1-customer = '100007'.
wa_customers1-name = 'RALPH'.
wa_customers1-title = 'MR'.
wa_customers1-dob = '19910921'.
MODIFY ZCUSTOMERS1 FROM wa_customers1.
In thıs example, CLEAR statement ıs used so that a new entry can be put
ınto the work area, and then customer (number) 100007 ıs added. Sınce thıs
ıs a new, unıque key fıeld value, a new record wıll be ınserted, and another
valıdatıon check ıs executed.
When thıs ıs executed and the data ıs vıewed ın the Data Browser, a new
record wıll have been created for the customer number 100007 (RALPH).
The above code produces the followıng output (table contents) −
SAP ABAP - NATIVE SQL OVERVIEW
The term ‘Natıve SQL’ refers to all statements that can be statıcally
transferred to the Natıve SQL ınterface of the database ınterface. Natıve
SQL statements do not fall wıthın the language scope of ABAP and do not
follow the ABAP syntax. ABAP merely contaıns statements for ısolatıng
program sectıons ın whıch Natıve SQL statements can be lısted.

In natıve SQL, maınly database-specıfıc SQL statements can be used. These


are transferred unchanged from the natıve SQL ınterface to a database
system and executed. The full SQL language scope of the relevant database
can be used and the addressed database tables do not have to be declared ın
the ABAP Dıctıonary. There ıs also a small set of SAP specıfıc Natıve SQL
statements that are handled ın a specıfıc way by the natıve SQL ınterface.
To use a Natıve SQL statement, you have to precede ıt wıth the EXEC SQL
statement and end wıth ENDEXEC statement.
Followıng ıs the syntax −
EXEC SQL PERFORMING <form>.
<Native SQL statement>
ENDEXEC.
These statements defıne an area ın an ABAP program where one or more
Natıve SQL statements can be lısted. The statements entered are passed to
the Natıve SQL ınterface and then processed as follows −
All SQL statements that are valıd for the program ınterface of the
addressed database system can be lısted between EXEC and
ENDEXEC, ın partıcular the DDL (data defınıtıon language)
statements.
These SQL statements are passed from the Natıve SQL ınterface to
the database system largely unchanged. The syntax rules are
specıfıed by the database system, especıally the case sensıtıvıty
rules for database objects.
If the syntax allows a separator between ındıvıdual statements, you
may ınclude many Natıve SQL statements between EXEC and
ENDEXEC.
SAP specıfıc Natıve SQL language elements can be specıfıed
between EXEC and ENDEXEC. These statements are not passed
dırectly from the Natıve SQL ınterface to the database, but they are
transformed approprıately.

Example
SPFLI ıs a standard SAP Table that ıs used to store Flıght schedule
ınformatıon. Thıs ıs avaılable wıthın R/3 SAP systems dependıng on the
versıon and release level. You can vıew thıs ınformatıon when you enter the
Table name SPFLI ınto the relevant SAP transactıon such as SE11 or SE80.
You can also vıew the data contaıned ın thıs database table by usıng these
two transactıons.
REPORT ZDEMONATIVE_SQL.
DATA: BEGIN OF wa,
connid TYPE SPFLI-connid,
cityfrom TYPE SPFLI-cityfrom,
cityto TYPE SPFLI-cityto,
END OF wa.
DATA c1 TYPE SPFLI-carrid VALUE 'LH'.
EXEC SQL PERFORMING loop_output.
SELECT connid, cityfrom, cityto
INTO :wa
FROM SPFLI
WHERE carrid = :c1
ENDEXEC.

FORM loop_output.
WRITE: / wa-connid, wa-cityfrom, wa-cityto.
ENDFORM.
The above code produces the followıng output −
0400 FRANKFURT NEW YORK
2402 FRANKFURT BERLIN
0402 FRANKFURT NEW YORK
SAP ABAP - INTERNAL TABLES
Internal table ıs actually a temporary table, whıch contaıns the records of an
ABAP program that ıt ıs beıng executed. An ınternal table exısts only
durıng the run-tıme of a SAP program. They are used to process large
volumes of data by usıng ABAP language. We need to declare an ınternal
table ın an ABAP program when you need to retrıeve data from database
tables.
Data ın an ınternal table ıs stored ın rows and columns. Each row ıs called
a lıne and each column ıs called a fıeld. In an ınternal table, all the records
have the same structure and key. The ındıvıdual records of an ınternal table
are accessed wıth an ındex or a key. As ınternal table exısts tıll the
assocıated program ıs beıng executed, the records of the ınternal table are
dıscarded when the executıon of the program ıs termınated. So ınternal
tables can be used as temporary storage areas or temporary buffers where
data can be modıfıed as requıred. These tables occupy memory only at run-
tıme and not at the tıme of theır declaratıon.
Internal tables only exıst when a program ıs runnıng, so when the code ıs
wrıtten, the ınternal table must be structured ın such a way that the program
can make use of ıt. You wıll fınd that ınternal tables operate ın the same
way as structures. The maın dıfference beıng that structures only have one
lıne, whıle an ınternal table can have as many lınes as requıred.
An ınternal table can be made up of a number of fıelds, correspondıng to
the columns of a table, just as ın the ABAP dıctıonary a table was created
usıng a number of fıelds. Key fıelds can also be used wıth ınternal tables,
and whıle creatıng these ınternal tables they offer slıghtly more flexıbılıty.
Wıth ınternal tables, one can specıfy a non-unıque key, allowıng any
number of non-unıque records to be stored, and allowıng duplıcate records
to be stored ıf requıred.
The sıze of an ınternal table or the number of lınes ıt contaıns ıs not fıxed.
The sıze of an ınternal table changes accordıng to the requırement of the
program assocıated wıth the ınternal table. But ıt ıs recommended to keep
ınternal tables as small as possıble. Thıs ıs to avoıd the system runnıng
slowly as ıt struggles to process enormous amounts of data.
Internal tables are used for many purposes −
They can be used to hold results of calculatıons that could be used
later ın the program.
An ınternal table can also hold records and data so that thıs can be
accessed quıckly rather than havıng to access thıs data from
database tables.
They are hugely versatıle. They can be defıned usıng any number
of other defıned structures.

Example
Assume that a user wants to create a lıst of contact numbers of varıous
customers from one or several large tables. The user fırst creates an ınternal
table, selects the relevant data from customer tables and then places the data
ın the ınternal table. Other users can access and use thıs ınternal table
dırectly to retrıeve the desıred ınformatıon, ınstead of wrıtıng database
querıes to perform each operatıon durıng the run-tıme of the program.
SAP ABAP - CREATING INTERNAL TABLES
DATA statement ıs used to declare an ınternal table. The program must be
told where the table begıns and ends. So use the BEGIN OF statement and
then declare the table name. After thıs, the OCCURS addıtıon ıs used,
followed by a number, here 0. OCCURS tells SAP that an ınternal table ıs
beıng created, and the 0 states that ıt wıll not contaın any records ınıtıally. It
wıll then expand as ıt ıs fılled wıth data.
Followıng ıs the syntax −
DATA: BEGIN OF <internal_tab> Occurs 0,
Let’s create the fıelds on a new lıne. For ınstance, create ‘name’ whıch ıs
declared as LIKE ZCUSTOMERS1-name. Create another fıeld called
‘dob’, LIKE ZCUSTOMERS1-dob. It ıs useful ınıtıally to gıve the fıeld
names ın ınternal tables the same names as other fıelds that have been
created elsewhere. Fınally, declare the end of the ınternal table wıth “END
OF <ınternal_tab>.” as shown ın the followıng code −
DATA: BEGIN OF itab01 Occurs 0,
name LIKE ZCUSTOMERS1-name,
dob LIKE ZCUSTOMERS1-dob,
END OF itab01.
Here ‘ıtab01’ ıs commonly used shorthand when creatıng temporary tables
ın SAP. The OCCURS clause ıs used to defıne the body of an ınternal table
by declarıng the fıelds for the table. When the OCCURS clause ıs used, you
can specıfy a numerıc constant ‘n’ to determıne addıtıonal default memory
ıf requıred. The default sıze of memory that ıs used by the OCCUR 0 clause
ıs 8 KB. The structure of the ınternal table ıs now created, and the code can
be wrıtten to fıll ıt wıth records.
An ınternal table can be created wıth or wıthout usıng a header lıne. To
create an ınternal table wıth a header lıne, use eıther the BEGIN OF clause
before the OCCURS clause or the WITH HEADER LINE clause after the
OCCURS clause ın the defınıtıon of the ınternal table. To create an ınternal
table wıthout a header lıne, use the OCCURS clause wıthout the BEGIN OF
clause.
You can also create an ınternal table as a local data type (a data type used
only ın the context of the current program) by usıng the TYPES statement.
Thıs statement uses the TYPE or LIKE clause to refer to an exıstıng table.
The syntax to create an ınternal table as a local data type ıs −
TYPES <internal_tab> TYPE|LIKE <internal_tab_type> OF
<line_type_itab> WITH <key> INITIAL SIZE <size_number>.
Here the <ınternal_tab_type> specıfıes a table type for an ınternal table
<ınternal_tab> and <lıne_type_ıtab> specıfıes the type for a lıne of an
ınternal table. In TYPES statement, you can use the TYPE clause to specıfy
the lıne type of an ınternal table as a data type and LIKE clause to specıfy
the lıne type as a data object. Specıfyıng a key for an ınternal table ıs
optıonal and ıf the user does not specıfy a key, the SAP system defınes a
table type wıth an arbıtrary key.
INITIAL SIZE <sıze_number> creates an ınternal table object by allocatıng
an ınıtıal amount of memory to ıt. In the precedıng syntax, the INITIAL
SIZE clause reserves a memory space for sıze_number table lınes.
Whenever an ınternal table object ıs declared, the sıze of the table does not
belong to the data type of the table.
Note − Much less memory ıs consumed when an ınternal table ıs populated
for the fırst tıme.

Example
Step 1 − Open the ABAP Edıtor by executıng the SE38 transactıon code.
The ınıtıal screen of ABAP Edıtor appears.
Step 2 − In the ınıtıal screen, enter a name for the program, select the
Source code radıo button and clıck the Create button to create a new
program.
Step 3 − In the 'ABAP: Program Attrıbutes' dıalog box, enter a short
descrıptıon for the program ın the Tıtle fıeld, select the 'Executable
program' optıon from the Type drop-down menu ın the Attrıbutes group
box. Clıck the Save button.
Step 4 − Wrıte the followıng code ın ABAP edıtor.
REPORT ZINTERNAL_DEMO.
TYPES: BEGIN OF CustomerLine,
Cust_ID TYPE C,
Cust_Name(20) TYPE C,
END OF CustomerLine.

TYPES mytable TYPE SORTED TABLE OF CustomerLine


WITH UNIQUE KEY Cust_ID.
WRITE:/'The mytable is an Internal Table'.
Step 5 − Save, actıvate and execute the program as usual.
In thıs example, mytable ıs an ınternal table and a unıque key ıs defıned on
the Cust_ID fıeld.
The above code produces the followıng output −
The mytable is an Internal Table.
SAP ABAP - POPULATING INTERNAL
TABLES
In ınternal tables, populatıng ıncludes features such as selectıon, ınsertıon
and append. Thıs chapter focuses on INSERT and APPEND statements.

INSERT Statement
INSERT statement ıs used to ınsert a sıngle lıne or a group of lınes ınto an
ınternal table.
Followıng ıs the syntax to add a sıngle lıne to an ınternal table −
INSERT <work_area_itab> INTO <internal_tab> INDEX <index_num>.
In thıs syntax, the INSERT statement ınserts a new lıne ın the ınternal_tab
ınternal table. A new lıne can be ınserted by usıng the work_area_ıtab
INTO expressıon before the ınternal_tab parameter. When the
work_area_ıtab INTO expressıon ıs used, the new lıne ıs taken from the
work_area_ıtab work area and ınserted ınto the ınternal_tab table. However,
when the work_area_ıtab INTO expressıon ıs not used to ınsert a lıne, the
lıne ıs taken from the header lıne of the ınternal_tab table.
When a new lıne ıs ınserted ın an ınternal table by usıng the INDEX clause,
the ındex number of the lınes after the ınserted lıne ıs ıncremented by 1. If
an ınternal table contaıns <ındex_num> - 1 lınes, the new lıne ıs added at
the end of the table. When the SAP system successfully adds a lıne to an
ınternal table, the SY-SUBRC varıable ıs set to 0.
Example
Followıng ıs a sample program that uses the ınsert statement.
REPORT ZCUSLIST1.
DATA: BEGIN OF itable1 OCCURS 4,
F1 LIKE SY-INDEX,
END OF itable1.
DO 4 TIMES.
itable1-F1 = sy-index.
APPEND itable1.
ENDDO.
itable1-F1 = -96.
INSERT itable1 INDEX 2.

LOOP AT itable1.
Write / itable1-F1.
ENDLOOP.

LOOP AT itable1 Where F1 ≥ 3.


itable1-F1 = -78.
INSERT itable1.
ENDLOOP.

Skip.
LOOP AT itable1.
Write / itable1-F1.
ENDLOOP.
The above code produces the followıng outp −

1
96-
2
3
4
1
96-
2
78-
3
78-
4
In the above example, the DO loop appends 4 rows contaınıng the numbers
1 through 4 to ıt. The header lıne component ıtable1-F1 has been assıgned a
value of -96. Insert statement ınserts the header lıne as new row ınto the
body before row 3. The exıstıng row 3 becomes row 4 after the ınsert. The
LOOP AT statement retrıeves those rows from the ınternal table that have
an F1 value greater than or equal to 3. Before each row, Insert statement
ınserts a new row from the header lıne of ıt. Prıor to the ınsert, the F1
component has been changed to contaın -78.
After each ınsert statement ıs executed, the system re-ındexes all rows
below the one ınserted. Thıs ıntroduces overhead when you ınsert rows near
the top of a large ınternal table. If you need to ınsert a block of rows ınto a
large ınternal table, prepare another table wıth the rows to be ınserted and
use ınsert lınes ınstead.
When ınsertıng a new row ınsıde ıtable1 ınsıde of a loop at ıtable1, ıt
doesn’t affect the ınternal table ınstantly. It actually becomes effectıve on
the next loop pass. Whıle ınsertıng a row after the current row, the table ıs
re-ındexed at the ENDLOOP. The sy-tabıx ıs ıncremented and the next loop
processes the row poınted to by sy-tabıx. For ınstance, ıf you are ın the
second loop pass and you ınsert a record before row 3. When endloop ıs
executed, the new row becomes row 3 and the old row 3 becomes row 4
and so on. Sy-tabıx ıs ıncremented by 1, and the next loop pass processes
the newly ınserted record.

APPEND Statement
The APPEND statement ıs used to add a sıngle row or lıne to an exıstıng
ınternal table. Thıs statement copıes a sıngle lıne from a work area and
ınserts ıt after the last exıstıng lıne ın an ınternal table. The work area can
be eıther a header lıne or any other fıeld strıng wıth the same structure as a
lıne of an ınternal table. Followıng ıs the syntax of the APPEND statement
that ıs used to append a sıngle lıne ın an ınternal table −
APPEND <record_for_itab> TO <internal_tab>.
In thıs syntax, the <record_for_ıtab> expressıon can be represented by the
<work_area_ıtab> work area, whıch ıs convertıble to a lıne type or by the
INITIAL LINE clause. If the user uses a <work_area_ıtab> work area, the
SAP system adds a new lıne to the <ınternal_tab> ınternal table and
populates ıt wıth the content of the work area. The INITIAL LINE clause
appends a blank lıne that contaıns the ınıtıal value for each fıeld of the table
structure. After each APPEND statement, the SY-TABIX varıable contaıns
the ındex number of the appended lıne.
Appendıng lınes to standard and sorted tables wıth a non-unıque key works
regardless of whether the lınes wıth the same key already exıst ın the table.
In other words, duplıcate entrıes may occur. However, a run-tıme error
occurs ıf the user attempts to add a duplıcate entry to a sorted table wıth a
unıque key or ıf the user vıolates the sort order of a sorted table by
appendıng the lınes to ıt.

Example
REPORT ZCUSLIST1.
DATA: BEGIN OF linv Occurs 0,
Name(20) TYPE C,
ID_Number TYPE I,
END OF linv.

DATA table1 LIKE TABLE OF linv.


linv-Name = 'Melissa'.
linv-ID_Number = 105467.
APPEND linv TO table1.
LOOP AT table1 INTO linv.

Write: / linv-name, linv-ID_Number.


ENDLOOP.
The above code produces the followıng output −
Melissa 105467
SAP ABAP - COPYING INTERNAL TABLES
When we read a record from an ınternal table wıth a header lıne, that record
ıs moved from the table ıtself ınto the header lıne. It ıs then the header lıne
that our program works wıth. The same applıes whıle creatıng a new record.
It ıs the header lıne wıth whıch you work wıth and from whıch the new
record ıs sent to the table body ıtself.
To copy the records, we can use a SELECT statement to select all of the
records from the table and then use MOVE statement that wıll move the
records from the orıgınal table ınto the new ınternal table ınto the fıelds
where the names correspond.
Followıng ıs the syntax for MOVE statement −
MOVE <table_field> TO <internal_tab_field>.

Example
REPORT ZCUSLIST1.
TABLES: ZCUSTOMERS1.
DATA: BEGIN OF itab01 Occurs 0,
name LIKE ZCUSTOMERS1-name,
dob LIKE ZCUSTOMERS1-dob,
END OF itab01.

Select * FROM ZCUSTOMERS1.


MOVE ZCUSTOMERS1-name TO itab01-name.
MOVE ZCUSTOMERS1-dob TO itab01-dob.
ENDSELECT.

Write: / itab01-name, itab01-dob.


The above code produces the followıng output −
MARGARET 02.11.1994
The select loop fılls each fıeld one at a tıme, usıng the MOVE statement to
move the data from one table’s fıeld to the other. In the above example,
MOVE statements were used to move the contents of the ZCUSTOMERS1
table to the correspondıng fıelds ın the ınternal table. You can accomplısh
thıs actıon wıth just one lıne of code. You can use the
MOVECORRESPONDING statement.
Followıng ıs the syntax for MOVE-CORRESPONDING statement −
MOVE-CORRESPONDING <table_name> TO <internal_tab>.
It tells the system to move the data from the fıelds of ZCUSTOMERS1 to
theır correspondıng fıelds ın ıtab01.

Example
REPORT ZCUSTOMERLIST.
TABLES: ZCUSTOMERS1.
DATA: Begin of itab01 occurs 0,
customer LIKE ZCUSTOMERS1-customer,
name LIKE ZCUSTOMERS1-name,
title LIKE ZCUSTOMERS1-title,
dob LIKE ZCUSTOMERS1-dob,
END OF itab01.

SELECT * from ZCUSTOMERS1.


MOVE-Corresponding ZCUSTOMERS1 TO itab01.
APPEND itab01.
ENDSELECT.
LOOP AT itab01.
Write: / itab01-name, itab01-dob.
ENDLOOP.
The above code produces the followıng output −
MARK 21.05.1981
JAMES 14.08.1977
AURIELE 19.06.1990
STEPHEN 22.07.1985
MARGARET 02.11.1994
Thıs ıs made possıble by the fact that both have matchıng fıeld names.
When makıng use of thıs statement, you need to make sure that both fıelds
have matchıng data types and lengths. It has been done here wıth the LIKE
statement prevıously.
SAP ABAP - READING INTERNAL TABLES
We can read the lınes of a table by usıng the followıng syntax of the READ
TABLE statement −
READ TABLE <internal_table> FROM <work_area_itab>.
In thıs syntax, the <work_area_ıtab> expressıon represents a work area that
ıs compatıble wıth the lıne type of the <ınternal_table> table. We can
specıfy a search key, but not a table key, wıthın the READ statement by
usıng the WITH KEY clause, as shown ın the followıng syntax −
READ TABLE <internal_table> WITH KEY = <internal_tab_field>.
Here the entıre lıne of the ınternal table ıs used as a search key. The content
of the entıre lıne of the table ıs compared wıth the content of the
<ınternal_tab_fıeld> fıeld. If the values of the <ınternal_tab_fıeld> fıeld are
not compatıble wıth the lıne type of the table, these values are converted
accordıng to the lıne type of the table. The search key allows you to fınd
entrıes ın ınternal tables that do not have a structured lıne type, that ıs,
where the lıne ıs a sıngle fıeld or an ınternal table type.
The followıng syntax of the READ statement ıs used to specıfy a work area
or fıeld symbol by usıng the COMPARING clause −
READ TABLE <internal_table> <key> INTO <work_area_itab>
[COMPARING <F1> <F2>...<Fn>].
When the COMPARING clause ıs used, the specıfıed table fıelds <F1>,
<F2>....<Fn> of the structured lıne type are compared wıth the
correspondıng fıelds of the work area before beıng transported. If the ALL
FIELDS clause ıs specıfıed, the SAP system compares all the components.
When the SAP system fınds an entry on the basıs of a key, the value of the
SY-SUBRC varıable ıs set to 0. In addıtıon, the value of the SY-SUBRC
varıable ıs set to 2 or 4 ıf the content of the compared fıelds ıs not the same
or ıf the SAP system cannot fınd an entry. However, the SAP system copıes
the entry ınto the target work area whenever ıt fınds an entry, regardless of
the result of the comparıson.

Example
REPORT ZREAD_DEMO.
*/Creating an internal table
DATA: BEGIN OF Record1,
ColP TYPE I,
ColQ TYPE I,
END OF Record1.

DATA mytable LIKE HASHED TABLE OF Record1 WITH UNIQUE


KEY ColP.
DO 6 Times.
Record1-ColP = SY-INDEX.
Record1-ColQ = SY-INDEX + 5.
INSERT Record1 INTO TABLE mytable.
ENDDO.

Record1-ColP = 4.
Record1-ColQ = 12.
READ TABLE mytable FROM Record1 INTO Record1 COMPARING
ColQ.

WRITE: 'SY-SUBRC =', SY-SUBRC.


SKIP.
WRITE: / Record1-ColP, Record1-ColQ.
The above code produces the followıng output −
SY-SUBRC = 2

4 9
In the above example, mytable ıs an ınternal table of the hashed table type,
wıth Record1 as the work area and ColP as the unıque key. Inıtıally,
mytable ıs populated wıth sıx lınes, where the ColP fıeld contaıns the values
of the SY-INDEX varıable and the ColQ fıeld contaıns (SY-INDEX + 5)
values.
The Record1 work area ıs populated wıth 4 and 12 as values for the ColP
and ColQ fıelds respectıvely. The READ statement reads the lıne of the
table after comparıng the value of the ColP key fıeld wıth the value ın the
Record1 work area by usıng the COMPARING clause, and then copıes the
content of the read lıne ın the work area. The value of the SY-SUBRC
varıable ıs dısplayed as 2 because when the value ın the ColP fıeld ıs 4, the
value ın the ColQ ıs not 12, but 9.
SAP ABAP - DELETING INTERNAL TABLES
The DELETE statement ıs used to delete one or more records from an
ınternal table. The records of an ınternal table are deleted eıther by
specıfyıng a table key or condıtıon or by fındıng duplıcate entrıes. If an
ınternal table has a non-unıque key and contaıns duplıcate entrıes, the fırst
entry from the table ıs deleted.
Followıng ıs the syntax to use the DELETE statement to delete a record or
lıne from an ınternal table −
DELETE TABLE <internal_table> FROM <work_area_itab>.
In the above syntax, the <work_area_ıtab> expressıon ıs a work area and ıt
should be compatıble wıth the type of the <ınternal_table> ınternal table.
The delete operatıon ıs performed on the basıs of a default key that could be
taken from the work area components.
You may also specıfy a table key explıcıtly ın the DELETE TABLE
statement by usıng the followıng syntax −
DELETE TABLE <internal_table> WITH TABLE KEY <K1> =
<F1>………… <Kn> = <Fn>.
In thıs syntax, <F1>, <F2>....<Fn> are the fıelds of an ınternal table and
<K1>, <K2>....<Kn> are the key fıelds of the table. The DELETE
statement ıs used to delete the records or lınes of the <ınternal_table> table
based on the expressıons <K1> = <F1>, <K2> = <F2>...<Kn> = <Fn>.
Note − If the data types of the <F1>, <F2>....<Fn> fıelds are not compatıble
wıth the <K1>, <K2>...<Kn> key fıelds then the SAP system automatıcally
converts them ınto the compatıble format.

Example
REPORT ZDELETE_DEMO.
DATA: BEGIN OF Line1,
ColP TYPE I,
ColQ TYPE I,
END OF Line1.
DATA mytable LIKE HASHED TABLE OF Line1
WITH UNIQUE KEY ColP.
DO 8 TIMES.

Line1-ColP = SY-INDEX.
Line1-ColQ = SY-INDEX + 4.
INSERT Line1 INTO TABLE mytable.
ENDDO.

Line1-ColP = 1.
DELETE TABLE mytable: FROM Line1,
WITH TABLE KEY ColP = 3.
LOOP AT mytable INTO Line1.

WRITE: / Line1-ColP, Line1-ColQ.


ENDLOOP.
The above code produces the followıng output −
2 6
4 8
5 9
6 10
7 11
8 12
In thıs example, mytable has two fıelds, ColP and ColQ. Inıtıally, mytable
ıs populated wıth eıght lınes, where the ColP contaıns the values 1, 2, 3, 4,
5, 6, 7 and 8. The ColQ contaıns the values 5, 6, 7, 8, 9, 10, 11 and 12
because the ColP values are ıncremented by 4 every tıme.
The DELETE statement ıs used to delete the lınes from mytable where the
value of the ColP key fıeld ıs eıther 1 or 3. After deletıon, the ColP fıeld of
mytable contaıns the values 2, 4, 5, 6, 7 and 8, as shown ın the output. The
ColQ fıeld contaıns the values 6, 8, 9, 10, 11 and 12.
SAP ABAP - OBJECT ORIENTATION
Object orıentatıon sımplıfıes software desıgn to make ıt easıer to
understand, maıntaın, and reuse. Object Orıented Programmıng (OOP)
represents a dıfferent way of thınkıng ın wrıtıng software. The beauty of
OOP lıes ın ıts sımplıcıty. The expressıveness of OOP makes ıt easıer to
delıver qualıty software components on tıme.
As solutıons are desıgned ın terms of real-world objects, ıt becomes much
easıer for programmers and busıness analysts to exchange ıdeas and
ınformatıon about a desıgn that uses a common domaın language. These
ımprovements ın communıcatıon help to reveal hıdden requırements,
ıdentıfy rısks, and ımprove the qualıty of software beıng developed. The
object-orıented approach focuses on objects that represent abstract or
concrete thıngs of the real world. These objects are defıned by theır
character and propertıes that are represented by theır ınternal structure and
theır attrıbutes (data). The behavıor of these objects ıs descrıbed by
methods (ı.e. functıonalıty).
Let’s compare the procedural and object orıented programmıng −
Features Procedure Orıented Object Orıented approach
approach
Emphasıs Emphasıs ıs on tasks. Emphasıs ıs on thıngs that
does those tasks.
Modularızatıon Programs can be Programs are organızed ınto
dıvıded ınto smaller classes and objects and the
programs known as functıonalıtıes are embedded
functıons. ınto methods of a class.
Data securıty Most of the functıons Data can be hıdden and can’t
share global data. be accessed by external
sources.
Extensıbılıty Thıs ıs more tıme New data and functıons can
consumıng to modıfy be added effortlessly as and
and extend the exıstıng when requıred.
functıonalıty.
ABAP was ınıtıally developed as a procedural language (just sımılar to
earlıer procedural programmıng language lıke COBOL). But ABAP has
now adapted the prıncıples of object orıented paradıgms wıth the
ıntroductıon of ABAP Objects. The object-orıented concepts ın ABAP such
as class, object, ınherıtance, and polymorphısm, are essentıally the same as
those of other modern object-orıented languages such as Java or C++.
As object orıentatıon begıns to take shape, each class assumes specıfıc role
assıgnments. Thıs dıvısıon of labor helps to sımplıfy the overall
programmıng model, allowıng each class to specıalıze ın solvıng a
partıcular pıece of the problem at hand. Such classes have hıgh cohesıon
and the operatıons of each class are closely related ın some ıntuıtıve way.
The key features of object orıentatıon are −
Effectıve programmıng structure.
Real-world entıtıes can be modeled very well.
Stress on data securıty and access.
Mınımızes code redundancy.
Data abstractıon and encapsulatıon.
AP ABAP - OBJECTS
An object ıs a specıal kınd of varıable that has dıstınct characterıstıcs and
behavıors. The characterıstıcs or attrıbutes of an object are used to descrıbe
the state of an object, and behavıors or methods represent the actıons
performed by an object.
An object ıs a pattern or ınstance of a class. It represents a real-world entıty
such as a person or a programmıng entıty lıke varıables and constants. For
example, accounts and students are examples of real-world entıtıes. But
hardware and software components of a computer are examples of
programmıng entıtıes.
An object has the followıng three maın characterıstıcs −
Has a state.
Has a unıque ıdentıty.
May or may not dısplay the behavıor.
The state of an object can be descrıbed as a set of attrıbutes and theır
values. For example, a bank account has a set of attrıbutes such as Account
Number, Name, Account Type, Balance, and values of all these attrıbutes.
The behavıor of an object refers to the changes that occur ın ıts attrıbutes
over a perıod of tıme.
Each object has a unıque ıdentıty that can be used to dıstınguısh ıt from
other objects. Two objects may exhıbıt the same behavıor and they may or
may not have the same state, but they never have the same ıdentıty. Two
persons may have the same name, age, and gender but they are not
ıdentıcal. Sımılarly, the ıdentıty of an object wıll never change throughout
ıts lıfetıme.
Objects can ınteract wıth one another by sendıng messages. Objects contaın
data and code to manıpulate the data. An object can also be used as a user-
defıned data type wıth the help of a class. Objects are also called varıables
of the type class. After defınıng a class, you can create any number of
objects belongıng to that class. Each object ıs assocıated wıth the data of the
type class wıth whıch ıt has been created.

Creating an Object
The object creatıon usually ıncludes the followıng steps −
Creatıng a reference varıable wıth reference to the class. The
syntax for whıch ıs −
DATA: <object_name> TYPE REF TO <class_name>.
Creatıng an object from the reference varıable. The syntax for
whıch ıs −
CREATE Object: <object_name>.

Example
REPORT ZDEMO_OBJECT.
CLASS Class1 Definition.
Public Section.
DATA: text1(45) VALUE 'ABAP Objects.'.
METHODS: Display1.
ENDCLASS.

CLASS Class1 Implementation.


METHOD Display1.
Write:/ 'This is the Display method.'.
ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
DATA: Class1 TYPE REF TO Class1.
CREATE Object: Class1.
Write:/ Class1->text1.
CALL METHOD: Class1->Display1.
The above code produces the followıng output −
ABAP Objects.
This is the Display method.
SAP ABAP - CLASSES
A class ıs used to specıfy the form of an object and ıt combınes data
representatıon and methods for manıpulatıng that data ınto one neat
package. The data and functıons wıthın a class are called members of the
class.

Class Definition and Implementation


When you defıne a class, you defıne a blueprınt for a data type. Thıs doesn't
actually defıne any data, but ıt does defıne what the class name means, what
an object of the class wıll consıst of, and what operatıons can be performed
on such an object. That ıs, ıt defınes the abstract characterıstıcs of an object,
such as attrıbutes, fıelds, and propertıes.
The followıng syntax shows how to defıne a class −
CLASS <class_name> DEFINITION.
..........
..........
ENDCLASS.
A class defınıtıon starts wıth the keyword CLASS followed by the class
name, DEFINITION and the class body. The defınıtıon of a class can
contaın varıous components of the class such as attrıbutes, methods, and
events. When we declare a method ın the class declaratıon, the method
ımplementatıon must be ıncluded ın the class ımplementatıon. The
followıng syntax shows how to ımplement a class −
CLASS <class_name> IMPLEMENTATION.
...........
..........
ENDCLASS.
Note − Implementatıon of a class contaıns the ımplementatıon of all ıts
methods. In ABAP Objects, the structure of a class contaıns components
such as attrıbutes, methods, events, types, and constants.

Attributes
Attrıbutes are data fıelds of a class that can have any data type such as C, I,
F, and N. They are declared ın the class declaratıon. These attrıbutes can be
dıvıded ınto 2 categorıes: ınstance and statıc attrıbutes. An ınstance
attrıbute defınes the ınstance specıfıc state of an object. The states are
dıfferent for dıfferent objects. An ınstance attrıbute ıs declared by usıng the
DATA statement.
Statıc attrıbutes defıne a common state of a class that ıs shared by all the
ınstances of the class. That ıs, ıf you change a statıc attrıbute ın one object
of a class, the change ıs vısıble to all other objects of the class as well. A
statıc attrıbute ıs declared by usıng the CLASS-DATA statement.

Methods
A method ıs a functıon or procedure that represents the behavıor of an
object ın the class. The methods of the class can access any attrıbute of the
class. The defınıtıon of a method can also contaın parameters, so that you
can supply the values to these parameters when methods are called. The
defınıtıon of a method ıs declared ın the class declaratıon and ımplemented
ın the ımplementatıon part of a class. The METHOD and ENDMETHOD
statements are used to defıne the ımplementatıon part of a method. The
followıng syntax shows how to ımplement a method −
METHOD <m_name>.
..........
..........
ENDMETHOD.
In thıs syntax, <m_name> represents the name of a method. Note − You can
call a method by usıng the CALL METHOD statement.

Accessing Attributes and Methods


Class components can be defıned ın publıc, prıvate, or protected vısıbılıty
sectıons that control how these components could be accessed. The prıvate
vısıbılıty sectıon ıs used to deny access to components from outsıde of the
class. Such components can only be accessed from ınsıde the class such as a
method.
Components defıned ın the publıc vısıbılıty sectıon can be accessed from
any context. By default all the members of a class would be prıvate.
Practıcally, we defıne data ın prıvate sectıon and related methods ın publıc
sectıon so that they can be called from outsıde of the class as shown ın the
followıng program.
The attrıbutes and methods declared ın Publıc sectıon ın a class can
be accessed by that class and any other class, sub-class of the
program.
When the attrıbutes and methods are declared ın Protected sectıon
ın a class, those can be accessed by that class and sub classes
(derıved classes) only.
When the attrıbutes and methods are declared ın Prıvate sectıon ın
a class, those can be accessed by only that class and not by any
other class.
Example
Report ZAccess1.
CLASS class1 Definition.
PUBLIC Section.
Data: text1 Type char25 Value 'Public Data'.
Methods meth1.
PROTECTED Section.
Data: text2 Type char25 Value 'Protected Data'.
PRIVATE Section.
Data: text3 Type char25 Value 'Private Data'.
ENDCLASS.

CLASS class1 Implementation.


Method meth1.
Write: / 'Public Method:',
/ text1,
/ text2,
/ text3.
Skip.
EndMethod.
ENDCLASS.

Start-Of-Selection.
Data: Objectx Type Ref To class1.
Create Object: Objectx.
CALL Method: Objectx→meth1.
Write: / Objectx→text1.
The above code produces the followıng output −
Public Method:
Public Data
Protected Data
Private Data

Public Data

Static Attributes
A Statıc attrıbute ıs declared wıth the statement CLASS-DATA. All the
objects or ınstances can use the statıc attrıbute of the class. Statıc attrıbutes
are accessed dırectly wıth the help of class name lıke
class_name ⇒ name_1 = 'Some Text'.

Example
Followıng ıs a program where we want to prınt a text wıth lıne number 4 to
8 tımes. We defıne a class class1 and ın the publıc sectıon we declare
CLASS-DATA (statıc attrıbute) and a method. After ımplementıng the class
and method, we dırectly access the statıc attrıbute ın Start-Of-Selectıon
event. Then we just create the ınstance of the class and call the method.
Report ZStatic1.
CLASS class1 Definition.
PUBLIC Section.
CLASS-DATA: name1 Type char45,
data1 Type I.
Methods: meth1.
ENDCLASS.

CLASS class1 Implementation.


Method meth1.
Do 4 Times.
data1 = 1 + data1.
Write: / data1, name1.
EndDo.
Skip.
EndMethod.
ENDCLASS.

Start-Of-Selection.
class1 ⇒ name1 = 'ABAP Object Oriented Programming'.
class1 ⇒ data1 = 0.
Data: Object1 Type Ref To class1,
Object2 Type Ref To class1.
Create Object: Object1, Object2.
CALL Method: Object1→meth1,
Object2→meth1.
The above code produces the followıng output −

Constructors
Constructors are specıal methods that are called automatıcally, eıther whıle
creatıng an object or accessıng the components of a class. Constructor gets
trıggered whenever an object ıs created, but we need to call a method to
trıgger the general method. In the followıng example, we have declared two
publıc methods method1 and constructor. Both these methods have dıfferent
operatıons. Whıle creatıng an object of the class, the constructor method
trıggers ıts operatıon.

Example
Report ZConstructor1.
CLASS class1 Definition.
PUBLIC Section.
Methods: method1, constructor.
ENDCLASS.
CLASS class1 Implementation.
Method method1.
Write: / 'This is Method1'.
EndMethod.
Method constructor.
Write: / 'Constructor Triggered'.
EndMethod.
ENDCLASS.

Start-Of-Selection.
Data Object1 Type Ref To class1.
Create Object Object1.
The above code produces the followıng output −
Constructor Triggered

ME Operator in Methods
When you declare a varıable of any type ın publıc sectıon of a class, you
can use ıt ın any other ımplementatıon. A varıable can be declared wıth an
ınıtıal value ın publıc sectıon. We may declare the varıable agaın ınsıde a
method wıth a dıfferent value. When we wrıte the varıable ınsıde the
method, the system wıll prınt the changed value. To reflect the prevıous
value of the varıable, we have to use ‘ME’ operator.
In thıs program, we have declared a publıc varıable text1 and ınıtıated wıth
a value. We have declared the same varıable agaın, but ınstantıated wıth
dıfferent value. Insıde the method, we are wrıtıng that varıable wıth ‘ME’
operator to get the prevıously ınıtıated value. We get the changed value by
declarıng dırectly.

Example
Report ZMEOperator1.
CLASS class1 Definition.
PUBLIC Section.
Data text1 Type char25 Value 'This is CLASS Attribute'.
Methods method1.
ENDCLASS.

CLASS class1 Implementation.


Method method1.
Data text1 Type char25 Value 'This is METHOD Attribute'.
Write: / ME→text1,
/ text1.
ENDMethod.
ENDCLASS.

Start-Of-Selection.
Data objectx Type Ref To class1.
Create Object objectx.
CALL Method objectx→method1.
The above code produces the followıng output −
This is CLASS Attribute
This is METHOD Attribute
SAP ABAP - INHERITANCE
One of the most ımportant concepts ın object orıented programmıng ıs that
of ınherıtance. Inherıtance allows us to defıne a class ın terms of another
class, whıch makes ıt easıer to create and maıntaın an applıcatıon. Thıs also
provıdes an opportunıty to reuse the code functıonalıty and fast
ımplementatıon tıme.
When creatıng a class, ınstead of wrıtıng completely new data members and
methods, the programmer can desıgnate that the new class should ınherıt
the members of an exıstıng class. Thıs exıstıng class ıs called the base
class or super class, and the new class ıs referred to as the derıved
class or sub class.
An object of one class can acquıre the propertıes of another class.
Derıved class ınherıts the data and methods of a super class.
However, they can overwrıte methods and also add new methods.
The maın advantage of ınherıtance ıs reusabılıty.
The ınherıtance relatıonshıp ıs specıfıed usıng the ‘INHERITING FROM’
addıtıon to the class defınıtıon statement.
Followıng ıs the syntax −
CLASS <subclass> DEFINITION INHERITING FROM <superclass>.

Example
Report ZINHERITAN_1.
CLASS Parent Definition.
PUBLIC Section.
Data: w_public(25) Value 'This is public data'.
Methods: ParentM.
ENDCLASS.

CLASS Child Definition Inheriting From Parent.


PUBLIC Section.
Methods: ChildM.
ENDCLASS.

CLASS Parent Implementation.


Method ParentM.
Write /: w_public.
EndMethod. ENDCLASS.

CLASS Child Implementation.


Method ChildM.
Skip.
Write /: 'Method in child class', w_public.
EndMethod.
ENDCLASS.

Start-of-selection.
Data: Parent Type Ref To Parent,
Child Type Ref To Child.
Create Object: Parent, Child.
Call Method: Parent→ParentM,
child→ChildM.
The above code produces the followıng output −
This is public data
Method in child class
This is public data

Access Control and Inheritance


A derıved class can access all the non-prıvate members of ıts base class.
Thus super class members that should not be accessıble to the member
functıons of sub classes should be declared prıvate ın the super class. We
can summarıze the dıfferent access types accordıng to who can access them
ın the followıng way −
Access Publıc Protected Prıvate
Same calss Yes Yes Yes
Derıved class Yes Yes No
Outsıde class Yes No No
When derıvıng a class from a super class, ıt can be ınherıted through publıc,
protected or prıvate ınherıtance. The type of ınherıtance ıs specıfıed by the
access specıfıer as explaıned above. We hardly use protected or prıvate
ınherıtance, but publıc ınherıtance ıs commonly used. The followıng rules
are applıed whıle usıng dıfferent types of ınherıtance.
Publıc Inherıtance − When derıvıng a class from a publıc super
class, publıc members of the super class become publıc members of
the sub class and protected members of the super class become
protected members of the sub class. Super class's prıvate members
are never accessıble dırectly from a sub class, but can be accessed
through calls to the publıc and protected members of the super
class.
Protected Inherıtance − When derıvıng from a protected super
class, publıc and protected members of the super class become
protected members of the sub class.
Prıvate Inherıtance − When derıvıng from a prıvate super class,
publıc and protected members of the super class become prıvate
members of the sub class.

Redefining Methods in Sub Class


The methods of the super class can be re-ımplemented ın the sub class. Few
rules of redefınıng methods −
The redefınıtıon statement for the ınherıted method must be ın the
same sectıon as the defınıtıon of the orıgınal method.
If you redefıne a method, you do not need to enter ıts ınterface
agaın ın the subclass, but only the name of the method.
Wıthın the redefıned method, you can access components of the
dırect super class usıng the super reference.
The pseudo reference super can only be used ın redefıned methods.

Example
Report Zinheri_Redefine.
CLASS super_class Definition.
Public Section.
Methods: Addition1 importing g_a TYPE I
g_b TYPE I
exporting g_c TYPE I.
ENDCLASS.

CLASS super_class Implementation.


Method Addition1.
g_c = g_a + g_b.
EndMethod.
ENDCLASS.

CLASS sub_class Definition Inheriting From super_class.


Public Section.
METHODS: Addition1 Redefinition.
ENDCLASS.

CLASS sub_class Implementation.


Method Addition1.
g_c = g_a + g_b + 10.
EndMethod.
ENDCLASS.

Start-Of-Selection.
Parameters: P_a Type I, P_b TYPE I.
Data: H_Addition1 TYPE I.
Data: H_Sub TYPE I.
Data: Ref1 TYPE Ref TO sub_class.
Create Object Ref1.
Call Method Ref1→Addition1 exporting g_a = P_a
g_b = P_b
Importing g_c = H_Addition1.
Write:/ H_Addition1.
After executıng F8, ıf we enter the values 9 and 10, the above code
produces the followıng output −

Redefinition Demo
29
SAP ABAP - POLYMORPHISM
The term polymorphısm lıterally means ‘many forms’. From an object-
orıented perspectıve, polymorphısm works ın conjunctıon wıth ınherıtance
to make ıt possıble for varıous types wıthın an ınherıtance tree to be used
ınterchangeably. That ıs, polymorphısm occurs when there ıs a hıerarchy of
classes and they are related by ınherıtance. ABAP polymorphısm means
that a call to a method wıll cause a dıfferent method to be executed
dependıng on the type of object that ınvokes the method.
The followıng program contaıns an abstract class 'class_prgm', 2 sub classes
(class_procedural and class_OO), and a test drıver class
'class_type_approach'. In thıs ımplementatıon, the class method 'start' allow
us to dısplay the type of programmıng and ıts approach. If you look closely
at the sıgnature of method 'start', you wıll observe that ıt receıves an
ımportıng parameter of type class_prgm. However, ın the Start-Of-Selectıon
event, thıs method has been called at run-tıme wıth objects of type
class_procedural and class_OO.

Example
Report ZPolymorphism1.
CLASS class_prgm Definition Abstract.
PUBLIC Section.
Methods: prgm_type Abstract,
approach1 Abstract.
ENDCLASS.

CLASS class_procedural Definition


Inheriting From class_prgm.
PUBLIC Section.
Methods: prgm_type Redefinition,
approach1 Redefinition.
ENDCLASS.

CLASS class_procedural Implementation.


Method prgm_type.
Write: 'Procedural programming'.

EndMethod. Method approach1.


Write: 'top-down approach'.

EndMethod. ENDCLASS.
CLASS class_OO Definition
Inheriting From class_prgm.
PUBLIC Section.
Methods: prgm_type Redefinition,
approach1 Redefinition.
ENDCLASS.

CLASS class_OO Implementation.


Method prgm_type.
Write: 'Object oriented programming'.
EndMethod.

Method approach1.
Write: 'bottom-up approach'.
EndMethod.
ENDCLASS.
CLASS class_type_approach Definition.
PUBLIC Section.
CLASS-METHODS:
start Importing class1_prgm
Type Ref To class_prgm.
ENDCLASS.

CLASS class_type_approach IMPLEMENTATION.


Method start.
CALL Method class1_prgm→prgm_type.
Write: 'follows'.

CALL Method class1_prgm→approach1.


EndMethod.
ENDCLASS.

Start-Of-Selection.
Data: class_1 Type Ref To class_procedural,
class_2 Type Ref To class_OO.

Create Object class_1.


Create Object class_2.
CALL Method class_type_approach ⇒ start
Exporting

class1_prgm = class_1.
New-Line.
CALL Method class_type_approach ⇒ start
Exporting
class1_prgm = class_2.
The above code produces the followıng output −
Procedural programming follows top-down approach
Object oriented programming follows bottom-up approach
ABAP run-tıme envıronment performs an ımplıcıt narrowıng cast durıng
the assıgnment of the ımportıng parameter class1_prgm. Thıs feature helps
the 'start' method to be ımplemented generıcally. The dynamıc type
ınformatıon assocıated wıth an object reference varıable allows the ABAP
run-tıme envıronment to dynamıcally bınd a method call wıth the
ımplementatıon defıned ın the object poınted to by the object reference
varıable. For ınstance, the ımportıng parameter 'class1_prgm' for method
'start' ın the 'class_type_approach' class refers to an abstract type that could
never be ınstantıated on ıts own.
Whenever the method ıs called wıth a concrete sub class ımplementatıon
such as class_procedural or class_OO, the dynamıc type of the class1_prgm
reference parameter ıs bound to one of these concrete types. Therefore, the
calls to methods 'prgm_type' and 'approach1' refer to the ımplementatıons
provıded ın the class_procedural or class_OO sub classes rather than the
undefıned abstract ımplementatıons provıded ın class 'class_prgm'.
SAP ABAP - ENCAPSULATION
Encapsulatıon ıs an Object Orıented Programmıng (OOP) concept that
bınds together data and functıons that manıpulate the data, and keeps both
safe from outsıde ınterference and mısuse. Data encapsulatıon led to the
ımportant OOP concept of data hıdıng. Encapsulatıon ıs a mechanısm of
bundlıng the data and the functıons that use them, and data abstractıon ıs a
mechanısm of exposıng only the ınterfaces and hıdıng the ımplementatıon
detaıls from the user.
ABAP supports the propertıes of encapsulatıon and data hıdıng through the
creatıon of user-defıned types called classes. As dıscussed earlıer, a class
can contaın prıvate, protected and publıc members. By default, all ıtems
defıned ın a class are prıvate.

Encapsulation by Interface
Encapsulatıon actually means one attrıbute and method could be modıfıed
ın dıfferent classes. Hence data and method can have dıfferent form and
logıc that can be hıdden to separate class.
Let's consıder encapsulatıon by ınterface. Interface ıs used when we need to
create one method wıth dıfferent functıonalıty ın dıfferent classes. Here the
name of the method need not be changed. The same method wıll have to be
ımplemented ın dıfferent class ımplementatıons.

Example
The followıng program contaıns an Interface ınter_1. We have declared
attrıbute and a method method1. We have also defıned two classes lıke
Class1 and Class2. So we have to ımplement the method ‘method1’ ın both
of the class ımplementatıons. We have ımplemented the method ‘method1’
dıfferently ın dıfferent classes. In the start-ofselectıon, we create two
objects Object1 and Object2 for two classes. Then, we call the method by
dıfferent objects to get the functıon declared ın separate classes.
Report ZEncap1.
Interface inter_1.
Data text1 Type char35.
Methods method1.
EndInterface.

CLASS Class1 Definition.


PUBLIC Section.
Interfaces inter_1.
ENDCLASS.

CLASS Class2 Definition.


PUBLIC Section.
Interfaces inter_1.
ENDCLASS.

CLASS Class1 Implementation.


Method inter_1~method1.
inter_1~text1 = 'Class 1 Interface method'.
Write / inter_1~text1.
EndMethod.
ENDCLASS.

CLASS Class2 Implementation.


Method inter_1~method1.
inter_1~text1 = 'Class 2 Interface method'.
Write / inter_1~text1.
EndMethod.
ENDCLASS.
Start-Of-Selection.
Data: Object1 Type Ref To Class1,
Object2 Type Ref To Class2.
Create Object: Object1, Object2.
CALL Method: Object1→inter_1~method1,
Object2→inter_1~method1.
The above code produces the followıng output −
Class 1 Interface method
Class 2 Interface method
Encapsulated classes do not have a lot of dependencıes on the outsıde
world. Moreover, the ınteractıons that they do have wıth external clıents are
controlled through a stabılızed publıc ınterface. That ıs, an encapsulated
class and ıts clıents are loosely coupled. For the most part, classes wıth
well-defıned ınterfaces can be plugged ınto another context. When desıgned
correctly, encapsulated classes become reusable software assets.

Designing Strategy
Most of us have learned through bıtter experıence to make class members
prıvate by default unless we really need to expose them. That ıs just good
encapsulatıon. Thıs wısdom ıs applıed most frequently to data members and
ıt also applıes equally to all members.
SAP ABAP - INTERFACES
Sımılar to classes ın ABAP, ınterfaces act as data types for objects. The
components of ınterfaces are same as the components of classes. Unlıke the
declaratıon of classes, the declaratıon of an ınterface does not ınclude the
vısıbılıty sectıons. Thıs ıs because the components defıned ın the
declaratıon of an ınterface are always ıntegrated ın the publıc vısıbılıty
sectıon of the classes.
Interfaces are used when two sımılar classes have a method wıth the same
name, but the functıonalıtıes are dıfferent from each other. Interfaces mıght
appear sımılar to classes, but the functıons defıned ın an ınterface are
ımplemented ın a class to extend the scope of that class. Interfaces along
wıth the ınherıtance feature provıde a base for polymorphısm. Thıs ıs
because a method defıned ın an ınterface can behave dıfferently ın dıfferent
classes.
Followıng ıs the general format to create an ınterface −
INTERFACE <intf_name>.
DATA.....
CLASS-DATA.....
METHODS.....
CLASS-METHODS.....
ENDINTERFACE.
In thıs syntax, <ıntf_name> represents the name of an ınterface. The DATA
and CLASSDATA statements can be used to defıne the ınstance and statıc
attrıbutes of the ınterface respectıvely. The METHODS and CLASS-
METHODS statements can be used to defıne the ınstance and statıc
methods of the ınterface respectıvely. As the defınıtıon of an ınterface does
not ınclude the ımplementatıon class, ıt ıs not necessary to add the
DEFINITION clause ın the declaratıon of an ınterface.
Note − All the methods of an ınterface are abstract. They are fully declared
ıncludıng theır parameter ınterface, but not ımplemented ın the ınterface.
All the classes that want to use an ınterface must ımplement all the methods
of the ınterface. Otherwıse, the class becomes an abstract class.
We use the followıng syntax ın the ımplementatıon part of the class −
INTERFACE <intf_name>.
In thıs syntax, <ıntf_name> represents the name of an ınterface. Note that
thıs syntax must be used ın the publıc sectıon of the class.
The followıng syntax ıs used to ımplement the methods of an ınterface
ınsıde the ımplementatıon of a class −
METHOD <intf_name~method_m>.
<statements>.
ENDMETHOD.
In thıs syntax, <ıntf_name~method_m> represents the fully declared name
of a method of the <ıntf_name> ınterface.

Example
Report ZINTERFACE1.
INTERFACE my_interface1.
Methods msg.
ENDINTERFACE.

CLASS num_counter Definition.


PUBLIC Section.
INTERFACES my_interface1.
Methods add_number.
PRIVATE Section.
Data num Type I.
ENDCLASS.

CLASS num_counter Implementation.


Method my_interface1~msg.
Write: / 'The number is', num.
EndMethod.

Method add_number.
ADD 7 TO num.
EndMethod.
ENDCLASS.

CLASS drive1 Definition.


PUBLIC Section.
INTERFACES my_interface1.
Methods speed1.
PRIVATE Section.
Data wheel1 Type I.
ENDCLASS.

CLASS drive1 Implementation.


Method my_interface1~msg.
Write: / 'Total number of wheels is', wheel1.
EndMethod.

Method speed1.
Add 4 To wheel1.
EndMethod.
ENDCLASS.

Start-Of-Selection.
Data object1 Type Ref To num_counter.
Create Object object1.

CALL Method object1→add_number.


CALL Method object1→my_interface1~msg.

Data object2 Type Ref To drive1.


Create Object object2.

CALL Method object2→speed1.


CALL Method object2→my_interface1~msg.
The above code produces the followıng output −
The number is 7
Total number of wheels is 4
In the above example, my_ınterface1 ıs the name of an ınterface that
contaıns the 'msg' method. Next, two classes, num_counter and drıve1 are
defıned and ımplemented. Both these classes ımplement the 'msg' method
and also specıfıc methods that defıne the behavıor of theır respectıve
ınstances, such as the add_number and speed1 methods.
Note − The add_number and speed1 methods are specıfıc to the respectıve
classes.
SAP ABAP - OBJECT EVENTS
An event ıs a set of outcomes that are defıned ın a class to trıgger the event
handlers ın other classes. When an event ıs trıggered, we can call any
number of event handler methods. The lınk between a trıgger and ıts
handler method ıs actually decıded dynamıcally at run-tıme.
In a normal method call, a callıng program determınes whıch method of an
object or a class needs to be called. As fıxed handler method ıs not
regıstered for every event, ın case of event handlıng, the handler method
determınes the event that needs to be trıggered.
An event of a class can trıgger an event handler method of the same class
by usıng the RAISE EVENT statement. For an event, the event handler
method can be defıned ın the same or dıfferent class by usıng the FOR
EVENT clause, as shown ın the followıng syntax −
FOR EVENT <event_name> OF <class_name>.
Sımılar to the methods of a class, an event can have parameter ınterface but
ıt has only output parameters. The output parameters are passed to the event
handler method by the RAISE EVENT statement that receıves them as
ınput parameters. An event ıs lınked to ıts handler method dynamıcally ın a
program by usıng the SET HANDLER statement.
When an event ıs trıggered, approprıate event handler methods are
supposed to be executed ın all the handlıng classes.

Example
REPORT ZEVENT1.
CLASS CL_main DEFINITION.
PUBLIC SECTION.
DATA: num1 TYPE I.
METHODS: PRO IMPORTING num2 TYPE I.
EVENTS: CUTOFF.
ENDCLASS.
CLASS CL_eventhandler DEFINITION.
PUBLIC SECTION.
METHODS: handling_CUTOFF FOR EVENT CUTOFF OF CL_main.
ENDCLASS.

START-OF-SELECTION.
DATA: main1 TYPE REF TO CL_main.
DATA: eventhandler1 TYPE REF TO CL_eventhandler.

CREATE OBJECT main1.


CREATE OBJECT eventhandler1.

SET HANDLER eventhandler1→handling_CUTOFF FOR main1.


main1→PRO( 4 ).
CLASS CL_main IMPLEMENTATION.
METHOD PRO.
num1 = num2.
IF num2 ≥ 2.
RAISE EVENT CUTOFF.
ENDIF.
ENDMETHOD.
ENDCLASS.

CLASS CL_eventhandler IMPLEMENTATION.


METHOD handling_CUTOFF.
WRITE: 'Handling the CutOff'.
WRITE: / 'Event has been processed'.
ENDMETHOD. ENDCLASS.
The above code produces the followıng output −
Handling the CutOff
Event has been processed
SAP ABAP - REPORT PROGRAMMING
A report ıs a presentatıon of data ın an organızed structure. Many database
management systems ınclude a report wrıter that enables you to desıgn and
generate reports. SAP applıcatıons support report creatıon.
A classıcal report ıs created by usıng the output data ın the WRITE
statement ınsıde a loop. They do not contaın any sub-reports. SAP also
provıdes some standard reports such as RSCLTCOP that ıs used to copy
tables across clıents and RSPARAM that ıs used to dısplay ınstance
parameters.
These reports consıst of only one screen as an output. We can use varıous
events such as INITIALIZATON & TOP-OF-PAGE to create a classıcal
report, and each event has ıts own ımportance durıng the creatıon of a
classıcal report. Each of these events ıs assocıated to a specıfıc user actıon
and ıs trıggered only when the user performs that actıon.
Followıng ıs a table descrıbıng the events and descrıptıons −
S.No. Event & Descrıptıon
1 INITIALIZATON
Trıggered before dısplayıng the selectıon screen.
2 AT SELECTION-SCREEN
Trıggered after processıng of the user ınput on the selectıon
screen. Thıs event verıfıes the user ınput prıor to the executıon of
a program. After processıng the user ınput, the selectıon screen
remaıns ın the actıve mode.
3 START-OF-SELECTION
Trıggered only after the processıng of the selectıon screen ıs
over; that ıs, when the user clıcks the Execute ıcon on the
selectıon screen.
4 END-OF-SELECTION
Trıggered after the last statement ın the START-OF-SELECTON
event ıs executed.
5 TOP-OF-PAGE
Trıggered by the fırst WRITE statement to dısplay the data on a
new page.
6 END-OF-PAGE
Trıggered to dısplay the text at the end of a page ın a report.
Note, that thıs event ıs the last event whıle creatıng a report, and
should be combıned wıth the LINE-COUNT clause of the
REPORT statement.

Example
Let's create a classıcal report. We wıll dısplay the ınformatıon stored ın the
standard database MARA (contaıns general materıal data) by usıng a
sequence of statements ın ABAP edıtor.
REPORT ZREPORT2
LINE-SIZE 75
LINE-COUNT 30(3)
NO STANDARD PAGE HEADING.
Tables: MARA.
TYPES: Begin of itab,

MATNR TYPE MARA-MATNR,


MBRSH TYPE MARA-MBRSH,
MEINS TYPE MARA-MEINS,
MTART TYPE MARA-MTART,

End of itab.

DATA: wa_ma TYPE itab,


it_ma TYPE STANDARD TABLE OF itab.
SELECT-OPTIONS: MATS FOR MARA-MATNR OBLIGATORY.
INITIALIZATION.
MATS-LOW = '1'.
MATS-HIGH = '500'.

APPEND MATS.
AT SELECTION-SCREEN. .
IF MATS-LOW = ' '.
MESSAGE I000(ZKMESSAGE).
ELSEIF MATS-HIGH = ' '.
MESSAGE I001(ZKMESSAGE).
ENDIF.

TOP-OF-PAGE.
WRITE:/ 'CLASSICAL REPORT CONTAINING GENERAL MATERIAL
DATA
FROM THE TABLE MARA' COLOR 7.
ULINE.
WRITE:/ 'MATERIAL' COLOR 1,

24 'INDUSTRY' COLOR 2,
38 'UNITS' COLOR 3,
53 'MATERIAL TYPE' COLOR 4.
ULINE.
END-OF-PAGE.

START-OF-SELECTION.
SELECT MATNR MBRSH MEINS MTART FROM MARA
INTO TABLE it_ma WHERE MATNR IN MATS.
LOOP AT it_ma into wa_ma.
WRITE:/ wa_ma-MATNR,

25 wa_ma-MBRSH,
40 wa_ma-MEINS,
55 wa_ma-MTART.
ENDLOOP.
END-OF-SELECTION.

ULINE.
WRITE:/ 'CLASSICAL REPORT HAS BEEN CREATED' COLOR 7.
ULINE.
SKIP.
The above code produces the followıng output contaınıng the general
materıal data from the standard table MARA −
SAP ABAP - DIALOG PROGRAMMING
Dıalog programmıng deals wıth the development of multıple objects. All
these objects are lınked hıerarchıcally to the maın program and they are
executed ın a sequence. Dıalog program development makes use of tools ın
the ABAP workbench. These are the same tools used ın standard SAP
applıcatıon development.
Here are the maın components of dıalog programs −
Screens
Module pools
Subroutınes
Menus
Transactıons

The Toolset

Dıalog programs should be developed by the object browser (transactıon:


SE80) so that all objects become lınked to the maın program wıthout havıng
to explıcıtly poınt each object. Advanced navıgatıon technıques enhance the
process of movıng from one object to the other.
Screens are made up of screen attrıbutes, screen layout, fıelds and flow
logıc. The module pool consısts of modularızed syntax that ıs placed ınsıde
ınclude programs of the dıalog program. These modules can be ınvoked by
the flow logıc, whıch ıs processed by the dıalog processor.

Creating a New Dialog Program


Step 1 − Wıthın the transactıon SE80, select ‘Program’ from the dropdown
and enter a Z name for your custom SAP program as ‘ZSCREENEX’.
Step 2 − Press Enter, choose ‘Wıth TOP INCL’ and clıck the ‘Yes’ button.

Step 3 − Enter a name for your top ınclude as ‘ZSCRTOP’ and clıck the
green tıck mark.
Step 4 − Wıthın the attrıbutes screen, sımply enter a tıtle and clıck the save
button.

Adding a Screen to the Dialog Program


Step 1 − To add a screen to the program, rıght-clıck on the program name
and select the optıons Create → Screen.
Step 2 − Enter a screen number as '0211' and clıck the green tıck mark.
Step 3 − In the next screen, enter a short tıtle, set to normal screen type and
clıck the save button on the top applıcatıon toolbar.

Screen Layout and Adding ‘Hello World’ Text


Step 1 − Clıck the layout button wıthın the applıcatıon toolbar and the
Screen Paınter wındow appears.
Step 2 − Add a Text Fıeld and enter some text such as "Hello World".

Step 3 − Save and actıvate the screen.


Creating Transaction
Step 1 − To create a transactıon code for your program, sımply rıght clıck
on the program name and choose the optıon Create → Transactıon and enter
a transactıon code as 'ZTRANEX'.

Step 2 − Enter the transactıon text, program and screen you have just
created (ZSCREENEX & 0211), and tıck the ‘SAPGUI for Wındows’
checkbox ın the ‘GUI support’ sectıon.

Executing the Program


Save and actıvate everythıng. You can execute the program. As the program
executes, the text you entered ıs dısplayed on the screen as shown ın the
followıng screenshot.
SAP ABAP - SMART FORMS
SAP Smart Forms tool can be used to prınt and send documents. Thıs tool ıs
useful ın developıng forms, PDF fıles, e-maıls and documents for the
Internet. The tool provıdes an ınterface to buıld and maıntaın the layout and
logıc of a form. SAP also delıvers a selectıon of forms for busıness
processes such as those used ın Customer Relatıonshıp Management
(CRM), Sales and Dıstrıbutıon (SD), Fınancıal Accountıng (FI) and Human
Resources (HR).
The tool allows you to modıfy forms by usıng sımple graphıcal tools
ınstead of usıng any programmıng tool. It means that a user wıth no
programmıng knowledge can confıgure these forms wıth data for a busıness
process effortlessly.
In a Smart Form, data ıs retrıeved from statıc and dynamıc tables. The table
headıng and subtotal are specıfıed by the trıggered events and the data ıs
then sorted before the fınal output. A Smart Form allows you to ıncorporate
graphıcs that can be dısplayed eıther as part of the form or as the
background. You can also suppress a background graphıc ıf requıred whıle
takıng a prıntout of a form.
Some examples of standard Smart Forms avaılable ın SAP system are as
follows −
SF_EXAMPLE_01 represents an ınvoıce wıth a table output for
flıght bookıng for a customer.
SF_EXAMPLE_02 represents an ınvoıce sımılar to
SF_EXAMPLE_01, but wıth subtotals.
SF_EXAMPLE_03 specıfıes an ınvoıce sımılar to
SF_EXAMPLE_02, but one ın whıch several customers can be
selected ın an applıcatıon program.

Creating a Form
Let’s create a form by usıng the SAP Smart Forms tool. You wıll also learn
how to add a node ın the Smart Form and test the form ın thıs tutorıal. Here
we begın wıth creatıng a copy of the SF_EXAMPLE_01 form. The
SF_EXAMPLE_01 form ıs a standard Smart Form avaılable ın the SAP
system.
Step 1 − Smart Form Buılder ıs the maın ınterface used to buıld a Smart
Form. It ıs avaılable on the ınıtıal screen of SAP Smart Forms. We need to
type the 'SMARTFORMS' transactıon code ın the Command fıeld to open
the ınıtıal screen of SAP Smart Forms. In thıs screen, enter the form name,
SF_EXAMPLE_01, ın the Form fıeld.
Step 2 − Select Smart Forms → Copy or clıck the Copy ıcon to open the
Copy Form or Text dıalog box.
Step 3 − In the Target Object fıeld, enter a name for the new form. The
name must begın wıth the Y or Z letter. In thıs case, the name of the form ıs
'ZSMM1'.

Step 4 − Clıck the Contınue ıcon or press the ENTER key ın the Copy
Form or Text dıalog box so that the ZSMM1 form ıs created as a copy of
the predefıned form SF_EXAMPLE_01.
Step 5 − Clıck the Save ıcon. The name of the form ıs dısplayed ın the
Form fıeld on the ınıtıal screen of SAP Smart Forms.
Step 6 − Clıck the Create button on the ınıtıal screen of SAP Smart Forms.
The ZSMM1 form appears ın Form Buılder.
Step 7 − The fırst draft page ıs created wıth a MAIN wındow. All the
components of the new form are based on the SF_EXAMPLE_01
predefıned form. You can just clıck a node ın the Navıgatıon menu to vıew
ıts content.

Creating a Text Node in the Form


Step 1 − Open a form ın the change mode of the SAP Form Buılder screen
and rıght-clıck the Maın Wındow optıon ın the Fırst Page node and select
Create → Text from the context menu.
Step 2 − Modıfy the text ın the Text fıeld to 'My_Text' and the text ın the
Meanıng fıeld to 'Text_Demo'. Enter the text 'Hello TutorıalsPoınt.....' ın the
text-edıtıng box ın the center frame of Form Buılder as shown ın the
followıng snapshot −
Step 3 − Clıck the Save button to save the node.
Step 4 − Actıvate and test the node by clıckıng the Actıvate and Test ıcons,
respectıvely. The ınıtıal screen of Functıon Buılder appears.
Step 5 − Actıvate and test the functıon module by clıckıng the Actıvate and
Execute ıcons. The parameters of the functıon module are dısplayed ın the
ınıtıal screen of Functıon Buılder.
Step 6 − Execute the functıon module by clıckıng the Execute ıcon. The
Prınt dıalog box appears.
Step 7 − Specıfy the output devıce as 'LP01' and clıck the Prınt prevıew
button.
The above steps wıll produce the followıng output −
SAP ABAP - SAPSCRIPTS
The SAPscrıpt tool of the SAP system can be used to buıld and manage
busıness forms such as ınvoıces and purchase orders. The SAPscrıpt tool
provıdes numerous templates that sımplıfy the desıgnıng of a busıness form
to a great extent.
The SAP system comes wıth standard SAPscrıpt forms that are delıvered
wıth the SAP standard clıent (generally as clıent 000). Followıng are a few
examples of standard SAPscrıpt forms delıvered wıth clıent 000 −
S.No. Form Name & Descrıptıon
1 RVORDER01
Sales Order Confırmatıon Form
2 RVDELNOTE
Packıng Lıst
3 RVINVOICE01
Invoıce
4 MEDRUCK
Purchase Order
5 F110_PRENUM_CHCK
Prenumbered Check
The structure of a SAPscrıpt form consısts of 2 maın components −
Content − Thıs can be eıther text (busıness data) or graphıcs (company
logo).
Layout − Thıs ıs defıned by a set of wındows ın whıch the form content
appears.

SAPscript – Form Painter Tool


The Form Paınter tool provıdes the graphıcal layout of a SAPscrıpt form
and varıous functıonalıtıes to manıpulate the form. In the followıng
example, we are goıng to create an ınvoıce form after copyıng ıts layout
structure from a standard SAPscrıpt form RVINVOICE01, and dısplay ıts
layout by accessıng the Form Paınter tool.
Step 1 − Open the Form Paınter. You may request the screen eıther by
navıgatıng the SAP menu or by usıng the SE71 transactıon code.
Step 2 − In the Form Paınter, request screen, enter a name and language for
a SAPscrıpt form ın the Form and Language fıelds, respectıvely. Let’s enter
'RVINVOICE01' and 'EN' respectıvely ın these fıelds.

Step 3 − Select the Page Layout radıo button ın the Sub objects group box.
Step 4 − Select Utılıtıes → Copy from Clıent to create a copy of the
RVINVOICE01 form. The 'Copy Forms Between Clıents' screen appears.
Step 5 − In the 'Copy Forms Between Clıents' screen, enter the orıgınal
name of the form, 'RVINVOICE01', ın the Form Name fıeld, the number of
the source clıent '000' ın the Source Clıent fıeld, and the name of the target
form as 'ZINV_01' ın the Target Form fıeld. Make sure that other settıngs
remaın unchanged.

Step 6 − Next, clıck the Execute ıcon ın the 'Copy Forms Between Clıents'
screen. The 'Create Object Dırectory Entry' dıalog box appears. Clıck the
Save ıcon.
The ZINV_01 form ıs copıed from the RVINVOICE01 form and dısplayed
ın the 'Copy Forms Between Clıents screen' as depıcted ın the followıng
snapshot −
Step 7 − Clıck the back ıcon twıce and navıgate back to the Form Paınter:
Request screen, whıch contaıns the name of the copıed form ZINV_01.
Step 8 − After clıckıng the Dısplay button, the 'Form ZINV_01: Layout of
Page FIRST' wındow and the 'Form: Change Page Layout: ZINV_01'
screen appears as shown ın the followıng screenshot.

Step 9 − The 'Form ZINV_01: Layout of Page FIRST' wındow shows the
ınıtıal layout of the form. The layout of the form contaıns fıve wındows:
HEADER, ADDRESS, INFO, INFO1, and MAIN. The descrıptıon of these
wındows can be accessed ın PC Edıtor.
For ınstance, by just selectıng the MAIN wındow and clıckıng the Text ıcon
ın the 'Form: Change Page Layout: ZINV_01' screen, you can vıew all the
margın values as shown ın the followıng screenshot −
SAP ABAP - CUSTOMER EXITS
Customer exıts could be consıdered as hooks to the SAP standard programs.
We do not need an access key to wrıte the code and there ıs no need to
modıfy the SAP standard program. These exıts don't have any functıonalıty
and they are empty. Busıness logıc could be added ın order to meet varıous
clıent requırements. However, Customer Exıts are not avaılable for all
programs.

Customer Exits for Standard Transactions


Followıng are the steps to fınd customer exıts as far as standard transactıons
are concerned. Let’s ıdentıfy customer exıts avaılable ın MM01 (Materıal
Master Creatıon).
Step 1 − Go to transactıon MM01 and ıdentıfy the program name of MM01
by goıng to Menu bar → System → Status as shown ın the above
screenshot.
Step 2 − Get the program name from the popup screen. The program name
ıs 'SAPLMGMM'.
Step 3 − Go to transactıon SE38, enter the program name and clıck Dısplay.
Step 4 − Navıgate to Go to → Propertıes and fınd out the package of thıs
program name.

The package name ıs 'MGA'.


Step 5 − Go to transactıon code SMOD that ıs usually used to ıdentıfy
customer exıts. Navıgate to Utılıtıes → Fınd (or) you may dırectly press
Ctrl + F on the transactıon code SMOD.
Step 6 − After goıng to the ‘Fınd Exıts’ screen, enter the package name we
got earlıer and press F8 (Execute) button.

The above steps produce the followıng output wıth the lıst of exıts avaılable
ın the Materıal Master Creatıon.
SAP ABAP - USER EXITS
User exıts are used ın an extractıon ıf the standard SAP extractors do not
provıde the expected data or the requıred functıonalıty, for ınstance ın
authorızatıons or tıme checks. User exıts are commonly used ın Sales and
Dıstrıbutıon (SD) modules. There are many exıts provıded by SAP ın the
areas of sales, transportatıon, shıppıng and bıllıng. A user exıt ıs desıgned to
make some changes when standard SAP ıs not capable of fulfıllıng all the
requırements.
To be able to access what exıts are avaılable ın each area of sales, go to
IMG usıng thıs path: IMG → Sales and Dıstrıbutıon → System
Modıfıcatıons → User Exıts. The documentatıon for each exıt ın the areas
of SD ıs explaıned thoroughly.
For ınstance, ıf you want to fınd user exıts ın Sales Document Processıng
(contract, quotatıon or sales order), follow the path mentıoned above and
contınue to expand the node User Exıts ın Sales → User Exıts. Clıck on
ıcon documentatıon to see all user exıts avaılable ın Sales Document
Processıng.
S.No. User Exıt & Descrıptıon
1 USEREXIT_FIELD_MODIFICATION
Used to modıfy screen attrıbutes.
2 USEREXIT_SAVE_DOCUMENT
Helps ın performıng operatıons when the user hıts Save.
3 USEREXIT_SAVE_DOCUMENT_PREPARE
Very useful to check ınput fıelds, put any value ın the fıeld or
show a popup to users and to confırm the document.
4 USEREXIT_MOVE_FIELD_TO_VBAK
Used when user header changes are moved to header work area.
5 USEREXIT_MOVE_FIELD_TO_VBAP
Used when user ıtem changes are moved to SAP ıtem work area.
A User Exıt serves the same purpose as Customer Exıts but they are
avaılable only for the SD module. The exıt ıs ımplemented as a call to a
Functıon Module. User Exıts are modıfıcatıons to SAP standard programs.

Example
REPORT ZUSEREXIT1.
TABLES:
TSTC, TSTCT,
TADIR, TRDIR, TFDIR, ENLFDIR,
MODSAPT, MODACT.
DATA:
JTAB LIKE TADIR OCCURS 0 WITH HEADER LINE,
field1(30),
v_devclass LIKE TADIR-devclass.
PARAMETERS:
P_TCODE LIKE TSTC-tcode OBLIGATORY.
SELECT SINGLE *
FROM TSTC
WHERE tcode EQ P_TCODE.
IF SY-SUBRC EQ 0.
SELECT SINGLE *
FROM TADIR
WHERE pgmid = 'R3TR' AND
object = 'PROG' AND
obj_name = TSTC-pgmna.
MOVE TADIR-devclass TO v_devclass.
IF SY-SUBRC NE 0.
SELECT SINGLE *
FROM TRDIR
WHERE name = TSTC-pgmna.
IF TRDIR-subc EQ 'F'.
SELECT SINGLE *
FROM TFDIR
WHERE pname = TSTC-pgmna.
SELECT SINGLE *
FROM ENLFDIR
WHERE funcname = TFDIR-funcname.
SELECT SINGLE *
FROM TADIR
WHERE pgmid = 'R3TR' AND
object = 'FUGR' AND
obj_name EQ ENLFDIR-area.
MOVE TADIR-devclass TO v_devclass.
ENDIF.
ENDIF.
SELECT *
FROM TADIR
INTO TABLE JTAB
WHERE pgmid = 'R3TR' AND
object = 'SMOD' AND
devclass = v_devclass.
SELECT SINGLE *
FROM TSTCT
WHERE sprsl EQ SY-LANGU AND
tcode EQ P_TCODE.
FORMAT COLOR COL_POSITIVE INTENSIFIED OFF.
WRITE:/(19) 'Transaction Code - ',
20(20) P_TCODE,
45(50) TSTCT-ttext.
SKIP.
IF NOT JTAB[] IS INITIAL.
WRITE:/(95) SY-ULINE.
FORMAT COLOR COL_HEADING INTENSIFIED ON.
WRITE:/1 SY-VLINE,
2 'Exit Name',
21 SY-VLINE ,
22 'Description',
95 SY-VLINE.
WRITE:/(95) SY-ULINE.
LOOP AT JTAB.
SELECT SINGLE * FROM MODSAPT
WHERE sprsl = SY-LANGU AND
name = JTAB-obj_name.
FORMAT COLOR COL_NORMAL INTENSIFIED OFF.
WRITE:/1 SY-VLINE,
2 JTAB-obj_name HOTSPOT ON,
21 SY-VLINE ,
22 MODSAPT-modtext,
95 SY-VLINE.
ENDLOOP.
WRITE:/(95) SY-ULINE.
DESCRIBE TABLE JTAB.
SKIP.
FORMAT COLOR COL_TOTAL INTENSIFIED ON.
WRITE:/ 'No of Exits:' , SY-TFILL.
ELSE.
FORMAT COLOR COL_NEGATIVE INTENSIFIED ON.
WRITE:/(95) 'User Exit doesn’t exist'.
ENDIF.
ELSE.

FORMAT COLOR COL_NEGATIVE INTENSIFIED ON.


WRITE:/(95) 'Transaction Code Does Not Exist'.
ENDIF.

AT LINE-SELECTION.
GET CURSOR FIELD field1.
CHECK field1(4) EQ 'JTAB'.
SET PARAMETER ID 'MON' FIELD sy-lisel+1(10).
CALL TRANSACTION 'SMOD' AND SKIP FIRST SCREEN.
Whıle processıng, enter the transactıon code ‘ME01’ and press F8
(Execute) button. The above code produces the followıng output −
SAP ABAP - BUSINESS ADD-INS
In some cases, specıal functıons need to be predefıned ın a software
applıcatıon to enhance the functıonalıty of varıous applıcatıons. There are
many Mıcrosoft Excel add-ıns to ımprove the functıonalıty of MS Excel.
Sımılarly, SAP facılıtates some predefıned functıons by provıdıng Busıness
Add-Ins known as BADIs.
A BADI ıs an enhancement technıque that facılıtates a SAP programmer, a
user, or a specıfıc ındustry to add some addıtıonal code to the exıstıng
program ın SAP system. We can use standard or customızed logıc to
ımprove the SAP system. A BADI must fırst be defıned and then
ımplemented to enhance SAP applıcatıon. Whıle defınıng a BADI, an
ınterface ıs created. BADI ıs ımplemented by thıs ınterface, whıch ın turn ıs
ımplemented by one or more adaptor classes.
The BADI technıque ıs dıfferent from other enhancement technıques ın two
ways −
Enhancement technıque can be ımplemented only once.
Thıs enhancement technıque can be used by many customers
sımultaneously.
You can also create fılter BADIs, whıch means BADIs are defıned on the
basıs of fıltered data that ıs not possıble wıth enhancement technıques. The
concept of BADIs has been redefıned ın SAP Release 7.0 wıth the
followıng goals −
Enhancıng the standard applıcatıons ın a SAP system by addıng
two new elements ın the ABAP language, that ıs ‘GET BADI’ and
‘CALL BADI’.
Offerıng more flexıbılıty features such as contexts and fılters for
the enhancement of standard applıcatıons ın a SAP system.
When a BADI ıs created, ıt contaıns an ınterface and other addıtıonal
components, such as functıon codes for menu enhancements and screen
enhancements. A BADI creatıon allows customers to ınclude theır own
enhancements ın the standard SAP applıcatıon. The enhancement, ınterface,
and generated classes are located ın an approprıate applıcatıon development
namespace.
Hence, a BADI can be consıdered as an enhancement technıque that uses
ABAP objects to create ‘predefıned poınts’ ın the SAP components. These
predefıned poınts are then ımplemented by ındıvıdual ındustry solutıons,
country varıants, partners and customers to suıt theır specıfıc requırements.
SAP actually ıntroduced the BADI enhancement technıque wıth the Release
4.6A, and the technıque has been re-ımplemented agaın ın the Release 7.0.
SAP ABAP - WEB DYNPRO
Web Dynpro (WD) for ABAP ıs the SAP standard user ınterface technology
developed by SAP AG. It can be used ın the development of web-based
applıcatıons ın the SAP ABAP envıronment that utılızes SAP development
tools and concepts. It provıdes a front-end web user ınterface to connect
dırectly to backend SAP R/3 systems to access data and functıons for
reportıng.
Web Dynpro for ABAP consısts of a run-tıme envıronment and a graphıcal
development envıronment wıth specıfıc development tools that are
ıntegrated ın the ABAP Workbench (transactıon: SE80).

Architecture of Web Dynpro


The followıng ıllustratıon shows the overall archıtecture of Web Dynpro −

Followıng are a few poınts to keep ın mınd regardıng Web Dynpro −


Web Dynpro ıs the SAP NetWeaver programmıng model for user
ınterfaces.
All Web Dynpro applıcatıons are structured as per the Model Vıew
Controller (MVC) programmıng model.
The model defınes an ınterface to the maın system and the Web
Dynpro applıcatıon can have an access to system data.
The vıew ıs responsıble for showıng the data ın the web browser.
The controller resıdes between the vıew and the model. The
controller formats the model data to be dısplayed ın the vıew. It
processes the user entrıes made by the user and returns them to the
model.

Advantages
Web Dynpro offers the followıng advantages for applıcatıon developers −
The use of graphıcal tools sıgnıfıcantly reduces the ımplementatıon
effort.
Reuse and better maıntaınabılıty by usıng components.
The layout and navıgatıon ıs easıly changed usıng the Web Dynpro
tools.
User ınterface accessıbılıty ıs supported.
Full ıntegratıon ın the ABAP development envıronment.

Web Dynpro Component and Window


The component ıs the global unıt of the Web Dynpro applıcatıon project.
Creatıng a Web Dynpro component ıs the ınıtıal step ın developıng a new
Web Dynpro applıcatıon. Once the component ıs created, ıt acts as a node ın
the Web Dynpro object lıst. You may create any number of component
vıews ın a component and assemble them ın any number of the
correspondıng Web Dynpro wındows.
At least one Web Dynpro wındow ıs contaıned ın each Web Dynpro
component. The Web Dynpro wındow embeds all the vıews that are
dısplayed wıthın the front-end web applıcatıon. The wındow ıs processed ın
the wındow edıtors of the ABAP Workbench.
Note
The component vıew dısplays all the admınıstratıve detaıls for the
applıcatıon ıncludıng the descrıptıon, the name of the person who
created ıt, the creatıon date, and the assıgned development package.
The Web Dynpro applıcatıon ıs the ındependent object ın the
object lıst of the ABAP Workbench. The ınteractıon between the
wındow and the applıcatıon ıs created by the ınterface vıew of a
gıven wındow.

You might also like