22
Customizing SAP for Dummies: ABAP Tutorial for Students with some Programming Experience – Part 1 As you know, I am going through some kind of ABAP training right now with the help of one of the team members. I have spend the last two weeks reading the documentation of help.sap.com and feel I am starting to get a better grip of what ABAP is and how you can use it. The goal of this tutorial is to give any person with the basic knowledge of procedural programming , object- oriented programming (OOP) and SQL , a good overview of what the possibilities of the language are and how to understand a basic ABAP program. So let’s start. Programs in SAP are created, edited and debugged in the same presentation framework you normally use to access the SAP programs. These programs are called Transactions and are either local programs or belong to a package. The only reason for using packages is to group different files that belong together and make the transportation phase easier. Transportation meaning changing the status of a program, from the programing phase to the testing phase, to finally transporting the program to the SAP master. Any program written in ABAP begins with one of the following introductory statements, a space and the name of the program: REPORT PROGRAM FUNCTION-POOL CLASS-POOL INTERFACE-POOL TYPE-POOL What you normaly use is a report. A report is nothing more than an executable programm. Function pools are, as its name says, pools of functions you can include in your

Customizing SAP-ABAP for Dummies

Embed Size (px)

Citation preview

Page 1: Customizing SAP-ABAP for Dummies

Customizing SAP for Dummies: ABAP Tutorial for Students with some Programming Experience – Part   1

As you know, I am going through some kind of ABAP training right now with the help of one of the team members. I have spend the last two weeks reading the documentation of help.sap.com and feel I am starting to get a better grip of what ABAP is and how you can use it.

The goal of this tutorial is to give any person with the basic knowledge of procedural programming, object-oriented programming (OOP) and SQL, a good overview of what the possibilities of the language are and how to understand a basic ABAP program.

So let’s start.

Programs in SAP are created, edited and debugged in the same presentation framework you normally use to access the SAP programs. These programs are called Transactions and are either local programs or belong to a package. The only reason for using packages is to group different files that belong together and make the transportation phase easier. Transportation meaning changing the status of a program, from the programing phase to the testing phase, to finally transporting the program to the SAP master.

Any program written in ABAP begins with one of the following introductory statements, a space and the name of the program:

REPORT PROGRAM FUNCTION-POOL CLASS-POOL INTERFACE-POOL TYPE-POOL

What you normaly use is a report. A report is nothing more than an executable programm. Function pools are, as its name says, pools of functions you can include in your executable program. The same for type pools, which contains definitions of types to use in executable programs. Class pools are used in OOP, so we will come to that later (when I get to the topic in my training :-) )

Once you have defined the type of program and its name, you can start with the types and data definitions. I will explain both together as their similarities are many. To define a new type you have following syntax

TYPES new_type{TYPE {REF TO} defined_type} |{LIKE {REF TO} defined_variable}.

Page 2: Customizing SAP-ABAP for Dummies

This code defines a new type new_type based on the already defined type defined_type or based on the type of the variable defined_variable. If you use the keyword REF TO the new type will be only a pointer to a variable of that type.

Analogous we can use following syntax to define a variable:

DATA new_type{(c)}{TYPE {REF TO} defined_type} |{LIKE {REF TO} defined_variable}{VALUE d}.

Just notice that if you use the option (c) you will create an array of c elements of the defined type, and using the keyword VALUE you can assign a value d to the variable.

And for the end of this part I just want to show you the predefined types in ABAP you can use when defining new types or variables.

Numeric Types

Type IdentifierInitial Value

Lenght (in Bytes)

Format

Integer I 0 4 -Floating point number

F 0 8 -

Packed number P 0 1-160000- (negative) / 0000 (positive)

Character Types

Type Identifier Initial Value Lenght (in Bytes) FormatCharacters C ‘ ‘ 1 – 65535 -Date D ‘00000000 ‘ 8 YYYYMMDDTime T ‘000000 ‘ 6 HHMMSSHexadecimal field X 0 1 – 65535 X’0000..000′

Page 3: Customizing SAP-ABAP for Dummies

Customizing SAP for Dummies: ABAP Tutorial for Students with some Programming Experience – Part   2

