My Book

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

If you are new to ABAP, there are 2 point you should learn first, in following order:

1. Explore Data Dictionary


Data dictionary is a workplace for defining table, define data type, display table content.
Go to transaction code SE11 (SAP Menu->Tools->ABAP Workbench->Development->Data
Dictionary).
Then enjoy exploring data dictionary.
2. Create ABAP Report
Go to transaction code SE38 (SAP Menu->Tools->ABAP Workbench->Development->ABAP
Editor), and try to write your first program.

SUNDAY, JULY 15, 2007

Data Dictionary

Data dictionary provide:


1. Table definition
2. Type definition

Data dictionary can be accessed by transaction code SE11. (SAP Menu->Tools->ABAP


Workbench->Development->Data Dictionary).

Table definition.
Display table definition.Go to Transaction Code SE11, enter table name (example:
SFLIGHT), click Display.

You can assign the data type, length and short text in different ways:
1. You directly assign the field a data type, field length (and if necessary decimal
places) and short text in the table definition.
2. You can assign the field a data element. The data type, field length (and decimal
places) are determined from the domain of the data element. The short description of
the data element is assigned to the field as a short text. In above case, all fields refer
to data element (Field CARRID refer to date element S_CARR_ID). We will explain
about data element in Type Definition below.To toggle beetween data element /
direct type method click button "Data Element/Direct type".
To display table content, click Utilities -> Table Contents -> Display, then click
Execute (F8).
Type DefinitionWe can define type as reusable object. It means that when we create
new table, we can create field refer to data element. These type attributes can
either be defined directly in the data element or copied from a domain. Data element
is an object where we put short text for field, and domain is an object where we
store information like data type (CHAR, NUMC) and its length. We can see relation
beetween table field, data element and domain in figure below.
Benefit of using this hierarchy is when you change domain, for example change field
length, it will change all field length for all table using this domain.
To open a data element, go to TCode SE11, select data type, and enter data element
name (for example S_CARR_ID). You will see in this screen that data element
S_CARR_ID refer to domain S_CARR_ID ( in this case, data element and domain have a
same name), and also display field label tab, it contain short text that appear in short
text of table field.
To open a domain, go to TCode SE11, select domain, and enter domain name (for
example S_CARR_ID), you will see that this domain have CHAR (character) data type
and field length 3.

My First program

No, it's not a "hello world" program ;p

One of main job of an ABAPer is create ABAP report.


Report content 4 basic component.
1. Data declaration.
2. Selection screen.
3. Select Data.
4. Write Report.

I assume you already familiar with data dictionary. For an example, we have a table
"SFLIGHT", with following fields:
1. CARRID (Airline carrier ID)
2. CONNID (Flight connection Id)
3. FLDATE (Flight date).
4.SEATSMAX (Maximum capacity).
We wan to create a report that can be filtered based on Airline carrier ID and Flight
connection Id.
Go to transaction code SE38 (SAP Menu->Tools>ABAP Workbench->Development->ABAP
Editor), enter program name with prefix Z, for example ZTEST0001, then choose
"Create" button.

Then, enter title for program, and choose 1 "Executable Program" for program type. If
screen input for development class appear, click "Local Object".

Then, go to following steps.


1. Data declaration

TABLES: sflight.

DATA: BEGIN OF t_report OCCURS 3,


carrid LIKE sflight-carrid,
connid LIKE sflight-connid,
fldate LIKE sflight-fldate,
seatsmax LIKE sflight-seatsmax,
END OF t_report.

2. Selection screen

SELECT-OPTIONS s_carrid FOR sflight-carrid.


SELECT-OPTIONS s_connid FOR sflight-connid.

It will generate selection screen like picture below.


3. Select data

SELECT * FROM sflight


WHERE carrid IN s_carrid AND
connid IN s_connid.
t_report-carrid = sflight-carrid.
t_report-connid = sflight-connid.
t_report-fldate = sflight-fldate.
t_report-seatsmax = sflight-seatsmax.
APPEND t_report.
ENDSELECT.
IF sy-subrc NE 0. "sy-subrc = return code
WRITE 'Data not found'.
ENDIF.

4. Write data

LOOP AT t_report.
skip. "comment:Go to next line
WRITE t_report-carrid.
WRITE t_report-connid.
WRITE t_report-fldate.
WRITE t_report-seatsmax.
ENDLOOP.

