Wednesday, October 31, 2007

SAP and ABAP Tutorials in PDF

Here are some SAP & ABAP Tutorials in PDF:

Complete SAP 46c Tutorials Index

Tutorial related to ABAP
ABAP Programming (BC-ABA)
ALV Gird Control (BC-SRV-ALE)
BC - ABAP Dictionary
BC ABAP Workbench Tools
BC ABAP Workbench Tutorial

Read More......

Monday, September 3, 2007

Conversion routine

Write this simple code.


REPORT ZFIELDCONV.
data: d_auart LIKE vbak-auart.
d_auart = 'TA'.
WRITE d_auart.

What the result output on screen do you expect, "TA"? No, the result will be "OR".

The result comes from conversion routine of its domain. In SE11 open table VBAK, find field AUART, double click to go to data element AUART, then double click to open domain AUART. You will see its conversion routine, named AUART. Double click the conversion routine, you got at least two conversion routine conversion routine input & output. They are function module, you can test them in SE37.
The INPUT routine performs the conversion from display format to internal format. The OUTPUT routine performs the conversion from internal format to display format. Take attention when we hard code value as a condition value in "WHERE condition", you must convert its value into internal format.

Other commonly used conversion routine is ALPHA. It is conversion used to insert leading zero for document number. For example, in sales order (VBAK-VBELN length 10), when we key in order no, for example we key in '4302', conversion routine input will automatically add leading zero, so it will become '0000004302', this is real value stored in table VBAK. On conversion routine output, it will delete leading zero.

In SE16, we can view the data wheather in internal format or display format, to toggle format, in display list, go to Setting->User Parameter, in tab strip "Data browser" choose "format, field conversion exit".

Read More......

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.

Read More......

Thursday, August 23, 2007

Find out table source in SAP


Find out where the data is stored in SAP is a very challenging task. In some cases, we can find it easily in few minutes, but in another case it takes more then one day to find it.

I will try to give tips to find it out step by step.

1. Technical Information.
Put cursor in screen field, press F1, it will show description about this field. Then press "Technical Information" button, you can get field name and table / structure / view. If you get a table / view field, check the data in table / view, you may find it there. But, if you get structure field, go to next step.

Example of table field: tcode VA03 (display sales order), screen field order (VBAK-VBELN)
Example of stucture field: tcode IE03 (display equipment), screen field equipment (RM63E-EQUNR).

2. Exploring Search help (display available list to help input value by pressing F4 in screen field).
Sometimes field with search help can be found be displaying field in its search help. Example, tcode XD03 (display customer) field Customer (RF02D-KUNNR). It is a stucture field, to find the table, place cursor in screen field, press F4 (display search help), run search help until it show available list. In this list, place cursor in field customer, then press F1 to display technical info. You will get table/view field for this.

3. SQL Trace
Open your transaction , before type anything in screen, open transaction ST05 (SQL trace) in another session, start SQL trace by pressing "Trace on". SQL trace is a tools to record database request. Make sure you don't run other program beside transaction you want to trace. After starting SQL trace then go back to your transaction, run it until you get field to search shown in screen. Then go to SQL trace session, end SQL trace by pressing "Trace off". Then click "List trace", here you will get all table requested while SQL trace on. Explore table listed there, usually stucture field and table field having same field name.

4. Explore based on SAP module.
Go to SE16. Click F4, then click "SAP Application". Here you got table tree groupped in SAP module.

5. Find it in source code.
Well... you already finished up to step 4, but didn't find the table source yet. It's time to take a harder step. Click F1 then technical information to get program name and field name. Then go to SE38, open the program, then find field name. May be your detective instinct can help out there.

6. Debugging abap program.
Debugging abap could be a sweating task. "Watchpoints" will help you very much in debugging. Considering it could be more than one day to accomplish this task, don't forget to capture and create documentation while debugging when you reached an interesting point. I always do that, when I saw good point, I capture it by "print screen", paste it into Microsoft word, then give a remark on it.

Here is another tips that could help you.

1. Adjacent table will have similar prefix:
sample in sales order, prefik ( V / VB ) : VBAK, VBAP, VBPA...
sample in material management ( M ) : MKPF, MSEG, MARD, MARC,..

2. Almost all SAP Table have a view
To find related table, goto SE11, click where-used list, choose Views.

3. Another tools to find table field:
Goto SE15, Choose: ABAP Dictionary -> Field -> Table Fields.

Read More......

Thursday, August 16, 2007

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:

PROGRAMDYNPRODYNBEGINFNAMFVAL
program1screen1X
BDC_CURSORpos1
BDC_OKCODEcomm1
fieldname1fieldvalue1
fieldname2fieldvalue2
program2screen2X
BDC_CURSORpos1
BDC_OKCODEcomm1
fieldname1fieldvalue1
fieldname2fieldvalue2


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:
PROGRAMDYNPRODYNBEGINFNAMFVAL
SAPLWBABAP100X
BDC_CURSORRS38M-FUNC_HEAD
BDC_OKCODE=CHAP
RS38M-PROGRAMMZAALTESTBDC
RS38M-FUNC_EDIT
RS38M-FUNC_HEADX
SAPLSEDTATTR200X
BDC_CURSORRS38M-REPTI
BDC_OKCODE=CONT
RS38M-REPTITest change title BDC
TRDIR-SUBC1
TRDIR-FIXPTX
SAPLWBABAP100X
BDC_CURSORRS38M-PROGRAMM
BDC_OKCODE=BACK
RS38M-PROGRAMMZAALTESTBDC
RS38M-FUNC_HEADX


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

Read More......

Monday, August 13, 2007

Site Map ABAP Sample Code

ABAP Sample Code:
ALV
Simple ALV
Field Catalog in ALV
Change Layout Format in ALV
FTP
FTP
Business Document
Store Document / File to SAP
Get Document / File Stored in SAP
Selection Screen
Help Value Request (F4)
Add Toolbar Button in Selection Screen
Hide Parameter / Select Option in Selection Screen
Tab Strip in Selection Screen
Miscellaneous
Dynamic Table Maintenance

Read More......

Site Map ABAP Tutorial

ABAP Tutorial:
Step by Step learning ABAP
New ABAP Trial Software Available
Data Dictionary
My first ABAP Program
Data Declaration
Processing Internal Tables
BDC Tutorial for Data Transfer
Debugging ABAP Program
Event in ABAP Report
Find out table source in SAP
Screen Painter

Read More......

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.

Read More......

Tuesday, July 31, 2007

Tabstrip in selection screen

To define a tabstrip area with tab pages in selection screen, use the following statements in your selection screen definition:


SELECTION-SCREEN: BEGIN OF TABBED BLOCK FOR LINES,
TAB () USER-COMMAND
[DEFAULT [PROGRAM ] SCREEN ],
TAB () USER-COMMAND
[DEFAULT [PROGRAM ] SCREEN ],
...
END OF BLOCK .


This defines a tabstrip control with size . The tab pages , ... are assigned to the tab area. defines the width of the tab title. You must assign a function code area to each tab title.

For each tab title, the system automatically creates a character field in the ABAP program with the same name. In ititialization, you can assign a text to the field. This then appears as the title of the corresponding tab page on the selection screen.

You must assign a subscreen to each tab title. This will be displayed in the tab area when the user chooses that title. You can assign one of the following as a subscreen:



1. A subscreen screen defined using the Screen Painter.
The dialog modules containing its flow logic must be defined in the current ABAP program.

2. A selection screen subscreen, defined in an ABAP program.
User actions will trigger the AT SELECTION-SCREEN event at least twice – firstly for the "included" selection screen (we can use it for validation), then for the selection screen on which it appears.

This is simple program with three tab title.
Tab1 demonstrating input validation.
Don't forget to create subscreen 0300 for Tab3.

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

TABLES: sscrfields.

* SUBSCREEN 1 For Tabstrip BUTTON1

SELECTION-SCREEN BEGIN OF SCREEN 100 AS SUBSCREEN.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.
PARAMETERS: p1(10) TYPE c,
p2(10) TYPE c,
p3(10) TYPE c.
SELECTION-SCREEN END OF BLOCK b1.
SELECTION-SCREEN END OF SCREEN 100.

* SUBSCREEN 2 For Tabstrip BUTTON2