In the first part of this tutorial I explained the basics of programs and types in ABAP. Today I will introduce you to the a first ABAP program, which in a way is a Hello World program for ABAP, using the possible features of ABAP in a single simple report. As I said last time a report is nothing more than an executable file.

So here it is:

view source

print ? 01 *&--------------------------------------------* 02 *& Report  ZSER1301 03 *& 04 *&--------------------------------------------* 05 *& 06 *& 07 *&--------------------------------------------* 08 REPORT  zser1301.

09   10 * Declarative Section11 TYPES: BEGIN OF zsf_type, 12   carrid LIKE sflight-carrid, 13   carrname LIKE scarr-carrname, 14   currcode LIKE scarr-currcode, 15   minpay LIKE sflight-paymentsum, 16   maxpay LIKE sflight-paymentsum, 17   avgpay LIKE sflight-paymentsum, 18   totpay LIKE sflight-paymentsum, 19 END OF zsf_type. 20   21 DATA: i_tab_fl TYPE STANDARD TABLE OF zsf_type, 22       wa_tab_fl TYPE zsf_type, 23       time_beg TYPE i, 24       time_end TYPE i, 25       time_tot TYPE i.

26   27 * Program Statements 28 GET RUN TIME FIELD time_beg. 29   30 SELECT sflight~carrid scarr~carrname 31   MIN( paymentsum ) AS minpay 32   MAX( paymentsum ) AS maxpay 33   AVG( paymentsum ) AS avgpay 34   SUM( paymentsum ) AS totpay

Page 4: Customizing SAP-ABAP for Dummies

35   FROM sflight JOIN scarr 36   ON sflight~carrid = scarr~carrid 37   INTO CORRESPONDING FIELDS OF TABLE tab_fl 38   GROUP BY sflight~carrid scarr~carrname 39   ORDER BY sflight~carrid. 40   41 GET RUN TIME FIELD time_end. 42   43 time_tot = ( time_end - time_beg ).

44   45 WRITE:/ 'Carriers Data'. 46 ULINE. 47 WRITE:/(8) 'CARRIER', (20) 'NAME',(23) 'MIN PAY', 48 (23) 'MAX PAY', (23) 'AVG PAY', (23) 'SUM PAY'. 49 ULINE.

50   51 LOOP AT i_tab_fl INTO wa_tab_fl. 52   WRITE:/(8) wa_tab_fl-carrid, 53   (20) wa_tab_fl-carrname, 54   (17) wa_tab_fl-minpay, wa_tab_fl-currcode, 55   (17) wa_tab_fl-maxpay, wa_tab_fl-currcode, 56   (17) wa_tab_fl-avgpay, wa_tab_fl-currcode, 57   (17) wa_tab_fl-totpay, wa_tab_fl-currcode. 58 ENDLOOP.

59   60 WRITE: /, / 'Runtime = ', time_tot.

Now let’s go through it and I will try to explain how all this code works.

Lines 11-19:

Here we declare the structure type we will use in the program. sflight and scarr are two tables declared in the ABAP dictionary. All tables declared in this dictionary are global and may be used in any program. In scarr information about different airlines in store, and in sflight that about flights of these airlines.

Lines 21-25:

We declare an internal table, which is of type of this structure. This means we have one or more entries, and in each entry such an structure. After this we declare a work area, of the same type as the table entry. And declare three integer variables, which we use to calculate the runtime of the report.

Lines 28, 41, 43:

We get the user time before the logic, after it, and then calculate the total.

Lines 30-39:

SQL query. I asume you have some idea of SQL, so I am just going to explain all the ABAP related stuff. We select diferent fields and use some aggregate functions on the

Page 5: Customizing SAP-ABAP for Dummies

paymentsum field of sflight. In order to do this we use group by, and use order by just to have a better output. We are selecting from a join of two different tables.Pay attention to line 37. In this line we tell the database to store the result of the SQL query into the internal table tab_fl, and to only store the fields of the select, which exist in the internal table.

Lines 45-49:

We display the title and fields of the table.

Lines 51-58:

We display the data store in our internal table. To do this we use a loop at the internal table into the work area. This means that in each iteration of the loop we will get another entry of the internal table in the work area. We use the syntax workarea-fieldname to access the field values.

We let the program run, and get the following screen:

Screen of Report ZSER1301

