Vous êtes sur la page 1sur 16

Introduction to ABAP

Gabor Viragh
1/20/2014
This document is partially based on the development blog ‘How to Develop in ABAP’ by
Thomas Weiss.

Contents
Developing on the server .............................................................................................................................. 2
Navigation in the Application Server ............................................................................................................ 3
A Hello World Program ................................................................................................................................. 4
The Debugger ................................................................................................................................................ 5
The Data Dictionary ...................................................................................................................................... 6
Data Types................................................................................................................................................. 6
Structures and Tables ............................................................................................................................... 7
Program Elements......................................................................................................................................... 8
Data Declarations ...................................................................................................................................... 8
Most common statements........................................................................................................................ 8
IF............................................................................................................................................................ 8
DO ......................................................................................................................................................... 9
WHILE .................................................................................................................................................... 9
Messages............................................................................................................................................... 9
OpenSQL statements ............................................................................................................................ 9
Dealing with internal tables .................................................................................................................... 10
Subroutines ............................................................................................................................................. 11
Function Modules ................................................................................................................................... 12
Parameters and selection screen............................................................................................................ 13
System Fields............................................................................................................................................... 14
Common Transactions ................................................................................................................................ 15
Usefull Links ................................................................................................................................................ 15
Developing on the server

 The SAP Netweaver Application Server consists of three layers: presentation,


application, and database. The purpose of this division in three layers is high
performance and scalability.
o The database contains the user data, but also the entire program code of
the SAP Application Server and application programs, all administrative
data, and Customizing settings. That is, the programs you develop are
stored in the database of the system.
o The application layer lies between the database layer and the presentation
layer. It consists of one or more application servers and a single message
server which is responsible for communication and load distribution within
this layer. The programs that process the business logic of an application
and all the development tools such as the ABAP Workbench run in this
layer.
o The presentation layer represents the interface with the user and is
responsible for the screen display.
 You write your programs using the ABAP Editor of the ABAP Workbench and the
tools integrated there, which are all part of the server.
 Creating or modifying of an object will only create an inactive version of the
object. The inactive object is not visible to other objects or programs. By
activating it, an active version will be generated that is visible to programs,
classes, etc.
 Basically every object is visible to any other object in the system, regardless of
package assignment.
Navigation in the Application Server

 Command field for the 'transaction code'. This is a shortcut to the transaction
(functionality) you want to execute.

 The prefix /n ends the current transaction and starts the new one, /o starts the
transaction in a new session.
 The SAP Easy Access screen has a folder for your favorite transactions, and
shows the SAP Menu.
 Input fields of a transaction marked with a checkbox are mandatory to fill:
. You will not be able to process to the next step
without filling these fields.
 Input fields having a button a their right side provide a ‘search-help’ that display a
list of possible values:

 Navigation buttons:
o The green button is the 'Back' button, which goes back to the previous
screen.
o The yellow button is the 'Exit' button that interrupts the transaction and
takes you straight back to the entry screen.
o The red button is the 'Cancel' button that stops the transaction, but will not
always return you to the entry screen (e.g. it can interrupt an input
validation).
A Hello World Program

 Start transaction SE80 or SE38 to write a program. In SE80 choose ‘Program’


from the dropdown list.
 Enter a program name that does not exist in the system. Create programs
beginning with Z or Y, most other prefixes are reserved for SAP.
 Choose a package for the program. $TMP is for local objects, whicht will not be
transported to other systems. Objects belonging to other packages will be added
to ‘transport requests’ and can be transported to other systems, like ‘Quality
Assurance / Consolidation’ system or the ‘Productive’ system. Development is
only done in Development systems.
 After the introductory statement (‘REPORT z_...’), every ABAP program follows a
fixed program structure that divides it into two parts:
o a global declaration part
o a procedural part
 Larger programs use a ‘TOP’ include to separate global declarations from the
procedures.
 Use the ‘Pretty Printer’ to format the code. Configure the Pretty Printer at Utilities-
Settings-ABAP Editor-Pretty Printer and select ‘Convert Uppercase/Lowercase’
and below ‘Keyword Uppercase’.
 Code sample:

REPORT zvg_test.

DATA: lv_string TYPE string.