SELECTION-SCREEN BEGIN OF SCREEN 200 AS SUBSCREEN.
SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME.
PARAMETERS: q1(10) TYPE c OBLIGATORY,
q2(10) TYPE c.
SELECTION-SCREEN END OF BLOCK b2.
SELECTION-SCREEN END OF SCREEN 200.

* Go to tcode SE51 Create screen no 0300
* with screen type = subscreen

* STANDARD SELECTION SCREEN

PARAMETERS: h1(10) TYPE c.

SELECTION-SCREEN: BEGIN OF TABBED BLOCK mytab FOR 10 LINES,
TAB (20) button1 USER-COMMAND push1
DEFAULT SCREEN 100,
TAB (20) button2 USER-COMMAND push2
DEFAULT SCREEN 200,
TAB (20) button3 USER-COMMAND push3
DEFAULT SCREEN 300,
END OF BLOCK mytab.

INITIALIZATION.
button1 = 'This is sel-screen 100'.
button2 = 'This is sel-screen 200'.
button3 = 'This is screen 300'.
mytab-prog = sy-repid.
mytab-dynnr = 100.
mytab-activetab = 'BUTTON1'.

AT SELECTION-SCREEN.
CASE sy-dynnr.
WHEN 1000.
WHEN 100.
IF p1 IS INITIAL.
sscrfields-ucomm = 'PUSH1'.
MESSAGE I888(sabapdocu) WITH 'Make an entry in P1'.
ENDIF.
WHEN 200.
MESSAGE s888(sabapdocu) WITH 'Validation on' sy-dynnr.
ENDCASE.


START-OF-SELECTION.
WRITE: / 'P1:', p1,'Q1:', q1,
/ 'P2:', p2,'Q2:', q2,
/ 'P3:', p3.

Read More......

Thursday, July 26, 2007

Change layout format in ALV

Layout format in ALV defined in structure IS_LAYOUT as importing structure of ALV REUSE_ALV_LIST_DISPLAY.


CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING
......
is_layout = wa_layout
......
EXCEPTIONS
program_error = 1
OTHERS = 2.


Commonly used field:

- IS_LAYOUT-ZEBRA: X=striped (zebra) pattern
- IS_LAYOUT-BOX_FIELDNAME: insert column checkbox for each row to select row.
How to inset checkbox:
1. insert new field with type C in internal table ALV data to record selection indicator (X:selected).
Example:

DATA: BEGIN OF t_alv OCCURS 1,
field1,
field2,
...
BOX,
END Of T_alv.

2. set IS_LAYOUT-BOX_FIELDNAME = fieldname in internal table ALV data.
Example:

IS_LAYOUT-BOX_FIELDNAME = 'BOX'.

3. At run time, t_alv-box will filled with 'X' for selected data.

CheckBox can be initiated in these ways:
1: selected, can not be changed
0: not selected, can not be changed
X: selected, can be changed
SPACE (blank):not selected, can be changed
-: disable check box

- IS_LAYOUT-COL_FIELDNAME
Cells can be colored individually using a color code which is contained in a column of the internal output table for the record containing the cell.
Assign the name of the field to this parameter.
The internal output table field must be of type SLIS_T_SPECIALCOL_ALV.
Principle: the color code field is entered for the record containing the cells to be colored. The field contains an internal table with the above structure, containing the field names of the cells to be colored and the color code. The cell coordinates are determined by the record position containing the color code and the column information in the color table.
The record structure of the internal color table of type SLIS_T_SPECIALCOL_ALV is as follows:
Color table-FIELDNAME = field name of the cell to be colored
Color table-COLOR-COL = color number (1 - 9)
Color table-COLOR-INT = bold (0 = off, 1 = on)
Color table-COLOR-INV = inverse (0 = off, 1 = on)
Color table-NOKEYCOL = ignore key coloring ('X' = yes, ' ' = no)
If the parameter color table-FIELDNAME is not filled, the coloring applies to all fields, so the entire record is colored.

Read More......

Field catalog in ALV

Field catalog containing descriptions of the list output fields. You can use fields of the catalog to determine the number format and column properties of the list to be displayed.

The field catalog contains more than 60 fields, some of which are only used internally. The field catalog is defined in the Data Dictionary through table type LVC_T_FCAT.

We can get field description by calling function module REUSE_ALV_FIELDCATALOG_MERGE. You can see the sample in my post about Simple ALV Report.

In general case, we only use few field to describe output field.
To make it easier I use this form:



FORM f_alv_fieldcatg USING
fu_types "internal table name
fu_fname "Field name of internal table field
fu_reftb "reference table name
fu_refld "reference table field
fu_noout "X= No out
fu_outln "out
fu_fltxt "output length
fu_dosum "X=Do sum (total)
fu_hotsp "X=hotspot on
fu_just. "Justification:L,R,C

CLEAR: t_fieldcat.
t_fieldcat-tabname = fu_types.
t_fieldcat-fieldname = fu_fname.
t_fieldcat-ref_tabname = fu_reftb.
t_fieldcat-ref_fieldname = fu_refld.
t_fieldcat-no_out = fu_noout.
t_fieldcat-outputlen = fu_outln.
t_fieldcat-seltext_l = fu_fltxt.
t_fieldcat-seltext_m = fu_fltxt.
t_fieldcat-seltext_s = fu_fltxt.
t_fieldcat-reptext_ddic = fu_fltxt.
t_fieldcat-no_out = fu_noout.
t_fieldcat-do_sum = fu_dosum.
t_fieldcat-hotspot = fu_hotsp.
t_fieldcat-just = fu_just.
APPEND t_fieldcat.
CLEAR t_fieldcat.
ENDFORM. " f_alv_fieldcatg

We can use it in two way:
1. refer to data dictionary:

PERFORM f_alv_fieldcatg USING 'T_REPORT' :
'EQUNR' 'EQUI' 'EQUNR' '' '' '' '' 'X' '',
'SPART' 'VBAK' 'SPART' '' '' '' '' '' ''.

2. or define yourself

PERFORM f_alv_fieldcatg USING 'T_REPORT' :
'FIELD1' '' '' '' '20' 'Text Field1' '' '' ''.


and for field with type number, I use this:

*&---------------------------------------------------------------------*
*& Form f_alv_fieldcatg_number
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM f_alv_fieldcatg_number USING fu_types
fu_fname
fu_noout
fu_outln
fu_no_sign
fu_no_zero
fu_fltxt
fu_dosum
fu_exponent
fu_decimals_out.

CLEAR: t_fieldcat.
t_fieldcat-tabname = fu_types.
t_fieldcat-fieldname = fu_fname.
t_fieldcat-no_out = fu_noout.
t_fieldcat-outputlen = fu_outln.
t_fieldcat-seltext_l = fu_fltxt.
t_fieldcat-seltext_m = fu_fltxt.
t_fieldcat-seltext_s = fu_fltxt.
t_fieldcat-reptext_ddic = fu_fltxt.
t_fieldcat-no_zero = fu_no_zero.
t_fieldcat-no_sign = fu_no_sign.
t_fieldcat-exponent = fu_exponent.
t_fieldcat-decimals_out = fu_decimals_out.
t_fieldcat-do_sum = fu_dosum.
t_fieldcat-datatype = 'FLTP'.
APPEND t_fieldcat.
CLEAR t_fieldcat.


ENDFORM. " f_alv_fieldcatg_number


For field currency, there are two options, fixed currency or variable currency.

*&---------------------------------------------------------------------*
*& Form f_alv_fieldcatg_curr
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM f_alv_fieldcatg_curr USING fu_types
fu_fname
fu_reftb
fu_refld
fu_noout
fu_outln
fu_fltxt
fu_dosum
fu_hotsp
fu_cfield.

CLEAR: t_fieldcat.
t_fieldcat-tabname = fu_types.
t_fieldcat-fieldname = fu_fname.
t_fieldcat-ref_tabname = fu_reftb.
t_fieldcat-ref_fieldname = fu_refld.
t_fieldcat-no_out = fu_noout.
t_fieldcat-outputlen = fu_outln.
t_fieldcat-seltext_l = fu_fltxt.
t_fieldcat-seltext_m = fu_fltxt.
t_fieldcat-seltext_s = fu_fltxt.
t_fieldcat-reptext_ddic = fu_fltxt.
t_fieldcat-do_sum = fu_dosum.
t_fieldcat-hotspot = fu_hotsp.
t_fieldcat-cfieldname = fu_cfield.
t_fieldcat-ctabname = fu_types.
t_fieldcat-no_zero = 'X'.
APPEND t_fieldcat.
CLEAR t_fieldcat.