That was not so difficult, was it? I know you are probably thinking this is not the kind of program you program in every language, but it is the most typical report in ABAP. Actually, a report is call this way, because you get information and report it back.

So now that we have written a program that works, let’s try to write the same thing and use the right tools for it.

In the next example we will do exactly the same. For it we will declare the structure type in the ABAP dictionary and create a function module (FM), which works like a subroutine, to get the information from the database. This way we make use of the principle of modularity, reusability and continuity.

So the first thing we do, is create the structure type, using the object explorer:

Page 6: Customizing SAP-ABAP for Dummies

ZSF_TYPE Structure

We rewrite the report:

view source

print ? 01 *&--------------------------------------------* 02 *& Report  ZSER1300 03 *& 04 *&--------------------------------------------* 05 *& 06 *& 07 *&--------------------------------------------* 08 REPORT  zser1300.

09   10 * Declarative Section

11   12 *TABLES: scarr, sflight.

13   14 DATA: i_tab_fl TYPE STANDARD TABLE OF zsf_type, 15       wa_tab_fl TYPE zsf_type, 16       time_tot TYPE i.

17   18 * Program Statements

19   20 CALL FUNCTION 'ZSER1300_FM'21   IMPORTING 22     time_tot = time_tot 23   TABLES 24     tab_fl   = i_tab_fl.

25   26 WRITE:/ 'Carriers Data'. 27 ULINE. 28 WRITE:/(8) 'CARRIER', (20) 'NAME',(23) 'MIN PAY',

Page 7: Customizing SAP-ABAP for Dummies

29 (23) 'MAX PAY', (23) 'AVG PAY', (23) 'SUM PAY'. 30 ULINE.

31   32 LOOP AT i_tab_fl INTO wa_tab_fl. 33   WRITE:/(8) wa_tab_fl-carrid, 34   (20) wa_tab_fl-carrname, 35   (17) wa_tab_fl-minpay, wa_tab_fl-currcode, 36   (17) wa_tab_fl-maxpay, wa_tab_fl-currcode, 37   (17) wa_tab_fl-avgpay, wa_tab_fl-currcode, 38   (17) wa_tab_fl-totpay, wa_tab_fl-currcode. 39 ENDLOOP.

40   41 WRITE: /, / 'Runtime = ', time_tot.

and in line 20 we call the function module ZSER1300_FM and pass the variables which are going to be return.

If you take a look at the interface of ZSER1300_FM, you will see the declaration of the variables used in the FM. All not optional will have to be assigned at the time of the FM call.

view source

print ? 01 FUNCTION ZSER1300_FM. 02 *&--------------------------------------------* 03 *"Local Interface:

04 *"  EXPORTING 05 *"     REFERENCE(TIME_TOT) TYPE  I

06 *"  TABLES 07 *"      TAB_FL STRUCTURE  ZSF_TYPE

08 *"  CHANGING 09 *"     REFERENCE(TIME_BEG) TYPE  I OPTIONAL

10 *"     REFERENCE(TIME_END) TYPE  I OPTIONAL 11 *&--------------------------------------------*

12   13 GET RUN TIME FIELD time_beg. 14 SELECT sflight~carrid scarr~carrname 15   MIN( paymentsum ) AS minpay 16   MAX( paymentsum ) AS maxpay 17   AVG( paymentsum ) AS avgpay 18   SUM( paymentsum ) AS totpay 19   FROM sflight JOIN scarr 20   ON sflight~carrid = scarr~carrid 21   INTO CORRESPONDING FIELDS OF TABLE tab_fl 22   GROUP BY sflight~carrid scarr~carrname 23   ORDER BY sflight~carrid. 24 GET RUN TIME FIELD time_end. 25   26 time_tot = ( time_end - time_beg ).

27   

Page 8: Customizing SAP-ABAP for Dummies

28 ENDFUNCTION.

Page 9: Customizing SAP-ABAP for Dummies

 

Customizing SAP for Dummies: ABAP Tutorial for Students with some Programming Experience – Part   3

It is time to continue the ABAP tutorial. After the basics in Part 1 and the first report in Part 2, in this part we are going to take a look at the different events in ABAP used in what is called an Interactive Report. An Interactive Report is supposed to display information about a list of items, and each time we click on one of these items, to display the details of such item in a so called subscreen.