lv_string = 'HELLO WORLD'.

WRITE: lv_string.

 Start a line with * to make it a comment line. Add in-line comments with “.

 Execute a syntax check with CTRL + F2 or

 Activate your code using CTRL + F3 or

 Run the code by pressing F8 or (or in SE38)


 Place the cursor on a keyword an use F1 to get the documentation for theat
keyword.
The Debugger

 You can place break-points at the most program statements. Use the button
to set break-points. Execute the program and it will stop at the first break-point.

 Use the following buttons to step forward:


o F5 executes the next step only
o F6 executes a form module, class method or function module without
jumping into in
o F7 executes and leaves the current module you are in
o F8 continues program execution until it reaches the next break-point (or
watch-point) or the end of the program.
 Double click on a variable to show it’s value or content.
 Choose the ‘Standard’ tab to see the call stack.

 Use the button to create a watch-point. The watch-point is reached


and program execution is interrupted when the entered variable changes its
value or content.

 Use the button to create further break-points, like bp. on a command, form,
method, etc.
The Data Dictionary

Data definitions (metadata) are created and managed in the ABAP Dictionary. The
ABAP Dictionary permits a central description of all the data used in the system without
redundancies.

You can create the corresponding objects (tables or views) in the underlying relational
database using these data definitions. The ABAP Dictionary therefore describes the
logical structure of the objects used in application development and shows how they are
mapped to the underlying relational database in tables or views.

Enter the Data Dictionary by calling the transaction SE11.

Data Types

Data types are defined on three levels:

 On the lowest level there are the basic types, like CHAR, DEC, INT4, RAW, etc.
They can not be used directly in programs or structure definitions. They can only
be used to define higher level data types (domains or data elements).
 A ‘domain’ defines the technical properties of the data, the basic type, length,
number of decimal places, value range and some others. For instance
Z_DEC_100 could be of type DEC, length 3, no decimal places, with possible
values between 0 and 100.
 A ‘data element’ defines the semantics of the data. For instance you can define
Z_EFFICIENCY to represent some percentual values, referring to the domain
Z_DEC_100, as the values are decimal and allways between 0 and 100. Data
element definitions also contain texts of different lengths called ‘Field Labels’
describing the data type. The field labels will be shown on screens, table
headers, tooltips and so on, like the ‘Program’ text on the following image:

The multi-level structure of data types allows the re-use of the data definitions and
assures that each data element has a fixed semantics. A new data element,
Z_WATER_TEMP, could use the same Z_DEC_100 domain, providing a new
semantics for values between 0 and 100.

Data elements can refer directly to low level basic data types, skipping the domain, if no
value restriction applies.
Structures and Tables

By defining a table in the Data Dictionary a corresponding table will be created in the
database itself. The definitions of sructures and tables are similar, but structures have
no content, nor a corresponding database object.

Structures are used for variable declarations in ABAP programs, or as interface


parameters of forms, methods or function modules.

Table names can also be used for the above purposes, just like structure names. In all
these cases the table name will mean the structure of the table, and will not define a
internal table or anything similar.

In SE11 enter a table name into the field ‘Database table’ to edit a table.

Structures and Data Elements are edited by entering their names into the field ‘Data
type’.

To create a table, enter a name that does not exist yet (and starts with Z …), and click
‘Create’. Add a short text and set the ‘Delivery Class’. Navigate to the ‘Field’ tab and
define the fields of the table: each field has a name and a data type, besides that they
can be marked as key fields.

Most tables will have the MANDT field as the first field, with data type MANDT, making
the table ‘client dependent’. That means that each ‘client’ of the SAP system will access
only those records of the table that belong to it. E.g. programs in client 100 will only
read and write records having 100 as MANDT value. Programs implicitely deal with the
MANDT field. That means that in most cases the developer does not have to deal with
the MANDT field, not even in DB statements like SELECT or UPDATE.

Currency and quantity fields always need a reference field, this can be set on the
‘Currency/quantity fields’ tab.

Click the ‘Technical Settings’ button to maintain mandatory technical properties of the
table.

Click ‘Activate’ to save and generate the active object.

Table content can be viewed with the transaction SE16N.


Program Elements

Data Declarations