ENDFORM. " F_FIELDCATG_CURR

*&---------------------------------------------------------------------*
*& Form f_alv_fieldcatg_curr
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM f_alv_fieldcatg_curr_fixed USING fu_types
fu_fname
fu_reftb
fu_refld
fu_noout
fu_outln
fu_fltxt
fu_dosum
fu_hotsp
fu_curr.

CLEAR: t_fieldcat.
t_fieldcat-tabname = fu_types.
t_fieldcat-fieldname = fu_fname.
t_fieldcat-ref_tabname = fu_reftb.
t_fieldcat-ref_fieldname = fu_refld.
t_fieldcat-no_out = fu_noout.
t_fieldcat-outputlen = fu_outln.
t_fieldcat-seltext_l = fu_fltxt.
t_fieldcat-seltext_m = fu_fltxt.
t_fieldcat-seltext_s = fu_fltxt.
t_fieldcat-reptext_ddic = fu_fltxt.
t_fieldcat-do_sum = fu_dosum.
t_fieldcat-hotspot = fu_hotsp.
* t_fieldcat-cfieldname = fu_cfield.
* t_fieldcat-ctabname = fu_types.
t_fieldcat-datatype = 'CURR'.
t_fieldcat-currency = fu_curr.
t_fieldcat-no_zero = 'X'.
APPEND t_fieldcat.
CLEAR t_fieldcat.

ENDFORM. " F_FIELDCATG_CURR

Read More......

Wednesday, July 25, 2007

Hide parameter / select option in selection screen

We can Hide parameter / select option in selection screen dynamically by manipulating screen object.

SCREEN is like an internal table with a header line. However, you do not have to declare it in your program. Go to debugging mode and then view structure of screen.

You can modify SCREEN in your ABAP program during the PBO event of a screen. Its contents override the static attributes of the screen fields for a single screen call. The only statements that you can use with SCREEN are:


LOOP AT SCREEN.
...
MODIFY SCREEN.
...
ENDLOOP.

We can hide parameter by set screen-active to 0.
Here is example of how to hide parameter in selection screen. Write it, and change click on radiobutton to hide parameter.


REPORT ZAALGAL0006.

DATA: d_ucomm LIKE sy-ucomm.

PARAMETERS: p_grpa1(10) MODIF ID A,
p_grpa2(5) MODIF ID A,
p_grpb1(2) MODIF ID B.

PARAMETERS: p_actA RADIOBUTTON GROUP rad1 USER-COMMAND ACT DEFAULT 'X',
p_actB RADIOBUTTON GROUP rad1.

AT SELECTION-SCREEN.
d_ucomm = sy-ucomm.

AT SELECTION-SCREEN OUTPUT.
LOOP AT screen.
IF p_actA = 'X'.
IF screen-group1 = 'B'.
screen-active = 0.
ENDIF.
ELSEIF p_actB = 'X'.
IF screen-group1 = 'A'.
screen-active = 0.
ENDIF.
ENDIF.
MODIFY screen.
ENDLOOP.

START-OF-SELECTION.


Read More......

Tuesday, July 24, 2007

Add toolbar button on selection screen


We can add new toolbar (maximal 5) in ABAP report to make program more interactive to user on selection screen.

Do following step.

1. Declare work area sscrfields.

TABLES: sscrfields.

2. Define text displayed in button in initialization event.

INITIALIZATION.
MOVE 'This is button 1' TO sscrfields-functxt_0n." n = 1 up to 5

3. Activate toolbar in selection screen.

SELECTION-SCREEN FUNCTION KEY n.

4. Check user command in AT selection screen

AT SELECTION-SCREEN.
IF sy-ucomm = 'FC0n'." n = 1 up to 5
....
ENDIF.

This is complete sample:

REPORT ZAALGAL0005 .

TABLES: sscrfields.

DATA: d_butt1(4).

PARAMETERS: p_grpa1(10) MODIF ID A,
p_grpa2(10) MODIF ID A,
p_grpb1(10) MODIF ID B.

SELECTION-SCREEN FUNCTION KEY 1.
SELECTION-SCREEN FUNCTION KEY 2.

INITIALIZATION.
MOVE 'This is button 1' TO sscrfields-functxt_01.
MOVE 'Toggle 1' TO sscrfields-functxt_02.
d_butt1 = 'NO'.

AT SELECTION-SCREEN.
IF sy-ucomm = 'FC01'.
d_butt1 = 'YES'.
sscrfields-ucomm = 'ONLI'.
ELSEIF sy-ucomm = 'FC02'.
IF sscrfields-functxt_02 = 'Toggle 1'.
sscrfields-functxt_02 = 'Toggle 2'.
ELSE.
sscrfields-functxt_02 = 'Toggle 1'.
ENDIF.
ENDIF.

START-OF-SELECTION.
WRITE d_butt1.


Read More......

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.

Read More......

Performance Tuning - Operation on Internal Table

In development server you may not aware of performance tuning on operation on internal table, but it could become a problem when you are working on internal table containing more 10 thousand rows.

These are tips to improve your program performance.

READ Table WITH Criteria
By default, read command on internal table will read it sequentially. The binary search algorithm helps faster search of a value in an internal table. But you must sort it before use binary search. Binary search repeatedly divides the search interval in half. If the value to be searched is less than the item in the middle of the interval, the search is narrowed to the lower half, otherwise the search is narrowed to the upper half.


SORT TABLE BY field1.

READ TABLE table1 WITH KEY field1 = criteria1 BINARY SEARCH.
Do try it for internal table containing more than 10 thousand rows, you will find it significantly improve performance tuning.

You can apply binary search method to improve performance on nested loop.
Nested loop:


LOOP AT inttab1.
LOOP AT inttab2 WHERE intab2-field1 = intab1-field1.
ENDLOOP.
ENDLOOP.



Replace above code with additional binary search.



SORT inttab2 BY field1.
LOOP AT inttab1.
READ TABLE inttab2 WITH KEY field1 = inttab1-field1 BINARY SEARCH.
CHECK sy-subrc = 0.
LOOP AT inttab2 FROM sy-tabix.
IF inttab2-field1 NE inttab1-field1.
EXIT.
ENDIF.
ENDLOOP.
ENDLOOP.


Read More......

Sunday, July 22, 2007

Help Value Request (F4)

This sample program demonstrate how to create "Help Value Request". It will appear when user pressing F4 (help) on an input field to request list of available value.

To do this we are using function module F4IF_INT_TABLE_VALUE_REQUEST. It provide help value request with following feature:
1. single / multiple choice.
2. update screen without PBO, so it can be used to update more than one field on one request.

Here is the code:


REPORT ZAALGAL0003 .

TABLES: usr02.

parameters: p_bname LIKE usr02-bname,
p_class LIKE usr02-class.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_bname.
PERFORM f_valuerequest_vbeln.
*&---------------------------------------------------------------------*
*& Form f_valuerequest_vbeln
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM f_valuerequest_vbeln.

DATA: BEGIN OF t_data OCCURS 1,
data(20),
END OF t_data.

DATA: lwa_dfies TYPE dfies.

data h_field_wa LIKe dfies.
data h_field_tab like dfies occurs 0 with header line.
data h_dselc like dselc occurs 0 with header line.

SELECT * FROM usr02.
t_data = usr02-bname. APPEND t_data.
t_data = usr02-class. APPEND t_data.
ENDSELECT.

PERFORM f_fieldinfo_get USING 'USR02'
'BNAME'
CHANGING h_field_wa.
APPEND h_field_wa TO h_field_tab.
PERFORM f_fieldinfo_get USING 'USR02'
'CLASS'
CHANGING h_field_wa.
APPEND h_field_wa TO h_field_tab.

h_dselc-fldname = 'BNAME'.
h_dselc-dyfldname = 'P_BNAME'.
APPEND h_dselc.
h_dselc-fldname = 'CLASS'.
h_dselc-dyfldname = 'P_CLASS'.
APPEND h_dselc.