As always let’s start with the source code of the report and the source code of the different FMs:

view source

print ? 001 *&------------------------------------------------* 002 *& Report  Z_TEST_INT_REP 003 *& 004 *&------------------------------------------------* 005 *& 006 *& 007 *&------------------------------------------------*

008   009 REPORT  z_test_int_rep NO STANDARD PAGE HEADING. 010   011 DATA: vbak_input TYPE zvbak_input, 012       vbak_output TYPE STANDARD TABLE OF ztabfm01, 013       wa_vbak TYPE ztabfm01,

014   015       vbap_input TYPE STANDARD TABLE OF ztabfm02, 016       vbap_output TYPE STANDARD TABLE OF ztabfm02, 017       wa_vbap TYPE ztabfm02, 018       vbeln_n TYPE n LENGTH 10,

019   020       vbep_output TYPE STANDARD TABLE OF ztabfm03, 021       wa_vbep TYPE ztabfm03,

022   023       box TYPE c.

024   025 PARAMETERS: vbtyp LIKE vbak_input-vbtyp 026             OBLIGATORY VALUE CHECK, 027             auart LIKE vbak_input-auart 028             OBLIGATORY VALUE CHECK, 029             vkorg LIKE vbak_input-vkorg 030             OBLIGATORY VALUE CHECK,

Page 10: Customizing SAP-ABAP for Dummies

031             vtweg LIKE vbak_input-vtweg 032             OBLIGATORY VALUE CHECK, 033             spart LIKE vbak_input-spart 034             OBLIGATORY VALUE CHECK.

035   036 INITIALIZATION. 037   vbtyp = 'C'. 038   auart = 'TA'. 039   vkorg = '1000'. 040   vtweg = '10'. 041   spart = '00'.

042   043 TOP-OF-PAGE. 044   WRITE:/ 'Sales Header'. 045   ULINE. 046   WRITE:/ 'Document', (10) 'Category', 047         (12) 'Organization'. 048   ULINE.

049   050 TOP-OF-PAGE DURING LINE-SELECTION. 051   CASE sy-lsind. 052     WHEN 1. 053       WRITE:/ 'Sales Item'. 054       ULINE. 055       WRITE:/(15) 'Document', (10) 'Item', 056       (17) 'Material Number', (12) 'Price'. 057       ULINE. 058     WHEN 2. 059       WRITE:/ 'Sales Schedule Line'. 060       ULINE. 061       WRITE:/(15) 'Document', (10) 'Item', 062       (18) 'Schedule Number', (15) 'Order quantity', 063       (15) 'Conf. quantity'. 064       ULINE. 065   ENDCASE.

066   067 AT SELECTION-SCREEN. 068   PERFORM vbak_in_to_out. 069   IF vbak_output IS INITIAL. 070     MESSAGE e888(sabapdocu) WITH 'No entries found'. 071   ENDIF.

072   073 START-OF-SELECTION. 074   PERFORM vbak_in_to_out. 075   PERFORM display_data_vbak.

076   077 AT LINE-SELECTION. 078   CASE sy-lsind. 079     WHEN 1. 080       CLEAR: vbap_input. 081       DO. 082         CLEAR: wa_vbap.

Page 11: Customizing SAP-ABAP for Dummies

083         READ LINE sy-index FIELD VALUE box 084         wa_vbak-vbeln INTO vbeln_n. 085         IF sy-subrc NE 0. 086           EXIT. 087         ELSEIF box = 'X'. 088           MOVE vbeln_n TO wa_vbap-vbeln. 089           APPEND wa_vbap TO vbap_input. 090         ENDIF. 091       ENDDO.

092   093       IF vbap_input IS INITIAL. 094         MESSAGE s888(sabapdocu) WITH095         'No line selected'. 096         sy-lsind = sy-lsind - 1. 097       ELSE. 098         CLEAR vbap_output. 099         CALL FUNCTION 'Z_TEST_FUNCT_BE_INT2'100           TABLES 101             vbeln_in = vbap_input 102             vbap_det = vbap_output. 103         IF vbap_output IS INITIAL. 104           MESSAGE s888(sabapdocu) WITH105           'No items to display'. 106           sy-lsind = sy-lsind - 1. 107         ELSE. 108           PERFORM display_data_vbap. 109         ENDIF. 110       ENDIF.