The result :
Here is the complete program:

REPORT ZTEST0001 .
*Data Declaration
tables: sflight.

DATA: BEGIN OF t_report OCCURS 3,


carrid LIKE sflight-carrid,
connid LIKE sflight-connid,
fldate LIKE sflight-fldate,
seatsmax LIKE sflight-seatsmax,
END OF t_report.

*Selection Screen
SELECT-OPTIONS s_carrid FOR sflight-carrid.
SELECT-OPTIONS s_connid FOR sflight-connid.

*Get Data
SELECT * FROM sflight
WHERE carrid IN s_carrid AND
connid IN s_connid.
t_report-carrid = sflight-carrid.
t_report-connid = sflight-connid.
t_report-fldate = sflight-fldate.
t_report-seatsmax = sflight-seatsmax.
APPEND t_report.
ENDSELECT.
IF sy-subrc NE 0.
WRITE 'Data not found'.
ENDIF.

*Write Data
LOOP AT t_report.
skip. "comment:Go to next line
WRITE t_report-carrid.
WRITE t_report-connid.
WRITE t_report-fldate.
WRITE t_report-seatsmax.
ENDLOOP.

Declare single value variable:


Variable can be declared by two ways, first by declaring based on data type, second
by declared it refer to table field.

Data d_data1 TYPE type. " berdasarkan type data


Data d_data2 LIKE customer-customerno. " refer ke field tertentu

Data types:
C (character)
N (numeric)
D (date)
T (Time)
X (byte / hexadecimal)
I (integer)
P (packed number)
F (floating point number)

Internal Table
Internal tables provide a means of taking data from a fixed structure and storing it in
working memory in ABAP. The data is stored line by line in memory, and each line has
the same structure. In ABAP, internal tables fulfill the function of arrays.
DATA: BEGIN OF t_report OCCURS 10,
field1 LIKE customer-customerno,
field2 TYPE I,
field3(20) TYPE C,
END OF t_report.

In above example, we define internal table contatin 3 fields, field-1 refer to field
customer-customerno, field-2 type Integer, field-3 type character with field length 10
character. OCCURS 10 means that it reserve 10 line of memory at initialization, in run
time memory will expand automatically when data exceed the limit.
TUESDAY, JULY 17, 2007

Processing Internal Tables

Internal Table is a main part of ABAP programming. In ABAP, internal tables fulfill the
function of arrays. It is a data that have a structure and could contain more than one
row.

Please refer to Data declaration for creating an internal table.

These are how to process internal table.

1. Sort Internal Table

SORT inttab BY field1 field2 ... field-n.

2. Loop At internal Table


LOOP AT inttab.
ENDLOOP.

3. Modify
3.1. Modify in a LOOP.
LOOP AT inttab.
MOVE ... TO inttab-field1.
MODIFY inttab.
ENDLOOP.

3.2. MODIFY BY index


MODIFY inttab INDEX int_index.

3.3. MODIFY BY condition


MODIFY inttab WHERE field1 = ....

4. Insert / Append
APPEND inttab.

5. Delete
Delete have variant similar to modify, delete in a loop, delete by index, and delete by
criteria.

BDC (Batch Data Communication) Tutorial for Data Transfer

What is BDC
BDC ( Batch Data Communication ) is used for uploading mass data into SAP system. In
SAP system BDC also referred to batch input or data tranfer.
Typical Uses
Typical uses of batch input include the one-time import of data from a legacy system
into a newly installed R/3 System. Another typical use is for periodic transfers of data
from external systems or legacy systems that are still in use into SAP.

Background of BDC
To ensure data consistency in SAP system, we must not update SAP data directly from
ABAP program. We must upload data through similar program flow compared to
manual input by user. SAP provide this by BDC. BDC works by simulating the user input
from transactional screen via an ABAP program. This means that you do not bypass
any of the standard SAP consistency checks, authorisations, update conjunction
tables, etc.

How it works
Data input entered by user simulated in BDC by data packet. The transaction then
started using this internal table as the input and executed in the background.

Data packet is an internal table has a structure of BDCDATA, it has fields:


1. PROGRAM (program name)
2. DYNPRO (screen number)
3. DYNBEGIN (New screen start) X=new screen
4. FNAM (Field name)
5. FVAL (Field value)
Data packet contain of screen by screen packets. One screen packet contain:
1. Screen no
2. Cursor position
3. User command
4. Input fields
It implemented in internal table in this format:

PROGRAM DYNPRO DYNBEGIN FNAM FVAL


program1 screen1 X
BDC_CURSOR pos1
BDC_OKCODE comm1
fieldname1 fieldvalue1
fieldname2 fieldvalue2
program2 screen2 X
BDC_CURSOR pos1
BDC_OKCODE comm1
fieldname1 fieldvalue1
fieldname2 fieldvalue2
For example, we want to create a BDC to change ABAP program title.
Here is what we do manually: Go to screen SE38, enter program, select radiobutton
"Attributes", then click "Change". After that, change title then press "Save" button.
In BDC, we simulate this by following internal table:

PROGRAM DYNPRO DYNBEGIN FNAM FVAL


SAPLWBABAP 100 X
BDC_CURSOR RS38M-FUNC_HEAD
BDC_OKCODE =CHAP
RS38M-PROGRAMM ZAALTESTBDC
RS38M-FUNC_EDIT
RS38M-FUNC_HEAD X
SAPLSEDTATTR 200 X
BDC_CURSOR RS38M-REPTI
BDC_OKCODE =CONT
RS38M-REPTI Test change title BDC
TRDIR-SUBC 1
TRDIR-FIXPT X
SAPLWBABAP 100 X
BDC_CURSOR RS38M-PROGRAMM
BDC_OKCODE =BACK
RS38M-PROGRAMM ZAALTESTBDC
RS38M-FUNC_HEAD X

To accomodate you to build data packet, SAP provide BDC recording in tcode SHDB.
Do following action:
1. Go to tcode SHDB
2. click "New recording", enter recording name to identified your record, and TCode
to be recorded.
3. You will enter recording mode of the transaction, simulate action you want to
perform in this transaction
4. At the end it will result internal table ready to upload to data transfer methods
(Call transaction or BDC sessions).

After internal table created then we pass this to data transfer methods. There are
two alternatives of data transfer methods, using Call Transaction or BDC session.
Call transaction performed by calling command ‘Call Transaction’. ABAP program
must do the error handling based on returning table from call transaction command.
It is used for real-time interfaces and custom error handling & logging features. This is
suitable when processing sequential update, it means, next data will depend on
previous data.
In BDC Sessions, the ABAP program creates a session with all the transactional data,
and this session can be viewed, scheduled and processed (using Transaction SM35) at
a later time. The latter technique has a built-in error processing mechanism too.

Other useful article:


BDC Tutorial in SAP-Img

MONDAY, JULY 23, 2007

Debugging ABAP Program

To go to ABAP debugging mode, activate ABAP debuggin then press enter. In ABAP
report, usually debuggin mode started from selection screen, just before we enter
execute command (F8).

There are two way to alternative way to activate debugging mode:


1. Type "/h" in command field (little box in upper left corner, where we usually type
transaction code in it)
2. Go To System -> Utilities -> Debug ABAP

In ABAP debugger we can view and change data. It also allow you to execute your
program line by line.
FRIDAY, AUGUST 10, 2007

Event in ABAP Report

Event in ABAP report determine process flow of a program. The events are triggered
depended on the way the output is generated. They begin after event keyword and
end when the next event reached.

Event keyword:
INITIALIZATION.
Occurs when report initialized.
We can use it to check user authorization or prepare output for selection screen.

AT SELECTION-SCREEN OUTPUT :
Occurs each time selection screen about to generated.
We can use it to modify selection screen, for example hide / unhide parameter.

AT SELECTION-SCREEN.
Occurs each user command in selection screen. we can use it to perform checking on
user input.

START-OF-SELECTION
Occurs after the standard selection screen has been processed.,
data is read in this event.

END-OF-SELECTION
Occurs after start-of-selection.

TOP-OF-PAGE
Occurs when a new page starts.
Use it for write report header.

END-OF-PAGE
Occurs when a page ends.
Use it for write report footer.

AT LINE-SELECTION
Occurs when the user double-click on report.

AT USER-COMMAND
Occurs when the user push toolbar button.

This is program to demonstrate how to use event properly.


REPORT ZAALGAL0008
LINE-COUNT 10(1).

*http://abap-gallery.blogspot.com

TABLES: sflight.

DATA: BEGIN OF t_report OCCURS 3,


carrid LIKE sflight-carrid,
connid LIKE sflight-connid,
END OF t_report.