Besides simple variables, in ABAP it’s possible to use structures and internal tables as
variables. Structures simply have multiple fields. Tables are called ‘internal tables’ as
they are available only internally for the running program, just like any other variable.
Tables have a structure and can store multiple lines.

There are no pre-defined arrays in ABAP, in most cases internal tables are used
instead! Although an OO implementation of such data types (array, stack, tree, etc.) is
possible.

DATA:
* simple variable referring to the data element S_CONN_ID:
lv_connid TYPE s_conn_id,
* simple variable that has the same type as the field CARRID of
* the DB table SPFLI
lv_carrid TYPE spfli-carrid,
* structure referring to DB table SPFLI:
ls_flight TYPE spfli,
* internal table referring to DB table SPFLI:
lt_flights TYPE STANDARD TABLE OF spfli.

It is recommended to use a prefix according to the standard naming conventions:


 L – local
 G – global
 V – variable
 S – structure
 T – table
That is, LS_FLIGHT is a local structure.

Most common statements

IF

IF sy-subrc = 0.
lv_carrid = ls_spfli-carrid.
ELSEIF sy-subrc = 1.
CLEAR lv_carrid.
ELSE.
RETURN.
ENDIF.

DO

DO 10 TIMES. “ will be executed 10 times


lv_counter = lv_counter + 1.
ENDDO.

DO. “ the loop can be left with the EXIT statement


lv_counter = lv_counter + 1.

IF lv_counter = 10.
EXIT.
ENDIF.
ENDDO.

WHILE

WHILE lv_counter <= 10.


lv_counter = lv_counter + 10.
ENDWHILE.

Messages

Messages of type E will end the program:

MESSAGE 'Error' TYPE 'E'.

Information messages will be shown in a pop-up dialog:


MESSAGE 'Succes' TYPE 'I'.

OpenSQL statements

ABAP programs can directly access the database tables. The syntax is ABAP-specific
and independent from the physical database management system (Oracle, DB2, etc.).

Read DB table SPFLI, filter by CARRID, and put the records into the internal table
LT_SPFLI:

SELECT * FROM spfli INTO TABLE lt_spfli


WHERE carrid = p_carrid.
If the field list does not fully match the target structure, use the addition ‘INTO
CORRESPONDING FIELDS OF …’
SELECT carrid connid fltime
FROM spfli
INNER JOIN scarr
ON scarr~carrid = spfli~carrid
INTO CORRESPONDING FIELDS OF TABLE lt_spfli
WHERE carrid = p_carrid
ORDER BY connid.

Modify DB table SPFLI, overwriting the record having the same keys as LS_SPFLI with
the values contained in LS_SPFLI:

UPDATE spfli FROM ls_spfli.

Insert a new record into DB table SPFLI:


INSERT spfli FROM ls_spfli.

Delete records of DB table SPFLI that have CARRID = p_carrid:


DELETE FROM spfli WHERE carrid = p_carrid.

Dealing with internal tables

Delete all the records of an internal table:

CLEAR lt_spfli.

Add a row to the internal table LT_SPFLI:

APPEND ls_spfli TO lt_spfli.

Read a single record of the internal table LT_SPFLI by row number and store it in the
structure LS_SPFLI:

READ TABLE lt_spfli INDEX 2 INTO ls_spfli.

Read record specifying table keys:


READ TABLE lt_spfli INTO ls_spfli
WITH KEY carrid = p_carrid
connid = p_connid.

Read the records of the internal table LT_SPFLI one by one and put them into the
structure LS_SPFLI:
LOOP AT lt_spfli INTO ls_spfli.
WRITE: sy-tabix,
ls_spfli-carrid,
/ls_spfli-connid, “ / means new line
/ls_spfli-fltime.
ENDLOOP.

Note that sy-tabix has the index (row nubmer) of the current line!

Delete table row:

DELETE lt_spfli INDEX 2.

Delete current line within a loop (processing will continue with the next line):

LOOP AT lt_spfli INTO ls_spfli.


DELETE lt_spfli.
ENDLOOP.

Subroutines

Subroutines in ABAP are called forms. They are only used in the procedural ABAP
programming. In ABAP OO the modularization is accomplished by methods.

Form declaration:

