You are on page 1of 69

ABAP

Objectives

At the end of this lesson, you will be able to:


◼ Have an overview on ABAP Programming Language
◼ Understand the basic syntax of ABAP
◼ Navigate ABAP editor and Get help
◼ Create and Execute a simple report in SAP

2
Table of Content

◼ ABAP Overview
◼ ALV Report
◼ Basic ABAP
◼ Revisit Basic ABAP
◼ Transaction Code
◼ ABAP Dictionary
◼ Internal Table
◼ Open SQL
◼ Selection Screen
◼ Function Module
◼ Revisit ALV Report
◼ Conclusion

3
ABAP Overview
Overview of ABAP

T h e A B A P /4 P ro g ra m m in g L a n g u a g e

A dvanced
B u s in e s s
A p p lic a tio n
P ro g ra m m in g
/
4

© SAP AG

5
Overview of ABAP

◼ ABAP is a programming language developed by SAP for the interactive


development of application programming

◼ ABAP is a fourth generation language

◼ ABAP is tightly integrated across all the modules like SD, MM, FI, CO, HCM, etc.
and most or all of the modules and transactions (note that the screens are
created using ABAP)

◼ All application programs, along with parts of the R/3 Basis system, are written in
the ABAP Workbench using ABAP

◼ The individual components of application programs are stored in a special


section of the database called Repository

◼ Not every SAP user is allowed to create ABAP objects in SAP system, only user
with developer key is allowed

6
ABAP Workbench

◼ The ABAP Workbench provides access to development tools which cover the
entire software development cycle

◼ These tools can be used both for customer-specific developments and


enhancements to standard applications supplied by SAP

◼ All applications created with the ABAP Workbench can run without further
modifications on any platforms, database systems and graphical user interfaces
supported by SAP

◼ ABAP Workbench consists of many tools for like ABAP Editor, Screen Painter,
Function Builder, Menu painter… for you to create or write Programs like
Reports or Screens or Menus etc ...

7
ABAP Objects

A B A P /4 P ro g ra m O b je c ts
D e v e lo p m e n t c la s s

D e v . c la s s o b je c t ty p e s P ro g ra m

D ic tio n a ry o b je c ts G lo b a l d a ta

P ro g ra m s P B O m o d u le s

F u n c tio n g ro u p s P A I m o d u le s

In c lu d e s S u b ro u tin e s

T ra n s a c tio n s S c re e n s

L o g ic a l d a ta b a s e s G U I s ta tu s

M e s s a g e c la s s e s In c lu d e s

.
. T ra n s a c tio n s
.
© SAP AG

8
ALV Report
Introducing ALV

◼ ALV name comes from "ABAP List


Viewer", as named initially because it is
only available in ABAP

◼ ABAP List Viewer is a tool that outputs


data in a table form (rows and columns),
with integrated functions to manipulate
output (sort, totals, filter, column order,
hide, etc.) and export it (Excel, Crystal
report, CSV files, etc.)

◼ It is also possible to make ALV editable


via ALV control

◼ ALV tool proposes 3 display types:

10
Navigating ALV Report

11
Sorting Record

2 1

12
Showing Record Details

13
Filtering Record

2 1

14
Counting Total

15
Counting Total

16
Counting Subtotal

2
1

17
Counting Subtotal

18
Downloading Report as HTML Format

19
Downloading Report as Text Format

20
Changing Layout

21
Basic ABAP
Creating an ABAP Program

◼ To open ABAP programs


directly by using the ABAP
Editor, choose ABAP
Editor in the ABAP
Workbench screen or start
Transaction code SE38,
and enter a program name

23
Maintaining an Existing Program

◼ To edit an existing program, enter its name on the initial screen of the ABAP
Editor (Transaction code SE38), select one of the following components, and
then choose Display or Change
◼ Source Code – Starts the ABAP Editor
◼ Variants – Allows you to define fixed values for the input fields on the
selection screen of a report
◼ Attributes – Allows you to change the program attributes
◼ Documentation – Allows you to write documentation for a particular
executable program (report)
◼ Text elements – Allows you to maintain text elements including title and
headers, selection texts, and numbered texts, the text elements are
language independence of your program

24
Naming Convention

◼ Custom reports must start with Y or Z

◼ The name consists of at least one character, to a maximum of 40 characters

◼ Do not use any special characters except underscore (_)

25
Types of ABAP Program

◼ Report Program
◼ Generates a list from database tables in a user defined format
◼ Does not alter the data in the database but only analysis (reads) them
◼ Can display the results on the screen or send to a printer
◼ Can either be run online or as background job

◼ Dialog Program (also known as Module Pool Program)


◼ Can read and change database tables
◼ Accepts user information, processes the information and updates the
database
◼ Cannot be executed in background

26
ABAP Syntax

A B A P /4 S y n ta x
DATA COUNTER TYPE I.
DATA NAME(20).

MOVE 1 TO COUNTER.
MOVE 'ABC' TO NAME.
. A B A P /4 p ro g ra m
.
. s ta te m e n t
WRITE NAME.
WRITE COUNTER.
w o rd 1 w o rd 2 w o rd 3

w o rd 4 .