DATA: ld_repid LIKE sy-repid.
ld_repid = sy-repid.

CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
EXPORTING
retfield = 'P_BNAME'
dynpprog = ld_repid
dynpnr = '1000'
dynprofield = 'P_BNAME'
* multiple_choice = ''
* value_org = 'S'
TABLES
value_tab = t_data
field_tab = h_field_tab
* return_tab = return_tab
DYNPFLD_MAPPING = h_dselc
EXCEPTIONS
OTHERS = 0.

ENDFORM. " f_valuerequest_vbeln
*&---------------------------------------------------------------------*
*& Form f_fieldinfo_get
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_0079 text
* -->P_0080 text
* <--P_H_FIELD_WA text
*----------------------------------------------------------------------*
FORM f_fieldinfo_get USING fu_tabname
fu_fieldname
CHANGING fwa_field_tab.


CALL FUNCTION 'DDIF_FIELDINFO_GET'
EXPORTING
TABNAME = fu_tabname
FIELDNAME = fu_fieldname
LFIELDNAME = fu_fieldname
IMPORTING
DFIES_WA = fwa_field_tab
EXCEPTIONS
NOT_FOUND = 1
INTERNAL_ERROR = 2
OTHERS = 3
.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.


ENDFORM. " f_fieldinfo_get

Read More......

Friday, July 20, 2007

New ABAP Trial Software Available !!!


If you are new to ABAP / SAP. You might find this link useful.

Download the latest version trial version of SAP NetWeaver 7.0 ABAP technology (SP12), including Web Dynpro for ABAP! .
Download ABAP Trial Version

Try to download it, and if it success, try to create your first ABAP program.


Enjoy installing ABAP trial :)

Read More......

Get Document / File stored in SAP

Previous post: Store Document / File to SAP

Get document / file stored in SAP Business Document.


REPORT ZAALTES1 line-size 300.

INCLUDE officeintegrationinclude. " used by DOI
* Get descr_list
TYPES: BEGIN OF SOI_DOCUMENT_TYPE_DESCR,
DOCUMENT_TYPE TYPE SOI_DOCUMENT_TYPE, " prog id
TYPE_FULL_NAME(70) TYPE C, " full name
TYPE_SHORT_NAME(20) TYPE C, " short name
APPLICATION_NAME(50) TYPE C, " application name
SUPPORTED_INTERFACES(256) TYPE C, " supported SAP interfaces
AVAILABLE(1) TYPE C, " X on return when available
END OF SOI_DOCUMENT_TYPE_DESCR.
DATA:
descr_list TYPE soi_document_type_descr_list,
wa_descr_list TYPE SOI_DOCUMENT_TYPE_DESCR,
retcode TYPE t_oi_ret_string.
DATA: application_cntl TYPE REF TO i_oi_ole_container_control.
* ----------

* -- connection table and the internal structure of the ITAB
DATA: BEGIN OF i_bds_conn OCCURS 10. " ITAB for the actual
INCLUDE STRUCTURE bdn_con. " document
DATA: objecttext LIKE toasp-objecttext,
objecttext2 LIKE toasd-objecttext,
objecttext3 LIKE toasr-objecttext,
ntext LIKE tojtt-ntext,
END OF i_bds_conn.
DATA: BEGIN OF bds_doctype_list OCCURS 10,
mandt TYPE mandt,
classname LIKE bapibds01-classname,
contrep LIKE i_bds_conn-contrep,
docuclass LIKE i_bds_conn-docuclass,
docuclass_text LIKE toasd-objecttext,
doc_type LIKE i_bds_conn-doc_type,
doc_type_text LIKE toasp-objecttext,
appl_type LIKE toadd-appl_type,
appl_type_text(50) TYPE c,
standard LIKE toadv-standard,
check_box LIKE toadv-standard,
END OF bds_doctype_list.

TYPES: ole2_application(70) TYPE c.

DATA:
logical_system LIKE bds_conn00-log_system,
display_struc LIKE i_bds_conn OCCURS 2 WITH HEADER LINE,
classname_select LIKE bdn_con-classname,
classtype_select LIKE bdn_con-classtype,
objkey_select LIKE bdn_con-objkey,
mask(20) TYPE c,
signature LIKE bapisignat OCCURS 1 WITH HEADER LINE,
build_all(1) TYPE c, " get all OLE apps
ole2_app TYPE ole2_application,
i_ole2_app_text LIKE bds_doctype_list-appl_type_text,
d_tabix LIKE sy-tabix,
vartemp
.
TYPES: BEGIN OF ole_apps_struc,
appl_type LIKE toadd-appl_type,
appl_type_text LIKE bds_doctype_list-appl_type_text,
mimetype LIKE bdn_con-mimetype,
docuclass LIKE bdn_con-docuclass,
END OF ole_apps_struc.
DATA: ole_apps TYPE ole_apps_struc OCCURS 5.

PARAMETERS: p_class LIKE bdn_con-classname OBLIGATORY,
p_objct LIKE bdn_con-objkey OBLIGATORY.


start-of-selection.
classname_select = p_class.
classtype_select = 'OT'.
objkey_select = p_objct.
PERFORM f_bds_call_navigator.

at line-selection.
PERFORM f_line_selection.


FORM f_bds_call_navigator.
* Function : BDS_CALL_NAVIGATOR
* -- data declaration -- *
DATA: i_connections LIKE bdn_con OCCURS 0 WITH HEADER LINE,
calling_signa LIKE bapisignat OCCURS 0 WITH HEADER LINE,
i_count LIKE sy-index,
i_gui_type LIKE bapibds01-type,
web_excluding LIKE bdn_fkt OCCURS 1 WITH HEADER LINE.

* -- get all connections for the objtype/objkey -- *
CALL FUNCTION 'BDS_ALL_CONNECTIONS_GET'
EXPORTING
logical_system = logical_system
classname = classname_select
classtype = classtype_select
objkey = objkey_select
client = sy-mandt
all = ' ' " <- get the newest version
IMPORTING
count = i_count
TABLES
signature = signature
all_connections = i_connections
* framework = framework
* -- Begin correction 388230 ------------------------------ *
* EXCEPTIONS
* no_objects_found = 1
* error_kpro = 2
* internal_error = 3
* OTHERS = 4.
* IF sy-subrc > 1.
* MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
* WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
* RAISING internal_error.
* ENDIF.
EXCEPTIONS
no_objects_found = 1
error_kpro = 2
internal_error = 3
not_authorized = 4
OTHERS = 5.
*End Function : BDS_CALL_NAVIGATOR

*Function : BDS_DISPLAY_LIST_VIA_TREE
* -- get some text fields -- *
DATA:
i_toasp LIKE toasp OCCURS 1 WITH HEADER LINE,
i_toasd LIKE toasd OCCURS 1 WITH HEADER LINE,
i_toasr LIKE toasr OCCURS 1 WITH HEADER LINE,
class_text like tojtt-ntext
.
SELECT * FROM toasp INTO TABLE i_toasp WHERE language = sy-langu.
SELECT * FROM toasd INTO TABLE i_toasd WHERE language = sy-langu.
SELECT * FROM toasr INTO TABLE i_toasr WHERE language = sy-langu.
APPEND LINES OF i_connections TO i_bds_conn.
LOOP AT i_bds_conn.
IF i_bds_conn-doc_type <> space. " -> Corr. (402788) A.M.
READ TABLE i_toasp WITH KEY ar_object = i_bds_conn-doc_type.
IF sy-subrc = 0.
i_bds_conn-objecttext = i_toasp-objecttext.
ENDIF.
ENDIF. " -> Corr. (402788) A.M.
READ TABLE i_toasr WITH KEY archiv_id = i_bds_conn-contrep.
IF sy-subrc = 0.
i_bds_conn-objecttext3 = i_toasr-objecttext.
ENDIF.
IF i_bds_conn-docuclass <> space.
READ TABLE i_toasd WITH KEY doc_type = i_bds_conn-docuclass.
IF sy-subrc = 0.
i_bds_conn-objecttext2 = i_toasd-objecttext.
ENDIF.
ENDIF.

i_bds_conn-ntext = class_text.

MODIFY i_bds_conn.
ENDLOOP.