111   112     WHEN 2. 113       CALL FUNCTION 'Z_TEST_FUNCT_BE_INT3'114         EXPORTING 115           vbeln_in = wa_vbap-vbeln 116           posnr_in = wa_vbap-posnr 117         TABLES 118           vbep_det = vbep_output. 119       PERFORM display_data_vbep.

120   121     WHEN 3. 122       MESSAGE s888(sabapdocu) 123       WITH 'No more subscreens available'. 124   ENDCASE.

125   126 *&------------------------------------------------* 127 *&      Form  display_data_vbak 128 *&------------------------------------------------* 129 *       text 130 *&------------------------------------------------* 131 FORM display_data_vbak. 132   LOOP AT vbak_output INTO wa_vbak. 133     WRITE:/ box AS CHECKBOX, wa_vbak-vbeln, 134     (10) wa_vbak-vbtyp, (12) wa_vbak-vkorg. 135   ENDLOOP.

Page 12: Customizing SAP-ABAP for Dummies

136 ENDFORM. "display_data_vbak

137   138 *&------------------------------------------------* 139 *&      Form  display_data_vbap 140 *&------------------------------------------------* 141 *       text 142 *&------------------------------------------------* 143 FORM display_data_vbap. 144   LOOP AT vbap_output INTO wa_vbap. 145     WRITE:/(15) wa_vbap-vbeln, (10) wa_vbap-posnr, 146     (17) wa_vbap-matnr, (12) wa_vbap-netpr. 147     HIDE wa_vbap. 148   ENDLOOP. 149 ENDFORM. "display_data_vbap

150   151 *&------------------------------------------------* 152 *&      Form  display_data_vbep 153 *&------------------------------------------------* 154 *       text 155 *&------------------------------------------------* 156 FORM display_data_vbep. 157   LOOP AT vbep_output INTO wa_vbep. 158     WRITE:/(15) wa_vbep-vbeln, (10) wa_vbep-posnr, 159     (17) wa_vbep-etenr, (15) wa_vbep-wmeng, 160     (15) wa_vbep-bmeng. 161   ENDLOOP. 162 ENDFORM. "display_data_vbep

163   164 *&------------------------------------------------* 165 *&      Form  vbak_in_to_out 166 *&------------------------------------------------* 167 *       text 168 *&------------------------------------------------* 169 FORM vbak_in_to_out. 170   CLEAR: vbak_input, vbak_output. 171   vbak_input-vbtyp = vbtyp. 172   vbak_input-auart = auart. 173   vbak_input-vkorg = vkorg. 174   vbak_input-vtweg = vtweg. 175   vbak_input-spart = spart.

176   177   CALL FUNCTION 'Z_TEST_FUNCT_BE_INT1' 178     EXPORTING 179       vbak_input = vbak_input 180     TABLES 181       vbak_det   = vbak_output. 182 ENDFORM.                    "vbak_in_to_out

view source

print ? 01 FUNCTION Z_TEST_FUNCT_BE_INT1. 02 *"------------------------------------------------

Page 13: Customizing SAP-ABAP for Dummies

03 *"*"Local Interface:

04 *"  IMPORTING 05 *"     REFERENCE(VBAK_INPUT) TYPE  ZVBAK_INPUT

06 *"  TABLES 07 *"      VBAK_DET STRUCTURE  ZTABFM01 08 *"------------------------------------------------

09   10   SELECT vbeln erdat erzet ernam vbtyp 11     auart vkorg vtweg spart FROM vbak 12     INTO CORRESPONDING FIELDS OF TABLE vbak_det 13 *    UP TO 100 ROWS14     WHERE vbtyp = vbak_input-vbtyp AND15     auart = vbak_input-auart AND16     vkorg = vbak_input-vkorg AND17     vtweg = vbak_input-vtweg AND18     spart = vbak_input-spart.

19   20 ENDFUNCTION.

view source

print ? 01 FUNCTION z_test_funct_be_int2. 02 *"------------------------------------------------ 03 *"*"Local Interface:

04 *"  TABLES 05 *"      VBELN_IN STRUCTURE  ZTABFM02

06 *"      VBAP_DET STRUCTURE  ZTABFM02 07 *"------------------------------------------------

