Vous êtes sur la page 1sur 27

Introduction to ABAP

This document is partially based on the development blog How to Develop in ABAP by
Thomas Weiss.

Contents
SAP as ERP System ........................................................................................................................................ 3
About SAP ..................................................................................................................................................... 3
Developing on the server .............................................................................................................................. 4
Navigation on the Application Server ........................................................................................................... 6
ABAP Workbench Basics ............................................................................................................................... 8
The Debugger .............................................................................................................................................. 10
The Data Dictionary .................................................................................................................................... 11
Data Types............................................................................................................................................... 11
Structures and Tables ............................................................................................................................. 12
DDIC Overview ........................................................................................................................................ 13
How to create a DB table or structure in SE11 ....................................................................................... 13
Where-Used-List ......................................................................................................................................... 14
Program Elements....................................................................................................................................... 17
Data Declarations .................................................................................................................................... 17
Most common ABAP statements ............................................................................................................ 19
IF.......................................................................................................................................................... 19
DO ....................................................................................................................................................... 19
WHILE .................................................................................................................................................. 19
Messages............................................................................................................................................. 19
OpenSQL statements .......................................................................................................................... 20
Dealing with internal tables .................................................................................................................... 20
Subroutines ............................................................................................................................................. 21
Function Modules ................................................................................................................................... 23
Parameters and Select-Options on a Selection Screen .......................................................................... 24
System Fields............................................................................................................................................... 25
Common Transactions ................................................................................................................................ 26
Usefull Links ................................................................................................................................................ 26
SAP as ERP System

http://en.wikipedia.org/wiki/Enterprise_resource_planning

Enterprise resource planning (ERP) is business management software usually a suite


of integrated applications that a company can use to collect, store, manage and
interpret data from many business activities, including:
Product planning, cost
Manufacturing or service delivery
Marketing and sales
Inventory management
Shipping and payment
ERP provides an integrated view of core business processes, using common
databases maintained by a database management system. Operates in (or near) real
time without relying on periodic updates. Has a consistent look and feel across
modules.

About SAP

http://en.wikipedia.org/wiki/SAP_SE

SAP SE (Systems, Applications & Products in Data Processing) is a German


multinational software corporation that makes enterprise software to manage business
operations and customer relations. SAP is headquartered in Walldorf, Baden-
Wrttemberg, Germany, with regional offices in 130 countries.

Number of employees: 74,400 (2014) (Microsoft: 128.000)

Revenue: 16.81 billion (2013)

Profit: 3.32 billion (2013)


Developing on the server

The SAP Netweaver Application Server consists of three layers: database,


application and presentation layer. 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. All the ABAP programs are executed in this layer.
o The presentation layer represents the interface with the user and is
responsible for the screen display. This is the SAP GUI on the PC.

o
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. Development objects are assigned to packages for
administration purposes.
Navigation on the Application Server

Command field for the 'transaction code'. This is a shortcut to the transaction
(program) 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 on their right side provide a search-help that
displays 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).
Transactions:
o Programs can be started with transaction codes. A transaction in SAP is
basically an ABAP program.
o Many transactions start with a so-called selection screen. This allows
entering filter criteria for the data that is going to be selected. On a
selection screen there can be parameters or select options. A parameter
means a single value. A select option can define complex criteria for the
filter.
o
o Above you see a simple selection screen. Country key is a parameter. The
selection will read only those lines, that have TR as country key.
Connection number is a select option. The selection will consider all lines
that have this number between 100 and 900. The conditions of the
selection screen will be joined with an AND (that is, the selected lines
have to fulfill all criteria, both country key TR and con. nr. between 100
and 900).
o A select option can define not only intervals, but also more complex
criteria. Click the button with the right arrow to have access to further
options:

o
o In the above example there are three intervals defined (joined with an
OR). But its also possible to define a list of single values, or to exclude
values from the selection.
Double-click navigation:
o Several screen fields respond to double-click. It usually takes you to the
definition of the object, like a table definition in the dictionary.
o Also use double-click in the ABAP code to navigate to the objects
definition (e.g. declaration of a variable or method, dictionary objects, etc.)
ABAP Workbench Basics

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. Also selection-screens are usually defined in the TOP-include.
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.
Use double-click for navigation. Click a variable to jump to the declaration, click a
form/method/function module name to go to the implementation. Several objects
in the ABAP code will react to double-click, like screen numbers, text elements,
messages, titlebars, program statuses and so on.
Use the Where-Used-List to find a variable/form/method/etc. in your code. Place
the cursor on the object name and click the Where-Used-List button .
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 its 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.

Note that table types can also be defined in the dictionary. A program variable or
method parameter can be multi-dimensional; you can use a table type to define these
tables.
DDIC Overview

How to create a DB table or structure in SE11

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.

Where-Used-List

The where-used-list button is available in several transactions of the ABAP


Workbench. Use this button to find where the selected object is referred. This can be an
ABAP program code, but also other development objects in the system.

Example:

You are displaying a table in SE11:

Click the where-used-list to see where this table is referred. Mark only those object
types that you are interested in:
When you get the result list, you can navigate to the listed objects by double click.
Program Elements

Data Declarations

Besides simple variables, in ABAP its 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,
* internal table defined by a table type defined in the dictionary:
lt_flights_2 TYPE spfli_tab.

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.

Examples for variables:

Simple variable

Value of lv_connid:

106

Declare it using:
A data element: s_conn_id
A reference to a structure/table field: spfli-connid

Structure

Content of ls_sflight (structure):

AA 17 US NEW YORK JFK US SAN FRANCISCO SFO 6:01

Declare it using:
A table name: spfli
A structure name:

Internal table

Content of lt_flights (internal table):

AA 17 US NEW YORK JFK US SAN FRANCISCO SFO 6:01


AZ 555 IT ROME FCO DE FRANKFURT FRA 2:05
AZ 789 JP TOKYO TYO IT ROME FCO 15:40
DL 106 US NEW YORK JFK DE FRANKFURT FRA 7:55
JL 407 JP TOKYO NRT DE FRANKFURT FRA 12:05
JL 408 DE FRANKFURT FRA JP TOKYO NRT 11:15
LH 400 DE FRANKFURT FRA US NEW YORK JFK 7:24
LH 401 US NEW YORK JFK DE FRANKFURT FRA 7:15
LH 402 DE FRANKFURT FRA US NEW YORK JFK 7:35
LH 2402 DE FRANKFURT FRA DE BERLIN SXF 1:05

Declare it using:
A table type (no table!) defined in the DDIC
A structure/table and TYPE TABLE OF construct, see examples above.
Most common ABAP 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 stores 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 Select-Options on a 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