*End Function : BDS_DISPLAY_LIST_VIA_TREE
skip.
LOOP AT i_bds_conn.
d_tabix = sy-tabix.
HIDE d_tabix.
WRITE : / i_bds_conn-BDN_TAB_I,
i_bds_conn-descript,
(50) i_bds_conn-comp_id,
i_bds_conn-objecttext,
i_bds_conn-objecttext2,
i_bds_conn-objecttext3,
i_bds_conn-ntext.

ENDLOOP.
CLEAR d_tabix.

EXIT.

PERFORM f_get_registered_doc_types.
*Program: LBDS_TOOLSF01, Form: display_selected_document
MOVE-CORRESPONDING i_bds_conn TO display_struc.
* -- find ole2_app -- *
build_all = ' '.
PERFORM get_ole2_app2 TABLES descr_list
USING display_struc-docuclass
display_struc-mimetype
ole2_app i_ole2_app_text
build_all.


ENDFORM. "f_bds_call_navigator
*&---------------------------------------------------------------------*
*& Form display_selected_document_out
*&---------------------------------------------------------------------*
* display selected document NOT in-place.
*----------------------------------------------------------------------*
FORM display_selected_document_out
TABLES display_struc STRUCTURE i_bds_conn.

* -- data declaration -- *
DATA: display_signature LIKE bapisignat OCCURS 5 WITH HEADER LINE.
* -------------------------------------------------------------------- *

CLEAR: display_signature.
REFRESH: display_signature.

READ TABLE display_struc INDEX 1.

display_signature-doc_count = '1'.
display_signature-doc_id = display_struc-loio_id.
display_signature-doc_ver_no = display_struc-doc_ver_no.
display_signature-doc_var_id = display_struc-doc_var_id.
display_signature-doc_var_tg = display_struc-doc_var_tg.
APPEND display_signature.

CALL FUNCTION 'BDS_OBJECT_DISPLAY_INTERN'
EXPORTING
logical_system = display_struc-log_system
classname = display_struc-classname
classtype = display_struc-classtype
client = display_struc-client
object_key = display_struc-objkey
bds_docid = display_struc-bds_docid
bds_contrep = display_struc-contrep
bds_docuclass = display_struc-docuclass
TABLES
signature = display_signature
EXCEPTIONS
error_kpro = 1
internal_error = 2
OTHERS = 3.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.


ENDFORM. " display_selected_document_out

*&---------------------------------------------------------------------*
*& Form GET_OLE2_APP2
*&---------------------------------------------------------------------*
* Get the correct OLE-App.
*----------------------------------------------------------------------*
FORM get_ole2_app2 TABLES descr_list
USING i_docuclass LIKE bdn_con-docuclass
i_mimetype LIKE bdn_con-mimetype
ole2_app LIKE toadd-appl_type
ole2_app_text LIKE bds_doctype_list-appl_type_text
build_all LIKE bdn_con-stor_tab.
* -- Data declaration ------------------------------------------------ *
TYPES: BEGIN OF document_type_descr,
document_type(70) TYPE c,
type_full_name(70) TYPE c,
type_short_name(20) TYPE c,
application_name(50) TYPE c,
supported_interfaces(256) TYPE c,
available(1) TYPE c,
END OF document_type_descr.

DATA: i_ole2_app TYPE toadd OCCURS 1 WITH HEADER LINE,
descr_list_struc TYPE document_type_descr,
local_langu LIKE sy-langu,
ole_apps_line TYPE ole_apps_struc,
temp_mimetype LIKE bdn_con-mimetype.

* -- Initilize Data -------------------------------------------------- *
local_langu = sy-langu.
* -------------------------------------------------------------------- *

IF build_all = ' '. " -> get a special ole_type
SET LOCALE LANGUAGE local_langu.
TRANSLATE i_mimetype TO LOWER CASE.
SET LOCALE LANGUAGE space.

IF i_docuclass <> space.
SELECT * INTO TABLE i_ole2_app FROM toadd
WHERE doc_type = i_docuclass.
temp_mimetype = i_mimetype.
i_mimetype = space.
ELSEIF i_mimetype <> space.
SELECT * INTO TABLE i_ole2_app FROM toadd
WHERE mimetype = i_mimetype.
i_docuclass = space.
temp_mimetype = i_mimetype.
ENDIF.
CLEAR i_ole2_app.
LOOP AT i_ole2_app.
IF i_ole2_app-appl_type <> space.
IF ( i_ole2_app-appl_type = 'EAIWEB.WEBVIEWER2D' OR
i_ole2_app-appl_type = 'EAIWEB.WEBVIEWER3D' OR
i_ole2_app-appl_type = 'SAPHTML' ).
ole2_app = i_ole2_app-appl_type.
i_mimetype = temp_mimetype.
EXIT.
ENDIF.
LOOP AT descr_list INTO descr_list_struc.
SET LOCALE LANGUAGE local_langu.
TRANSLATE descr_list_struc-document_type TO UPPER CASE.
SET LOCALE LANGUAGE space.
IF i_ole2_app-appl_type = descr_list_struc-document_type.
ole2_app = i_ole2_app-appl_type.
ole2_app_text = descr_list_struc-application_name.
i_docuclass = i_ole2_app-doc_type.
i_mimetype = i_ole2_app-mimetype.
EXIT.
ENDIF.
ENDLOOP.
ELSE.
ole2_app = space.
ole2_app_text = space.
i_mimetype = i_ole2_app-mimetype.
i_docuclass = i_ole2_app-doc_type.
EXIT.
ENDIF.
ENDLOOP.
ELSE. " -> get the complete list
CLEAR i_ole2_app.
SELECT * INTO TABLE i_ole2_app FROM toadd.
LOOP AT i_ole2_app.
IF ( i_ole2_app-appl_type <> space AND
i_ole2_app-appl_type <> 'ACROEXCH.DOCUMENT' ). "#EC NOTEXT
LOOP AT descr_list INTO descr_list_struc.
SET LOCALE LANGUAGE local_langu.
TRANSLATE descr_list_struc-document_type TO UPPER CASE.
SET LOCALE LANGUAGE space.
IF i_ole2_app-appl_type = descr_list_struc-document_type.
ole_apps_line-appl_type = i_ole2_app-appl_type.
ole_apps_line-appl_type_text =
descr_list_struc-application_name.
ole_apps_line-docuclass = i_ole2_app-doc_type.
ole_apps_line-mimetype = i_ole2_app-mimetype.
APPEND ole_apps_line TO ole_apps.
ENDIF.
ENDLOOP.
ENDIF.
ENDLOOP.

SORT ole_apps BY appl_type.
DELETE ADJACENT DUPLICATES FROM ole_apps COMPARING appl_type.

ENDIF.

ENDFORM. " GET_OLE2_APP2
*&---------------------------------------------------------------------*
*& Form f_get_registered_doc_types
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM f_get_registered_doc_types.
if application_cntl is initial.
* -- Create Container Control -- *
call method c_oi_ole_control_creator=>get_ole_container_control
importing
control = application_cntl
retcode = retcode.

if retcode <> c_oi_errors=>ret_ok.
call method c_oi_errors=>show_message
exporting
type = 'E'.
endif.

call method application_cntl->get_registered_doc_types
exporting
interface_type = ' '
importing
descr_list = descr_list
retcode = retcode.

if retcode <> c_oi_errors=>ret_ok.
call method c_oi_errors=>show_message
exporting
type = 'E'.
endif.

endif.
ENDFORM. " f_get_registered_doc_types
*&---------------------------------------------------------------------*
*& Form f_line_selection
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM f_line_selection.
READ TABLE i_bds_conn INDEX d_tabix.
CHECK sy-subrc = 0.
MOVE-CORRESPONDING i_bds_conn TO display_struc.

PERFORM display_selected_document_out TABLES display_struc.


CALL METHOD cl_gui_cfw=>flush
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2.
IF sy-subrc <> 0.
MESSAGE e324(sbds).
ELSE.
WRITE :/ 'success'.
ENDIF.

ENDFORM. " f_line_selection

Read More......

Thursday, July 19, 2007

Store Document / File to SAP

To store document / image in SAP, we can use Business Document Navigator.
To go to the Business Document Navigator, choose Office -> Business Documents -> Documents -> Find (transaction code : OAOR).