*begin selection screen


PARAMETERS p_datum LIKE sy-datum.
PARAMETERS p_check AS CHECKBOX.
*end selection screen

INITIALIZATION.
*begin initialization
MOVE sy-datum TO p_datum.
*end initialization

AT SELECTION-SCREEN.
*begin at selection-screen
MESSAGE I888(sabapdocu) WITH 'At selection-screen'.
IF p_check = 'X'.
MESSAGE E888(sabapdocu) WITH 'Clear checkbox'.
ENDIF.
*end at selection-screen

AT SELECTION-SCREEN OUTPUT.
*begin at selection-screen output
MESSAGE I888(sabapdocu) WITH 'At selection-screen output'.
*end at selection-screen output

START-OF-SELECTION.
*begin start-of-selection.
MESSAGE I888(sabapdocu) WITH 'start-of-selection'.
SELECT * FROM sflight.
MOVE sflight-carrid TO t_report-carrid.
MOVE sflight-connid TO t_report-connid.
APPEND t_report.
ENDSELECT.
*end start-of-selection.
END-OF-SELECTION.
*begin end-of-selection.
MESSAGE I888(sabapdocu) WITH 'end-of-selection'.
FORMAT COLOR col_normal.
DO 30 TIMES.
LOOP AT t_report.
WRITE / t_report-carrid.
WRITE t_report-connid.
ENDLOOP.
ENDDO.
*end end-of-selection.

TOP-OF-PAGE.
FORMAT COLOR col_heading.
WRITE 'This is header'.

END-OF-PAGE.
FORMAT COLOR col_total.
WRITE 'This is footer'.

AT LINE-SELECTION.
WRITE: / 'Cursor Row:', sy-curow.
WRITE: / 'Cursor Col:', sy-cucol.

MONDAY, AUGUST 27, 2007

Screen Painter

Screen Painter is an ABAP Editor tools allowed us to create dialog screen. Dialog
screen usually created as a screen to catch user input. It can be accessed by tcode
SE51.

Screen Painter Architecture:


1. Screen Attributes
Define screen title, define its type (normal, subscreen).

2. Flow logic
Flow logic control flow of program. The event block is introduced by the
corresponding keyword statement, and it concludes either when the next block is
introduced, or at the end of the program.
There are four event blocks, each of which is introduced with the screen keyword
PROCESS:
PROCESS BEFORE OUTPUT.
...
PROCESS AFTER INPUT.
...
PROCESS ON HELP-REQUEST.
...
PROCESS ON VALUE-REQUEST.

A simple sample of flow logic.

PROCESS BEFORE OUTPUT.


MODULE PBO_module1.
MODULE PBO_module2.
PROCESS AFTER INPUT.
MODULE PAI_module1.
MODULE PAI_module2.

Flow logic structured on event, in above example we can see there are two event
involved, "process before output" (PBO) and "process after input" (PAI).
PBO processed before the screen is displayed. It allow us to define toolbar and title,
positioning the cursor, showing and hiding fields and changing field attributes
dynamically.
PAI processed after user command (double click, push button). It is allow us to
validate user input, and determine next process based on user command.
In flow logic, we only define module name to define program flow, to create "real
code", double click on module name to create it, then write program in it.

3. Layout Editor
We use layout editor to place screen element in screen layout. There are two modes
in editing layout: Graphical and alphanumeric. Both modes offer the same functions
but use different interfaces. In graphical mode, you use a drag and drop interface
similar to a drawing tool. In alphanumeric mode, you use your keyboard and menus. It
is easier to work in graphical mode, to toggle beetween this mode, in SE51 go to:
Utilities->Settings: in screen painter tabs check graphical layout editor.
Layout editor containing this tools:
i. Element pallete
On left screen you will find list of element (textbox, label, checkbox) you can use.
Drag and drog element to put it on screen.
ii. Name & Text
Aftef put element on screen, write its name and text (in textbox, text will set default
value).
iii. Attributes Window
Double click the element to display its attributes, or select it then click :Goto-
>Secondary window->attributes. For example, in textbox element, we can set its
length, read only mode, in this window.
iv. Dictionary/program field.
If we want to create a field refer to field in data dictionay or field already declared in
program, use this menu to create text field with the same type compared to its
referral.

4. Element list.
Element list shown all element in screen. We rarely use this menu, because it easier
to maintain element in layout editor.

You might also like