* signature:
FORM read_db_spfli
* parameter table that can be modified, having the sructure SPFLI
TABLES pt_spfli STRUCTURE spfli
* input parameters that can not be modified
USING p_carrid TYPE s_carr_id
p_connid TYPE s_conn_id
* parameter that can be modified
CHANGING p_spfli TYPE spfli.

* coding:
SELECT * FROM spfli
INTO TABLE pt_spfli
WHERE carrid = p_carrid AND
connid = p_connid.

READ TABLE pt_spfli INDEX 1 INTO p_spfli.

ENDFORM. "read_db_spfli
Calling a form:

PERFORM read_db_spfli
TABLES lt_spfli
USING p_carrid
p_connid
CHANGING ls_spfli.

Function Modules

Function modules allow you to encapsulate and reuse global functions in the SAP
System. They are managed in a central function library.

Function modules are procedures that are defined in special ABAP programs only,
so-called function groups, but can be called from all ABAP programs. Function
groups act as containers for function modules that logically belong together.

SE37 – Function Builder:


Calling a function module:

CALL FUNCTION 'F4IF_FIELD_VALUE_REQUEST'


EXPORTING
tabname = 'DD02L'
fieldname = 'FNAME'
IMPORTING
user_reset = lv_reset
TABLES
return_tab = lt_return
EXCEPTIONS
field_not_found = 1
no_help_for_field = 2
inconsistent_help = 3
no_values_found = 4
OTHERS = 5.

IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

Parameters and selection screen

Parameters and select-options are variables just like any other variable declared
with ‘DATA’. But a so called ‘selection screen’ will be generated that displays the
corresponding fields, accepting input. Select-options are value ranges (internal
tables) that can be used in the WHERE clauses of SELECT statements ( … WHERE
fltime IN so_ftime …)

PARAMETERS: p_carrid TYPE s_carr_id,


p_connid TYPE s_conn_id.

TABLES: spfli.
SELECT-OPTIONS: so_ftime FOR spfli-fltime.

Result:

Select Goto->Text Elements->Selection Texts and mark the checkboxes ‘Dictionary


Reference’ to set the field labels from the dictionary. Activate!
Result:

System Fields

System variables can be accessed during runtime using the ‘sy’ structure. For a full
list of fields display the structure SYST in SE11.

sy-subrc Return code of statements, methods, function modules, etc.


sy-datum Current date
sy-uzeit Time (HHMMSS)
sy-index Loop index within DO and WHILE loops
sy-tabix Loop index (line number) within LOOP AT itab
sy-uname User ID
sy-dbcnt Record count after OpenSQL commands (SELECT, UPDATE,
MODIFY, DELETE)
sy-repid Name of the current program
sy-tcode Current transaction code
Common Transactions

SE80 ABAP Workbench - Object Navigator


SE11 Data Dictionary
SE16N Table Content
SE38 Program Editor
SE24 Class Builder
SE37 Function Builder

Usefull Links

 Data Dictionary
http://help.sap.com/erp2005_ehp_06/helpdata/en/cf/21ea0b446011d189700000e8322d00/content.ht
m?frameset=/en/cf/21eac5446011d189700000e8322d00/frameset.htm

 Flight Model
http://help.sap.com/erp2005_ehp_06/helpdata/en/cf/21f304446011d189700000e8322d00/content.ht
m?frameset=/en/cf/21eac5446011d189700000e8322d00/frameset.htm

 Interne Tabelle
http://help.sap.com/saphelp_erp2004/helpdata/de/fc/eb35de358411d1829f0000e829fbfe/frameset.ht
m

 Modularisierungstechniken
http://help.sap.com/saphelp_nw2004s/helpdata/de/9f/db970e35c111d1829f0000e829fbfe/content.ht
m

 Bildschirme
http://help.sap.com/saphelp_47x200/helpdata/de/d3/2e974d35c511d1829f0000e829fbfe/frameset.ht
m


Funktionsbausteine http://help.sap.com/saphelp_nw70/helpdata/de/9f/db988735c111d1829f0000e82
9fbfe/content.htm

 ABAP OO Tutorial http://scn.sap.com/docs/DOC-10236

Vous aimerez peut-être aussi