In the upper part of the screen, the relevant documents, sorted by document type (the document types in turn belong to specified application objects) are displayed in the tree. The lower left part of the screen contains tab pages with the functions Detailed display, Document information (version string), Keywords and Storing. On the right-hand side of the screen, you can display a selected document in-place.

A bundled document structured by Class Name, Class Type and object Key. We can add additional attributes for document, including key word, title, description.

To save and get file programmatically, here are the codes.

I split them into two program, store document and get document. Program to get and display document can be found in next post.
Program to store document:


REPORT ZAALGAL0002 NO STANDARD PAGE HEADING.
* Store document in BDN

* -- connection table and the internal structure of the ITAB
DATA: BEGIN OF i_bds_conn OCCURS 10. " ITAB for the actual
INCLUDE STRUCTURE bdn_con. " document
DATA: objecttext LIKE toasp-objecttext,
objecttext2 LIKE toasd-objecttext,
objecttext3 LIKE toasr-objecttext,
ntext LIKE tojtt-ntext,
END OF i_bds_conn.
DATA: BEGIN OF bds_doctype_list OCCURS 10,
mandt TYPE mandt,
classname LIKE bapibds01-classname,
contrep LIKE i_bds_conn-contrep,
docuclass LIKE i_bds_conn-docuclass,
docuclass_text LIKE toasd-objecttext,
doc_type LIKE i_bds_conn-doc_type,
doc_type_text LIKE toasp-objecttext,
appl_type LIKE toadd-appl_type,
appl_type_text(50) TYPE c,
standard LIKE toadv-standard,
check_box LIKE toadv-standard,
END OF bds_doctype_list.
DATA: file_extension LIKE toadd-doc_type, " file-extension
i_files LIKE bapifiles OCCURS 1 WITH HEADER LINE,
i_signature LIKE bapisignat OCCURS 1 WITH HEADER LINE,
logical_system LIKE bds_conn00-log_system.

PARAMETERS: p_class LIKE bdn_con-classname OBLIGATORY,
p_objct LIKE bdn_con-objkey OBLIGATORY,
p_descr LIKE bdn_con-descript OBLIGATORY.

start-of-selection.

PERFORM create_doc_via_file.

*&---------------------------------------------------------------------*
*& Form CREATE_DOC_VIA_FILE
*&---------------------------------------------------------------------*
* create a new document via file
*----------------------------------------------------------------------*
FORM create_doc_via_file.
* -- data declaration ------------------------------------------------ *
DATA: classname_select LIKE bdn_con-classname,
classtype_select LIKE bdn_con-classtype,
objkey_select LIKE bdn_con-objkey,
mask(20) TYPE c,
answer TYPE c,
mimetype LIKE toadd-mimetype,
i_toadd LIKE toadd,
filename_all LIKE sapb-sapfiles,
file_path LIKE sapb-sapfiles,
file_path_memory(250) TYPE c, " path für SAP memory
file_path_length TYPE i, " length of the file_path
file_name LIKE sapb-sapfiles.


classname_select = p_class.
classtype_select = 'OT'.
objkey_select = p_objct.


GET PARAMETER ID 'OAP' FIELD file_path.
IF sy-subrc <> 0. " no file_path found.
file_path = space.
ENDIF.

CONCATENATE ',*.' '*' ',*.' '*' '.' INTO mask.
CALL FUNCTION 'WS_FILENAME_GET'
EXPORTING
def_path = file_path
mask = mask
mode = 'O'
title = 'Select File to upload'
IMPORTING
filename = filename_all
EXCEPTIONS
inv_winsys = 01
no_batch = 02
selection_cancel = 03
selection_error = 04.

IF sy-subrc = 3.
MESSAGE s012(sbds). " cancel file selection
EXIT.
ELSEIF sy-subrc <> 0 AND sy-subrc <> 3.
MESSAGE e011(sbds). " error during file selection
ENDIF.

* -- split filename -- *
PERFORM split_path(oaall) USING filename_all file_path file_name.

* -- set new file_path to SAP memory -- *
file_path_length = strlen( file_path ).

IF file_path <> space AND file_path_length < 250.
file_path_memory = file_path.
SET PARAMETER ID 'OAP' FIELD file_path_memory.
ELSE.
file_path_memory = space.
SET PARAMETER ID 'OAP' FIELD file_path_memory.
ENDIF.

* -- check documentclass -- *
PERFORM get_file_extension USING file_name
file_extension.

* -- check file extension -- *
IF file_extension = space.
* -> if no docuclass is found from the document
* -> default docuclass from the doctype!
file_extension = bds_doctype_list-docuclass.
ENDIF.

* -- get the mimetype of the docuclass -- *
PERFORM mimetype_get(oaall) USING file_extension
CHANGING i_toadd.
MOVE i_toadd-mimetype TO mimetype.

* -- fill file and signature structure -- *
CLEAR: i_files, i_signature.
REFRESH : i_files, i_signature.

i_files-doc_count = 1.
i_files-directory = file_path.
i_files-filename = file_name.
i_files-mimetype = mimetype.
APPEND i_files.

i_signature-doc_count = 1.
i_signature-prop_name = 'BDS_DOCUMENTCLASS'.
i_signature-prop_value = file_extension.
APPEND i_signature.
i_signature-prop_name = 'BDS_CONTREP'.
IF bds_doctype_list-contrep = space.
i_signature-prop_value = ' '. "#EC NOTEXT
ELSE.
i_signature-prop_value = bds_doctype_list-contrep.
ENDIF.
APPEND i_signature.
i_signature-prop_name = 'BDS_DOCUMENTTYPE'.
i_signature-prop_value = bds_doctype_list-doc_type.
APPEND i_signature.
i_signature-prop_name = 'DESCRIPTION'.
i_signature-prop_value = p_descr.
APPEND i_signature.
i_signature-prop_name = 'LANGUAGE'.
i_signature-prop_value = sy-langu.
APPEND i_signature.

* -- create new document via KPro -- *
CALL FUNCTION 'BDS_BUSINESSDOCUMENT_CREATEF'
EXPORTING
logical_system = logical_system
classname = classname_select
classtype = classtype_select
client = sy-mandt
object_key = objkey_select
TABLES
files = i_files
signature = i_signature
EXCEPTIONS
internal_error = 1
OTHERS = 2.

WRITE:/ 'sy-subrc:',sy-subrc.



ENDFORM. " CREATE_DOC_VIA_FILE
*&---------------------------------------------------------------------*
*& Form GET_FILE_EXTENSION
*&---------------------------------------------------------------------*
* try to get the extension of the uploaded file
*----------------------------------------------------------------------*
FORM get_file_extension USING file_name file_extension.
* -- data declaration ------------------------------------------------ *
DATA: length TYPE i,
single_c TYPE c.
* -------------------------------------------------------------------- *
CLEAR: single_c.

length = strlen( file_name ).
IF length > 0.
WHILE length > 0.
single_c = file_name+length(1).
IF single_c CO '.'.
length = length + 1.
EXIT.
ELSE.
length = length - 1.
ENDIF.
ENDWHILE.
IF length > 0.
file_extension = file_name+length.
ELSE.
file_extension = space.
ENDIF.
ELSE.
file_extension = space.
ENDIF.

IF file_extension <> space.
SET LOCALE LANGUAGE sy-langu.
TRANSLATE file_extension TO UPPER CASE. "#EC TRANSLANG
SET LOCALE LANGUAGE space.
ENDIF.

ENDFORM. " GET_FILE_EXTENSION

Read More......

Wednesday, July 18, 2007

Customizing SAP

If you want to be a SAP consultant, customizing will be a knowledge you must have.

Customizing covered following function:
1. Define Organization Structure
Example:
Business Area (Finance Accounting)
Maintain Storage Location (Material Management)
2. Maintain Master Data
Example:
-Define Customer Group (Sales and Distribution)
3. Rules:
-Define required entry in each module
4. Output Layout
-Define output layout

How to customize SAP

Go to TCode SPRO then click "SAP Reference IMG".
You will see tree of customizing Menu.
For example we want to maintain Sales Group, double click this menu from tree, you will see table maintenance view to maintain master data.



To have a competencies in customizing you must understand SAP business process.


Read More......

Tuesday, July 17, 2007

Step by step learning ABAP