k e y w o rd p a ra m e te r, fie ld ,
c o n s ta n t
© SAP AG

27
ABAP Syntax

◼ An ABAP program consists of


individual statements

◼ Each statement must end with a


period (.)

◼ The first word of a statement is


known as keyword

◼ Words are separated from each other


by at least one blank

◼ Statements can be indented

◼ Statements can be extended over


several lines

28
Chain Statement

C h a in S ta te m e n ts

WRITE 'XYZ'.
WRITE NAME.
WRITE COUNTER.

WRITE: 'XYZ', NAME,


COUNTER.

ADD 1 TO COUNTER1.
ADD 1 TO COUNTER2.
ADD 1 TO COUNTER3.

ADD 1 TO: COUNTER1,


COUNTER2,
COUNTER3.

© SAP AG

29
Formatting ABAP Statement

◼ ABAP has no format restriction

◼ You must separate word within a statement with at least one space

◼ You can write several statements on one line, or spread a single statement over
several lines

◼ You can concatenate consecutive statements with an identical first part into a
chain statement by writing the identical part only once and placing a colon (:)
after it
◼ Example - WRITE: SPFLI-CITYFROM, SPFLI-CITYTO, SPFLI-AIRPTO.

30
Comment

◼ You can insert comments


into a program in two
ways:
◼ An asterisk (*) in
column 1 flags the
whole line as a
comment
◼ A quotation mark (")
within a line flags the
remainder of the line
as a comment

31
ABAP Program Layout

◼ To write a high quality program, you should keep certain layout standards for
ABAP Program

◼ Follow when structuring your program flow, and use as many as informative
comments as possible

◼ If you follow these suggestions your program will be


◼ More readable
◼ Easier to test and change
◼ More reliable

◼ To improve the quality of your programs, the following techniques can be


adopted:
◼ Indent statement block
◼ Use modularization tool
◼ Insert program comment

32
ABAP Program Layout

◼ Indent statement block


◼ Combine statements that belong together into a single block
◼ Indent each block by at least two columns

◼ Use modularization tool


◼ Write larger processing blocks as subroutines and the logical structure of the
program becomes easier to understand
◼ Allows you to sort the subroutines according to the tasks they perform

◼ Insert program comment


◼ Avoid placing comments on statement line
◼ Place comment on separate line to improve the readability of the program
◼ Insert subroutine heading and comment in your program by using the ready-
made structures available in ABAP Editor

33
Pretty Printer

◼ The ABAP Editor


includes a tool which
helps you to design the
layout of your program
more easily

◼ It follows ABAP Layout


guidelines

34
Pretty Printer

35
WRITE Statement

* Syntax
WRITE <f> <AT position(length)>
<OPTION>
<AS CHECKBOX>
<AS SYMBOL>
<AS ICON>
<AS LINE>

36
WRITE Statement

◼ <OPTION> it is for formatting if placed behind the WRITE statement


◼ Example: NO-ZERO, NO-SIGN, No GROUPING, DD/MM/YY, LEFT-
JUSTIFIED, CENTERED, RIGHT-JUSTIFIED

◼ The most commonly used options are:


◼ CURRENCY w - to treat the content of the field <f> as a currency amount
with w as a currency key
◼ DECIMALS d - to determine the number of decimals to be displayed
◼ UNIT u - to format the value according to the unit specified in the field u

◼ Common example:

37
WRITE Statement

◼ WRITE outputs the


contents of a field or
constant in the format
appropriate for the type

◼ Consecutive WRITE
statements output data
on the same line

◼ If there is no more space


on one line, the output
continues on the next line

◼ SKIP generates blank


line

38
Common System Fields

Name Type Length Meaning


Current Date of Application
SY-DATUM DATS 8
Server

Current Time of Application


SY-UZEIT TIMS 6
Server
SY-UNAME CHAR 12 User Name

SY-SUBRC INT4 10 Return Value of ABAP Statements

SY-CPROG CHAR 40 Calling Program

SY-TCODE CHAR 20 Current Transaction Code

Language Key of Current Text


SY-LANGU LANG 1
Environment

39
Common Functions in ABAP Editor

◼ Pretty Printer
◼ Use to standardize the
layout of your program

◼ Activate
◼ When you activate a
program, the activation
process saves the
program in inactive
version first, checks
for syntax errors and
then generates the
active version

◼ Direct Processing
◼ Use to run the
program
40
Using ABAP Help

◼ To know more about


ABAP keywords,
highlight the
keywords and hit the
F1 function key for
documentation

◼ For other fields, SAP


provides
Performance
Assistant when hitting
the F1 function key

41
Tutorial

Create a program called ZXXXR_CHECKDATE

◼ In the program, show the wording ‘Today is’ and the


current date in the first line

◼ In the second line, show the wording ‘My User ID is’


and your user ID

42
Revisit Basic ABAP
Data Type

◼ Describes the technical


attributes of all the objects
with that type

◼ Three categories of types,


which are: standard, local
and global

44
Standard Data Type

◼ Complete ABAP standard types


◼ D - Type for date, format: YYYYMMDD, length 8 (fixed)
◼ T - Type for time, format: HHMMSS, length 6 (fixed)
◼ I - Type for integer, length 4 (fixed)
◼ F - Type for floating point number, length 8 (fixed)
◼ STRING - Type for dynamic length character string
◼ XSTRING - Type for dynamic length byte sequence (HeXadecimal string)

◼ Incomplete ABAP standard types (Not contain a fixed length)


◼ C - Type for character string (Character)
◼ N - Type for numerical character string (Numerical character)
◼ X - Type for byte sequence (HeXadecimal string)
◼ P - Type for packed number (Packed number with or without decimal point)

45
Local Data Type

◼ Based entirely on predefined standard data types

◼ Can only be used in a program

◼ Use the TYPES statement to define your own local data type

◼ For examples:

TYPES number TYPE I.

DATA no_fights TYPE number.

46
Global Data Type

◼ Defined in the ABAP


Dictionary

◼ Can be used throughout


the entire system

47
DATA Statement

* Syntax
DATA var[(length)] [TYPE type] [Decimals number].
DATA var LIKE Table-Field [VALUE initial value].

48
CLEAR Statement

* Syntax
CLEAR <data object>.

49
Arithmetic Operation

◼ ABAP supports the four basic arithmetic operations, as well as power calculation

◼ You can specify the following arithmetic operators in a mathematical expression:

+ Addition
- Subtraction
* Multiplication
/ Division
DIV Integral division without remainder
MOD Remainder after integral division
** Exponentiation

50
Looping Operation

◼ Four kinds of looping in ABAP:


◼ Unconditional loop using the DO statement
◼ Conditional loop using the WHILE statement
◼ Loops through internal table and extract dataset using the LOOP statement
◼ Loops through datasets from database table using SELECT statement

51
Looping Operation - DO Statement

◼ DO Statement:

DO <n> TIMES.

statements

ENDDO.

SY-INDEX Loop index

52
Looping Operation – WHILE Statement

◼ WHILE Statement:

WHILE <logical expression>.


statements

ENDWHILE.

SY-INDEX Loop index

WHILE COUNTER > 0.


.
.
.
SUBTRACT 1 FROM COUNTER.
ENDWHILE.

53
Looping Operation – LOOP Statement

◼ LOOP Statement:

LOOP AT <internal table>.

statements

ENDLOOP.

SY-INDEX Loop index

54
IF Statement

IF <logical expression>.
statements
ENDIF.

IF <logical expression>.
statements

ELSE.
statements
ENDIF.

IF <logical expression>.
statements

ELSEIF <logical expression>.


statements

ELSEIF <logical expression>.


statements

ELSE.
statements

ENDIF.

55
CASE Statement

◼ Executes different statement CASE <field>.


blocks depending on the
contents of particular data fields WHEN <value1>.
statements
◼ WHEN OTHERS:
WHEN <value2>.
◼ Contents of <field> does not statements
equal to any of the <value>
contents WHEN <value3>.
statements

WHEN OTHERS.
statements

ENDCASE.

56
CHECK Statement

CHECK <logical expression>.

1 CHECK within a loop structure.

WHILE COUNTER GT 0.
◼ Bypass the following
statements and go to statements
next record CHECK FLAG NE SPACE.
statements

ENDWHILE.

2 CHECK outside loop structures.


◼ Terminate a subroutine statements
conditionally
CHECK <condition>.
statements

57
EXIT Statement

EXIT.

1 EXIT within a loop structure.


DO ...
◼ Terminate loop statements
entirely without any IF COUNTER GE 100.
condition EXIT.
ENDIF.
statements

ENDDO.
2 EXIT outside loop structure.

◼ Terminate a statements
subroutine without IF ... .
any condition EXIT.
ENDIF.
statements

58
CONTINUE Statement

CONTINUE.

DO 100 TIMES.
statements
◼ Terminate loop pass
immediately without IF SY-INDEX GE 10
any condition AND SY-INDEX LE 20.

CONTINUE.

ENDIF.
statements

ENDDO.

59
Logical Expression

◼ AND
COUNTER GE 0
◼ OR

◼ NOT
COUNTER GT 0 AND
FLAG IS INITIAL

( FLAG1 NE SPACE OR FLAG2


NE SPACE ) AND COUNTER
BETWEEN 0 AND 100

COUNTER EQ 0 AND NOT


( FLAG1 EQ SPACE AND FLAG2
EQ SPACE )

60
MESSAGE Statement

* Syntax
MESSAGE text TYPE mtype.
MESSAGE { ID mid TYPE mtype NUMBER num | tn(id) }.

61
MESSAGE Statement

◼ Example:

62
MESSAGE Statement

◼ Example:

63
Message (SE91)

64
Tutorial

Create a program called ZXXXR_LOOP

◼ In the program, list out numbers 1 to 10 in every


line

◼ Replace every odd number by ‘This is odd’

◼ If the number can be divided by 4, skip

◼ Pop-up a message box with a text ‘Finish!’

65
Transaction Code
Transaction (SE93)

1
2

67
Tutorial

Create a t-code called ZXXX_LOOP

◼ Assign this t-code to report ZXXXR_LOOP

68

You might also like