08   09   SELECT vbeln posnr matnr kwmeng netpr werks 10    INTO CORRESPONDING FIELDS OF TABLE vbap_det 11    FROM vbap 12    FOR ALL ENTRIES IN vbeln_in 13    WHERE vbeln EQ vbeln_in-vbeln. 14   15 ENDFUNCTION.

view source

print ? 01 FUNCTION z_test_funct_be_int3. 02 *"------------------------------------------------ 03 *"*"Local Interface:

04 *"  IMPORTING 05 *"     REFERENCE(VBELN_IN) TYPE  VBELN_VA

06 *"     REFERENCE(POSNR_IN) TYPE  POSNR 07 *"  TABLES

08 *"      VBEP_DET STRUCTURE  ZTABFM03 09 *"------------------------------------------------

10   11   SELECT vbeln posnr etenr edatu wmeng bmeng 12     FROM vbep

Page 14: Customizing SAP-ABAP for Dummies

13     INTO CORRESPONDING FIELDS OF TABLE vbep_det 14     WHERE vbeln = vbeln_in AND posnr = posnr_in. 15   16 ENDFUNCTION.

The way this report works is the following:

When you run the report you are presented with a screen, called Selection Screen, where you can select the kind of Sales you want to display information about.

Selection Screen

After choosing the kind and clicking continue (the green tick button on the upper left) you are presented with the sales headers, corresponding to what you have previously chosen.

First Display

Page 15: Customizing SAP-ABAP for Dummies

After you have chosen the headers and clicked on the magnifying glass, the details of the chosen headers will be displayed, i.e. the sales items belonging to the chosen headers.

Second Display

Finally, after double clicking any sales item, you will get information about the different schedules for the item.

Third and last display

As you can see, there are many new things in this example. I am just going to go through it and try to explain them all.

First of all, I am not declaring the types in the report anymore. I declare the types in the ABAP Data Dictionary. Any type declared here can be used in any report or FM in the SAP implementation, which is exactly what I do, using the same types for tables in the report and in the FM, as you can see at the declaration of the FMs. The following four types are declared:

ZTABFM01

Page 16: Customizing SAP-ABAP for Dummies

ZTABFM02

ZTABFM03

ZVBAK_INPUT

The next important thing is Forms. Forms are nothing more than function which you can call inside your report. These are called with the keyword PERFORM and declared between FORM and ENDFORM. They help structure the code and reuse it, as I am doing with vbak_in_to_out.

You could probably identify most of the events used, now that you know how the report works. However, I will explain each one of them:

INITIALIZATION (line 36)This is how parameters are initialized for the display of the selection screen. The default values for the input, so to say.

AT SELECTION-SCREEN (line 67)This happens exactly at the moment you click continue at the selection screen. In this example I am using a message code “MESSAGE e888(sabapdocu)” which means, if it is triggered, the selection is not finished, and the user is brought back to the selection screen.

START-OF-SELECTION (line 73)Happens exactly after AT SELECTION-SCREEN. Is where you actually do the selection and prepare everything for display.

TOP-OF-PAGE (line 43)Is the display at the top of the page when displaying the first selection. You normally write the page header here.

AT LINE-SELECTION (line 77)This is the most complex of all events in this example. It is the event triggered by the double click on a line, or the click of the magnifying glass. sy-lsind is an important variable in this event, which tells you in which selection screen

Page 17: Customizing SAP-ABAP for Dummies

the report is. If it is equal to 1 then you are selecting items of the first selection, if it is equal to 2 then from the second, etc.Lines 83 to 90 is the code used to check if there is any item selected. It goes through the lines of display and checks whether it is an item line and whether the box is checked.Lines 93 to 110 just checks if there is any items to display. Just remember that if there is nothing to display we have to set sy-lsind back and tell the report we are still at the first display screen.

TOP-OF-PAGE DURING LINE-SELECTION (line 50)Uses the same logic of AT LINE-SELECTION to display headers for the subscreens.

One last important thing is line 147. The hide statement allow us to save a variable into memory when double clicking a line, so we can use it for display in the next event (line 115 – wa_vbap).

Please feel free to create this example yourself and make small changes to it. It will surely give you a much deeper understanding of events in ABAP, and about interactive reports. Until next time.