ABAP stands for Advanced Business Applicatian Programming. The ABAP programming language was originally used by SAP developers to develop the SAP R/3 platform. It was also intended to be used by SAP customers to enhance SAP applications customers can develop custom reports and interfaces with ABAP programming. The language is fairly easy to learn for programmers but it is not a tool for direct use by non-programmers. Good programming skills, including knowledge of relational database design and preferably also of object-oriented concepts, are required to create ABAP programs.


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.

Here are other useful links for beginner:
ABAP Overview in Wikipedia
Blog Series in SAP Developer Network

Read More......

Dynamic Table Maintenance

It is a good sample of how to create dynamic table maintenance by utilized SAP function module "STC1_FULLSCREEN_TABLE_CONTROL".
I copy this code from ITToolbox

You can use it to replace funtion of SM31 with additional benefit.
1. Because it is written in as customer program we can add additional feature, for example change history.
2. There is no need to create screen maintenance for all table because screen will automatically generated.

But, remember, this is only a demonstration of how to use STC1_FULLSCREEN_TABLE_CONTROL. Use it carefully and only for customer table (Z*).


Because there is difficulty in write "<" and ">" in blogger, I replace "<" with "{" and ">" with "}" in this sample code. Copy this sample code, then replace all "{" with "<" then replace all "}" with ">".


REPORT ZAALGAL002 NO STANDARD PAGE HEADING.

TYPE-POOLS: rsds.

DATA: is_x030l TYPE x030l,
it_dfies TYPE TABLE OF dfies,
is_dfies TYPE dfies,
it_fdiff TYPE TABLE OF field_dif,
is_fdiff TYPE field_dif.

DATA: w_selid TYPE rsdynsel-selid,
it_tables TYPE TABLE OF rsdstabs,
is_tables TYPE rsdstabs,
it_fields TYPE TABLE OF rsdsfields,
it_expr TYPE rsds_texpr,
it_ranges TYPE rsds_trange,
it_where TYPE rsds_twhere,
is_where TYPE rsds_where,
w_active TYPE i.

DATA: it_content TYPE REF TO data,
it_modif TYPE REF TO data,
it_fcat TYPE lvc_t_fcat.

DATA: w_okcode TYPE sy-ucomm.


FIELD-SYMBOLS: {itab} TYPE STANDARD TABLE,
{ntab} TYPE STANDARD TABLE.


* Macros
DEFINE table_error.
message e398(00) with 'Table' p_table &1.
END-OF-DEFINITION.

DEFINE fixed_val.
is_fdiff-fieldname = is_dfies-fieldname.
is_fdiff-fixed_val = &1.
is_fdiff-no_input = 'X'.
append is_fdiff to it_fdiff.
END-OF-DEFINITION.


* Selection screen
SELECTION-SCREEN: BEGIN OF BLOCK b01 WITH FRAME.
PARAMETERS: p_table TYPE tabname OBLIGATORY "table
MEMORY ID dtb
MATCHCODE OBJECT dd_dbtb_16.
SELECTION-SCREEN: BEGIN OF LINE,
PUSHBUTTON 33(20) selopt USER-COMMAND sel,
COMMENT 55(15) selcnt,
END OF LINE.
SELECTION-SCREEN: SKIP.
PARAMETERS: p_rows TYPE i. "rows
SELECTION-SCREEN: END OF BLOCK b01,
SKIP,
BEGIN OF BLOCK b02 WITH FRAME.
PARAMETERS: p_displ TYPE c AS CHECKBOX. "display
SELECTION-SCREEN: END OF BLOCK b02.

* Initialization
INITIALIZATION.
MOVE '@4G@ Filter records' TO selopt.

* PBO
AT SELECTION-SCREEN OUTPUT.
IF w_active IS INITIAL.
CLEAR: selcnt.
ELSE.
WRITE w_active TO selcnt LEFT-JUSTIFIED.
ENDIF.

* PAI
AT SELECTION-SCREEN.
IF p_table NE is_x030l-tabname.
CALL FUNCTION 'DDIF_NAMETAB_GET'
EXPORTING
tabname = p_table
IMPORTING
x030l_wa = is_x030l
TABLES
dfies_tab = it_dfies
EXCEPTIONS
OTHERS = 1.
IF is_x030l IS INITIAL.
table_error 'does not exist or is not active'.
ELSEIF is_x030l-tabtype NE 'T'.
table_error 'is not selectable'.
ELSEIF is_x030l-align NE 0.
table_error 'has alignment - cannot continue'.
ENDIF.

* Default values for system fields
REFRESH: it_fdiff.
is_fdiff-tabname = p_table.
LOOP AT it_dfies INTO is_dfies.
IF is_dfies-datatype = 'CLNT'.
fixed_val sy-mandt.
ELSEIF is_dfies-rollname = 'ERDAT'
OR is_dfies-rollname = 'ERSDA'
OR is_dfies-rollname = 'AEDAT'
OR is_dfies-rollname = 'LAEDA'.
fixed_val sy-datum.
ELSEIF is_dfies-rollname = 'ERTIM'
OR is_dfies-rollname = 'AETIM'.
fixed_val sy-uzeit.
ELSEIF is_dfies-rollname = 'ERNAM'
OR is_dfies-rollname = 'AENAM'.
fixed_val sy-uname.
ENDIF.
ENDLOOP.

* Prepare free selection on table
REFRESH it_tables.
is_tables-prim_tab = p_table.
APPEND is_tables TO it_tables.

CLEAR: w_selid.
ENDIF.

IF sy-ucomm = 'SEL'.
IF w_selid IS INITIAL.
* Init free selection dialog
CALL FUNCTION 'FREE_SELECTIONS_INIT'
EXPORTING
expressions = it_expr
IMPORTING
selection_id = w_selid
expressions = it_expr
TABLES
tables_tab = it_tables
EXCEPTIONS
OTHERS = 1.
ENDIF.

* Display free selection dialog
CALL FUNCTION 'FREE_SELECTIONS_DIALOG'
EXPORTING
selection_id = w_selid
title = 'Selection'
status = 1
as_window = 'X'
IMPORTING
expressions = it_expr
field_ranges = it_ranges
number_of_active_fields = w_active
TABLES
fields_tab = it_fields
EXCEPTIONS
OTHERS = 1.
ENDIF.


* Start of processing
START-OF-SELECTION.

PERFORM f_create_table USING p_table.

PERFORM f_select_table.

PERFORM f_display_table.


*---------------------------------------------------------------------*
* FORM f_create_table *
*---------------------------------------------------------------------*
FORM f_create_table USING in_tabname.

FIELD-SYMBOLS: {fcat> TYPE lvc_s_fcat.

CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = in_tabname
CHANGING
ct_fieldcat = it_fcat
EXCEPTIONS
OTHERS = 1.
IF sy-subrc = 0.
* Complete field catalog
LOOP AT it_fcat ASSIGNING {fcat>.
{fcat>-tabname = in_tabname.
ENDLOOP.
CALL FUNCTION 'LVC_FIELDCAT_COMPLETE'
CHANGING
ct_fieldcat = it_fcat
EXCEPTIONS
OTHERS = 1.
ELSE.
WRITE: 'Error building field catalog'.
STOP.
ENDIF.

* Create dynamic table for data
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = it_fcat
IMPORTING
ep_table = it_content.
IF sy-subrc = 0.
ASSIGN it_content->* TO {itab}.
ELSE.
WRITE: 'Error creating internal table'.
STOP.
ENDIF.

* Create dynamic table for modif
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = it_fcat
IMPORTING
ep_table = it_modif.
IF sy-subrc = 0.
ASSIGN it_modif->* TO {ntab}.
ELSE.
WRITE: 'Error creating internal table'.
STOP.
ENDIF.

ENDFORM.


*---------------------------------------------------------------------*
* FORM f_select_table *
*---------------------------------------------------------------------*
FORM f_select_table.

IF w_active = 0.
SELECT * FROM (p_table)
INTO CORRESPONDING FIELDS OF TABLE {itab}
UP TO p_rows ROWS.
ELSE.
* Selection with parameters
CALL FUNCTION 'FREE_SELECTIONS_RANGE_2_WHERE'
EXPORTING
field_ranges = it_ranges
IMPORTING
where_clauses = it_where.
READ TABLE it_where INTO is_where WITH KEY tablename = p_table.

SELECT * FROM (p_table)
INTO CORRESPONDING FIELDS OF TABLE {itab}
UP TO p_rows ROWS
WHERE (is_where-where_tab).
ENDIF.

IF sy-dbcnt = 0.
WRITE: 'No record selected'.
STOP.
ENDIF.
ENDFORM.


*---------------------------------------------------------------------*
* FORM f_display_table *
*---------------------------------------------------------------------*
FORM f_display_table.
DATA: l_answer TYPE c,
l_eflag TYPE c.

CLEAR: w_okcode.
REFRESH: {ntab}.
* Display table contents
CALL FUNCTION 'STC1_FULLSCREEN_TABLE_CONTROL'
EXPORTING
header = p_table
tabname = p_table
display_only = p_displ
endless = 'X'
no_button = space
IMPORTING
okcode = w_okcode
TABLES
nametab = it_dfies
table = {itab}
fielddif = it_fdiff
modif_table = {ntab}
EXCEPTIONS
OTHERS = 1.
IF sy-subrc = 0.
IF p_displ IS INITIAL AND w_okcode = 'SAVE'.
* Confirm update
CALL FUNCTION 'POPUP_TO_CONFIRM'
EXPORTING
titlebar = p_table
text_question = 'Do you want to update table ?'
default_button = '2'
display_cancel_button = ' '
IMPORTING
answer = l_answer
EXCEPTIONS
OTHERS = 1.
IF l_answer = '1'.
* Apply modifications
IF NOT {ntab}[] IS INITIAL.
PERFORM f_add_system USING space.
MODIFY (p_table) FROM TABLE {ntab}.
IF sy-subrc NE 0.
l_eflag = 'X'.
ENDIF.
ENDIF.
* Apply deletions
IF l_eflag IS INITIAL.
REFRESH: {ntab}.
CALL FUNCTION 'STC1_GET_DATA'
TABLES
deleted_data = {ntab}
EXCEPTIONS
OTHERS = 1.
IF NOT {ntab}[] IS INITIAL.
DELETE (p_table) FROM TABLE {ntab}.
IF sy-subrc NE 0.
ROLLBACK WORK.
l_eflag = 'X'.
ENDIF.
ENDIF.
ENDIF.
* Apply creations
IF l_eflag IS INITIAL.
REFRESH: {ntab}.
CALL FUNCTION 'STC1_GET_DATA'
TABLES
new_data = {ntab}
EXCEPTIONS
OTHERS = 1.
IF NOT {ntab}[] IS INITIAL.
PERFORM f_add_system USING 'X'.
INSERT (p_table) FROM TABLE {ntab}.
IF sy-subrc NE 0.
ROLLBACK WORK.
l_eflag = 'X'.
ENDIF.
ENDIF.
ENDIF.
IF l_eflag IS INITIAL.
COMMIT WORK.
MESSAGE s261(53).
ELSE.
MESSAGE s075(3i).
PERFORM f_select_table.
ENDIF.
ENDIF.
* Display table again
PERFORM f_display_table.
ENDIF.
ENDIF.

ENDFORM.


*---------------------------------------------------------------------*
* FORM f_add_system *
*---------------------------------------------------------------------*
FORM f_add_system USING new TYPE c.

FIELD-SYMBOLS: {irec} TYPE ANY,
{upd} TYPE ANY.

LOOP AT it_fdiff INTO is_fdiff.
READ TABLE it_dfies INTO is_dfies
WITH KEY fieldname = is_fdiff-fieldname.
LOOP AT {ntab} ASSIGNING {irec}.
ASSIGN COMPONENT is_fdiff-fieldname OF STRUCTURE {irec} TO {upd}.
IF is_dfies-datatype = 'CLNT'.
{upd} = sy-mandt.
ELSE.
CASE is_dfies-rollname.
WHEN 'AENAM'.
{upd} = sy-uname.
WHEN 'AEDAT' OR 'LAEDA'.
{upd} = sy-datum.
WHEN 'AETIM'.
{upd} = sy-uzeit.
WHEN OTHERS.
ENDCASE.
ENDIF.
ENDLOOP.
ENDLOOP.

ENDFORM.


Read More......

FTP



SAP provide sample code to connect to FTP.

It provided in SAP function module, FTP_CONNECT (connect to FTP), FTP_COMMAND (send command by FTP), FTP_DISCONNECT (disconnect),FTP_R3_TO_SERVER (put file to FTP),FTP_SERVER_TO_R3 (get file from FTP).

Instead of writing a new code, I will try to give you explanation on SAP sample code.

I will explore program RSFTP002.


Program RSFTP002 demonstrate how to send command to FTP.



You can send any FTP command by this program.
For RFC destination use SAPFTP or SAPFTPA. Those are standard RFC destination to connect to FTP. SAPFTP run on client, and SAPFTPA run on application server.

First step of this program is using FTP_CONNECT, don't forget to use scrambled password.
Then we send a command to FTP by using FTP_COMMAND.
To disconnect, use FTP_DISCONNECT.

To copy, or get a file from SAP to/from FTP, you can explore RSFTP002. It demonstrate how to use FTP_R3_TO_SERVER (put file to FTP) & FTP_SERVER_TO_R3 (get file from FTP).
Other way is send put command using FTP_COMMAND. To put a file, do this steps, change FTP directory using "cd dir", change local directory using "lcd localdir", then send put command "put file".

Read More......

Simple ALV

ALV is very an useful tools in reporting. Just pass the data to ALV, than it will display interactive report easy to sort, filter, calculate total, etc.

There are two ALV model, list report (function REUSE_ALV_LIST_DISPLAY) and grid report (function REUSE_ALV_GRID_DISPLAY). Please read the function module documentation for complete guidance.

There are two main part of simple ALV report.
1. Create field catalog.
Field catalog containing descriptions of the list output fields.
2. Pass data and field catalog to ALV function (function REUSE_ALV_LIST_DISPLAY or REUSE_ALV_GRID_DISPLAY).

You can also playing with ALV format, find it in "Change Format in ALV".

This is a simple sample of how to use ALV.


REPORT ZAALGAL0001.
*-----------------------------------------
*Data declaration
TABLES: mara,
makt.

* Data output
DATA: BEGIN OF t_report OCCURS 3,
matnr LIKE mara-matnr,
mtart LIKE mara-mtart,
maktx LIKE makt-maktx,
END OF t_report.

* Field desciption / field catalog
TYPE-POOLS: SLIS.
data t_fcat type SLIS_T_FIELDCAT_ALV.

*-
DATA: d_repid LIKE sy-repid.

*-----------------------------------------
*--Selection Screen
SELECT-OPTIONS: s_matnr FOR mara-matnr,
s_mtart FOR mara-mtart.

*-----------------------------------------
START-OF-SELECTION.
*-Read data
SELECT * FROM mara
WHERE matnr IN s_matnr AND
mtart IN s_mtart.
CLEAR makt.
SELECT SINGLE *
FROM makt
WHERE matnr = mara-matnr AND
spras = sy-langu.
MOVE: mara-matnr TO t_report-matnr,
mara-mtart TO t_report-mtart,
makt-maktx TO t_report-maktx.
APPEND t_report.
ENDSELECT.

IF sy-subrc NE 0.
WRITE 'No data found'.
EXIT.
ENDIF.

*-----------------------------------------
*-Create Field Catalog

* Store report name
d_repid = sy-repid.

* Create Fieldcatalogue from internal table
* Use capital letter for I_INTERNAL_TABNAME
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
I_PROGRAM_NAME = d_repid
I_INTERNAL_TABNAME = 'T_REPORT'
I_INCLNAME = d_repid
CHANGING
CT_FIELDCAT = t_fcat
EXCEPTIONS
INCONSISTENT_INTERFACE = 1
PROGRAM_ERROR = 2
OTHERS = 3.

IF SY-SUBRC <> 0.
write: / 'Error:',sy-subrc,
'when Create Field Catalog'.
EXIT.
ENDIF.

* Call ALV List Display
CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = d_repid
IT_FIELDCAT = t_fcat
TABLES
T_OUTTAB = t_report
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2.

IF SY-SUBRC <> 0.
write: / 'Error:',sy-subrc,
'when Call ALV List Display'.
EXIT.
ENDIF.

Read More......

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.

Read More......

Data Declaration

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.

Read More......

Monday, July 16, 2007

my first ABAP 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.

Read More......