Sap Abap Programming

April 26, 2018 | Author: Rohini Kumar | Category: Control Flow, Database Index, Computer Data, Areas Of Computer Science, Software Engineering
Share Embed Donate


Short Description

its example to all sap abap porgrams importent program in sap abap you can pratice to own and learn the programming ...

Description

Rohini kumar

sap abap consultant

Internal Table Basics Internal table is a data object in ABAP that exists only at run time of a program. It means when the program execution is complete then the internal table will be lost. We use internal table to store database table data after fetching it by a select query. The ABAP runtime system dynamically manages the internal table’s memory. It means we developer do not need to work on memory management of internal table. Internal table has three parts – rows, columns & work area. Rows are the line type of internal table. It is a structure which contains several fields. Those fields are of data elements. We need to declare the structure locally or globally to declare the internal table. 2. Columns are the fields of internal table. Those fields are of different data elements declared by locally or globally. 3. The most important part of an internal table is its work area. Work area is basically the line type of an internal table. It means it has the same structure of the rows of internal table. Work area contains the same fields of same type of the rows. It is of two types – implicit & explicit work area. A. When we declare an internal table with header line then a work area is automatically created with the same name of the table. This work area is called implicit work area which is actually the header line. There is no need to declare work area separately. This work area / header line contains the same table as of the internal table. 1.

Example – TYPES: BEGIN OF ty_mat, matnr TYPE mara-matnr, werks TYPE marc-werks, lgort TYPE mard-lgort, END OF ty_mat. DATA: it_mat TYPE STANDARD TABLE OF ty_mat WITH NON-UNIQUE KEY matnr WITH HEADER LINE.

Here we have declared a local structure ty_mat. This is the line type / row of the internal table. It means the table will contain rows which has three fields (matnr, werks & lgort). We declare the internal table it_mat with this local structure. We also can declare with global structure also. DATA: it_qinfo TYPE TABLE OF slis_qinfo_alv WITH HEADER LINE WITH NON-UNIQUE KEY type.

Here slis_qinfo_alv is a structure which has been declared globally in data dictionary. We can declare the internal table directly with the table type also. DATA: it_qinfo TYPE slis_t_add_fieldcat WITH HEADER LINE.

Here slis_t_add_fieldcat is a table type declared in data dictionary. Header line concept: MATNR

WERKS

LGORT

Rohini kumar

sap abap consultant

The name of this work area / header line is IT_MAT. When we create the internal table then it is like following: MATNR

WERKS

LGORT

It also contains the same name IT_MAT but it is mentioned IT_MAT[] in the program. B. If we declare an internal table without header line then we need to declare its work area seperately. Since we are declaring the work area explicitly it is called explicit work area. This work area contains the different name from the internal table. Example – TYPES: BEGIN OF ty_mat, matnr TYPE mara-matnr, werks TYPE marc-werks, lgort TYPE mard-lgort, END OF ty_mat. DATA: it_mat TYPE STANDARD TABLE OF ty_mat, wa_mat TYPE ty_mat.

Similarly we can declare internal table with globally declared structure or table type also. DATA: it_qinfo TYPE TABLE OF slis_qinfo_alv WITH NON-UNIQUE KEY type. DATA: it_qinfo TYPE slis_t_qinfo_alv.

Work area concept: MATNR

WERKS

LGORT

The name of this work area is WA_MAT. When we create the internal table then it is like following: MATNR

WERKS

LGORT

The table contains the name IT_MAT. In today’s programming header line is not used in internal table. It is now obsolete. There are two main reasons for that.

Rohini kumar

sap abap consultant

1.

The automatically generated header line / implicit work area has the same name as of internal table. That’s why it loses the readability of program. 2. When we use nested data objects (internal table has components of structure which is another internal table) then header line is not allowed. In object oriented programming header line is not allowed. To declare an internal table there is three basic specifications. They are 1. Row type, 2. Key & 3. Types of internal table. Internal table is of three types – standard table, sorted table & hashed table. Standard & sorted tables are called index table because we can access its records by its index. Index is nothing but a row number of the internal table. 1.

Standard table is an index table which has non-unique key. It can be accessed by index or key also. If we want to access by key then the key must be defined otherwise default key would be considered. The declaration is as follows: DATA: it_mat TYPE STANDARD TABLE OF ty_mat WITH NON-UNIQUE KEY matnr. OR DATA: it_mat TYPE TABLE OF ty_mat WITH NON-UNIQUE KEY matnr.

If we don’t mention “Standard table of” clause then by default the system takes it as a standard internal table. We can enter data into a standard internal table by using the APPEND statement. Append always enters data at the last row of the table. APPEND wa_mat TO it_mat.

2.

Sorted table is another kind of index table which has unique / non unique key. It also can be accessed via index or key. For sorted table the key must be specified. The declaration is as follows: DATA: it_mat TYPE SORTED TABLE OF ty_mat WITH UNIQUE KEY matnr, it_mat TYPE SORTED TABLE OF ty_mat WITH NON-UNIQUE KEY matnr.

Unique key means the MATNR (material no) will must be unique. If same material number is inserted then a run time error will happen. However we can declare the sorted table with non unique key also. In this case same material number can be entered but it will be sorted after entering the number. Here the sorted table behaves similar to sorted standard table. We use INSERT statement to enter any records to the sorted table. INSERT wa_mat INTO it_mat.

3.

Hashed table is not an index table. It follows the hash algorithm. Here the declaration of key is must and also the key must be unique. Hence no duplicate entry will be in the hashed table. We can access records only by the key. DATA: it_mat TYPE HASHED TABLE OF ty_mat WITH UNIQUE KEY matnr.

Similar to sorted tables data can be inserted here by INSERT statement. Hashed tables are used when the internal table contains huge volume of data. INSERT wa_mat INTO TABLE it_mat.

Rohini kumar

Table kind Index Access Key Access Key Uniqueness Usage

sap abap consultant

Index Tables Standard Table Sorted Table Yes Yes Yes Yes Non unique Unique/Non unique Index access Key access

Hashed Tables No Yes Unique Only key access

Standard Internal Table Standard table is an index table which has non-unique key. It can be accessed by index or key also. If we want to access by key then the key must be defined otherwise default key would be considered. The declaration is as follows: DATA: it_mat TYPE STANDARD TABLE OF ty_mat WITH NON-UNIQUE KEY matnr. OR DATA: it_mat TYPE TABLE OF ty_mat WITH NON-UNIQUE KEY matnr. OR DATA: it_mat TYPE TABLE OF ty_mat.

If we don’t mention “STANDARD TABLE OF” clause then by default the system takes it as a standard internal table. We can enter data into a standard internal table by using the APPEND statement. APPEND always enters data at the last row of the table. APPEND wa_mat TO it_mat.

We can sort data records in the standard table. SORT itab BY item.

Here item is the field of the table. We can sort table by any of its fields or multiple fields. Here we have an example of Standard table. REPORT

zabap_gui.

* Declaring the local structure of internal table TYPES: BEGIN OF ty_tab, item TYPE char10, quantity TYPE i, price TYPE i, END OF ty_tab. * Declaring the Standard internal table with non unique key DATA: itab TYPE STANDARD TABLE OF ty_tab WITH NON-UNIQUE KEY item, wtab TYPE ty_tab. * Entering records to each field wtab-item = 'Rice'. wtab-quantity = 2. wtab-price = 80. * Now one single row has been fulfilled with data * Next appending one single row data into the table APPEND wtab TO itab. wtab-item = 'Suger'. wtab-quantity = 1. wtab-price = 90. APPEND wtab TO itab. wtab-item = 'Tea'. wtab-quantity = 1. wtab-price = 100.

Rohini kumar

sap abap consultant

APPEND wtab TO itab. wtab-item = 'Rice'. wtab-quantity = 3. wtab-price = 150. APPEND wtab TO itab. wtab-item = 'Horlicks'. wtab-quantity = 1. wtab-price = 200. APPEND wtab TO itab. wtab-item = 'Suger'. wtab-quantity = 2. wtab-price = 70. APPEND wtab TO itab. WRITE:

/3 'Item', 17 'Quantity', 32 'Price'. WRITE / '=========================================='. SKIP. " Skipping one single line LOOP AT itab INTO wtab. WRITE:

/3 wtab-item, 12 wtab-quantity, 25 wtab-price.

ENDLOOP. SKIP. WRITE '=========================================='.

Sorted Internal Table Sorted table is another kind of index table which has unique / non unique key. It also can be accessed via index or key. For sorted table the key must be specified. The declaration is as follows: DATA: it_mat TYPE SORTED TABLE OF ty_mat WITH UNIQUE KEY matnr, it_mat TYPE SORTED TABLE OF ty_mat WITH NON-UNIQUE KEY matnr.

Rohini kumar

sap abap consultant

Unique key means the MATNR (material no) will must be unique. If same material number is inserted then a run time error will happen. However we can declare the sorted table with non unique key also. In this case same material number can be entered but it will be sorted after entering the number. Here the sorted table behaves similar to sorted standard table. We use INSERT statement to enter any records to the sorted table. INSERT wa_mat INTO TABLE it_mat.

Here is an example of sorted table by using unique key concept. REPORT

zabap_gui.

* Declaring the local structure of internal table TYPES: BEGIN OF ty_tab, item TYPE char10, quantity TYPE i, price TYPE i, END OF ty_tab. * Declaring the Sorted internal table with non unique key DATA: itab TYPE SORTED TABLE OF ty_tab WITH UNIQUE KEY item, wtab TYPE ty_tab. * Entering records to each field wtab-item = 'Rice'. wtab-quantity = 2. wtab-price = 80. * Now one single row has been fulfilled with data * Next inserting one single row data into the table INSERT wtab INTO TABLE itab. wtab-item = 'Suger'. wtab-quantity = 1. wtab-price = 90. INSERT wtab INTO TABLE itab. wtab-item = 'Tea'. wtab-quantity = 1. wtab-price = 100. INSERT wtab INTO TABLE itab. wtab-item = 'Rice'. wtab-quantity = 3. wtab-price = 150. INSERT wtab INTO TABLE itab. wtab-item = 'Horlicks'. wtab-quantity = 1. wtab-price = 200. INSERT wtab INTO TABLE itab. wtab-item = 'Suger'. wtab-quantity = 2. wtab-price = 70. INSERT wtab INTO TABLE itab. WRITE:

/3 'Item', 13 'Quantity (KG)', 28 'Price (Rs)'. WRITE / '=========================================='. SKIP. " Skipping one single line LOOP AT itab INTO wtab. WRITE: ENDLOOP.

/3 wtab-item, 12 wtab-quantity, 25 wtab-price.

Rohini kumar

sap abap consultant

SKIP. WRITE '=========================================='.

Since the key is unique the similar entries are ignored by the system. Here is an example of sorted table by using non unique key concept. REPORT

zabap_gui.

* Declaring the local structure of internal table TYPES: BEGIN OF ty_tab, item TYPE char10, quantity TYPE i, price TYPE i, END OF ty_tab. * Declaring the Standard internal table with non unique key

Rohini kumar

sap abap consultant

DATA: itab TYPE SORTED TABLE OF ty_tab WITH NON-UNIQUE KEY item, wtab TYPE ty_tab. * Entering records to each field wtab-item = 'Rice'. wtab-quantity = 2. wtab-price = 80. * Now one single row has been fulfilled with data * Next inserting one single row data into the table INSERT wtab INTO TABLE itab. wtab-item = 'Suger'. wtab-quantity = 1. wtab-price = 90. INSERT wtab INTO TABLE itab. wtab-item = 'Tea'. wtab-quantity = 1. wtab-price = 100. INSERT wtab INTO TABLE itab. wtab-item = 'Rice'. wtab-quantity = 3. wtab-price = 150. INSERT wtab INTO TABLE itab. wtab-item = 'Horlicks'. wtab-quantity = 1. wtab-price = 200. INSERT wtab INTO TABLE itab. wtab-item = 'Suger'. wtab-quantity = 2. wtab-price = 70. INSERT wtab INTO TABLE itab. WRITE:

/3 'Item', 13 'Quantity(KG)', 28 'Price(Rs)'. WRITE / '=========================================='. SKIP. " Skipping one single line LOOP AT itab INTO wtab. WRITE:

/3 wtab-item, 12 wtab-quantity, 25 wtab-price.

ENDLOOP. SKIP. WRITE '=========================================='.

Rohini kumar

sap abap consultant

Hashed Internal Table Hashed table is not an index table. It follows the hash algorithm. Here the declaration of key is must and also the key must be unique. Hence no duplicate entry will be in the hashed table. We can access records only by the key. Similar to sorted tables data can be inserted here by INSERT statement. Hashed tables are used when the internal table contains huge volume of data. REPORT

zabap_gui.

* Declaring the local structure of internal table TYPES: BEGIN OF ty_tab, item TYPE char10, quantity TYPE i, price TYPE i, END OF ty_tab. * Declaring the Hashed internal table with non unique key DATA: itab TYPE HASHED TABLE OF ty_tab WITH UNIQUE KEY item, wtab TYPE ty_tab. * Entering records to each field wtab-item = 'Rice'. wtab-quantity = 2. wtab-price = 80. * Now one single row has been fulfilled with data * Next inserting one single row data into the table INSERT wtab INTO TABLE itab. wtab-item = 'Suger'. wtab-quantity = 1. wtab-price = 90. INSERT wtab INTO TABLE itab. wtab-item = 'Tea'. wtab-quantity = 1. wtab-price = 100. INSERT wtab INTO TABLE itab.

Rohini kumar

sap abap consultant

wtab-item = 'Rice'. wtab-quantity = 3. wtab-price = 150. INSERT wtab INTO TABLE itab. wtab-item = 'Horlicks'. wtab-quantity = 1. wtab-price = 200. INSERT wtab INTO TABLE itab. wtab-item = 'Suger'. wtab-quantity = 2. wtab-price = 70. INSERT wtab INTO TABLE itab. WRITE:

/3 'Item', 13 'Quantity(KG)', 28 'Price(Rs)'. WRITE / '=========================================='. SKIP. " Skipping one single line LOOP AT itab INTO wtab. WRITE:

/3 wtab-item, 12 wtab-quantity, 25 wtab-price.

ENDLOOP. SKIP. WRITE '=========================================='.

Due to uniqueness of the key similar entries are ignored.

Rohini kumar

sap abap consultant

Hashed table can be sorted. If we sort this table it will be as follows.

Continue Statement The CONTINUE statement is used inside a loop only. When the program controller finds the CONTINUE statement it quits the current loop pass. Hence all other statements after the CONTINUE will not be executed inside that loop. The program controller then goes to the next loop iteration. REPORT

zabap_gui.

* Declaring the local structure of internal table TYPES: BEGIN OF ty_tab, item TYPE char10, quantity TYPE i, price TYPE i, END OF ty_tab. * Declaring the Standard internal table with non unique key DATA: itab TYPE STANDARD TABLE OF ty_tab, wtab TYPE ty_tab. * Entering records to each field wtab-item = 'Rice'. wtab-quantity = 2. wtab-price = 80. * Now one single row has been fulfilled with data * Next appending one single row data into the table APPEND wtab TO itab. wtab-item = 'Sugar'. wtab-quantity = 1. wtab-price = 90.

Rohini kumar

sap abap consultant

APPEND wtab TO itab. wtab-item = 'Tea'. wtab-quantity = 1. wtab-price = 100. APPEND wtab TO itab. wtab-item = 'Rice'. wtab-quantity = 3. wtab-price = 150. APPEND wtab TO itab. wtab-item = 'Horlicks'. wtab-quantity = 1. wtab-price = 200. APPEND wtab TO itab. wtab-item = 'Sugar'. wtab-quantity = 2. wtab-price = 70. APPEND wtab TO itab. WRITE:

/3 'Item', 13 'Quantity(KG)', 28 'Price(Rs)'. WRITE / '=========================================='. SKIP. " Skipping one single line LOOP AT itab INTO wtab. IF wtab-item = 'Rice'. * Continue always moves to the next iteration of the loop CONTINUE. ENDIF. WRITE:

/3 wtab-item, 12 wtab-quantity, 25 wtab-price. CLEAR wtab. ENDLOOP. SKIP. WRITE '=========================================='.

Rohini kumar

sap abap consultant

Whenever the program controller finds Rice item it just ignores it and goes to next loop iteration. That is why Rice item is not coming to the output.

Check Statement The CHECK statement checks a condition first inside a loop. If the condition is true then rest of the statements inside that loop will be executed. If the condition is false then the program controller terminates the current loop pass and it will go to the next loop iteration. Hence the condition is the prerequisite of CHECK statement. No condition will lead the syntax error of the program. Outside of the loop CHECK will leave the current processing block of the program. REPORT

zabap_gui.

* Declaring the local structure of internal table TYPES: BEGIN OF ty_tab, item TYPE char10, quantity TYPE i, price TYPE i, END OF ty_tab. * Declaring the Standard internal table with non unique key DATA: itab TYPE STANDARD TABLE OF ty_tab, wtab TYPE ty_tab. * Entering records to each field wtab-item = 'Rice'. wtab-quantity = 2. wtab-price = 80. * Now one single row has been fulfilled with data * Next appending one single row data into the table APPEND wtab TO itab. wtab-item = 'Sugar'. wtab-quantity = 1. wtab-price = 90. APPEND wtab TO itab. wtab-item = 'Tea'. wtab-quantity = 1. wtab-price = 100. APPEND wtab TO itab. wtab-item = 'Rice'. wtab-quantity = 3. wtab-price = 150. APPEND wtab TO itab. wtab-item = 'Horlicks'. wtab-quantity = 1. wtab-price = 200. APPEND wtab TO itab. wtab-item = 'Sugar'. wtab-quantity = 2. wtab-price = 70. APPEND wtab TO itab. * Sorting the itab by item. SORT itab BY item. WRITE:

/3 'Item', 13 'Quantity(KG)', 28 'Price(Rs)'. WRITE / '=========================================='.

Rohini kumar

sap abap consultant

SKIP. " Skipping one single line LOOP AT itab INTO wtab. * If the condition is true then it will execute further statements * If CHECK condition is false then it will move to the next iteration of loop CHECK wtab-item = 'Rice'. WRITE:

/3 wtab-item, 12 wtab-quantity, 25 wtab-price. CLEAR wtab. ENDLOOP. SKIP. WRITE '=========================================='.

Whenever the check condition finds Rice item it goes to the rest of the statements of current loop. If the condition finds false (other than Rice) it leaves the current loop iteration and goes to the next loop pass.

Exit Statement The EXIT statement quits the processing of the current loop. If we use EXIT outside of the loop then it leaves the current processing block. Hence no other statement of that processing block will be executed. It is recommended that EXIT needs to be used inside a loop. We can use it outside of loop as per requirement as well. If there are nested loops and we use EXIT statement in the inner loop then the system will leave the inner loop after executing the EXIT statement. The program control will continue with the outer loop execution. REPORT

zabap_gui.

Rohini kumar

sap abap consultant

* Declaring the local structure of internal table TYPES: BEGIN OF ty_tab, item TYPE char10, quantity TYPE i, price TYPE i, END OF ty_tab. * Declaring the Standard internal table with non unique key DATA: itab TYPE STANDARD TABLE OF ty_tab, wtab TYPE ty_tab. * Entering records to each field wtab-item = 'Rice'. wtab-quantity = 2. wtab-price = 80. * Now one single row has been fulfilled with data * Next appending one single row data into the table APPEND wtab TO itab. wtab-item = 'Sugar'. wtab-quantity = 1. wtab-price = 90. APPEND wtab TO itab. wtab-item = 'Tea'. wtab-quantity = 1. wtab-price = 100. APPEND wtab TO itab. wtab-item = 'Rice'. wtab-quantity = 3. wtab-price = 150. APPEND wtab TO itab. wtab-item = 'Horlicks'. wtab-quantity = 1. wtab-price = 200. APPEND wtab TO itab. wtab-item = 'Sugar'. wtab-quantity = 2. wtab-price = 70. APPEND wtab TO itab. * Sorting the itab by item. SORT itab BY item. WRITE:

/3 'Item', 13 'Quantity(KG)', 28 'Price(Rs)'. WRITE / '=========================================='. SKIP. " Skipping one single line LOOP AT itab INTO wtab. WRITE: /3 wtab-item, 12 wtab-quantity, 25 wtab-price. CLEAR wtab. ENDLOOP. WRITE / '=========================================='. SKIP. " Skipping one single line LOOP AT itab INTO wtab. IF wtab-item = 'Rice'. * Exit always quits the control from the loop iteration * After exit the loop execution will be finished EXIT.

Rohini kumar

sap abap consultant

ENDIF. WRITE:

/3 wtab-item, 12 wtab-quantity, 25 wtab-price. CLEAR wtab. ENDLOOP. SKIP. WRITE '=========================================='.

Here the first set of output has come without the exit statement. Second set has come by exit statement. Whenever the program controller finds the item Rice then it executes exit. The system leaves the loop totally. That is why only one output has come yet.

Read Table and Describe Table There are different kinds of fetching the records of internal table. One of these is READ TABLE statement. By using READ TABLE we can fetch only the first record from the table. Hence it can fetch a single record always. Read table statement can be used to any type of table to fetch the record. Now if the table is sorted then we can use BINARY SEARCH to fetch record. Now fetching records may take time but if we use BINARY SEARCH then the system performance will improve. Obviously if the table is sorted then the fetching operation will be faster. We can describe an internal table as well. If the table stores 300 records then the system will inform us by the system field SY-TFILL. Here note that SY-TFILL will not work alone. We need to describe the table first then we can get data from SY-TFILL field. DESCRIBE TABLE itab. WRITE: / 'Number of table records: ', sy-tfill.

Rohini kumar

sap abap consultant

Here we are describing the internal table itab. After that we are getting records from SYTFILL. REPORT

zabap_gui.

* Declaring the local structure of internal table TYPES: BEGIN OF ty_tab, item TYPE char10, quantity TYPE i, price TYPE i, END OF ty_tab. * Declaring the Standard internal table with non unique key DATA: itab TYPE STANDARD TABLE OF ty_tab, wtab TYPE ty_tab. * Entering records to each field wtab-item = 'Rice'. wtab-quantity = 2. wtab-price = 80. * Now one single row has been fulfilled with data * Next appending one single row data into the table APPEND wtab TO itab. wtab-item = 'Suger'. wtab-quantity = 1. wtab-price = 90. APPEND wtab TO itab. wtab-item = 'Tea'. wtab-quantity = 1. wtab-price = 100. APPEND wtab TO itab. wtab-item = 'Rice'. wtab-quantity = 3. wtab-price = 150. APPEND wtab TO itab. wtab-item = 'Horlicks'. wtab-quantity = 1. wtab-price = 200. APPEND wtab TO itab. wtab-item = 'Suger'. wtab-quantity = 2. wtab-price = 70. APPEND wtab TO itab. SORT itab BY item. WRITE:

/3 'Item', 13 'Quantity(KG)', 28 'Price(Rs)'. WRITE / '=========================================='. SKIP. " Skipping one single line READ TABLE itab INTO wtab WITH KEY item = 'Rice' BINARY SEARCH. IF sy-subrc = 0. WRITE: /3 wtab-item, 12 wtab-quantity, 25 wtab-price. ENDIF. SKIP. WRITE '=========================================='.

Rohini kumar

sap abap consultant

DESCRIBE TABLE itab. WRITE: / 'Number of table records: ', sy-tfill.

Though the output is showing only one record the table still contains six records.

Modify Table Statement Internal table can be edited as per requirement. We can modify one particular record when it is required. Two kinds of statements can be used to modify one single line. MODIFY TABLE internal_tab FROM work_area. MODIFY internal_tab FROM work_area INDEX sy-tabix TRANSPORTING field1 field2.

Note that by these statements one single line can be modified. We can add more fields as per requirement. We can modify any type of internal table by these statements. After modification the old record is lost completely from the table. If we store it to any outer structure before modification then the old record can be reused. REPORT

zabap_gui.

* Declaring the local structure of internal table TYPES: BEGIN OF ty_tab, item TYPE char10, quantity TYPE i, price TYPE i, END OF ty_tab. * Declaring the Standard internal table with non unique key DATA: itab TYPE STANDARD TABLE OF ty_tab, wtab TYPE ty_tab.

Rohini kumar

sap abap consultant

* Entering records to each field wtab-item = 'Rice'. wtab-quantity = 2. wtab-price = 80. * Now one single row has been fulfilled with data * Next appending one single row data into the table APPEND wtab TO itab. wtab-item = 'Suger'. wtab-quantity = 1. wtab-price = 90. APPEND wtab TO itab. wtab-item = 'Tea'. wtab-quantity = 1. wtab-price = 100. APPEND wtab TO itab. wtab-item = 'Rice'. wtab-quantity = 3. wtab-price = 150. APPEND wtab TO itab. wtab-item = 'Horlicks'. wtab-quantity = 1. wtab-price = 200. APPEND wtab TO itab. wtab-item = 'Suger'. wtab-quantity = 2. wtab-price = 70. APPEND wtab TO itab. WRITE:

/3 'Item', 13 'Quantity(KG)', 28 'Price(Rs)'. WRITE / '=========================================='. SKIP. " Skipping one single line LOOP AT itab WRITE: /3 12 25 ENDLOOP.

INTO wtab. wtab-item, wtab-quantity, wtab-price.

SKIP. WRITE '=========================================='. SKIP. LOOP AT itab INTO wtab. IF wtab-item = 'Horlicks'. wtab-item = 'Complan'. wtab-price = 220. MODIFY TABLE itab FROM wtab. ENDIF. WRITE: ENDLOOP.

/3 wtab-item, 12 wtab-quantity, 25 wtab-price.

Rohini kumar

sap abap consultant

Here the item Horlicks has been modified to Complan.

Delete Table Statement Internal table can be edited as per requirement. We can delete a particular line of it based on the condition. The statement is “DELETE TABLE itab FROM wtab.” Here itab is internal table and wtab is its work area. This statement can be used in any type of internal table whenever there is a requirement. REPORT

zabap_gui.

* Declaring the local structure of internal table TYPES: BEGIN OF ty_tab, item TYPE char10, quantity TYPE i, price TYPE i, END OF ty_tab. * Declaring the Standard internal table with non unique key DATA: itab TYPE STANDARD TABLE OF ty_tab, wtab TYPE ty_tab. * Entering records to each field wtab-item = 'Rice'. wtab-quantity = 2. wtab-price = 80. * Now one single row has been fulfilled with data

Rohini kumar

sap abap consultant

* Next appending one single row data into the table APPEND wtab TO itab. wtab-item = 'Suger'. wtab-quantity = 1. wtab-price = 90. APPEND wtab TO itab. wtab-item = 'Tea'. wtab-quantity = 1. wtab-price = 100. APPEND wtab TO itab. wtab-item = 'Rice'. wtab-quantity = 3. wtab-price = 150. APPEND wtab TO itab. wtab-item = 'Horlicks'. wtab-quantity = 1. wtab-price = 200. APPEND wtab TO itab. wtab-item = 'Suger'. wtab-quantity = 2. wtab-price = 70. APPEND wtab TO itab. WRITE:

/3 'Item', 13 'Quantity(KG)', 28 'Price(Rs)'. WRITE / '=========================================='. SKIP. " Skipping one single line LOOP AT itab INTO wtab. IF wtab-item = 'Rice'. DELETE TABLE itab FROM wtab. ELSEIF wtab-item = 'Suger'. DELETE TABLE itab FROM wtab. ENDIF. ENDLOOP. LOOP AT itab WRITE: /3 12 25 ENDLOOP.

INTO wtab. wtab-item, wtab-quantity, wtab-price.

SKIP. WRITE '=========================================='. SKIP.

Rohini kumar

sap abap consultant

Whenever the program controller finds Rice & Suger it deletes from the internal table.

Delete Adjacent Duplicate DELETE ADJACENT DUPLICATE statement works logically on sorted standard table and sorted table with non-unique key. It compares the adjacent rows. If similar records are found based on the comparing fields then it deletes from the second records onwards. The system keeps only the first record. Let us suppose we are having the following table with records. Item Rice Sugar Rice Tea Sugar

Quantity (KG) 2 1 3 1 2

Price (Rs) 100 50 150 100 120

We know that in standard table APPEND statement is used to enter data records. Now APPEND appends data to the last position of the standard table. It is not possible to enter in any other position. Now if we run the Delete adjacent duplicate command on the previous table then the data records will remain same. Nothing will be changed. Because the command will check every single adjacent line and found nothing to similar. Hence in this case we need to sort the table (standard table). Item Rice Rice Sugar Sugar Tea

Quantity (KG) 2 3 1 2 1

Price (Rs) 100 150 50 120 100

Now we have found the sorted structure of the data records. In this position if the command runs then it will find two similar records based on the comparison of the item field. After evaluating this command we shall have the following. Item Rice Sugar Tea

Quantity (KG) 2 1 1

Price (Rs) 100 50 100

The command will remove always the second record onwards. It will keep only the first record. Here if we use Sorted table with non-unique key then after inserting the records we can fins the sorted structure of the data records. That is why DELETE ADJACENT DUPLICATE command always run sorted standard table or sorted table with non-unique key properly.

Rohini kumar

REPORT

sap abap consultant

zabap_gui.

* Declaring the local structure of internal table TYPES: BEGIN OF ty_tab, item TYPE char10, quantity TYPE i, price TYPE i, END OF ty_tab. * Declaring the Sorted internal table with non unique key DATA: itab TYPE SORTED TABLE OF ty_tab WITH NON-UNIQUE KEY item, wtab TYPE ty_tab. * Entering records to each field wtab-item = 'Rice'. wtab-quantity = 2. wtab-price = 80. * Now one single row has been fulfilled with data * Next inserting one single row data into the table INSERT wtab INTO TABLE itab. wtab-item = 'Suger'. wtab-quantity = 2. wtab-price = 70. INSERT wtab INTO TABLE itab. wtab-item = 'Tea'. wtab-quantity = 1. wtab-price = 100. INSERT wtab INTO TABLE itab. wtab-item = 'Rice'. wtab-quantity = 2. wtab-price = 80. INSERT wtab INTO TABLE itab. wtab-item = 'Horlicks'. wtab-quantity = 1. wtab-price = 200. INSERT wtab INTO TABLE itab. wtab-item = 'Suger'. wtab-quantity = 2. wtab-price = 70. INSERT wtab INTO TABLE itab. WRITE:

/3 'Item', 13 'Quantity(KG)', 28 'Price(Rs)'. WRITE / '=========================================='. SKIP. " Skipping one single line DELETE ADJACENT DUPLICATES FROM itab. LOOP AT itab WRITE: /3 12 25 ENDLOOP.

INTO wtab. wtab-item, wtab-quantity, wtab-price.

SKIP. WRITE '=========================================='.

Rohini kumar

sap abap consultant

Collect Statement COLLECT statement can be used in the internal table whose non key fields are all numeric. It means the statement finds the key (non numeric fields) and if the records are same then the statement will add all other numeric field values. The prerequisite of this statement is that the internal table must have numeric data type fields which are non key fields. COLLECT statement can be applied on any type of internal tables. Item Rice Wheat Rice Tea Sugar Wheat

Quantity (Kg) 2 1 1 1 1 3

Price (Rs) 100 40 60 120 90 90

If we run the collect statement upon this table then the output will be as following Item Rice Wheat Tea Sugar REPORT

Quantity (Kg) 3 4 1 1

Price (Rs) 160 130 120 90

zabap_gui.

* Declaring the local structure of internal table TYPES: BEGIN OF ty_tab, item TYPE char10, quantity TYPE i, price TYPE i,

Rohini kumar

sap abap consultant

END OF ty_tab. * Declaring the Standard internal table with non unique key DATA: itab TYPE STANDARD TABLE OF ty_tab, wtab TYPE ty_tab. * Entering records to each field wtab-item = 'Rice'. wtab-quantity = 2. wtab-price = 80. * Now one single row has been fulfilled with data * Next collecting one single row data into the table COLLECT wtab INTO itab. wtab-item = 'Suger'. wtab-quantity = 1. wtab-price = 90. COLLECT wtab INTO itab. wtab-item = 'Tea'. wtab-quantity = 1. wtab-price = 100. COLLECT wtab INTO itab. wtab-item = 'Rice'. wtab-quantity = 3. wtab-price = 150. COLLECT wtab INTO itab. wtab-item = 'Horlicks'. wtab-quantity = 1. wtab-price = 200. COLLECT wtab INTO itab. wtab-item = 'Suger'. wtab-quantity = 2. wtab-price = 70. COLLECT wtab INTO itab. WRITE:

/3 'Item', 13 'Quantity(KG)', 28 'Price(Rs)'. WRITE / '=========================================='. SKIP. " Skipping one single line LOOP AT itab INTO wtab. WRITE:

/3 wtab-item, 12 wtab-quantity, 25 wtab-price.

ENDLOOP. SKIP. WRITE '=========================================='.

Rohini kumar

sap abap consultant

Control Break - AT NEW statement AT NEW is one of a control break statements. It works inside the LOOP – ENDLOOP. Let’s take an example. We have prepared an internal table where we are storing 50 rows of material, plant & storage location data. Now we run the operation of AT NEW for plant record. Whenever the system finds a new plant with respect to previous then it will trigger the AT NEW statement. Here the internal table must be sorted. Otherwise similar plant may come afterwards and system will trigger AT NEW wrongly.

Rohini kumar

sap abap consultant

The program is as follows. TYPES: BEGIN OF ty_tab, werks TYPE mard-werks, matnr TYPE mard-matnr, lgort TYPE mard-lgort, END OF ty_tab. DATA: wtab TYPE ty_tab, itab TYPE TABLE OF ty_tab. START-OF-SELECTION. SELECT matnr werks lgort UP TO 30 ROWS FROM mard INTO CORRESPONDING FIELDS OF TABLE itab. IF sy-subrc = 0. SORT itab. WRITE: / 'Material', 20 'Plant', 27 'Storage Location'. ULINE. LOOP AT itab INTO wtab.

Rohini kumar

sap abap consultant

WRITE: / wtab-matnr, 20 wtab-werks, 27 wtab-lgort. AT NEW werks. WRITE: '***** AT NEW plant triggers at ', sy-tabix. ENDAT. ENDLOOP. ENDIF.

Now we shall see at debugging mode how AT NEW works. At the first loop iteration AT NEW will definitely trigger because the plant is read as new. Here SY-TABIX = 1.

After entering into AT NEW, we can see the other fields which are right side of the mentioned field contain ***. Only the mentioned field contains the proper data.

After that whenever system finds a new plant data it will trigger AT NEW similarly. Here at SY-TABIX = 3.

Rohini kumar

sap abap consultant

Similarly at SY-TABIX = 16 it will be triggered.

In this way AT NEW is triggered inside the loop. The output is as follows.

Rohini kumar

sap abap consultant

Control Break - AT END OF Statement

AT END OF statement always triggers when there is any change of fields’ data. This statement triggers at the last occurrence of that value. Let’s take an example. We have a table MARD which contains material, plant & storage location. Now we want to trigger AT END OF at the last occurrence of plant data as follows.

Rohini kumar

sap abap consultant

As we can see that the statement triggers at the last occurrence of plant data. REPORT

zctrlbrk_at_end_of NO STANDARD PAGE HEADING.

TYPES: BEGIN OF ty_tab, werks TYPE mard-werks, matnr TYPE mard-matnr, lgort TYPE mard-lgort, END OF ty_tab. DATA: wtab TYPE ty_tab, itab TYPE TABLE OF ty_tab. START-OF-SELECTION. SELECT matnr werks lgort UP TO 30 ROWS FROM mard INTO CORRESPONDING FIELDS OF TABLE itab. SORT itab BY werks. WRITE: / 'Material Number', 20 'Plant', 27 'Storage Location'.

Rohini kumar

sap abap consultant

ULINE. LOOP AT itab INTO wtab. WRITE: / wtab-matnr, 20 wtab-werks, 27 wtab-lgort. AT END OF werks. WRITE: '=== At End Of plant - triggers at line ', sy-tabix. ENDAT. ENDLOOP.

Here we have shifted the plant (WERKS) at field 1 in structure level. Otherwise system will consider material & plant both as a key. Because the AT END OF will trigger if there is any change from left most field to the particular field. Now we want to see at the debugging level of AT END OF statement. On second line it has triggered because that is the last occurrence of plant 1200. SY-TABIX = 2. For the rest of the rows system will skip the AT END OF statement.

Similarly it has triggered at last occurrence of plant 3000. SY-TABIX = 15.

There is only one entry for plant 7500. So it triggers at the next loop iteration. SY-TABIX = 16.

Rohini kumar

sap abap consultant

At the last of the table record AT END OF triggers. Here the plant occurrence & table data both are of last entry.

The output is as follows:

Rohini kumar

sap abap consultant

Control Break - AT FIRST and AT LAST

AT FIRST triggers at the first loop iteration whereas AT LAST triggers at the last loop iteration. We can’t mention any particular field here as we can do in AT NEW or AT END OF. The main purpose of this control break is to write some header or footer information. We can write some header info by using AT FIRST statement and similarly some footer info by using AT LAST statement. REPORT

zctrlbrk_at_first_last NO STANDARD PAGE HEADING.

TYPES: BEGIN OF ty_tab,

Rohini kumar

werks matnr lgort END OF

sap abap consultant

TYPE mard-werks, TYPE mard-matnr, TYPE mard-lgort, ty_tab.

DATA: wtab TYPE ty_tab, itab TYPE TABLE OF ty_tab. START-OF-SELECTION. SELECT matnr werks lgort UP TO 25 ROWS FROM mard INTO CORRESPONDING FIELDS OF TABLE itab. IF sy-subrc = 0. SORT itab BY werks. WRITE: / 'Material', 20 'Plant', 27 'Storage Location'. ULINE. LOOP AT itab INTO wtab. WRITE: / wtab-matnr, 20 wtab-werks, 27 wtab-lgort. AT FIRST. WRITE: '===AT FIRST will trigger here'. ENDAT. AT LAST. WRITE: '===AT LAST will trigger here'. ENDAT. ENDLOOP. ENDIF.

Now at the debugger level we see the following results. AT FIRST triggers at 1st loop iteration when SY-TABIX = 1. Here all fields of work area are ***.

After that the system triggers AT LAST statement at the last loop iteration when SY-TABIX = 25. Previously the values of work area are also ***.

Rohini kumar

The output is as follows.

Control Break - ON CHANGE OF

sap abap consultant

Rohini kumar

sap abap consultant

ON CHANGE OF triggers when there is any change of the first occurrence of mentioned fields’ value. It means it actually works like AT NEW statement. If there is any change of the value of the mentioned field then ON CHANGE OF will trigger. At the time of triggering all of other fields contain their respective data. Hence it doesn’t go for *** value at the time of triggering. ON CHANGE OF can be used outside the loop also. REPORT

zctrlbrk_on_change NO STANDARD PAGE HEADING.

TYPES: BEGIN OF ty_tab, werks TYPE mard-werks, matnr TYPE mard-matnr, lgort TYPE mard-lgort, END OF ty_tab. DATA: wtab TYPE ty_tab, itab TYPE TABLE OF ty_tab. START-OF-SELECTION. SELECT matnr werks lgort UP TO 25 ROWS FROM mard INTO CORRESPONDING FIELDS OF TABLE itab. IF sy-subrc = 0. SORT itab BY werks. WRITE: / 'Material', 20 'Plant', 27 'Storage Location'. ULINE. LOOP AT itab INTO wtab. WRITE: / wtab-matnr, 20 wtab-werks, 27 wtab-lgort. ON CHANGE OF wtab-werks. WRITE: '=== On Change Of triggers at plant - ', wtab-werks. ENDON. ENDLOOP. ENDIF.

Now at debugging level we can see as follows. At first loop iteration system will definitely triggers the ON CHANGE OF statement because the plant data is new. All other fields contain their respective data at the time of triggering.

Rohini kumar

sap abap consultant

Similarly when there is any change of plant then system triggers it again. In this case it is at SY-TABIX = 3.

In this way whenever there is any change of data then it works. The final output is as follows.

Rohini kumar

sap abap consultant

Posted by SANDIP ROY at 11/26/2015 05:23:00 PM Email ThisBlogThis!Share to TwitterShare to FacebookShare to Pinterest

Control Break with SUM statement

SUM statement can only be used in LOOP – ENDLOOP statement. It is considered in AT – END AT control break structure. Inside the control break SUM calculates the sum of all the fields which are like I or P or F type. SUM holds the summation at it is reflected to the respective work areas. This summation must be the total of current control break. SUM doesn’t work when the row type of the internal table contains table type components. Here we have prepared an example in which purchase order item table EKPO has been selected. Now we use SUM statement which calculates the summation of quantity & net price inside the AT END OF control statement. Hence at the last occurrence of PO the system calculates the total quantity & price for different item level. REPORT

z_sum NO STANDARD PAGE HEADING.

*-------Declaring tables for select option-----------------------------*

Rohini kumar

sap abap consultant

TABLES: ekpo. *------Declaring local structure for work area & table-----------------* TYPES: BEGIN OF ty_ekpo, ebeln TYPE ekpo-ebeln, ebelp TYPE ekpo-ebelp, menge TYPE ekpo-menge, meins TYPE ekpo-meins, netpr TYPE ekpo-netpr, END OF ty_ekpo. *-----Declaration of work area & internal table------------------------* DATA: wa_ekpo TYPE ty_ekpo, it_ekpo TYPE TABLE OF ty_ekpo, v_flag TYPE c. *-----Event Initialization---------------------------------------------* INITIALIZATION. SELECT-OPTIONS: s_ebeln FOR ekpo-ebeln. *-----Event Start of Selection-----------------------------------------* START-OF-SELECTION. SELECT ebeln ebelp menge meins netpr FROM ekpo INTO TABLE it_ekpo WHERE ebeln IN s_ebeln. IF sy-subrc = 0. "Table needs to be sorted. "Otherwise AT NEW & AT END OF will operate data wrongly SORT it_ekpo. LOOP AT it_ekpo INTO wa_ekpo. "Triggers at the first loop iteration only AT FIRST. WRITE: 'Purchase Order' COLOR 3, 20 'Item' COLOR 3, 35 'Quantity' COLOR 3, 45 'Unit' COLOR 3, 54 'Net Price' COLOR 3. ULINE. ENDAT. "Triggers when new PO will come into the loop AT NEW ebeln. v_flag = 'X'. ENDAT. IF v_flag = 'X'. WRITE: / wa_ekpo-ebeln, 20 wa_ekpo-ebelp, 27 wa_ekpo-menge, 45 wa_ekpo-meins, 50 wa_ekpo-netpr. ELSE. WRITE: /20 wa_ekpo-ebelp, 27 wa_ekpo-menge, 45 wa_ekpo-meins, 50 wa_ekpo-netpr.

Rohini kumar

sap abap consultant

ENDIF. "Triggers at the last occurrence of PO in the loop AT END OF ebeln. WRITE: /27 '=================', 50 '=============='. SUM. "SUM adds & holds all the I/P/F data "Here it holds for this control range WRITE: / 'Sub Total: ' COLOR 5, 27 wa_ekpo-menge, 50 wa_ekpo-netpr. SKIP. ENDAT. "Triggers at the last loop iteration only AT LAST. ULINE. SUM. "SUM adds & holds all the I/P/F data "Here it holds the total of loop range WRITE: / 'Quantity & Net Price' COLOR 4, / 'Grand Total: ' COLOR 4, 27 wa_ekpo-menge, 50 wa_ekpo-netpr. ENDAT. CLEAR: wa_ekpo, v_flag. ENDLOOP. ENDIF.

Now at the debugging level we see the behavior of SUM statement. At SY-TABIX = 4 one PO holds its last occurrence and the system calculates the total quantity & price by using the SUM statement. Here the total will be considered for this particular control only.

We can use SUM in AT LAST control break as well. In this case it will calculate the all total of quantity & price. Since AT LAST triggers at the last loop iteration, the SUM considers the total quantity & price of all different POs.

Rohini kumar

sap abap consultant

Below is the output.

Select Single and Select up to Select single statement only selects the first record of any series of records from a database table. That means this statement can read a single record from a database table. It keeps the data into a work area or header line. This work area is generally a line type of internal table.

Rohini kumar

sap abap consultant

In below example we are fetching records of PO no, item no, material, plant & storage location from table EKPO where the PO no is 3000000232. The database contains only 3 items for this PO.

REPORT

zabap_gui.

TABLES: ekpo.

Rohini kumar

sap abap consultant

* Creating a custom structure of Item Table TYPES: BEGIN OF ty_ekpo, ebeln TYPE ekpo-ebeln, ebelp TYPE ekpo-ebelp, matnr TYPE ekpo-matnr, werks TYPE ekpo-werks, lgort TYPE ekpo-lgort, END OF ty_ekpo. * Creating a line type of predefined structure DATA: wa_ekpo TYPE ty_ekpo. * Select single will fetch only the First record * from the PO Item table SELECT SINGLE ebeln ebelp matnr werks lgort FROM ekpo INTO wa_ekpo WHERE ebeln = '3000000232'. WRITE:/ 15 28 48 55

'PO No.', 'Item No', 'Material', 'Plant', 'Storage'.

15 28 48 55

wa_ekpo-ebeln, wa_ekpo-ebelp, wa_ekpo-matnr, wa_ekpo-werks, wa_ekpo-lgort.

ULINE. SKIP. WRITE:/

Output of this:

The database contains 3 records here. But select single statement fetches only single record from database. Here the first record is always fetched by this statement.

Rohini kumar

sap abap consultant

Select up to statement is used to mention the rows need to be selected from the database table. If the database contains that number of rows then it will display accordingly. Here we are declaring an internal table to store the multiple row records and print the output as well. REPORT

zabap_gui.

TABLES: ekpo. * Creating a custom structure of Item Table TYPES: BEGIN OF ty_ekpo, ebeln TYPE ekpo-ebeln, ebelp TYPE ekpo-ebelp, matnr TYPE ekpo-matnr, werks TYPE ekpo-werks, lgort TYPE ekpo-lgort, END OF ty_ekpo. * Creating a line type of predefined structure DATA: wa_ekpo TYPE ty_ekpo, it_ekpo TYPE STANDARD TABLE OF ty_ekpo. * Select up to n rows fetches n rows * from the PO Item table SELECT ebeln ebelp matnr werks lgort UP TO 5 ROWS FROM ekpo INTO TABLE it_ekpo WHERE ebeln = '3000000232'. WRITE:/ 15 28 48 55

'PO No.', 'Item No', 'Material', 'Plant', 'Storage'.

ULINE. SKIP. LOOP AT it_ekpo INTO wa_ekpo. WRITE:/ wa_ekpo-ebeln, 15 wa_ekpo-ebelp, 28 wa_ekpo-matnr, 48 wa_ekpo-werks, 55 wa_ekpo-lgort. ENDLOOP.

Output of this:

Rohini kumar

sap abap consultant

Since there are only three records in database, the system shows only 3 records in spite of being mentioned “UP TO 5 ROWS”.

Select Distinct Select distinct only selects the unique entries of the fields in the select statement. It will not allow any duplicate entry into the internal table. In the below example we are having a selection screen where we are defining a selection range of PO number by select option. At first we are fetching the records with normal select statement and we find six records from the database. REPORT

zabap_gui.

TABLES: ekpo. * Creating a custom structure of Item Table TYPES: BEGIN OF ty_ekpo, ebeln TYPE ekpo-ebeln, ebelp TYPE ekpo-ebelp, matnr TYPE ekpo-matnr, werks TYPE ekpo-werks, lgort TYPE ekpo-lgort, END OF ty_ekpo. * Creating a line type of predefined structure DATA: wa_ekpo TYPE ty_ekpo, it_ekpo TYPE STANDARD TABLE OF ty_ekpo. SELECT-OPTIONS: s_ebeln FOR ekpo-ebeln. SELECT ebeln ebelp matnr werks lgort FROM ekpo INTO TABLE it_ekpo WHERE ebeln IN s_ebeln. WRITE:/

'PO No.', 15 'Item No', 28 'Material', 48 'Plant',

Rohini kumar

55 'Storage'. ULINE. SKIP. LOOP AT it_ekpo INTO wa_ekpo. WRITE:/ wa_ekpo-ebeln, 15 wa_ekpo-ebelp, 28 wa_ekpo-matnr, 48 wa_ekpo-werks, 55 wa_ekpo-lgort. ENDLOOP.

Selection Range with select option:

The output is:

sap abap consultant

Rohini kumar

sap abap consultant

Now with the similar selection range we use select distinct statement and we are getting only three records. This is because we have selected only the PO number in select statement with distinct clause. Now distinct will not allow any duplicate entry of PO number. REPORT

zabap_gui.

TABLES: ekpo. * Creating a custom structure of Item Table TYPES: BEGIN OF ty_ekpo, ebeln TYPE ekpo-ebeln, END OF ty_ekpo. * Creating a line type of predefined structure DATA: wa_ekpo TYPE ty_ekpo, it_ekpo TYPE STANDARD TABLE OF ty_ekpo. SELECT-OPTIONS: s_ebeln FOR ekpo-ebeln. SELECT DISTINCT ebeln FROM ekpo INTO TABLE it_ekpo WHERE ebeln IN s_ebeln. WRITE:/ ULINE. SKIP.

'PO No.'.

LOOP AT it_ekpo INTO wa_ekpo. WRITE:/ wa_ekpo-ebeln. ENDLOOP.

Hence the output is as follows.

Rohini kumar

sap abap consultant

Here we know that one PO can have multiple items. Hence in database table EKPO the PO entries are having duplicate entries for different items. But selecting the PO number with distinct clause will fetch only the unique PO number from the database. If we select here the item also with the distinct clause the SAP system will treat both of those fields as unique. In that case the system will recognize PO number and corresponding item number is the unique. In this way if we increase the fields in selection the system will give uniqueness according to the combination of all those selected fields.

Select Total Field Records How to select total records from one or more than one fields from database? We can do that by not using the WHERE condition as simple as that. Below is an example where we have selected PO numbers & items from table EKPO and we haven’t used WHERE condition. The system fetches all records from those two fields and prints the output. To know the total numbers of rows we have used describe table statement so that we can see the total number records. REPORT

zabap_gui.

TABLES: ekpo. * Creating a custom structure of Item Table TYPES: BEGIN OF ty_ekpo, ebeln TYPE ekpo-ebeln, ebelp TYPE ekpo-ebelp, END OF ty_ekpo. * Creating a line type of predefined structure DATA: wa_ekpo TYPE ty_ekpo, it_ekpo TYPE STANDARD TABLE OF ty_ekpo.

Rohini kumar

sap abap consultant

* Not using Where condition will fetch all rows from the fields SELECT ebeln ebelp FROM ekpo INTO TABLE it_ekpo. DESCRIBE TABLE it_ekpo. WRITE:/ 'Total Entries: ', sy-tfill. SKIP. WRITE:/ ULINE.

'PO No.', 15 'Item'.

LOOP AT it_ekpo INTO wa_ekpo. WRITE:/ wa_ekpo-ebeln, 15 wa_ekpo-ebelp. ENDLOOP.

Here is the output.

Select with Appending We can directly append records into an internal table with select statement by using APPENDING clause. Syntax is as follows. SELECT db_field1, db_field2,… FROM db_table APPENDING TABLE internal_table WHERE db_field = condition.

Where is optional. By using APPENDING clause we can re-select and fetch another sort of records into the same internal table. Here the system appends the records to the last position of the internal table. After appending these records when we write the data then it will come one by one (not in the same row). In below example at first we have selected PO number. So the system will append the PO numbers only. After that in the second select the system will select the materials and append those to the last position of the internal table and so on.

Rohini kumar

REPORT

sap abap consultant

zabap_gui.

TABLES: ekpo. * Creating a custom structure of Item Table TYPES: BEGIN OF ty_ekpo, ebeln TYPE ekpo-ebeln, ebelp TYPE ekpo-ebelp, matnr TYPE ekpo-matnr, werks TYPE ekpo-werks, lgort TYPE ekpo-lgort, END OF ty_ekpo. * Creating a line type of predefined structure DATA: wa_ekpo TYPE ty_ekpo, it_ekpo TYPE STANDARD TABLE OF ty_ekpo. REFRESH it_ekpo. SELECT ebeln FROM ekpo APPENDING TABLE it_ekpo WHERE ebeln = '3000000232'. SELECT matnr FROM ekpo APPENDING TABLE it_ekpo WHERE ebeln = '3000000232'. SELECT werks FROM ekpo APPENDING TABLE it_ekpo WHERE ebeln = '3000000232'. SELECT lgort FROM ekpo APPENDING TABLE it_ekpo WHERE ebeln = '3000000232'. LOOP AT it_ekpo INTO wa_ekpo. WRITE:/ wa_ekpo-ebeln, wa_ekpo-matnr, wa_ekpo-werks, wa_ekpo-lgort. ENDLOOP.

The output is like this. One by one record will come for same row information.

Rohini kumar

sap abap consultant

We can use CORRESPONDING FIELDS OF statement also. SELECT db_field1, db_field2,… FROM db_table APPENDING CORRESPONDING FIELDS OF TABLE internal_table WHERE db_field = condition.

In this case the output will come with the corresponding fields. The system will make a default tab for every record on the output screen. But the records will come one by one (different rows) rather the same row. REPORT

zabap_gui.

TABLES: ekpo.

Rohini kumar

sap abap consultant

* Creating a custom structure of Item Table TYPES: BEGIN OF ty_ekpo, ebeln TYPE ekpo-ebeln, ebelp TYPE ekpo-ebelp, matnr TYPE ekpo-matnr, werks TYPE ekpo-werks, lgort TYPE ekpo-lgort, END OF ty_ekpo. * Creating a line type of predefined structure DATA: wa_ekpo TYPE ty_ekpo, it_ekpo TYPE STANDARD TABLE OF ty_ekpo. REFRESH it_ekpo. SELECT ebeln FROM ekpo APPENDING CORRESPONDING FIELDS OF TABLE it_ekpo WHERE ebeln = '3000000232'. SELECT matnr FROM ekpo APPENDING CORRESPONDING FIELDS OF TABLE it_ekpo WHERE ebeln = '3000000232'. SELECT werks FROM ekpo APPENDING CORRESPONDING FIELDS OF TABLE it_ekpo WHERE ebeln = '3000000232'. SELECT lgort FROM ekpo APPENDING CORRESPONDING FIELDS OF TABLE it_ekpo WHERE ebeln = '3000000232'. LOOP AT it_ekpo INTO wa_ekpo. WRITE:/ wa_ekpo-ebeln, wa_ekpo-matnr, wa_ekpo-werks, wa_ekpo-lgort. ENDLOOP.

The output is like this.

Rohini kumar

sap abap consultant

Average Sum Maximum Minimum by Select We can calculate the average and sum of any quantity type field directly from select statement. Similarly we can extract the maximum value or minimum value from quantity type field. In the below example the system fetches the data of MENGE field from EKPO table and calculate its average and sum and then put into the packed type variable ‘average’ & ‘sum’ respectively. Similarly it fetches the maximum and minimum values from MENGE and put it packed type variable ‘maximum’ & ‘minimum’. Here the WHERE clause is optional. To avoid the entire field records we have used this clause. In the database we find this MENGE field with PO number '3000000057'.

Rohini kumar

REPORT

sap abap consultant

zabap_gui.

DATA: average sum maximum minimum

TYPE TYPE TYPE TYPE

p p p p

DECIMALS DECIMALS DECIMALS DECIMALS

2, 2, 2, 2.

* Here the MENGE field of EKPO table has been used * to calculate the average, sum, maximum & minimum SELECT AVG( menge ) SUM( menge ) MAX( menge ) MIN( menge )

Rohini kumar

sap abap consultant

FROM ekpo INTO (average, sum, maximum, minimum) WHERE ebeln = '3000000057'. WRITE: / / / /

'Average 'Sum 'Maximum 'Minimum

= = = =

', ', ', ',

average, sum, maximum, minimum.

Here is the output.

Client Specified Select Client specified clause switches off the automatic client handling by open SQL. If we select the MANDT (client) field then we have to use client specified clause like follows: SELECT mandt field1 field2 ... fieldn INTO TABLE internal_table FROM database_table CLIENT SPECIFIED "MANDT has been selected "hence client specified is must WHERE mandt = '800' AND field1 IN select_option.

This statement is always mentioned after the FROM clause. If the addition CLIENT SPECIFIED is specified, but the client ID in the WHERE condition is not, the SELECT statement circumvents the SAP buffering. REPORT

zabap_gui.

TABLES: kna1. * Local structure for local internal table * and work area TYPES: BEGIN OF ty_kna1, mandt TYPE kna1-mandt, kunnr TYPE kna1-kunnr, land1 TYPE kna1-land1, name1 TYPE kna1-name1,

Rohini kumar

sap abap consultant

ort01 TYPE kna1-ort01, pstlz TYPE kna1-pstlz, regio TYPE kna1-regio, END OF ty_kna1. * Local internal table & work area DATA: it_kna1 TYPE TABLE OF ty_kna1, wa_kna1 TYPE ty_kna1. * Selection range by select option internal table SELECT-OPTIONS: s_kunnr FOR kna1-kunnr. START-OF-SELECTION. * Selection of the specific fields SELECT mandt kunnr land1 name1 ort01 pstlz regio INTO TABLE it_kna1 FROM kna1 CLIENT SPECIFIED "MANDT has been selected "hence client specified is must WHERE mandt = '800' AND kunnr IN s_kunnr. IF sy-subrc WRITE:/ 5 14 24 60 100 112 ULINE. SKIP.

= 0. 'Clnt', 'Customer No', 'Country', 'Name', 'City', 'Postal', 'Region'.

LOOP AT it_kna1 INTO wa_kna1. WRITE:/ wa_kna1-mandt, 5 wa_kna1-kunnr, 14 wa_kna1-land1, 24 wa_kna1-name1, 60 wa_kna1-ort01, 100 wa_kna1-pstlz, 112 wa_kna1-regio. ENDLOOP. ENDIF.

Here is the output.

Rohini kumar

sap abap consultant

Bypassing Buffer in Select One of an important feature of open SQL is that it fetches the data records from the buffer of SAP system. Now fetching records directly from database may take time. Hence performance will go down. That’s why SQL fetches data from buffer. Now if the database table changes frequently (table like transaction table) then it will be a problem to select updated data which will not be present in buffer. To avoid this problem SAP system has introduced the BYPASSING BUFFER clause in the select statement after from clause. This statement ensures that the records are updated data records fetched from the database. REPORT

zabap_gui.

TABLES: kna1. * Local structure for local internal table * and work area TYPES: BEGIN OF ty_kna1, kunnr TYPE kna1-kunnr, land1 TYPE kna1-land1,

Rohini kumar

sap abap consultant

name1 TYPE kna1-name1, ort01 TYPE kna1-ort01, pstlz TYPE kna1-pstlz, regio TYPE kna1-regio, END OF ty_kna1. * Local internal table & work area DATA: it_kna1 TYPE TABLE OF ty_kna1, wa_kna1 TYPE ty_kna1. * Selection range by select option internal table SELECT-OPTIONS: s_kunnr FOR kna1-kunnr. START-OF-SELECTION. * Selection of the specific fields SELECT kunnr land1 name1 ort01 pstlz regio INTO TABLE it_kna1 FROM kna1 BYPASSING BUFFER "it ensures that the system fetches "data directly from the database "not from the buffer WHERE kunnr IN s_kunnr. IF sy-subrc WRITE: /5 14 24 60 100 112 ULINE. SKIP.

= 0. 'Customer No', 'Country', 'Name', 'City', 'Postal', 'Region'.

LOOP AT it_kna1 INTO wa_kna1. WRITE: /5 wa_kna1-kunnr, 14 wa_kna1-land1, 24 wa_kna1-name1, 60 wa_kna1-ort01, 100 wa_kna1-pstlz, 112 wa_kna1-regio. ENDLOOP. ENDIF.

Here is the output.

Rohini kumar

sap abap consultant

Select Dynamic Column We can select the columns dynamically in a select statement. The syntax is like this: SELECT (local_internal_table) FROM database_table INTO TABLE internal_table.

Here the local internal table contains the field names dynamically. This table also has a line type which holds the data of field names like this. DATA: line TYPE char100, itab TYPE TABLE OF line. line = 'ebeln ebelp matnr werks lgort'. APPEND line TO itab.

Now after appending the text to the itab it can be used dynamically in select statement. Here the WHERE clause is optional. If we don’t use it then the total rows/records of the fields will have been fetched by the system.

Rohini kumar

REPORT

sap abap consultant

zabap_gui.

TABLES: ekpo. * Creating a custom structure of Item Table TYPES: BEGIN OF ty_ekpo, ebeln TYPE ekpo-ebeln, ebelp TYPE ekpo-ebelp, matnr TYPE ekpo-matnr, werks TYPE ekpo-werks, lgort TYPE ekpo-lgort, END OF ty_ekpo. * Creating a line type of predefined structure DATA: wa_ekpo TYPE ty_ekpo, it_ekpo TYPE STANDARD TABLE OF ty_ekpo, * Creating a line type and internal table * to use as dynamic columns specification line TYPE char100, itab TYPE TABLE OF line. line = 'ebeln ebelp matnr werks lgort'. APPEND line TO itab. SELECT (itab) FROM ekpo INTO TABLE it_ekpo WHERE ebeln = '3000000232'. WRITE:/ 15 28 48 55

'PO No.', 'Item No', 'Material', 'Plant', 'Storage'.

ULINE. SKIP. LOOP AT it_ekpo INTO wa_ekpo. WRITE:/ wa_ekpo-ebeln, 15 wa_ekpo-ebelp, 28 wa_ekpo-matnr, 48 wa_ekpo-werks, 55 wa_ekpo-lgort. ENDLOOP.

Here is the output.

Rohini kumar

sap abap consultant

Group By Group By clause categorizes and summarizes the lines of database table based on a single field. That single field is mentioned in the group by clause. We can use multiple fields in the group by clause also. Here the database table records will be summarized according to those fields. The fields mentioned in the group by clause cannot be specified for the aggregate function. We have to use other fields which are not specified in group by clause to calculate the aggregate function. Now we have the a PO with the following quantity.

Rohini kumar

REPORT

sap abap consultant

zabap_gui.

DATA: ebeln po_max po_min tq_max tq_min WRITE:/ 15 35 55 70

TYPE TYPE TYPE TYPE TYPE

ekpo-ebeln, p DECIMALS 2, p DECIMALS 2, p DECIMALS 2, p DECIMALS 2.

'PO No', 'Max PO Quantity', 'Min PO Quantity', 'Max Target', 'Min Target'.

SELECT ebeln MAX( menge ) MIN( menge ) MAX( ktmng ) MIN( ktmng ) FROM ekpo INTO (ebeln, po_max, po_min, tq_max, tq_min) GROUP BY ebeln. WRITE:/ 15 35 55 70

ebeln, po_max, po_min, tq_max, tq_min.

ENDSELECT.

Here is the output:

Rohini kumar

sap abap consultant

Using Cursor in ABAP A cursor is a database object which is used to manipulate data in a set of row by row. We can say that a cursor is a set of rows with a pointer and this pointer actually points to the current row. Since cursor works row by row, it actually kills the performance. So it is better to go with another way with the help ABAP logic. In ABAP we use cursor with the following four processes.



Declare the Cursor: The cursor is declared by the DATA statement with keyword CURSOR.

       

Open Cursor Statement: Open cursor opens a database cursor for a specific selection, defined after FOR. It links the cursor variable (cr_spfli) to the database cursor. If the cursor variable is already opened then it cannot be reopened. The statement takes the cursor position at the first row of the resulting set. The select statement declared after FOR doesn’t enter any record into any table or work area. Select single statement cannot be used here. Only a limited number of database cursor can be open at the same time. Open cursor actually initialize the cursor at the first position of database.

     

  

Fetch Next Cursor Statement: It extracts the requested rows from the database. We can enter the fetched data into a table or work area. The append work can also be done here. It changes the position of the database cursor to the next line to be extracted. System can fetch one or more data records by this statement. Sy-subrc will be zero when the system fetches data. When the cursor is at the last position of rows then the next cursor will cause sy-subrc = 4. Because no line will be extracted further. Close Cursor Statement: It closes the database cursor and initializes the cursor variable. We should close all the open database cursor if they are no longer required. Once the cursor is closed it no longer is accessed. In the following example we have demonstrated a program where cursor has been used. REPORT

zsr_test NO STANDARD PAGE HEADING.

TABLES spfli. DATA: wa_spfli TYPE spfli. "Declare cursor data: cr_spfli TYPE cursor. PARAMETERS p_from TYPE spfli-countryfr. "Open Cursor OPEN CURSOR cr_spfli FOR SELECT *

Rohini kumar

sap abap consultant

FROM spfli WHERE countryfr = p_from. IF sy-subrc WRITE: / 10 30 45 66 86 100 121 142 160 175 ULINE. SKIP. ENDIF.

= 0. 'Airline', 'Flight Number', 'Country From', 'City From', 'Departure airport', 'Country To', 'City To', 'Destination airport', 'Departure time', 'Arrival time', 'Distance'.

DO. "Fetch Next Cursor FETCH NEXT CURSOR cr_spfli INTO wa_spfli. IF sy-subrc = 0. CHECK wa_spfli-countryfr = p_from. WRITE: /3 wa_spfli-carrid, 10 wa_spfli-connid, 30 wa_spfli-countryfr, 45 wa_spfli-cityfrom, 66 wa_spfli-airpfrom, 86 wa_spfli-countryto, 100 wa_spfli-cityto, 121 wa_spfli-airpto, 142 wa_spfli-deptime, 160 wa_spfli-arrtime, 175 wa_spfli-distance. ELSE. EXIT. ENDIF. ENDDO. "Close Cursor CLOSE CURSOR cr_spfli. The output is as follows.

Rohini kumar

This program can be done in another way as follows. REPORT

zsr_test NO STANDARD PAGE HEADING.

TABLES spfli. DATA: wa_spfli TYPE spfli, it_spfli TYPE TABLE OF spfli. "Declare Cursor DATA: cr_spfli TYPE cursor. PARAMETERS p_from TYPE spfli-countryfr. "Open Cursor OPEN CURSOR cr_spfli FOR SELECT * FROM spfli WHERE countryfr = p_from. "Fetch Cursor FETCH NEXT CURSOR cr_spfli INTO TABLE it_spfli. IF sy-subrc = 0. WRITE: / 'Airline', 10 'Flight Number',

sap abap consultant

Rohini kumar

30 45 66 86 100 121 142 160 175

sap abap consultant

'Country From', 'City From', 'Departure airport', 'Country To', 'City To', 'Destination airport', 'Departure time', 'Arrival time', 'Distance'.

ULINE. SKIP. LOOP AT it_spfli INTO wa_spfli. WRITE: /3 wa_spfli-carrid, 10 wa_spfli-connid, 30 wa_spfli-countryfr, 45 wa_spfli-cityfrom, 66 wa_spfli-airpfrom, 86 wa_spfli-countryto, 100 wa_spfli-cityto, 121 wa_spfli-airpto, 142 wa_spfli-deptime, 160 wa_spfli-arrtime, 175 wa_spfli-distance. ENDLOOP. ENDIF. "Close Cursor CLOSE CURSOR cr_spfli. The output is similar as before.

1. 9. ALV Grid Interactive with a Button 10. ALV List Display Report 11. ALV List Interactive Report 12. F4 Help for Parameter and Select Option 13. Two ALV Grids in Single Screen using OOPs 14. ALV Hierarchical Report 15. Report to Report with Parameter 16. Report to Report with Select Option 17. At Selection Screen event

Rohini kumar

sap abap consultant

18. At Selection Screen Output 19. Editable ALV Report

Classical Report of Single Table The following program is displaying some useful information of material from table MARA based on the material number by a select option in the selection screen. Here we have created a custom structure of the internal table named it_mara. This structure ty_mara contains Material No, Creation Date, Name, Material Type and Group. Hence output will have only these fields. We have declared an work area wa_mara for internal table. Here the internal table is of standard table type. Now under INITIALIZATION event we have initialized the Program name, date and user name which are to be displayed at the Top of page. TOP OF PAGE is another event where we are declaring the select option s_matnr in a selection screen for input of material no. Next we are declaring the START-OF-SELECTION event under which we mention a subroutine get_mara. Subroutine is declared by the key word perform. Under this perform we select required fields from MARA into internal table it_mara based on the where condition. So after getting proper data from the MARA table we shall prepare the output in the subroutine of get_output. Now on the next event END-OF-SELECTION we are declaring a subroutine get_output and inside there we are looping the internal table it_mara into wa_mara. Hence we fetch the data from internal table to the work area and then display it with simple write statement. Since this is a loop, so one by one record will be fetched into work area and then it will be displayed. Now we want to display the name of the fields heading at the beginning. To do this we call control break statement at first & endat inside the loop. At first statement will be triggered at the first time of the loop. Similarly when the listing will be completed then At Last - endat statement will be triggered to display ending of report message. Next we raise the event TOP-OF-PAGE which is used to display the top message. On the top we can write program name, user name, date etc. REPORT

zabap_gui.

*Declaring the line type of database table TABLES: mara.

Rohini kumar

sap abap consultant

*------Declaring the local types for Internal table & Work area--------* TYPES: BEGIN OF ty_mara, matnr TYPE mara-matnr, "Material No. ersda TYPE mara-ersda, "Creation Data ernam TYPE mara-ernam, "Created By mtart TYPE mara-mtart, "Material Type matkl TYPE mara-matkl, "Material Group END OF ty_mara. *-----Declaring the Internal table & work area-------------------------* DATA: wa_mara TYPE ty_mara, it_mara TYPE STANDARD TABLE OF ty_mara, v_repid TYPE sy-repid, "Program name v_date TYPE sy-datum, "Current date v_user TYPE sy-uname. "User name *-------------Event initialization-------------------------------------* INITIALIZATION. v_repid = sy-repid. v_date = sy-datum. v_user = sy-uname. *-Declaring the selection screen & select option for input-------------* SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001. SELECT-OPTIONS s_matnr FOR mara-matnr OBLIGATORY. SELECTION-SCREEN END OF BLOCK b1. *-----Event start of selection-----------------------------------------* START-OF-SELECTION. PERFORM get_mara. *---Event end of selection---------------------------------------------* END-OF-SELECTION. PERFORM get_output. *---Event top of page--------------------------------------------------* TOP-OF-PAGE. PERFORM top_page. *&---------------------------------------------------------------------* *& Form get_mara *&---------------------------------------------------------------------* * Select data from MARA table *----------------------------------------------------------------------* FORM get_mara . SELECT matnr ersda ernam mtart matkl FROM mara INTO TABLE it_mara WHERE matnr IN s_matnr. IF sy-subrc 0. MESSAGE 'Material Doesn''t Exist.' TYPE 'I'. LEAVE LIST-PROCESSING. ENDIF. ENDFORM. " get_mara *&---------------------------------------------------------------------* *& Form get_output *&---------------------------------------------------------------------* * Preparing the classical output with WRITE statement

Rohini kumar

sap abap consultant

*----------------------------------------------------------------------* FORM get_output . IF it_mara IS NOT INITIAL. LOOP AT it_mara INTO wa_mara. "Control break statement – it will display one time at first line AT FIRST. WRITE: /3 'Material No.', 25 'Created By', 40 'Group', 55 'Type', 70 'Creation Date'. ULINE. SKIP. ENDAT. WRITE: / 25 40 55 70

wa_mara-matnr, wa_mara-ernam, wa_mara-matkl, wa_mara-mtart, wa_mara-ersda.

"Control break statement – it will display one time at last line AT LAST. ULINE. WRITE: /15 '~~End of Material Display~~'. ENDAT. ENDLOOP. ENDIF. ENDFORM. " get_output *&---------------------------------------------------------------------* *& Form top_page *&---------------------------------------------------------------------* * Top pf page to display top information *----------------------------------------------------------------------* FORM top_page . WRITE: / / / / ULINE. SKIP.

'Material Details', 'Date: ', 12 v_date DD/MM/YYYY, 'User: ', 12 v_user, 'Report: ', 12 v_repid.

ENDFORM.

Below is the output:

" top_page

Rohini kumar

sap abap consultant

Output:

Classical Report of Multiple Tables After creating a classic report for single table it's time to create a report which will contain multiple tables. Here we shall fetch various records from various tables and then collate them to reach required output. We have created a report which contains multiple tables like MARA, MARC and MARD. The materials contain different Plant and Storage Location in MARC and MARD tables respectively. All those different plant and storage location will be displayed with material number in output here.

Rohini kumar

sap abap consultant

Here the plant is a big item and the storage location is small item. One single plant contains different storage locations. Materials are stored inside one of these storage locations. Now different materials can be stored in the same storage location. In the program we have prepared internal table it_mara from MARA table based on the Select Option material number range. If material number is entered then only the program will give an output. After preparing valid information of it_mara the program selects data from MARC (plant) table into it_marc for all entries in it_mara. Here we have to check the it_mara table if it is not initial. If we don't give this checking then all records will be selected from MARC table and that would be wrong. Hence we can say that this table checking is one of a prerequisites of For All Entries. Similarly after preparing the it_marc table we shall prepare the it_mard table from MARD (storage location) for all entries in it_marc. Similarly the table checking of it_marc must be there. Now we have material (it_mara), plant (it_marc) & storage location (it_mard) information. We have to collate all these information to display the required output. To do this we have prepared an internal table it_out with an work area wa_out. This internal table & work area contains the structure ty_out as per the report output requirement. Now we are looping it_mara to fetch material & type into the output work area. Here MARA is a master table hence we have to loop this table. Now one material can be maintained for different storage locations under one plant or different plant. Hence to populate output table we have to loop into it_marc and it_mard table. Here we are looping it_marc and it_mard with WHERE clause because we are going to fetch all records of plant and storage location at one time. So where clause will help us to point out the particular material and also will increase the performance. Finally at the output population we have used control break statement like AT FIRST, AT END OF, AT LAST. With the help of that we have synchronized the output in different lines. Following is the coding of the classical report. REPORT

zabap_gui.

*-------Declaring the line type of database tables---------------------* TABLES: mara, marc, mard.

Rohini kumar

sap abap consultant

*------Declaring the types of work areas & internal tables-------------* TYPES: BEGIN OF ty_mara, matnr TYPE mara-matnr, mtart TYPE mara-mtart, END OF ty_mara, BEGIN OF ty_marc, matnr TYPE marc-matnr, werks TYPE marc-werks, xchar TYPE marc-xchar, END OF ty_marc, BEGIN OF ty_mard, matnr TYPE mard-matnr, werks TYPE mard-werks, lgort TYPE mard-lgort, pstat TYPE mard-pstat, END OF ty_mard, BEGIN OF ty_out, matnr TYPE marc-matnr, werks TYPE marc-werks, lgort TYPE mard-lgort, mtart TYPE mara-mtart, xchar TYPE marc-xchar, pstat TYPE mard-pstat, END OF ty_out.

"Material "Plant "Storage Location "Material Type "Batch number "Maintenance Status

*-----Declaring work areas & internal tables---------------------------* DATA: wa_mara TYPE ty_mara, wa_marc TYPE ty_marc, wa_mard TYPE ty_mard, wa_out TYPE ty_out, it_mara TYPE STANDARD TABLE OF ty_mara, it_marc TYPE STANDARD TABLE OF ty_marc, it_mard TYPE STANDARD TABLE OF ty_mard, it_out TYPE STANDARD TABLE OF ty_out, v_prog TYPE sy-repid, "Program name v_date TYPE sy-datum, "Current date v_time TYPE sy-uzeit. "Current time *----------Declaring constants to CONSTANTS: c_material TYPE char12 c_plant TYPE char5 c_storage TYPE char8 c_type TYPE char6 c_batch TYPE char6 c_maint TYPE char18 c_end TYPE char40

avoid VALUE VALUE VALUE VALUE VALUE VALUE VALUE

the hard codes-----------------* 'MATERIAL NO', 'PLANT', 'STORAGE', 'M TYPE', 'BATCH', 'MAINTENANCE STATUS', 'End of Material Details'.

*------Event initialization--------------------------------------------* INITIALIZATION. v_prog = sy-repid. v_date = sy-datum. v_time = sy-uzeit. *-----------Declaring selection screen with select option for input----* SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001. SELECT-OPTIONS: s_matnr FOR mara-matnr.

Rohini kumar

sap abap consultant

SELECTION-SCREEN END OF BLOCK b1. *-----Event start of selection-----------------------------------------* START-OF-SELECTION. PERFORM get_mara. PERFORM get_marc. PERFORM get_mard. *---Event end of selection---------------------------------------------* END-OF-SELECTION. PERFORM get_output. PERFORM display. *---Event top of page--------------------------------------------------* TOP-OF-PAGE. PERFORM top_of_page. *&---------------------------------------------------------------------* *& Form GET_MARA *&---------------------------------------------------------------------* * Select data from MARA table *----------------------------------------------------------------------* FORM get_mara . IF s_matnr IS NOT INITIAL. SELECT matnr mtart FROM mara INTO TABLE it_mara WHERE matnr IN s_matnr. IF sy-subrc = 0. SORT it_mara BY matnr. ELSE. MESSAGE 'Material doesn''t exist' TYPE 'I'. ENDIF. ENDIF. ENDFORM. " GET_MARA *&---------------------------------------------------------------------* *& Form GET_MARC *&---------------------------------------------------------------------* * Select data from MARC table *----------------------------------------------------------------------* FORM get_marc . IF it_mara IS NOT INITIAL. "Prerequisite of FOR ALL ENTRIES IN SELECT matnr werks xchar FROM marc INTO TABLE it_marc FOR ALL ENTRIES IN it_mara WHERE matnr = it_mara-matnr. IF sy-subrc = 0. SORT it_marc BY matnr. ELSE. MESSAGE 'Plant doesn''t exist' TYPE 'I'. ENDIF. ENDIF. ENDFORM. " GET_MARC *&---------------------------------------------------------------------* *& Form GET_MARD

Rohini kumar

sap abap consultant

*&---------------------------------------------------------------------* * Select data from MARD table *----------------------------------------------------------------------* FORM get_mard . IF it_marc IS NOT INITIAL. "Prerequisite of FOR ALL ENTRIES IN SELECT matnr werks lgort pstat FROM mard INTO TABLE it_mard FOR ALL ENTRIES IN it_marc WHERE matnr = it_marc-matnr AND werks = it_marc-werks. IF sy-subrc = 0. SORT it_mard BY matnr. ELSE. MESSAGE 'Storage Location doesn''t exist' TYPE 'I'. ENDIF. ENDIF. ENDFORM. " GET_MARD *&---------------------------------------------------------------------* *& Form GET_OUTPUT *&---------------------------------------------------------------------* * Preparing the output table by using Loop *----------------------------------------------------------------------* FORM get_output . IF it_mara IS NOT INITIAL. LOOP AT it_mara INTO wa_mara. wa_out-matnr = wa_mara-matnr. wa_out-mtart = wa_mara-mtart. LOOP AT it_marc INTO wa_marc WHERE matnr = wa_mara-matnr. wa_out-werks = wa_marc-werks. wa_out-xchar = wa_marc-xchar. LOOP AT it_mard INTO wa_mard WHERE matnr = wa_marc-matnr AND werks = wa_marc-werks. wa_out-lgort = wa_mard-lgort. wa_out-pstat = wa_mard-pstat. APPEND wa_out TO it_out. CLEAR: wa_out, wa_mara, wa_marc, wa_mard. ENDLOOP. ENDLOOP. ENDLOOP. ENDIF. ENDFORM. " GET_OUTPUT *&---------------------------------------------------------------------* *& Form DISPLAY *&---------------------------------------------------------------------* * Displaying the classical output by using WRITE statement *----------------------------------------------------------------------* FORM display . IF it_out IS NOT INITIAL. LOOP AT it_out INTO wa_out.

Rohini kumar

sap abap consultant

AT FIRST. "Control break statement – display one time at first WRITE: / c_material, 21 c_plant, 27 c_storage, 37 c_type, 45 c_batch, 54 c_maint. ULINE. SKIP. ENDAT. WRITE: / 21 27 37 45 54

wa_out-matnr, wa_out-werks, wa_out-lgort, wa_out-mtart, wa_out-xchar, wa_out-pstat.

IF wa_out-matnr IS INITIAL. AT END OF matnr. "Control break statement SKIP. ENDAT. ENDIF. AT LAST. "Control break statement – display one time at last ULINE. WRITE: / c_end. ENDAT. ENDLOOP. ENDIF. ENDFORM. " DISPLAY *&---------------------------------------------------------------------* *& Form TOP_OF_PAGE *&---------------------------------------------------------------------* * Top of page of Classical output *----------------------------------------------------------------------* FORM top_of_page . WRITE: / v_prog, / v_date DD/MM/YYYY, / v_time. ULINE. ENDFORM.

Output: 1. Selection Screen -

" TOP_OF_PAGE

Rohini kumar

sap abap consultant

2. Classical Display -

Classical Interactive Report Interactive program generally interacts with user on the output screen. Let us suppose we have an output for a particular program/report. Now if we want details description by double clicking on a particular field or

Rohini kumar

sap abap consultant

clicking on a button then it will be called interaction with output. By doing this we can go to the detailed report or secondary output. When we are on the secondary output then we can back to our first out by clicking on Back button. We can generate 19 secondary list of output. Hence a total of 20 output screen (1 Primary + 19 Secondary) can be generated by Interactive program. We have a requirement where we have to prepare a primary output containing Purchase Order, Company Code, Category, Type and Vendor with PO Info. The secondary list will be generated with PO Item, Material, Plant, Storage Location, Material Group, Quantity and Unit of measure. The secondary list will be generated if we double click on PO field in primary output. We have mentioned the tables EKKO, EKPO and T161T. Now we have declared the structure types of internal tables of it_ekko, it_text, it_out1 and it_ekpo. Here it_out1 contains the combination of two internal tables it_ekko and it_text and it_ekpo is for the secondary output. For internal operation we have declared work area of every internal tables. Next we have mentioned the event INITIALIZATION. Under this we are calling program name, user name and system date of the program. We have declared here selection screen begin with block b1 and under this we are mentioning obligatory select option. Now under START-OF-SELECTION we are declaring all the operations for primary listing. We have made 4 subroutines. In get_ekko we select fields from EKKO into internal table it_ekko. Then in get_t161t we select fields from t161t into internal table it_text for all entries in it_ekko. After that we are preparing the primary output in the basic_output subroutine. Now we have made another subroutine for displaying the primary output and that is disp_basic. Hence the primary list is prepared now and now we have to create the secondary list. To make the secondary list we are calling event AT LINESELECTION which raises functionality when user double clicks in any field. Now we are mentioning GET CURSOR for that field and for that value of the field. When these two are matches with Purchase Order (EBELN) field then we are calling two subroutines for secondary list. Subroutine get_ekpo Selects fields from EKPO into table it_ekpo for all entries in it_ekko. Then it prepares the secondary output display in the subroutine of ekpo_output. We also have raised events TOP-OF-PAGE which shows the header list of primary output and TOP-OF-PAGE DURING LINE-SELECTION which shows the top list of secondary output. Here TOP-OF-PAGE DURING LINE-

Rohini kumar

sap abap consultant

SELECTION is used only for interactive reports. That means when user double clicks on the field the TOP-OF-PAGE DURING LINE-SELECTION event triggers to make the secondary output header. REPORT

zabap_gui.

* Declaring line structure of database tables TABLES: ekko, ekpo, t161t. * Declaring local structures for internal table & work area TYPES: BEGIN OF ty_ekko, ebeln TYPE ekko-ebeln, "Purchase Order bukrs TYPE ekko-bukrs, "Company Code bstyp TYPE ekko-bstyp, "Category bsart TYPE ekko-bsart, "Type lifnr TYPE ekko-lifnr, "Vendor END OF ty_ekko, BEGIN OF ty_text, spras TYPE t161t-spras, bsart TYPE t161t-bsart, bstyp TYPE t161t-bstyp, batxt TYPE t161t-batxt, "PO Info END OF ty_text, BEGIN OF ty_ekpo, ebeln TYPE ekpo-ebeln, ebelp TYPE ekpo-ebelp, matnr TYPE ekpo-matnr, werks TYPE ekpo-werks, lgort TYPE ekpo-lgort, matkl TYPE ekpo-matkl, menge TYPE ekpo-menge, meins TYPE ekpo-meins, END OF ty_ekpo,

"Purchase Order "PO Item "Material "Plant "Storage Location "Material Group "Quantity "Unit

BEGIN OF ty_out1, ebeln TYPE ekko-ebeln, bukrs TYPE ekko-bukrs, bstyp TYPE ekko-bstyp, bsart TYPE ekko-bsart, lifnr TYPE ekko-lifnr, batxt TYPE t161t-batxt, END OF ty_out1. * Declaring work area & internal table DATA: wa_ekko TYPE ty_ekko, it_ekko TYPE STANDARD TABLE OF ty_ekko, wa_text TYPE ty_text, it_text TYPE STANDARD TABLE OF ty_text, wa_out1 TYPE ty_out1, it_out1 TYPE STANDARD TABLE OF ty_out1, wa_ekpo TYPE ty_ekpo, it_ekpo TYPE STANDARD TABLE OF ty_ekpo, v_repid TYPE sy-repid, v_user TYPE sy-uname,

"Header table work area "Header internal table "Info table work area "Info internal table "Basic output work area "Basic output internal table "Item table work area "Item internal table

Rohini kumar

v_date v_field1 v_field2 v_value1 v_value2

sap abap consultant

TYPE sy-datum, TYPE TYPE TYPE TYPE

char40, char40, char40, char40.

* Event Initialization INITIALIZATION. v_repid = sy-repid. "Program Name v_user = sy-uname. "User name v_date = sy-datum. "Current Date * Declaring selection screen SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001. SELECT-OPTIONS s_ebeln FOR ekko-ebeln OBLIGATORY. SELECTION-SCREEN END OF BLOCK b1. * Event Start of Selection START-OF-SELECTION. PERFORM get_ekko. "Get data from header table PERFORM get_t161t. "Get data from Info table PERFORM basic_output. "Preparing the primary output PERFORM disp_basic. "Displaying output of first list * Event At line selection for Double click operation AT LINE-SELECTION. * It passes the field and value to the current cursor position * When double click is happened on the PO field of Primary list GET CURSOR FIELD v_field1 VALUE v_value1. *

CASE v_field1. When we double click on PO number on Basic output list WHEN 'WA_OUT1-EBELN'. PERFORM get_ekpo. "Get data from Item table PERFORM ekpo_output. "Displaying output of second list ENDCASE.

* It passes the field and value to the current cursor position * When double click is happened on the Material field of Secondary list GET CURSOR FIELD v_field2 VALUE v_value2. *

CASE v_field2. When we double click on Material on Second list WHEN 'WA_EKPO-MATNR'. PERFORM get_mara. "Get Information by calling MM03 Transaction ENDCASE.

* Event top of page for Basic list / Primary list TOP-OF-PAGE. PERFORM top_page1. * Event top of page for Second list TOP-OF-PAGE DURING LINE-SELECTION. PERFORM top_page2. *&---------------------------------------------------------------------* *& Form get_ekko *&---------------------------------------------------------------------*

Rohini kumar

sap abap consultant

* Get data from header table *----------------------------------------------------------------------* FORM get_ekko . * Selection of header table data SELECT ebeln bukrs bstyp bsart lifnr FROM ekko INTO TABLE it_ekko WHERE ebeln IN s_ebeln. IF sy-subrc = 0. SORT it_ekko BY ebeln. ELSE. MESSAGE 'Purchase Order doesn''t exist.' TYPE 'I'. LEAVE LIST-PROCESSING. ENDIF. ENDFORM. " get_ekko *&---------------------------------------------------------------------* *& Form get_t161t *&---------------------------------------------------------------------* * Get data from Info table *----------------------------------------------------------------------* FORM get_t161t . * Seelction of Info table data IF it_ekko IS NOT INITIAL. "Prerequisite of For all Entries SELECT spras bsart bstyp batxt FROM t161t INTO TABLE it_text FOR ALL ENTRIES IN it_ekko WHERE spras = sy-langu "System language at login time AND bsart = it_ekko-bsart AND bstyp = it_ekko-bstyp. IF sy-subrc = 0. SORT it_text BY bsart bstyp. ENDIF. ENDIF. ENDFORM. " get_t161t *&---------------------------------------------------------------------* *& Form basic_output *&---------------------------------------------------------------------* * Preparing the primary output *----------------------------------------------------------------------* FORM basic_output . * Preparing the basic output table IF it_ekko IS NOT INITIAL. LOOP AT it_ekko INTO wa_ekko. wa_out1-ebeln = wa_ekko-ebeln. wa_out1-bukrs = wa_ekko-bukrs. wa_out1-bstyp = wa_ekko-bstyp. wa_out1-bsart = wa_ekko-bsart. wa_out1-lifnr = wa_ekko-lifnr. READ TABLE it_text INTO wa_text WITH KEY bsart = wa_ekko-bsart bstyp = wa_ekko-bstyp BINARY SEARCH. IF sy-subrc = 0. wa_out1-batxt = wa_text-batxt.

Rohini kumar

sap abap consultant

ENDIF. APPEND wa_out1 TO it_out1. CLEAR: wa_out1, wa_ekko, wa_text. ENDLOOP. ENDIF. ENDFORM. " basic_output *&---------------------------------------------------------------------* *& Form disp_basic *&---------------------------------------------------------------------* * Displaying output of first list *----------------------------------------------------------------------* FORM disp_basic . IF it_out1 IS NOT INITIAL. LOOP AT it_out1 INTO wa_out1. AT FIRST. "Control Break Statement - triggers at first WRITE: / 'Purchase Order', 20 'Company', 30 'Category', 40 'Type', 50 'Vendor', 65 'PO Info.'. ULINE. SKIP. ENDAT. WRITE: / 20 33 40 50 65

wa_out1-ebeln, wa_out1-bukrs, wa_out1-bstyp, wa_out1-bsart, wa_out1-lifnr, wa_out1-batxt.

AT LAST. "Control Break Statement - triggers at last SKIP. ULINE. WRITE: /12 '~~End of Report~~'. ENDAT. ENDLOOP. ENDIF. ENDFORM. " disp_basic *&---------------------------------------------------------------------* *& Form get_ekpo *&---------------------------------------------------------------------* * Get data from Item table *----------------------------------------------------------------------* FORM get_ekpo . * Local temporary variable for conversion DATA: lv_ebeln TYPE ekko-ebeln. IF v_value1 IS NOT INITIAL. * *

To convert the value from output format to input format and passing it from input format to output format CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT' EXPORTING

Rohini kumar

sap abap consultant

input = v_value1 IMPORTING output = lv_ebeln. IF lv_ebeln IS NOT INITIAL. *

Selection of Item table SELECT ebeln ebelp matnr werks lgort matkl menge meins FROM ekpo INTO TABLE it_ekpo WHERE ebeln = lv_ebeln. IF sy-subrc 0. MESSAGE 'PO Item doesn''t Exist.' TYPE 'I'. LEAVE LIST-PROCESSING. ENDIF. ENDIF. ENDIF.

ENDFORM. " get_ekpo *&---------------------------------------------------------------------* *& Form ekpo_output *&---------------------------------------------------------------------* * Displaying output of second list *----------------------------------------------------------------------* FORM ekpo_output . * Preparing secondary output IF it_ekpo IS NOT INITIAL. LOOP AT it_ekpo INTO wa_ekpo. AT FIRST. WRITE: / 20 30 48 55 65 83 100 ULINE. SKIP. ENDAT. WRITE: / 20 30 48 55 70 75 100

'Purchase Order', 'PO Item', 'Material', 'Plant', 'Storage', 'Material Group', 'PO Quantity', 'Unit'.

wa_ekpo-ebeln, wa_ekpo-ebelp, wa_ekpo-matnr, wa_ekpo-werks, wa_ekpo-lgort, wa_ekpo-matkl, wa_ekpo-menge, wa_ekpo-meins.

AT LAST. SKIP. ULINE. WRITE: /12 '~~End of PO Item~~'. ENDAT. ENDLOOP.

Rohini kumar

sap abap consultant

ENDIF. ENDFORM. " ekpo_output *&---------------------------------------------------------------------* *& Form get_mara *&---------------------------------------------------------------------* * Get Information by calling MM03 Transaction *----------------------------------------------------------------------* FORM get_mara . * Local temporary variable for conversion DATA: lv_matnr TYPE mara-matnr. IF v_value2 IS NOT INITIAL. * *

Converting material from output format to input format and passing it from input format to output format CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT' EXPORTING input = v_value2 IMPORTING output = lv_matnr EXCEPTIONS length_error = 1 OTHERS = 2. IF lv_matnr IS NOT INITIAL.

* *

Calling the MM03 transaction needs parameter ID which is available on domain of MATNR SET PARAMETER ID 'MAT' FIELD lv_matnr. CALL TRANSACTION 'MM03' AND SKIP FIRST SCREEN. ENDIF. ENDIF.

ENDFORM. " get_mara *&---------------------------------------------------------------------* *& Form top_page1 *&---------------------------------------------------------------------* * Event top of page for Basic list / Primary list *----------------------------------------------------------------------* FORM top_page1 . WRITE: / / / / ULINE. SKIP.

'Purchase Order Header', 'Date: ', 12 v_date DD/MM/YYYY, 'User: ', 12 v_user, 'Report: ', 12 v_repid.

ENDFORM. " top_page1 *&---------------------------------------------------------------------* *& Form top_page2 *&---------------------------------------------------------------------* * Event top of page for Second list *----------------------------------------------------------------------* FORM top_page2 . WRITE: / 'Purchase Order Item List', / 'Date: ', 12 v_date DD/MM/YYYY,

Rohini kumar

sap abap consultant

/ 'User: ', 12 v_user, / 'Report: ', 12 v_repid. ULINE. SKIP. ENDFORM.

" top_page2

Below is the output:

Primary Listing:

If we double click on the PO in this list then following will be generated:

Rohini kumar

sap abap consultant

Top of Page During Line Selection A normal classical report contains only one list where all necessary data (detail data) will be in one single report. In this way the user will see an extensive lengthy report on his screen. The user will not be able to interact with this kind of report. In this way the system will load every possible record for this report. Now this every details may not be needed for different users. But the user will have no other choice to use this kind of report. In this point of view SAP provides a solution which is interactive report. We can have a brief list report as the very first screen and then as per the need the user will drill the secondary lists which contain detailed data records. Hence the user can interact with the interactive report and the detail list will be generated when the user will need to see that details. This will increase the visibility with user friendly approach of the user. The events we use in interactive report are: At line-selection (double click functionality) At user-command (Push button functionality) Top-of-page during line selection (It triggers the top of page when we do any drilling of the report) In the below example we have raised an interactive report where we have used the double click functionality. It means when we see the basic list then we can double click on a particular field (Airline code in this example). By the time we double click on that field the system will show the first secondary list (1st drilling) which is the detailed list of Flight Schedule. That secondary list can also be drilled in to Flight when we double click on the Flight number field. In this way we have three secondary lists. Now we need to populate different top of page at the time of secondary lists of this report. We have a system table SY-LSIND which stores the values of drilled number. It means when we see the basic list SY-LSIND contains value 0. At the first secondary list it will contain the value of 1 and so on. Now with having the CASE of SY-LSIND we can reach to different top of page functionality and we can write different content. REPORT

zabap_gui.

*-------Declaring Database Tables for Line type------------------------* TABLES: scarr, spfli, sflight, sbook. *------Declaring local structure for Internal tables & Work Areas------* TYPES: BEGIN OF ty_scarr, carrid TYPE scarr-carrid, carrname TYPE scarr-carrname, currcode TYPE scarr-currcode, END OF ty_scarr, BEGIN OF ty_spfli, carrid TYPE spfli-carrid,

Rohini kumar

sap abap consultant

connid TYPE spfli-connid, airpfrom TYPE spfli-airpfrom, airpto TYPE spfli-airpto, END OF ty_spfli, BEGIN OF ty_sflight, carrid TYPE sflight-carrid, connid TYPE sflight-connid, fldate TYPE sflight-fldate, seatsmax TYPE sflight-seatsmax, seatsocc TYPE sflight-seatsocc, END OF ty_sflight, BEGIN OF ty_sbook, carrid TYPE sbook-carrid, connid TYPE sbook-connid, fldate TYPE sbook-fldate, bookid TYPE sbook-bookid, customid TYPE sbook-customid, END OF ty_sbook. *-----Declaring Work Areas & Internal tables---------------------------* DATA: wa_scarr TYPE ty_scarr, wa_spfli TYPE ty_spfli, wa_sflight TYPE ty_sflight, wa_sbook TYPE ty_sbook, it_scarr it_spfli it_sflight it_sbook v_field v_value

TYPE TYPE TYPE TYPE

TABLE TABLE TABLE TABLE

OF OF OF OF

ty_scarr, ty_spfli, ty_sflight, ty_sbook,

TYPE char20, TYPE char20.

*--Event Initialization------------------------------------------------* INITIALIZATION. SELECT-OPTIONS s_carrid FOR scarr-carrid. *--Event Start of Selection--------------------------------------------* START-OF-SELECTION. PERFORM data_scarr. *--Event At Line Selection for Interactive operations------------------* AT LINE-SELECTION. *--Get Cursor Teacnique - Field & Value will be populated--------------* GET CURSOR FIELD v_field VALUE v_value. CASE v_field. WHEN 'WA_SCARR-CARRID'. PERFORM data_spfli. WHEN 'WA_SPFLI-CONNID'. PERFORM data_sflight. WHEN 'WA_SFLIGHT-FLDATE'. PERFORM data_sbook. ENDCASE.

Rohini kumar

sap abap consultant

*--Event Top of Page for Basic/ Primary Listing------------------------* TOP-OF-PAGE. PERFORM top_basic_scarr. *--Event Top of Page During Line Selection for Interactive Operations--* TOP-OF-PAGE DURING LINE-SELECTION. *--SY-LSIND is system variable-----------------------------------------* CASE sy-lsind. "It will increase its value while drilling the lists "one by one by double clicking WHEN '1'. PERFORM top_spfli. WHEN '2'. PERFORM top_sflight. WHEN '3'. PERFORM top_sbook. ENDCASE. *&---------------------------------------------------------------------* *& Form data_scarr *&---------------------------------------------------------------------* * Get data from Airline table *----------------------------------------------------------------------* FORM data_scarr . IF s_carrid[] IS NOT INITIAL. SELECT carrid carrname currcode FROM scarr INTO TABLE it_scarr WHERE carrid IN s_carrid. IF sy-subrc = 0. SORT it_scarr. LOOP AT it_scarr INTO wa_scarr. WRITE:/ wa_scarr-carrid, 15 wa_scarr-carrname, 38 wa_scarr-currcode. AT LAST. SKIP. WRITE: / '==========End of Airline Table==========='. ENDAT. ENDLOOP. ELSE. MESSAGE 'Airline doesn''t exist' TYPE 'I'. LEAVE LIST-PROCESSING. ENDIF. ENDIF. ENDFORM. " data_scarr *&---------------------------------------------------------------------* *& Form data_spfli *&---------------------------------------------------------------------* * Get data from Flight Schedule table *----------------------------------------------------------------------* FORM data_spfli . IF v_value IS NOT INITIAL. SELECT carrid connid airpfrom airpto FROM spfli INTO TABLE it_spfli WHERE carrid = v_value.

Rohini kumar

sap abap consultant

IF sy-subrc = 0. SORT it_spfli. LOOP AT it_spfli INTO wa_spfli. WRITE:/ wa_spfli-carrid, 15 wa_spfli-connid, 30 wa_spfli-airpfrom, 45 wa_spfli-airpto. AT LAST. SKIP. WRITE: / '===========End of Flight Schedule============'. ENDAT. ENDLOOP. ELSE. MESSAGE 'No Flight Schedule' TYPE 'I'. ENDIF. ENDIF. ENDFORM. " data_spfli *&---------------------------------------------------------------------* *& Form data_sflight *&---------------------------------------------------------------------* * Get data from Flight table *----------------------------------------------------------------------* FORM data_sflight . IF v_value IS NOT INITIAL. SELECT carrid connid fldate seatsmax seatsocc FROM sflight INTO TABLE it_sflight WHERE connid = v_value. IF sy-subrc = 0. SORT it_sflight. LOOP AT it_sflight INTO wa_sflight. WRITE: / wa_sflight-carrid, 15 wa_sflight-connid, 30 wa_sflight-fldate, 45 wa_sflight-seatsmax, 60 wa_sflight-seatsocc. AT LAST. SKIP. WRITE: / '=============End of Flight============='. ENDAT. ENDLOOP. ELSE. MESSAGE 'No Flight is there' TYPE 'I'. ENDIF. ENDIF. ENDFORM. " data_sflight *&---------------------------------------------------------------------* *& Form data_sbook *&---------------------------------------------------------------------* * Get data from Flight Booking table *----------------------------------------------------------------------*

Rohini kumar

sap abap consultant

FORM data_sbook . DATA: v_date TYPE char10. IF v_value IS NOT INITIAL. *--The FLDATE field has different input output format------------------* CALL FUNCTION 'CONVERT_DATE_TO_INTERNAL' EXPORTING date_external = v_value * ACCEPT_INITIAL_DATE = IMPORTING date_internal = v_date EXCEPTIONS date_external_is_invalid = 1 OTHERS = 2. IF v_date IS NOT INITIAL. SELECT carrid connid fldate bookid customid FROM sbook INTO TABLE it_sbook WHERE fldate = v_date. IF sy-subrc = 0. SORT it_sbook. LOOP AT it_sbook INTO wa_sbook. WRITE:/ wa_sbook-carrid, 15 wa_sbook-connid, 30 wa_sbook-fldate, 45 wa_sbook-bookid, 60 wa_sbook-customid. AT LAST. SKIP. WRITE: / '==================End of Flight Booking==================='. ENDAT. ENDLOOP. ELSE. MESSAGE 'No Single Flight Booking Available' TYPE 'I'. ENDIF. ENDIF. ENDIF. ENDFORM. " data_sbook *&---------------------------------------------------------------------* *& Form top_basic_scarr *&---------------------------------------------------------------------* * Top of Page for Basic / Primary list *----------------------------------------------------------------------* FORM top_basic_scarr . WRITE: / 'Airline Details List' COLOR 1. WRITE: / 'Airline Code', 15 'Airline Name', 38 'Currency'. ULINE. SKIP.

Rohini kumar

sap abap consultant

ENDFORM. " top_basic_scarr *&---------------------------------------------------------------------* *& Form top_spfli *&---------------------------------------------------------------------* * Top of Page for first secondary List *----------------------------------------------------------------------* FORM top_spfli . WRITE: / 'Flight Schedule List' COLOR 2. WRITE: / 'Airline Code', 15 'Flight Number', 30 'Departure', 45 'Destination'. ULINE. SKIP. ENDFORM. " top_spfli *&---------------------------------------------------------------------* *& Form top_sflight *&---------------------------------------------------------------------* * Top of Page for second secondary List *----------------------------------------------------------------------* FORM top_sflight . WRITE: / 'Flight Details List' COLOR 3. WRITE: / 'Airline Code', 15 'Flight Number', 30 'Flight Date', 45 'Available', 60 'Occupied'. ULINE. SKIP. ENDFORM. " top_sflight *&---------------------------------------------------------------------* *& Form top_sbook *&---------------------------------------------------------------------* * Top of Page for third secondary List *----------------------------------------------------------------------* FORM top_sbook . WRITE: / 'Single Flight Booking List' COLOR 4. WRITE: / 'Airline Code', 15 'Flight Number', 30 'Flight Date', 45 'Booking Number', 60 'Customer Number'. ULINE. SKIP. ENDFORM.

Here is the output of this. Selection Screen:

" top_sbook

Rohini kumar

Basic List:

sap abap consultant

Rohini kumar

First Secondary List:

Second Secondary List:

sap abap consultant

Rohini kumar

Third Secondary List:

sap abap consultant

Rohini kumar

sap abap consultant

Classical Interactive with Push Button Interactive reports increase the visibility of user when the user interacts with the basic list to get detail secondary lists. The basic list contains the brief information whereas the detail list contains the detail information. As per the requirement the user interacts with the detailed list whenever it is needed. That is the reason why it increases the visibility. Instead of an extensive and detailed list the user can actively control data retrieval and display during the session through this interactive report. The user can interact with the report with having the cursor position and entering the command. There are several events for Interactive report: At line selection (when user double clicks to a particular field value) At user command (when user selects a particular field value and then clicks a push button) 3. Top of page during line selection (it displays different content at different secondary lists) 1. 2.

To interact by using a push button the R/3 system has an option which is menu painter. We can do this by using the PF-STATUS. With the help of that we can create our customized menu where we can set push button. After that we have to

Rohini kumar

sap abap consultant

write functionality under the event at user command. The push button must have a function code and by using that we can write our custom function. The system field SY-UCOMM is used to set the different functions in different push buttons. Here we have an example where we select Vendor account details based on the selection criteria by select options. The report will have a basic list which will display the vendor details from LFA1 table. Then by selecting a vendor account number if we click the push button Purchase Order then we shall go to the first secondary list. This list is coming from the EKKO table. After that if we select any purchase order and then click the PO item button then second secondary list will be coming. This list is coming from EKPO table. In this point of view we have used HIDE statement. HIDE actually hides the field value into the system to reuse that at any time. With the help of this statement the system loads the field name, field content and line number in which the line exists. The line number is stored in SY-LINNO field. Field symbols cannot be specified in HIDE. This statement works independently. If we select the cursor to any blank field then also the HIDE will store that value. REPORT

zabap_gui.

*-------Declaring database tables for using the line type--------------* TABLES: lfa1, ekko, ekpo. *------Declaring structures for work area & Internal table-------------* TYPES: BEGIN OF ty_lfa1, lifnr TYPE lfa1-lifnr, land1 TYPE lfa1-land1, name1 TYPE lfa1-name1, regio TYPE lfa1-regio, END OF ty_lfa1, BEGIN OF ty_ekko, ebeln TYPE ekko-ebeln, bukrs TYPE ekko-bukrs, aedat TYPE ekko-aedat, ernam TYPE ekko-ernam, lifnr TYPE ekko-lifnr, END OF ty_ekko, BEGIN OF ty_ekpo, ebeln TYPE ekpo-ebeln, ebelp TYPE ekpo-ebelp, matnr TYPE ekpo-matnr, werks TYPE ekpo-werks, lgort TYPE ekpo-lgort, END OF ty_ekpo. DATA: *-----Declaring Work Areas---------------------------------------------* wa_lfa1 TYPE ty_lfa1, wa_ekko TYPE ty_ekko, wa_ekpo TYPE ty_ekpo, *-----Declaring Internal Tables----------------------------------------* it_lfa1 TYPE TABLE OF ty_lfa1,

Rohini kumar

sap abap consultant

it_ekko TYPE TABLE OF ty_ekko, it_ekpo TYPE TABLE OF ty_ekpo. *---Event Initialization-----------------------------------------------* INITIALIZATION. SELECT-OPTIONS: s_lifnr FOR lfa1-lifnr. *---Event Start of Selection-------------------------------------------* START-OF-SELECTION. PERFORM get_data_lfa1. SET PF-STATUS 'PUSH_BUTTON'. "Setting PF status for Push Buttons *---Event At User Command----------------------------------------------* AT USER-COMMAND. "When user clicks any button CASE sy-ucomm. "It contains the function code of any button WHEN 'EKKO'. "Push button Purchase Order PERFORM get_data_ekko. WHEN 'EKPO'. "Push button Purchase Order Item PERFORM get_data_ekpo. WHEN 'EXIT'. "Push button Exit LEAVE TO SCREEN 0. WHEN 'CANCEL'. "Push button Cancel LEAVE TO SCREEN 0. ENDCASE. *---Event Top of Page for Basic List-----------------------------------* TOP-OF-PAGE. PERFORM top_basic_lfa1. *---Event Top of Page During Line Selection for Drilled List-----------* TOP-OF-PAGE DURING LINE-SELECTION. CASE sy-lsind. "It counts the drilled number WHEN '1'. PERFORM top_ekko. WHEN '2'. PERFORM top_ekpo. ENDCASE. *&---------------------------------------------------------------------* *& Form get_data_lfa1 *&---------------------------------------------------------------------* * Select data from Vendor table *----------------------------------------------------------------------* FORM get_data_lfa1 . REFRESH it_lfa1. IF s_lifnr[] IS NOT INITIAL. SELECT lifnr land1 name1 regio FROM lfa1 INTO TABLE it_lfa1 WHERE lifnr IN s_lifnr. IF sy-subrc = 0. LOOP AT it_lfa1 INTO wa_lfa1. WRITE: / wa_lfa1-lifnr, 20 wa_lfa1-land1, 25 wa_lfa1-name1, 65 wa_lfa1-regio.

Rohini kumar

sap abap consultant

HIDE wa_lfa1-lifnr. "Vendor is keeping Hide to store the data "into system temporarily AT LAST. SKIP 2. WRITE: /25 'End of Vendor Account List'. ENDAT. ENDLOOP. ELSE. MESSAGE 'Vendor doesn''t exist' TYPE 'I'. ENDIF. ELSE. MESSAGE 'Please enter a valid Vendor Account Number' TYPE 'I'. LEAVE LIST-PROCESSING. ENDIF. ENDFORM. " get_data_lfa1 *&---------------------------------------------------------------------* *& Form get_data_ekko *&---------------------------------------------------------------------* * Select data from Purchase Order Header table *----------------------------------------------------------------------* FORM get_data_ekko . REFRESH it_ekko. IF wa_lfa1-lifnr IS NOT INITIAL. SELECT ebeln bukrs aedat ernam lifnr FROM ekko INTO TABLE it_ekko WHERE lifnr = wa_lfa1-lifnr. IF sy-subrc = 0. LOOP AT it_ekko INTO wa_ekko. WRITE: / wa_ekko-ebeln, 15 wa_ekko-bukrs, 22 wa_ekko-aedat, 34 wa_ekko-ernam, 50 wa_ekko-lifnr. HIDE wa_ekko-ebeln. "PO is keeping Hide to store the data "into system temporarily AT LAST. SKIP 2. WRITE: /20 'End of Purchase Order List'. ENDAT. ENDLOOP. ELSE. MESSAGE 'Purchase Order doesn''t exist' TYPE 'I'. ENDIF. ENDIF. ENDFORM. " get_data_ekko *&---------------------------------------------------------------------* *& Form get_data_ekpo *&---------------------------------------------------------------------* * Select data from Purchase Order Item Table *----------------------------------------------------------------------* FORM get_data_ekpo .

Rohini kumar

sap abap consultant

REFRESH it_ekpo. IF wa_ekko-ebeln IS NOT INITIAL. SELECT ebeln ebelp matnr werks lgort FROM ekpo INTO TABLE it_ekpo WHERE ebeln = wa_ekko-ebeln. IF sy-subrc = 0. LOOP AT it_ekpo INTO wa_ekpo. WRITE: / wa_ekpo-ebeln, 15 wa_ekpo-ebelp, 22 wa_ekpo-matnr, 42 wa_ekpo-werks, 50 wa_ekpo-lgort. AT LAST. SKIP 2. WRITE: /25 'End of Material List'. ENDAT. ENDLOOP. ENDIF. ELSE. MESSAGE 'Select a Purchase Order' TYPE 'I'. ENDIF. ENDFORM. " get_data_ekpo *&---------------------------------------------------------------------* *& Form top_basic_lfa1 *&---------------------------------------------------------------------* * Top of page of basic list - Vendor list *----------------------------------------------------------------------* FORM top_basic_lfa1 . WRITE: / 'Vendor Account List' COLOR 3. WRITE: / 'Vendor Account', 20 'Land', 25 'Name', 65 'Region'. ENDFORM. " top_basic_lfa1 *&---------------------------------------------------------------------* *& Form top_ekko *&---------------------------------------------------------------------* * Top of page for first secondary list - PO header list *----------------------------------------------------------------------* FORM top_ekko . WRITE: / 'Purchase Order List' COLOR 5. WRITE: / 'Po Number', 15 'Company', 22 'Date', 34 'Name', 50 'Vendor Account Number'. ENDFORM. " top_ekko *&---------------------------------------------------------------------* *& Form top_ekpo *&---------------------------------------------------------------------* * Top of page for second secondary list - PO item list *----------------------------------------------------------------------*

Rohini kumar

sap abap consultant

FORM top_ekpo . WRITE: / 'Purchase Order List' COLOR 1. WRITE: / 'PO Number', 15 'PO Item', 22 'Material', 42 'Plant', 50 'Storage'. ENDFORM.

" top_ekpo

Here is the output of Selection Screen:

The basic list of Vendor details:

Rohini kumar

The first secondary list of Purchase Order:

sap abap consultant

Rohini kumar

sap abap consultant

Rohini kumar

sap abap consultant

The second secondary list of Purchase Order Item:

Classical Interactive with Push Button Interactive reports increase the visibility of user when the user interacts with the basic list to get detail secondary lists. The basic list contains the brief information whereas the detail list contains the detail information. As per the requirement the user interacts with the detailed list whenever it is needed. That is the reason why it increases the visibility. Instead of an extensive and detailed list the user can actively control data retrieval and display during the session through this interactive report.

Rohini kumar

sap abap consultant

The user can interact with the report with having the cursor position and entering the command. There are several events for Interactive report: 1. At line selection (when user double clicks to a particular field value) 2. At user command (when user selects a particular field value and then clicks a push button) 3. Top of page during line selection (it displays different content at different secondary lists) To interact by using a push button the R/3 system has an option which is menu painter. We can do this by using the PF-STATUS. With the help of that we can create our customized menu where we can set push button. After that we have to write functionality under the event at user command. The push button must have a function code and by using that we can write our custom function. The system field SY-UCOMM is used to set the different functions in different push buttons. Here we have an example where we select Vendor account details based on the selection criteria by select options. The report will have a basic list which will display the vendor details from LFA1 table. Then by selecting a vendor account number if we click the push button Purchase Order then we shall go to the first secondary list. This list is coming from the EKKO table. After that if we select any purchase order and then click the PO item button then second secondary list will be coming. This list is coming from EKPO table. In this point of view we have used HIDE statement. HIDE actually hides the field value into the system to reuse that at any time. With the help of this statement the system loads the field name, field content and line number in which the line exists. The line number is stored in SY-LINNO field. Field symbols cannot be specified in HIDE. This statement works independently. If we select the cursor to any blank field then also the HIDE will store that value. REPORT

zabap_gui.

*-------Declaring database tables for using the line type--------------* TABLES: lfa1, ekko, ekpo. *------Declaring structures for work area & Internal table-------------* TYPES: BEGIN OF ty_lfa1, lifnr TYPE lfa1-lifnr, land1 TYPE lfa1-land1, name1 TYPE lfa1-name1, regio TYPE lfa1-regio, END OF ty_lfa1, BEGIN OF ty_ekko, ebeln TYPE ekko-ebeln, bukrs TYPE ekko-bukrs, aedat TYPE ekko-aedat, ernam TYPE ekko-ernam, lifnr TYPE ekko-lifnr, END OF ty_ekko,

Rohini kumar

sap abap consultant

BEGIN OF ty_ekpo, ebeln TYPE ekpo-ebeln, ebelp TYPE ekpo-ebelp, matnr TYPE ekpo-matnr, werks TYPE ekpo-werks, lgort TYPE ekpo-lgort, END OF ty_ekpo. DATA: *-----Declaring Work Areas---------------------------------------------* wa_lfa1 TYPE ty_lfa1, wa_ekko TYPE ty_ekko, wa_ekpo TYPE ty_ekpo, *-----Declaring Internal it_lfa1 TYPE TABLE it_ekko TYPE TABLE it_ekpo TYPE TABLE

Tables----------------------------------------* OF ty_lfa1, OF ty_ekko, OF ty_ekpo.

*---Event Initialization-----------------------------------------------* INITIALIZATION. SELECT-OPTIONS: s_lifnr FOR lfa1-lifnr. *---Event Start of Selection-------------------------------------------* START-OF-SELECTION. PERFORM get_data_lfa1. SET PF-STATUS 'PUSH_BUTTON'. "Setting PF status for Push Buttons *---Event At User Command----------------------------------------------* AT USER-COMMAND. "When user clicks any button CASE sy-ucomm. "It contains the function code of any button WHEN 'EKKO'. "Push button Purchase Order PERFORM get_data_ekko. WHEN 'EKPO'. "Push button Purchase Order Item PERFORM get_data_ekpo. WHEN 'EXIT'. "Push button Exit LEAVE TO SCREEN 0. WHEN 'CANCEL'. "Push button Cancel LEAVE TO SCREEN 0. ENDCASE. *---Event Top of Page for Basic List-----------------------------------* TOP-OF-PAGE. PERFORM top_basic_lfa1. *---Event Top of Page During Line Selection for Drilled List-----------* TOP-OF-PAGE DURING LINE-SELECTION. CASE sy-lsind. "It counts the drilled number WHEN '1'. PERFORM top_ekko. WHEN '2'. PERFORM top_ekpo. ENDCASE. *&---------------------------------------------------------------------* *& Form get_data_lfa1 *&---------------------------------------------------------------------* * Select data from Vendor table

Rohini kumar

sap abap consultant

*----------------------------------------------------------------------* FORM get_data_lfa1 . REFRESH it_lfa1. IF s_lifnr[] IS NOT INITIAL. SELECT lifnr land1 name1 regio FROM lfa1 INTO TABLE it_lfa1 WHERE lifnr IN s_lifnr. IF sy-subrc = 0. LOOP AT it_lfa1 INTO wa_lfa1. WRITE: / wa_lfa1-lifnr, 20 wa_lfa1-land1, 25 wa_lfa1-name1, 65 wa_lfa1-regio. HIDE wa_lfa1-lifnr. "Vendor is keeping Hide to store the data "into system temporarily AT LAST. SKIP 2. WRITE: /25 'End of Vendor Account List'. ENDAT. ENDLOOP. ELSE. MESSAGE 'Vendor doesn''t exist' TYPE 'I'. ENDIF. ELSE. MESSAGE 'Please enter a valid Vendor Account Number' TYPE 'I'. LEAVE LIST-PROCESSING. ENDIF. ENDFORM. " get_data_lfa1 *&---------------------------------------------------------------------* *& Form get_data_ekko *&---------------------------------------------------------------------* * Select data from Purchase Order Header table *----------------------------------------------------------------------* FORM get_data_ekko . REFRESH it_ekko. IF wa_lfa1-lifnr IS NOT INITIAL. SELECT ebeln bukrs aedat ernam lifnr FROM ekko INTO TABLE it_ekko WHERE lifnr = wa_lfa1-lifnr. IF sy-subrc = 0. LOOP AT it_ekko INTO wa_ekko. WRITE: / wa_ekko-ebeln, 15 wa_ekko-bukrs, 22 wa_ekko-aedat, 34 wa_ekko-ernam, 50 wa_ekko-lifnr. HIDE wa_ekko-ebeln. "PO is keeping Hide to store the data "into system temporarily AT LAST. SKIP 2.

Rohini kumar

sap abap consultant

WRITE: /20 'End of Purchase Order List'. ENDAT. ENDLOOP. ELSE. MESSAGE 'Purchase Order doesn''t exist' TYPE 'I'. ENDIF. ENDIF. ENDFORM. " get_data_ekko *&---------------------------------------------------------------------* *& Form get_data_ekpo *&---------------------------------------------------------------------* * Select data from Purchase Order Item Table *----------------------------------------------------------------------* FORM get_data_ekpo . REFRESH it_ekpo. IF wa_ekko-ebeln IS NOT INITIAL. SELECT ebeln ebelp matnr werks lgort FROM ekpo INTO TABLE it_ekpo WHERE ebeln = wa_ekko-ebeln. IF sy-subrc = 0. LOOP AT it_ekpo INTO wa_ekpo. WRITE: / wa_ekpo-ebeln, 15 wa_ekpo-ebelp, 22 wa_ekpo-matnr, 42 wa_ekpo-werks, 50 wa_ekpo-lgort. AT LAST. SKIP 2. WRITE: /25 'End of Material List'. ENDAT. ENDLOOP. ENDIF. ELSE. MESSAGE 'Select a Purchase Order' TYPE 'I'. ENDIF. ENDFORM. " get_data_ekpo *&---------------------------------------------------------------------* *& Form top_basic_lfa1 *&---------------------------------------------------------------------* * Top of page of basic list - Vendor list *----------------------------------------------------------------------* FORM top_basic_lfa1 . WRITE: / 'Vendor Account List' COLOR 3. WRITE: / 'Vendor Account', 20 'Land', 25 'Name', 65 'Region'. ENDFORM. " top_basic_lfa1 *&---------------------------------------------------------------------* *& Form top_ekko *&---------------------------------------------------------------------* * Top of page for first secondary list - PO header list

Rohini kumar

sap abap consultant

*----------------------------------------------------------------------* FORM top_ekko . WRITE: / 'Purchase Order List' COLOR 5. WRITE: / 'Po Number', 15 'Company', 22 'Date', 34 'Name', 50 'Vendor Account Number'. ENDFORM. " top_ekko *&---------------------------------------------------------------------* *& Form top_ekpo *&---------------------------------------------------------------------* * Top of page for second secondary list - PO item list *----------------------------------------------------------------------* FORM top_ekpo . WRITE: / 'Purchase Order List' COLOR 1. WRITE: / 'PO Number', 15 'PO Item', 22 'Material', 42 'Plant', 50 'Storage'. ENDFORM.

" top_ekpo

Here is the output of Selection Screen:

The basic list of Vendor details:

Rohini kumar

The first secondary list of Purchase Order:

sap abap consultant

Rohini kumar

sap abap consultant

Rohini kumar

sap abap consultant

The second secondary list of Purchase Order Item:

Since this is a demo system there is data discrepancy at the database level. Hence Material and storage location is blank.

Simple ALV Grid Report The SAP system contains a lot of data like purchasing, production, warehouse, accounting, financial, employee data etc. We always need to retrieve those data to review, maintain and analyze as well. In the classical report we use formatting option to have an output i.e. WRITE statement. Now SAP system provides us ALV (ALV List Viewer) report option by which we can generate smarter output. The ALV report looks far better than a classical one. We can generate the spreadsheet (excel) from the output of ALV. We can do many other operations like SUM, AVG etc. at the output screen without any coding of the main program. In ALV report we have to declare the function module “REUSE_ALV_GRID_DISPLAY” in which we just need to pass the final output table, field catalog table, event table (at the time of interactive report), layout structure and program name. There are many other options which we can pass to that function module to make our ALV report enriched. We have to call a dictionary type pool SLIS. It contains all type of declaration to generate an ALV report. We use slis_fieldcat_alv for field catalog work area and slis_t_fieldcat_alv for field catalog internal table. In the below example we have shown how to populate this internal table by appending one by one information. Now to populate the layout we use slis_layout_alv structure. After populating with required fields we pass this to the ALV grid function module. Similarly we use top of page work area by slis_listheader structure and internal table by slis_t_listheader. In ALV grid we call another function module to populate the top of page and that is “REUSE_ALV_COMMENTARY_WRITE”. We need to pass the

Rohini kumar

sap abap consultant

internal table to populate the top of page in this function module. Here the top of page subroutine is needed to pass to ALV grid function module. Another mandatory data is the program name (SY-REPID) is needed to pass to the ALV grid. In this way we can generate an ALV grid report which is basically more user friendly to produce large volume of data. Here we have demonstrated a very simple example of ALV grid report with having the output coming from only one table. REPORT

zabap_gui.

*---Declaration of Database table for its line type--------------------* TABLES: scarr. *---Calling the type pool SLIS to inherit all of its fields------------* TYPE-POOLS: slis. *------Declaring local structure---------------------------------------* TYPES: BEGIN OF ty_scarr, carrid TYPE scarr-carrid, carrname TYPE scarr-carrname, currcode TYPE scarr-currcode, END OF ty_scarr. *-----Declaring work area and internal table---------------------------* DATA: wa_scarr TYPE ty_scarr, it_scarr TYPE TABLE OF ty_scarr. DATA: *-----Declaring the field catalog work area & internal table-----------* wa_fcat TYPE slis_fieldcat_alv, it_fcat TYPE slis_t_fieldcat_alv, *-----Declaring the work area of ALV Layout----------------------------* wa_layout TYPE slis_layout_alv, *-----Declaring the work area & internal table for Top of Page---------* wa_top TYPE slis_listheader, it_top TYPE slis_t_listheader. *---Event Initialization-----------------------------------------------* INITIALIZATION. SELECT-OPTIONS: s_carrid FOR scarr-carrid. "Input selection criteria *---Event Start of Selection-------------------------------------------* START-OF-SELECTION. PERFORM get_scarr. *---Event End of Selection---------------------------------------------* END-OF-SELECTION. PERFORM alv_field_catalog. PERFORM alv_layout. PERFORM alv_grid_display. *---Event Top of Page-----------------------------------------------* TOP-OF-PAGE. PERFORM top_of_scarr.

Rohini kumar

sap abap consultant

*&---------------------------------------------------------------------* *& Form get_scarr *&---------------------------------------------------------------------* * Selection of Airline table data *----------------------------------------------------------------------* FORM get_scarr . IF s_carrid[] IS NOT INITIAL. SELECT carrid carrname currcode FROM scarr INTO TABLE it_scarr WHERE carrid IN s_carrid. IF sy-subrc = 0. SORT it_scarr. ELSE. MESSAGE 'Airline doesn''t exist' TYPE 'I'. ENDIF. ENDIF. ENDFORM. " get_scarr *&---------------------------------------------------------------------* *& Form alv_field_catalog *&---------------------------------------------------------------------* * Preparing ALV field catalog *----------------------------------------------------------------------* FORM alv_field_catalog . *----Local variable to count the column position-----------------------* DATA: lv_col TYPE i VALUE 0. IF it_scarr IS NOT INITIAL. lv_col = 1 + lv_col. wa_fcat-col_pos = lv_col. wa_fcat-fieldname = 'CARRID'. wa_fcat-tabname = 'IT_SCARR'. wa_fcat-seltext_l = 'Airline Code'. APPEND wa_fcat TO it_fcat. CLEAR wa_fcat. lv_col wa_fcat-col_pos wa_fcat-fieldname wa_fcat-tabname wa_fcat-seltext_l APPEND wa_fcat TO CLEAR wa_fcat. lv_col wa_fcat-col_pos wa_fcat-fieldname wa_fcat-tabname wa_fcat-seltext_l APPEND wa_fcat TO CLEAR wa_fcat. ENDIF.

"Column position "Technical field name "Output table name "Field text "Preparing the fieldcat table

= 1 + lv_col. = lv_col. = 'CARRNAME'. = 'IT_SCARR'. = 'Airline Name'. it_fcat. = 1 + lv_col. = lv_col. = 'CURRCODE'. = 'IT_SCARR'. = 'Local Currency'. it_fcat.

ENDFORM. " alv_field_catalog *&---------------------------------------------------------------------* *& Form alv_layout *&---------------------------------------------------------------------*

Rohini kumar

sap abap consultant

* Preparing the ALV Layout *----------------------------------------------------------------------* FORM alv_layout . wa_layout-zebra = 'X'. "Calling the Zebra layout wa_layout-colwidth_optimize = 'X'. "Width of the column is optimized ENDFORM. " alv_layout *&---------------------------------------------------------------------* *& Form top_of_scarr *&---------------------------------------------------------------------* * Preparing the Top of Page *----------------------------------------------------------------------* FORM top_of_scarr . REFRESH it_top. wa_top-typ = 'H'. "Header type wa_top-info = 'Airline List'. "Header text APPEND wa_top TO it_top. CLEAR wa_top. wa_top-typ = 'S'. "Normal line type wa_top-info = 'Report: '. "Normal line text CONCATENATE wa_top-info sy-repid INTO wa_top-info. "Concatenating the text info with program name APPEND wa_top TO it_top. CLEAR wa_top. wa_top-typ = 'S'. wa_top-info = 'User: '. CONCATENATE wa_top-info sy-uname INTO wa_top-info. "Concatenating the text info with user name APPEND wa_top TO it_top. CLEAR wa_top. *-Calling Function Module for displaying Top of Page-------------------* CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE' EXPORTING it_list_commentary = it_top "Passing the internal table * I_LOGO = * I_END_OF_LIST_GRID = * I_ALV_FORM = . ENDFORM. " top_of_scarr *&---------------------------------------------------------------------* *& Form alv_grid_display *&---------------------------------------------------------------------* * Preparing the final output by using Grid Display *----------------------------------------------------------------------* FORM alv_grid_display . IF

*

it_scarr IS NOT INITIAL AND it_fcat IS NOT INITIAL. CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' EXPORTING I_INTERFACE_CHECK = ' '

Rohini kumar

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

sap abap consultant

I_BYPASSING_BUFFER I_BUFFER_ACTIVE i_callback_program I_CALLBACK_PF_STATUS_SET I_CALLBACK_USER_COMMAND i_callback_top_of_page I_CALLBACK_HTML_TOP_OF_PAGE I_CALLBACK_HTML_END_OF_LIST I_STRUCTURE_NAME I_BACKGROUND_ID I_GRID_TITLE I_GRID_SETTINGS is_layout it_fieldcat IT_EXCLUDING IT_SPECIAL_GROUPS IT_SORT IT_FILTER IS_SEL_HIDE I_DEFAULT I_SAVE IS_VARIANT IT_EVENTS IT_EVENT_EXIT IS_PRINT IS_REPREP_ID I_SCREEN_START_COLUMN I_SCREEN_START_LINE I_SCREEN_END_COLUMN I_SCREEN_END_LINE I_HTML_HEIGHT_TOP I_HTML_HEIGHT_END IT_ALV_GRAPHICS IT_HYPERLINK IT_ADD_FIELDCAT IT_EXCEPT_QINFO IR_SALV_FULLSCREEN_ADAPTER IMPORTING E_EXIT_CAUSED_BY_CALLER ES_EXIT_CAUSED_BY_USER TABLES t_outtab EXCEPTIONS program_error OTHERS

= ' ' = ' ' = sy-repid "Program name = ' ' = ' ' = 'TOP_OF_SCARR' = ' ' = ' ' = = ' ' = = = wa_layout = it_fcat = = = = = = 'X' = ' ' = = = = = = 0 = 0 = 0 = 0 = 0 = 0 = = = = = = = = it_scarr "Final output table = 1 = 2.

IF sy-subrc 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. MESSAGE 'Report not Generated' TYPE 'I'. ENDIF. ENDIF. ENDFORM.

" alv_grid_display

Here is the output of Selection Screen:

Rohini kumar

The ALV Grid display output:

sap abap consultant

Rohini kumar

sap abap consultant

ALV Grid Report - Multiple Tables ALV Grid display looks quite different from List display. It has a grid output. Here in the Function Module REUSE_ALV_GRID_DISPLAY we have to pass program name, field catalog table and output table. We can pass layout for design purpose. For the top of page we don't have pass any event table here. We have to mention the form of top of page from the REUSE_ALV_GRID_DISPLAY function module. Inside that form we have to make internal table (say it_top) and then we have to pass that table in the function module REUSE_ALV_COMMENTARY_WRITE. Below is the Program: *&--------------------------------------------------------------------*

Rohini kumar

sap abap consultant

*& Report ZSR_TEST *& *&--------------------------------------------------------------------* *& *& *&--------------------------------------------------------------------* REPORT

zsr_test.

TABLES: mara, marc, mard, makt. TYPE-POOLS: slis. TYPES: BEGIN OF ty_mara, matnr TYPE mara-matnr, ersda TYPE mara-ersda, ernam TYPE mara-ernam, mtart TYPE mara-mtart, END OF ty_mara, BEGIN OF ty_marc, matnr TYPE marc-matnr, werks TYPE marc-werks, xchar TYPE marc-xchar, END OF ty_marc, BEGIN OF ty_mard, matnr TYPE mard-matnr, werks TYPE mard-werks, lgort TYPE mard-lgort, END OF ty_mard, BEGIN OF ty_makt, matnr TYPE makt-matnr, spras TYPE makt-spras, maktx TYPE makt-maktx, END OF ty_makt, BEGIN OF ty_out, sel, matnr TYPE mara-matnr, werks TYPE marc-werks, lgort TYPE mard-lgort, mtart TYPE mara-mtart, ersda TYPE mara-ersda, ernam TYPE mara-ernam, xchar TYPE marc-xchar, maktx TYPE makt-maktx,

Rohini kumar

sap abap consultant

END OF ty_out. DATA: wa_mara wa_marc wa_mard wa_makt wa_out it_mara it_marc it_mard it_makt it_out

TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE

DATA: wa_fcat_out it_fcat_out wa_layout wa_top it_top

ty_mara, ty_marc, ty_mard, ty_makt, ty_out, STANDARD STANDARD STANDARD STANDARD STANDARD

TYPE TYPE TYPE TYPE TYPE

TABLE TABLE TABLE TABLE TABLE

OF OF OF OF OF

ty_mara, ty_marc, ty_mard, ty_makt, ty_out.

slis_fieldcat_alv, slis_t_fieldcat_alv, slis_layout_alv, slis_listheader, slis_t_listheader.

DATA: v_prog TYPE sy-repid, v_name TYPE sy-uname, v_date TYPE char12. INITIALIZATION. v_prog = sy-repid. v_name = sy-uname. SELECTION-SCREEN 001. PARAMETERS SELECT-OPTIONS SELECTION-SCREEN

BEGIN OF BLOCK b1 WITH FRAME TITLE textp_mtart TYPE mtart OBLIGATORY. s_matnr FOR mara-matnr. END OF BLOCK b1.

START-OF-SELECTION. PERFORM get_material. PERFORM get_plant. PERFORM get_storage. PERFORM get_description. PERFORM prepare_output. END-OF-SELECTION. PERFORM prepare_fieldcat. PERFORM prepare_layout. PERFORM alv_list_display. TOP-OF-PAGE. PERFORM top_of_page. *&--------------------------------------------------------------------* *& Form GET_MATERIAL

Rohini kumar

sap abap consultant

*&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * p1 text * p1 text * p1 text * p1 text * p1 text * p1 text * p1 text * p1 text * p1 text * p1 text * P_R_UCOM text * p1 text * p1 text * P_R_UCOMM text * p1 text * p1 text * p1 text * p1 text * p1 text * p1 text * p1 text * p1 text * p1 text * p1 text * p1 text * p1 text * P_R_UCOM text * get_ekko, purchase->get_ekpo. *&--------------------------------------------------------------------* *& Module STATUS_9000 OUTPUT *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* MODULE status_9000 OUTPUT. SET PF-STATUS 'GUI_9000'. SET TITLEBAR 'TITLE_9000'. "Object creation for custom container 1 exporting the name CREATE OBJECT ob_custom1 EXPORTING container_name = 'CONTAINER1'. "Object creation for custom container 2 exporting the name CREATE OBJECT ob_custom2 EXPORTING container_name = 'CONTAINER2'. "Object creation for ALV Grid 1 exporting the parent container 1 "It means container 1 contains ALV grid 1 - header table CREATE OBJECT ob_grid1 EXPORTING i_parent = ob_custom1. "Object creation for ALV Grid 2 exporting the parent

Rohini kumar

sap abap consultant

container 2 "It means container 2 contains ALV grid 2 - item table CREATE OBJECT ob_grid2 EXPORTING i_parent = ob_custom2. *Calling the method to display the output table in ALV Grid 1. *Here field catalog and output table are passed by changing parameter. *Header table is passed here. CALL METHOD ob_grid1->set_table_for_first_display CHANGING it_fieldcatalog = it_fcat_ekko it_outtab = it_ekko. *Calling the method to display the output table in ALV Grid 2. *Here field catalog and output table are passed by changing parameter. *Item table is passed here. CALL METHOD ob_grid2->set_table_for_first_display CHANGING it_fieldcatalog = it_fcat_ekpo it_outtab = it_ekpo. ENDMODULE. " STATUS_9000 OUTPUT *&--------------------------------------------------------------------* *& Module USER_COMMAND_9000 INPUT *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* MODULE user_command_9000 INPUT. IF sy-ucomm = 'BACK' OR sy-ucomm = 'EXIT' OR sy-ucomm = 'CANCEL'. FREE: ob_grid1, ob_grid2, ob_custom1, ob_custom2. REFRESH: it_ekko, it_ekpo. LEAVE TO SCREEN 0. ENDIF. ENDMODULE.

" USER_COMMAND_9000

INPUT

Rohini kumar

The Output is as follows: Selection Screen:

PO Item wise display:

sap abap consultant

Rohini kumar

sap abap consultant

ALV Hierarchical Report We can prepare a report which can display hierarchical data. There will be a header record and hierarchically there will be item wise record. This can be done by a function module REUSE_ALV_HIERSEQ_LIST_DISPLAY.     

Prerequisite of this Function Module: This module outputs two internal tables as a formatted hierarchical-sequential list. It needs an internal table containing the set of header information to be output. It needs an internal table containing the set of item information to be output. It needs a layout structure. It needs a field catalog (Header + Item) in the form of an internal table. The field catalog describes the fields to be output in the list. REPORT

zsr_test.

*-------Declaration of table for select option-------------------------* TABLES: ekko. *----Declaration of type pools for ALV slis----------------------------* TYPE-POOLS: slis. *------Declaration of internal table structures------------------------* TYPES: BEGIN OF ty_ekko, ebeln TYPE ekko-ebeln, bukrs TYPE ekko-bukrs, aedat TYPE ekko-aedat, ernam TYPE ekko-ernam, lifnr TYPE ekko-lifnr, bedat TYPE ekko-bedat, expand, "Expand operation on header list END OF ty_ekko, BEGIN OF ty_ekpo, ebeln TYPE ekpo-ebeln, ebelp TYPE ekpo-ebelp, aedat TYPE ekpo-aedat, matkl TYPE ekpo-matkl, menge TYPE ekpo-menge, meins TYPE ekpo-meins, netpr TYPE ekpo-netpr, peinh TYPE ekpo-peinh, END OF ty_ekpo. DATA: "Internal tables it_ekko TYPE TABLE OF ty_ekko, it_ekpo TYPE TABLE OF ty_ekpo, "Field catalog work area & tables wa_fcat TYPE slis_fieldcat_alv, it_fcat TYPE slis_t_fieldcat_alv, "ALV layout work area wa_layout TYPE slis_layout_alv,

Rohini kumar

sap abap consultant

"Key information for hierarchical list wa_key TYPE slis_keyinfo_alv. *------Event Initialization--------------------------------------------* INITIALIZATION. SELECT-OPTIONS: s_ebeln FOR ekko-ebeln. *-----Event Start of Selection-----------------------------------------* START-OF-SELECTION. PERFORM get_ekko. PERFORM get_ekpo. PERFORM field_catalog. PERFORM alv_layout. PERFORM key_info. PERFORM alv_hierseq_list_display. *&---------------------------------------------------------------------* *& Form get_ekko *&---------------------------------------------------------------------* * Get data from PO header *----------------------------------------------------------------------* FORM get_ekko . IF s_ebeln[] IS NOT INITIAL. SELECT ebeln bukrs aedat ernam lifnr bedat FROM ekko INTO TABLE it_ekko WHERE ebeln IN s_ebeln. IF sy-subrc = 0. SORT it_ekko. ELSE. MESSAGE 'PO doesn''t exist' TYPE 'I'. ENDIF. ELSE. MESSAGE 'Enter a Valid PO' TYPE 'I'. ENDIF. ENDFORM. " get_ekko *&---------------------------------------------------------------------* *& Form get_ekpo *&---------------------------------------------------------------------* * Get data from PO item *----------------------------------------------------------------------* FORM get_ekpo . IF it_ekko IS NOT INITIAL. SELECT ebeln ebelp aedat matkl menge meins netpr peinh FROM ekpo INTO TABLE it_ekpo FOR ALL ENTRIES IN it_ekko WHERE ebeln = it_ekko-ebeln. IF sy-subrc = 0. SORT it_ekpo. ENDIF. ENDIF. ENDFORM. " get_ekpo *&---------------------------------------------------------------------* *& Form field_catalog

Rohini kumar

sap abap consultant

*&---------------------------------------------------------------------* * Preparing the Field catalog for header & item both in one *----------------------------------------------------------------------* FORM field_catalog. DATA lv_col TYPE i VALUE 0. IF it_ekko IS NOT INITIAL. lv_col wa_fcat-col_pos wa_fcat-fieldname wa_fcat-tabname wa_fcat-seltext_l APPEND wa_fcat TO CLEAR wa_fcat.

= lv_col + 1. = lv_col. = 'EBELN'. = 'IT_EKKO'. = 'Purchase Order'. it_fcat.

lv_col wa_fcat-col_pos wa_fcat-fieldname wa_fcat-tabname wa_fcat-seltext_l APPEND wa_fcat TO CLEAR wa_fcat.

= lv_col + 1. = lv_col. = 'BUKRS'. = 'IT_EKKO'. = 'Company Code'. it_fcat.

lv_col wa_fcat-col_pos wa_fcat-fieldname wa_fcat-tabname wa_fcat-seltext_l APPEND wa_fcat TO CLEAR wa_fcat.

= lv_col + 1. = lv_col. = 'AEDAT'. = 'IT_EKKO'. = 'Creation Date'. it_fcat.

lv_col wa_fcat-col_pos wa_fcat-fieldname wa_fcat-tabname wa_fcat-seltext_l APPEND wa_fcat TO CLEAR wa_fcat.

= lv_col + 1. = lv_col. = 'ERNAM'. = 'IT_EKKO'. = 'Created By'. it_fcat.

lv_col wa_fcat-col_pos wa_fcat-fieldname wa_fcat-tabname wa_fcat-seltext_l APPEND wa_fcat TO CLEAR wa_fcat.

= lv_col + 1. = lv_col. = 'LIFNR'. = 'IT_EKKO'. = 'Vendor'. it_fcat.

lv_col wa_fcat-col_pos wa_fcat-fieldname wa_fcat-tabname wa_fcat-seltext_l APPEND wa_fcat TO CLEAR wa_fcat. ENDIF.

= lv_col + 1. = lv_col. = 'BEDAT'. = 'IT_EKKO'. = 'Purchasing Document Date'. it_fcat.

IF it_ekpo IS NOT INITIAL.

Rohini kumar

sap abap consultant

lv_col wa_fcat-col_pos wa_fcat-fieldname wa_fcat-tabname wa_fcat-seltext_l APPEND wa_fcat TO CLEAR wa_fcat.

= lv_col + 1. = lv_col. = 'EBELN'. = 'IT_EKPO'. = 'Purchase Order'. it_fcat.

lv_col wa_fcat-col_pos wa_fcat-fieldname wa_fcat-tabname wa_fcat-seltext_l APPEND wa_fcat TO CLEAR wa_fcat.

= lv_col + 1. = lv_col. = 'EBELP'. = 'IT_EKPO'. = 'Item'. it_fcat.

lv_col wa_fcat-col_pos wa_fcat-fieldname wa_fcat-tabname wa_fcat-seltext_l APPEND wa_fcat TO CLEAR wa_fcat.

= lv_col + 1. = lv_col. = 'MATKL'. = 'IT_EKPO'. = 'Material Group'. it_fcat.

lv_col wa_fcat-col_pos wa_fcat-fieldname wa_fcat-tabname wa_fcat-seltext_l APPEND wa_fcat TO CLEAR wa_fcat.

= lv_col + 1. = lv_col. = 'AEDAT'. = 'IT_EKPO'. = 'Creation Date'. it_fcat.

lv_col wa_fcat-col_pos wa_fcat-fieldname wa_fcat-tabname wa_fcat-seltext_l APPEND wa_fcat TO CLEAR wa_fcat.

= lv_col + 1. = lv_col. = 'MENGE'. = 'IT_EKPO'. = 'PO Quantity'. it_fcat.

lv_col wa_fcat-col_pos wa_fcat-fieldname wa_fcat-tabname wa_fcat-seltext_l APPEND wa_fcat TO CLEAR wa_fcat.

= lv_col + 1. = lv_col. = 'MEINS'. = 'IT_EKPO'. = 'Unit'. it_fcat.

lv_col wa_fcat-col_pos wa_fcat-fieldname wa_fcat-tabname wa_fcat-seltext_l APPEND wa_fcat TO CLEAR wa_fcat.

= lv_col + 1. = lv_col. = 'NETPR'. = 'IT_EKPO'. = 'Net Price'. it_fcat.

lv_col wa_fcat-col_pos wa_fcat-fieldname wa_fcat-tabname

= = = =

lv_col + 1. lv_col. 'PEINH'. 'IT_EKPO'.

Rohini kumar

sap abap consultant

wa_fcat-seltext_l = 'Price Unit'. APPEND wa_fcat TO it_fcat. CLEAR wa_fcat. ENDIF. ENDFORM. "field_catalog *&---------------------------------------------------------------------* *& Form alv_layout *&---------------------------------------------------------------------* * ALV layout *----------------------------------------------------------------------* FORM alv_layout . wa_layout-zebra wa_layout-colwidth_optimize wa_layout-expand_fieldname wa_layout-window_titlebar

= = = =

'X'. "Zebra looks 'X'. "Column width optimized 'EXPAND'. "Expand operation 'Hierarchical PO Header & Item Display'.

ENDFORM. " alv_layout *&---------------------------------------------------------------------* *& Form key_info *&---------------------------------------------------------------------* * Key information which is passed to ALV *----------------------------------------------------------------------* FORM key_info. wa_key-header01 = 'EBELN'. "Purchase Order number wa_key-item01 = 'EBELN'. "is the key for header & item table ENDFORM. "key_info *&---------------------------------------------------------------------* *& Form alv_hierseq_list_display *&---------------------------------------------------------------------* * Calling the ALV Hierseq List Display *----------------------------------------------------------------------* FORM alv_hierseq_list_display . IF it_fcat IS NOT INITIAL. CALL FUNCTION 'REUSE_ALV_HIERSEQ_LIST_DISPLAY' EXPORTING i_callback_program = sy-repid is_layout = wa_layout it_fieldcat = it_fcat i_tabname_header = 'IT_EKKO' i_tabname_item = 'IT_EKPO' is_keyinfo = wa_key TABLES t_outtab_header = it_ekko t_outtab_item = it_ekpo EXCEPTIONS program_error = 1 OTHERS = 2. IF sy-subrc 0. MESSAGE 'Internal Error' TYPE 'I'. ENDIF. ENDIF. ENDFORM.

" alv_hierseq_list_display

Rohini kumar

sap abap consultant

Now execute this program.

This is the output of this.

If we click to expand then it will be looking like this. We can again click there to minimize that also.

Rohini kumar

sap abap consultant

Calling a Report from another Report with Parameter In the following example we are calling a report (zsr_test1) from another report (zsr_test). To do these functions we use SUBMIT statement in the zsr_test report like following. SUBMIT zsr_test1. This statement will let us enter directly into the zsr_test1 report. We also can pass parameter values from one program to another like following. SUBMIT zsr_test1 WITH p_text1 EQ p_text. Here p_text is the parameter declared in zsr_test report and p_text1 is the parameter declared in zsr_test1 report. We can enter our custom text into p_text and want to display by zsr_test1 report. Here the calling program will be as follows. REPORT zsr_test NO STANDARD PAGE HEADING. PARAMETERS p_text TYPE char50. SUBMIT zsr_test1 WITH p_text1 EQ p_text. Next we need to create the called program zsr_test1 as follows. REPORT

zsr_test1 NO STANDARD PAGE HEADING.

PARAMETERS p_text1 TYPE char40. DO 5 TIMES.

Rohini kumar

sap abap consultant

WRITE: / p_text1. ENDDO. Now we shall look at the debugging level step by step. At first we set the break point on the calling program zsr_test and execute.

We can see that the p_text is accessible whereas p_text1 is not. Now pressing the F5 we directly enter into the called program and then p_text1 gets accessible and populates the data.

The output is as follows.

Rohini kumar

sap abap consultant

Report to Report Calling by Select Option We already have known to call a report from another report. We have called report by using parameter values. Basically we are passing the parameter values from the calling program to the called program. Now we shall do the same thing by using select option. We know that the select option is an internal table with header line. So we need to pass the values of that internal table from calling program to called program. To pass this we have to declare another internal table of a standard structure RSPARAMS. It contains 6 fields. Those are Selname (the name of the select option), Kind (type of selection), Sign, Option, Low & High. The last four fields are same as select option. In the following example our calling program is zsr_test and the called one is zsr_test1. We are declaring a select option s_ebeln in the calling program. We also prepare the table of RSPARAMS where we are mentioning the Selname of the called program (s_ebeln1). The sign, option, low & high will be from the current select option (s_ebeln). After preparing this we call the called program by SUBMIT statement. Here we are using the RETURN clause also to get back the intermediary steps. REPORT

zsr_test NO STANDARD PAGE HEADING.

TABLES: ekko, rsparams. DATA: it_params TYPE TABLE OF rsparams, wa_params TYPE rsparams. SELECT-OPTIONS s_ebeln FOR ekko-ebeln. LOOP AT s_ebeln. wa_params-selname = wa_params-kind = wa_params-sign = wa_params-option = wa_params-low = wa_params-high = APPEND wa_params TO ENDLOOP.

'S_EBELN1'. 'S'. s_ebeln-sign. s_ebeln-option. s_ebeln-low. s_ebeln-high. it_params.

Rohini kumar

sap abap consultant

SUBMIT zsr_test1 WITH SELECTION-TABLE it_params AND RETURN. Now in the called program we also have to declare the select option of s_ebeln1. After that to fetch the data from EKKO table we mention the ABAP logic. We also have mentioned the interactive operation of double click. The purpose is to go another report when user double clicks on the PO number of the header output (zsr_test1). REPORT

zsr_test1 NO STANDARD PAGE HEADING.

TABLES ekko. TYPES: BEGIN OF ty_ekko, ebeln TYPE ekko-ebeln, bukrs TYPE ekko-bukrs, ernam TYPE ekko-ernam, lifnr TYPE ekko-lifnr, END OF ty_ekko. DATA: wa_ekko it_ekko v_field v_value

TYPE TYPE TYPE TYPE

ty_ekko, TABLE OF ty_ekko, char40, char10.

INITIALIZATION. SELECT-OPTIONS s_ebeln1 FOR ekko-ebeln. START-OF-SELECTION. PERFORM po_header. AT LINE-SELECTION. GET CURSOR FIELD v_field VALUE v_value. CASE v_field. WHEN 'WA_EKKO-EBELN'. SUBMIT zsr_test2 WITH p_ebeln EQ v_value AND RETURN. ENDCASE. *&---------------------------------------------------------------------* *& Form po_header *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* FORM po_header . IF s_ebeln1[] IS NOT INITIAL. SELECT ebeln bukrs ernam lifnr FROM ekko INTO TABLE it_ekko WHERE ebeln IN s_ebeln1. IF sy-subrc = 0. SORT it_ekko.

Rohini kumar

sap abap consultant

LOOP AT it_ekko INTO wa_ekko. AT FIRST. WRITE: /3 'Purchase Order', 20 'Company', 30 'Creator', 45 'Vendor'. ULINE. SKIP. ENDAT. WRITE: /3 20 30 45 ENDLOOP. ENDIF. ENDIF. ENDFORM.

wa_ekko-ebeln, wa_ekko-bukrs, wa_ekko-ernam, wa_ekko-lifnr.

" po_header

After that we create the third program zsr_test2 for PO item wise report. The RETURN clause will get back to every single intermediary step. REPORT

zsr_test2 NO STANDARD PAGE HEADING.

TABLES ekpo. TYPES: BEGIN OF ty_ekpo, ebeln TYPE ekpo-ebeln, ebelp TYPE ekpo-ebelp, menge TYPE ekpo-menge, meins TYPE ekpo-meins, netpr TYPE ekpo-netpr, peinh TYPE ekpo-peinh, END OF ty_ekpo. DATA: wa_ekpo TYPE ty_ekpo, it_ekpo TYPE TABLE OF ty_ekpo. INITIALIZATION. PARAMETERS p_ebeln TYPE ekpo-ebeln. START-OF-SELECTION. PERFORM get_po_item. *&---------------------------------------------------------------------* *& Form get_PO_item *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* FORM get_po_item . IF p_ebeln IS NOT INITIAL.

Rohini kumar

sap abap consultant

SELECT ebeln ebelp menge meins netpr peinh FROM ekpo INTO TABLE it_ekpo WHERE ebeln = p_ebeln. IF sy-subrc = 0. SORT it_ekpo. LOOP AT it_ekpo INTO wa_ekpo. AT FIRST. WRITE: /3 'Purchase Order', 20 'Item', 30 'PO Quantity', 44 'Unit', 49 'Net Price', 63 'Currency'. ULINE. SKIP. ENDAT. WRITE: /3 20 27 44 49 63 ENDLOOP. ENDIF. ENDIF. ENDFORM.

wa_ekpo-ebeln, wa_ekpo-ebelp, wa_ekpo-menge, wa_ekpo-meins, wa_ekpo-netpr, wa_ekpo-peinh.

" get_PO_item

Now we execute the first program zsr_test and get the following output.

Rohini kumar

The selection screen is from the zsr_test (Calling Program for Testing).

After giving the selection range we get the following output.

sap abap consultant

Rohini kumar

sap abap consultant

Now the output is coming from the called one zsr_test1 (Called program - PO header). Now we double click on the PO – 4500006374 and get the following output from another report zsr_test2 (Called Program PO Item details).

At Selection Screen event AT-SELECTION SCREEN is an event which is used in ABAP at the time of processing the selection screen. Any validation for the screen fields are done with this event. It is triggered at the run time environment during the processing of selection screen. This event occurs immediately before sending a selection screen. AT SELECTION SCREEN is triggered twice. The first time when the system calls

Rohini kumar

sap abap consultant

the selection screen, it triggers and second time when the called screen links to another screen which is basically a sub screen, it triggers again. This event is triggered internally if we don’t mention in the program. Otherwise we mention this when we need to modify the screen operation. In the following program we have used AT-SELECTION SCREEN event with an error message. Though it is an error message, we see that the fields are enabled in the output. REPORT

zsr_test.

PARAMETERS: p_matnr TYPE mara-matnr, p_werks TYPE marc-werks, p_lgort TYPE mard-lgort. AT SELECTION-SCREEN. IF p_werks IS INITIAL. MESSAGE 'Enter Plant' TYPE 'E'. ENDIF.

Now we shall modify this event with adding a clause – ON FIELD. Here we see that the declared field is enabled only and the rest of the fields are disabled. REPORT

zsr_test.

PARAMETERS: p_matnr TYPE mara-matnr, p_werks TYPE marc-werks, p_lgort TYPE mard-lgort.

Rohini kumar

sap abap consultant

AT SELECTION-SCREEN ON p_werks. IF p_werks IS INITIAL. MESSAGE 'Enter Plant' TYPE 'E'. ENDIF.

At Selection Screen Output We can change selection screen dynamically by the event AT-SELECTION SCREEN OUTPUT. Now SCREEN contains the following fields which hold screen information at run time. We can customize the screen information by looping the screen and modify the screen as required. All screen modifications should be done under the event AT-SELECTION SCREEN OUTPUT. Component

Length Type Attribute

Description

name

132

c

Name

Name

group1

3

c

Group1

Modification group

group2

3

c

Group2

Modification group

group3

3

c

Group3

Modification group

group4

3

c

Group4

Modification group

required

1

c

Required-entry field

Mandatory field

input

1

c

Input

input-enabled field

output

1

c

Output

display field

intensified

1

c

Intensified

intensified field

Rohini kumar

sap abap consultant

invisible

1

c

Invisible

invisible element

length

1

x

Visible Length

Field length

active

1

c

Input/Output/Invisible active field

display_3d

1

c

Two-dimensional

Box

value_help

1

c

Input help

Input help key

request

1

c

-

Input exists

values_in_combo 1

c

Dropdown listbox

Value help exists

In the following example we have called a selection screen where two radio buttons (PO & Item) have been mentioned. PO is selected by default. Now the screen contains four parameters (purchase order, company code, item & material). Now for default selection of PO button the purchase order & company fields are appeared. If we select item button then purchase order, item & material input fields will be displayed. REPORT

zsr_test.

PARAMETERS: po RADIOBUTTON GROUP rad1 DEFAULT 'X' USER-COMMAND test, item RADIOBUTTON GROUP rad1, p_ebeln TYPE ekko-ebeln, p_bukrs TYPE ekko-bukrs, p_ebelp TYPE ekpo-ebelp, p_matnr TYPE ekpo-matnr. AT SELECTION-SCREEN OUTPUT. LOOP AT SCREEN. "If item is selected then Item field appears IF screen-name CP '*P_EBELP*'. IF item IS NOT INITIAL. screen-active = 1. ELSE. screen-active = 0. ENDIF. MODIFY SCREEN. ENDIF. "If item is selected then Material field appears IF screen-name CP '*P_MATNR*'. IF item IS NOT INITIAL. screen-active = 1. ELSE. screen-active = 0. ENDIF. MODIFY SCREEN. ENDIF. "If item is selected then Company field disappears

Rohini kumar

sap abap consultant

IF screen-name CP '*P_BUKRS*'. IF item IS NOT INITIAL. screen-active = 0. ELSE. screen-active = 1. ENDIF. MODIFY SCREEN. ENDIF. ENDLOOP.

Editable ALV Report We can edit an ALV report on the screen and then we can download it as per requirement. Here we are discussing about two ways to edit an ALV report. 1. The first way is to use the field catalog manually and there we can active the EDIT field of field catalog. In this way we can manually select the fields which need to be edited. The statement is as follows.

Rohini kumar

sap abap consultant

lv_col wa_fcat-col_pos wa_fcat-fieldname wa_fcat-tabname wa_fcat-edit wa_fcat-seltext_l APPEND wa_fcat TO CLEAR wa_fcat. 2.

= 1 + lv_col. = lv_col. = 'CURRCODE'. = 'IT_SCARR'. = 'X'. "Editable field = 'Local currency of airline'. it_fcat.

Second way is to active the EDIT field of layout. This way gets the whole report editable. We can edit each and every field. Statement is as follows. wa_layout-colwidth_optimize = 'X'. wa_layout-edit = 'X'. "This activation will let all fields editable

In the following example we have demonstrated the first approach where we are manually using the editable fields by field catalog. REPORT

zsr_test1 NO STANDARD PAGE HEADING.

TABLES scarr. TYPE-POOLS slis. DATA: wa_title TYPE lvc_title VALUE 'Editable ALV Report', "This will show the report title it_scarr TYPE TABLE OF scarr, wa_fcat it_fcat

TYPE slis_fieldcat_alv, TYPE slis_t_fieldcat_alv,

wa_layout TYPE slis_layout_alv, wa_top it_top

TYPE slis_listheader, TYPE slis_t_listheader.

INITIALIZATION. SELECT-OPTIONS s_carrid FOR scarr-carrid. START-OF-SELECTION. PERFORM get_flight. "Get data PERFORM field_catalog. "Creating PERFORM layout. "Creating PERFORM alv_grid_display."ALV grid TOP-OF-PAGE. PERFORM top_of_page.

from database table the field catalog manually the layout output

"Top of page of editable ALV

*&---------------------------------------------------------------------*

Rohini kumar

sap abap consultant

*& Form get_flight *&---------------------------------------------------------------------* * Get data from database table *----------------------------------------------------------------------* FORM get_flight . IF s_carrid[] IS NOT INITIAL. SELECT * FROM scarr INTO TABLE it_scarr WHERE carrid IN s_carrid. IF sy-subrc = 0. SORT it_scarr. ENDIF. ENDIF. ENDFORM. " get_flight *&---------------------------------------------------------------------* *& Form field_catalog *&---------------------------------------------------------------------* * Creating the field catalog manually *----------------------------------------------------------------------* FORM field_catalog . DATA lv_col TYPE i VALUE 0. IF it_scarr IS NOT INITIAL. lv_col = 1 + lv_col. wa_fcat-col_pos = lv_col. wa_fcat-fieldname = 'CARRID'. wa_fcat-tabname = 'IT_SCARR'. wa_fcat-seltext_l = 'Airline Code'. APPEND wa_fcat TO it_fcat. CLEAR wa_fcat. lv_col wa_fcat-col_pos wa_fcat-fieldname wa_fcat-tabname wa_fcat-seltext_l APPEND wa_fcat TO CLEAR wa_fcat.

= 1 + lv_col. = lv_col. = 'CARRNAME'. = 'IT_SCARR'. = 'Airline Name'. it_fcat.

lv_col wa_fcat-col_pos wa_fcat-fieldname wa_fcat-tabname wa_fcat-edit wa_fcat-seltext_l APPEND wa_fcat TO CLEAR wa_fcat.

= 1 + lv_col. = lv_col. = 'CURRCODE'. = 'IT_SCARR'. = 'X'. "Editable field = 'Local currency of airline'. it_fcat.

Rohini kumar

lv_col wa_fcat-col_pos wa_fcat-fieldname wa_fcat-tabname wa_fcat-edit wa_fcat-seltext_l APPEND wa_fcat TO CLEAR wa_fcat. ENDIF.

sap abap consultant

= 1 + lv_col. = lv_col. = 'URL'. = 'IT_SCARR'. = 'X'. = 'Airline URL'. it_fcat.

"Editable field

ENDFORM. " field_catalog *&---------------------------------------------------------------------* *& Form layout *&---------------------------------------------------------------------* * Creating the layout *----------------------------------------------------------------------* FORM layout . wa_layout-colwidth_optimize = 'X'. wa_layout-zebra = 'X'. ENDFORM. " layout *&---------------------------------------------------------------------* *& Form alv_grid_display *&---------------------------------------------------------------------* * ALV grid output *----------------------------------------------------------------------* FORM alv_grid_display . IF it_fcat IS NOT INITIAL. CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' EXPORTING i_callback_program = sy-repid i_callback_top_of_page = 'TOP_OF_PAGE' i_grid_title = wa_title is_layout = wa_layout it_fieldcat = it_fcat TABLES t_outtab = it_scarr EXCEPTIONS program_error = 1 OTHERS = 2. ENDIF. ENDFORM.

" alv_grid_display

*&---------------------------------------------------------------------* *& Form top_of_page *&---------------------------------------------------------------------*

Rohini kumar

sap abap consultant

* Top of page of editable ALV *----------------------------------------------------------------------* FORM top_of_page. DATA date TYPE char10. CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL' EXPORTING date_internal = sy-datum IMPORTING date_external = date EXCEPTIONS date_internal_is_invalid = 1 OTHERS = 2. REFRESH it_top. wa_top-typ = 'H'. wa_top-info = 'Airline Information'. APPEND wa_top TO it_top. CLEAR wa_top. wa_top-typ = 'S'. wa_top-info = 'Date: '. CONCATENATE wa_top-info date INTO wa_top-info. APPEND wa_top TO it_top. CLEAR wa_top. CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE' EXPORTING it_list_commentary = it_top. ENDFORM. The output is as follows.

The basic output.

"top_of_page

Rohini kumar

Now we can edit those two fields as per requirement.

sap abap consultant

Rohini kumar

sap abap consultant

Now we are using the second approach where we need to activate the EDIT field of layout. With this approach the whole report will be editable. REPORT

zsr_test1 NO STANDARD PAGE HEADING.

TABLES scarr. TYPE-POOLS slis. DATA: wa_title TYPE lvc_title VALUE 'Editable ALV Report', "This will show the report title it_scarr TYPE TABLE OF scarr, it_fcat TYPE slis_t_fieldcat_alv, wa_layout TYPE slis_layout_alv, wa_top

TYPE slis_listheader,

Rohini kumar

it_top

sap abap consultant

TYPE slis_t_listheader.

INITIALIZATION. SELECT-OPTIONS s_carrid FOR scarr-carrid. START-OF-SELECTION. PERFORM get_flight. "Get data PERFORM field_catalog. "Creating PERFORM layout. "Creating PERFORM alv_grid_display."ALV grid

from database table the field catalog the layout output

TOP-OF-PAGE. PERFORM top_of_page. "Top of page of editable ALV *&---------------------------------------------------------------------* *& Form get_flight *&---------------------------------------------------------------------* * Get data from database table *----------------------------------------------------------------------* FORM get_flight . IF s_carrid[] IS NOT INITIAL. SELECT * FROM scarr INTO TABLE it_scarr WHERE carrid IN s_carrid. IF sy-subrc = 0. SORT it_scarr. ENDIF. ENDIF. ENDFORM. " get_flight *&---------------------------------------------------------------------* *& Form field_catalog *&---------------------------------------------------------------------* * Creating the field catalog *----------------------------------------------------------------------* FORM field_catalog . IF it_scarr IS NOT INITIAL. "Since the whole structure is involved, we use field catalog merge "to create the field catalog CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE' EXPORTING i_program_name = sy-repid i_internal_tabname = 'IT_SCARR' i_structure_name = 'SCARR' CHANGING ct_fieldcat = it_fcat EXCEPTIONS

Rohini kumar

sap abap consultant

inconsistent_interface = 1 program_error = 2 OTHERS = 3. IF sy-subrc 0. MESSAGE 'Internal error of field catalog merge' TYPE 'I'. ENDIF. ENDIF. ENDFORM. " field_catalog *&---------------------------------------------------------------------* *& Form layout *&---------------------------------------------------------------------* * Creating the layout *----------------------------------------------------------------------* FORM layout . wa_layout-colwidth_optimize = 'X'. wa_layout-edit = 'X'. "This activation will let all fields editable ENDFORM. " layout *&---------------------------------------------------------------------* *& Form alv_grid_display *&---------------------------------------------------------------------* * ALV grid output *----------------------------------------------------------------------* FORM alv_grid_display . IF it_fcat IS NOT INITIAL. CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' EXPORTING i_callback_program = sy-repid i_callback_top_of_page = 'TOP_OF_PAGE' i_grid_title = wa_title is_layout = wa_layout it_fieldcat = it_fcat TABLES t_outtab = it_scarr EXCEPTIONS program_error = 1 OTHERS = 2. ENDIF. ENDFORM.

" alv_grid_display

*&---------------------------------------------------------------------* *& Form top_of_page *&---------------------------------------------------------------------* * Top of page of editable ALV *----------------------------------------------------------------------*

Rohini kumar

sap abap consultant

FORM top_of_page. DATA date TYPE char10. CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL' EXPORTING date_internal = sy-datum IMPORTING date_external = date EXCEPTIONS date_internal_is_invalid = 1 OTHERS = 2. REFRESH it_top. wa_top-typ = 'H'. wa_top-info = 'Airline Information'. APPEND wa_top TO it_top. CLEAR wa_top. wa_top-typ = 'S'. wa_top-info = 'Date: '. CONCATENATE wa_top-info date INTO wa_top-info. APPEND wa_top TO it_top. CLEAR wa_top. CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE' EXPORTING it_list_commentary = it_top. ENDFORM.

"top_of_page

Below is the output where we can see every field is available to edit.

Rohini kumar

We have edited one as per requirement.

sap abap consultant

Rohini kumar

sap abap consultant

Check Boxes and Push Buttons Module pool program has different modules which contains different logic for different screen. Module pool program can be called dialog programming. Any number of screens can be developed by module pool programming. Every screen has a logic which runs at the back end. Module pool program can be executed by Transaction Code only. Here we have created a program which contains two screens. Initial screen contains an input field and some check boxes. In the input field we are getting the Customer number manually. We have three check boxes. The box which is clicked will be shown on the second screen. That means

Rohini kumar

sap abap consultant

if we check Name and Country boxes then on the second screen we shall see only the customer name and country code with the customer number. Two push buttons are there and these two buttons have separate functionality for the data processing. Push button DISPLAY only displays the entered customer details and CANCEL will clear the initial screen. Below is the logic which is written according to the Includes. *&--------------------------------------------------------------------* *& Module Pool ZSR_MOD *& *&--------------------------------------------------------------------* *& *& *&--------------------------------------------------------------------* INCLUDE Data INCLUDE Modules INCLUDE Modules INCLUDE Routines

ZSR_TOP

.

" global

ZSR_O01

.

" PBO-

ZSR_I01

.

" PAI-

ZSR_F01

.

" FORM-

*&--------------------------------------------------------------------* *& Include ZSR_TOP Module Pool ZSR_MOD *& *&--------------------------------------------------------------------* PROGRAM

zsr_mod.

TABLES: kna1. TYPES: BEGIN OF ty_kna1, kunnr TYPE kna1-kunnr, land1 TYPE kna1-land1, name1 TYPE kna1-name1, ort01 TYPE kna1-ort01, END OF ty_kna1. DATA: wa_kna1 TYPE ty_kna1,

Rohini kumar

sap abap consultant

it_kna1 TYPE STANDARD TABLE OF ty_kna1, ok_code TYPE sy-ucomm, ok_code2 TYPE sy-ucomm, name TYPE c, country TYPE c, city TYPE c. *&--------------------------------------------------------------------* *& Include ZSR_O01 *&--------------------------------------------------------------------* *&--------------------------------------------------------------------* *& Module STATUS_9001 OUTPUT *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* MODULE status_9001 OUTPUT. SET PF-STATUS 'GUI_9001'. * SET TITLEBAR 'xxx'. ENDMODULE. " STATUS_9001 OUTPUT *&--------------------------------------------------------------------* *& Module STATUS_9002 OUTPUT *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* MODULE status_9002 OUTPUT. SET PF-STATUS 'GUI_9002'. * SET TITLEBAR 'xxx'. PERFORM process_list. ENDMODULE.

" STATUS_9002

OUTPUT

*&--------------------------------------------------------------------* *& Include ZSR_I01 *&--------------------------------------------------------------------* *&--------------------------------------------------------------------* *& Module USER_COMMAND_9001 INPUT

Rohini kumar

sap abap consultant

*&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* MODULE user_command_9001 INPUT. CASE ok_code. WHEN 'BACK'. PERFORM leave. WHEN 'EXIT'. PERFORM leave. WHEN 'CANCEL'. PERFORM leave. WHEN 'DISP'. PERFORM display. WHEN 'CANC'. PERFORM cancel. ENDCASE. ENDMODULE. " USER_COMMAND_9001 INPUT *&--------------------------------------------------------------------* *& Module USER_COMMAND_9002 INPUT *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* MODULE user_command_9002 INPUT. CASE ok_code2. WHEN 'BACK2'. PERFORM leave2. WHEN 'EXIT2'. PERFORM leave2. WHEN 'CANCEL2'. PERFORM leave2. ENDCASE. ENDMODULE.

" USER_COMMAND_9002

INPUT

*&--------------------------------------------------------------------* *& Include ZSR_F01 *&--------------------------------------------------------------------* *&--------------------------------------------------------------------* *& Form LEAVE

Rohini kumar

sap abap consultant

*&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * p1 text * p1 text * p1 text * p1 text * p1 text

Rohini kumar

sap abap consultant

* refresh_table_display, "FREE another method obj_cust_proj->free, obj_cust_prps->free. CLEAR: s_pspid, p_vbukr. REFRESH: it_proj, it_prps, s_pspid[], it_fcat_proj, it_fcat_prps. CLEAR: ok_code_sel, ok_code_pro. LEAVE TO SCREEN 0. ENDFORM. " LEAVE_ALV *&--------------------------------------------------------------------* *& Form CREATE_OBJECT *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * set_table_for_first_display EXPORTING i_buffer_active = i_bypassing_buffer = i_consistency_check = i_structure_name = is_variant = i_save = i_default = 'x' is_layout = wa_layout is_print = it_special_groups = it_toolbar_excluding = it_hyperlink = it_alv_graphics = it_except_qinfo = ir_salv_adapter = CHANGING it_outtab = it_prps it_fieldcatalog = it_fcat_prps it_sort = it_filter = EXCEPTIONS invalid_parameter_combination = 1 program_error = 2 too_many_lines = 3 OTHERS = 4.

ENDFORM. " GRID_METHOD *&--------------------------------------------------------------------* *& Form ALV_LAYOUT *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * Copy from client.

Rohini kumar

sap abap consultant

Step 9: We maintain the naming convention here as follows. We also can see the source client here.

Step 10: Save it and maintain a transport request.

Rohini kumar

Step 11: Now the form has been copied between clients.

sap abap consultant

Rohini kumar

sap abap consultant

Step 12: Now we can edit the custom form from SE71. Here we have to maintain the source language by which the form was created. For this form the source language was German (DE).

Rohini kumar

Step 13: Go to Page window > Main window.

Step 14: We want to create a Graphic here.

sap abap consultant

Rohini kumar

Step 15: Select the required Graphic and activate the form.

sap abap consultant

Rohini kumar

sap abap consultant

Step 16: Now go to the NACE again and the processing routines. Enter the newly updated custom form name.

Step 17: To check this go to VF03 - Billing document display.

Rohini kumar

Step 18: Enter a billing document number.

Step 19: Go to Billing Document > Issue output to.

sap abap consultant

Rohini kumar

sap abap consultant

Step 20: Select the invoice and click on the print preview.

Step 21: Finally we can see the form output which now contains the Graphic.

Rohini kumar

sap abap consultant

Convert SAP Script to PDF We can convert the script to PDF by using the standard program RSTXPDFT4. We are taking the previous script and perform the following steps. Step 1: Go to print program and execute it. We shall have a pop up window for output device. Click on the Print button there.

Rohini kumar

sap abap consultant

Step 2: This print request has been stored in spool. Now go to SP02 - list of spool request.

Step 3: Now we can see the spool request for the script has been updated in the list. Copy the spool request number - 10700 for this example.

Rohini kumar

sap abap consultant

Step 4: Go to SE38 and execute the standard program RSTXPDFT4.

Step 5: Enter the spool request, check the download PDF option and mention the path where the PDF needs to be stored.

Rohini kumar

Step 6: Browse the path with the proper file name and click on Save.

Step 7: Now the PDF has been stored into the entered location.

Step 8: Open the PDF.

sap abap consultant

Rohini kumar

sap abap consultant

Convert SAP Script to Text file We can convert SAP script to a normal text file. Sometimes we need to dump a form and then do several operations into the system. If the form gets corrupted then we can upload the dump form into the SAP system. With the help of RSTXSCRP we can export form to text file and also import the text file to the form.

Rohini kumar

sap abap consultant

Step 1: Go to SE38 and execute the program RSTXSCRP.

Step 2: Select the form name and mode. Export for download and Import for upload.

Step 3: Mention the path and file name then Save.

Rohini kumar

Step 4: Now the form has been exported to text file.

Step 5: We can see the text file also.

sap abap consultant

Rohini kumar

sap abap consultant

BAPI Concept

The full form of BAPI is Business Application Programming Interface. BAPI is an application interface program by which we can meet business function. Now interface is a program or a way or a process or something by which two different systems or applications can flow data smoothly. It means there must be a proper communication between those systems or applications. In SAP system we call BAPI as a function module which is remotely enabled. It means we can call BAPI by ABAP programs from the same system or any outside system also. Now BAPI always hits Business Object Repository (BOR) to meet the business function. There are lots of business object in the SAP system like customer, order, invoice, purchase etc. These objects are stored in BOR. We can use T-code SW01 to open business object builder. Business Object Repository (BOR) contains business objects in the SAP system. Technically we can say that a business object is like a class and it contains many methods which are BAPIs. Hence the class which is business object corresponds to an SAP table or a table hierarchy and we can access those tables by calling the methods of that class – BAPIs. Difference between BAPI & RFC:

Rohini kumar

sap abap consultant

Hence we can say that BAPI is remote function module which deals with business related data through BOR. So BAPI is a part of remote function module which hits BOR and any other kind of remote function which doesn’t hit BOR is not BAPI. RFC is a protocol, basically the interface protocol by which remote functionality works. Hence BAPIs and remote function module use this protocol to connect between two SAP or non-SAP systems. We have a lot of standard BAPIs into the SAP system. Some of those are as follows. GetList – Returns a list of available objects which meet the specified selection criteria. GetDetail – Returns the detailed information for an object. Here the complete key must be specified. Create, Change, Delete, Cancel – It allows us to do that. AddItem, RemoveItem – It allows us to do that. We can use T-code BAPI to go to the BAPI Explorer. From there we can navigate to the exact BAPI and we can enter to the Function Builder to view that BAPI by double clicking on that.

1. 2. 3. 4. 5.

Properties of BAPI: Naming convention BAPI__ BAPI is always remote enabled function module It contains neither user dialogs nor messages BAPI raise no exception Return parameter which is the export parameter is mandatory for BAPI to report the errors because BAPI doesn't return any sy-subrc value.

Object Oriented Classic Program for Single Table Class is the type of object. We can say that the class is a template of object. It is the abstract description of object. The components of the class define the attributes of the object. In ABAP we have two types of classes - Global class and Local class. Global class is defined in the Class builder (Transaction SE24). We can create and define a global class here. All ABAP programs can access Global class. Local classes are defined in local program. That program only accesses these classes. When we use class in our program the system searches that class locally at first. If it doesn’t find this one then it looks for global classes. 

Class contains components / attributes.

Rohini kumar

 

sap abap consultant

Each component is assigned to a visibility section (public / protected / private). Class implements methods.

Class Components: Any variable, constant, structure, work area, table declared inside the class are called class components. Methods are also treated as components of that class. This declaration must be defined under any of its visibility section. Hence according to the visibility the components will be available to the users of the class. Visibility Section: It defines the interface between the class and its users. The three visibility sections are as follows. Public Section – All users can access any components, methods declared in public section. Hence the components can be accessed by the same class or any of its subclasses or any other classes defined in the same program. Public components form an interface between the class and its users. Protected Section – Components and methods declared in the protected section can be accessed to all methods of this class and its subclasses which are inherited from it. Protected components form a special interface between the class and its inherited subclasses. Private Section – Components and methods declared in the private section of a class can only be accessed by that class. Private components don’t form any external interface. Here is a program which shows a list of data from a single table MARA based on the input on Selection Screen. Selection screen contains a select-option and the pattern is block b1. We are displaying 4 fields of MARA like Material, Date, Name & Type. Structure of internal table and the declaration of internal table are in the Public section of the class so that every attributes and methods can have the access of that. In the implementation part we are selecting fields from MARA and writing the desired output. We are creating instance under the START-OF-SELECTION event and calling the implemented method. Here the object must be reference to the class. REPORT

zsr_test.

*-------Declaration of Table for Select Option-------------------------* TABLES: mara. *----Event Initialization----------------------------------------------* INITIALIZATION. *-------Selection Screen with B1 block---------------------------------* SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.

Rohini kumar

sap abap consultant

"Mandatory Selection range SELECT-OPTIONS s_matnr FOR mara-matnr OBLIGATORY. SELECTION-SCREEN END OF BLOCK b1. *----------------------------------------------------------------------* * CLASS cls1 DEFINITION *----------------------------------------------------------------------* * Definition of Class *----------------------------------------------------------------------* CLASS cls1 DEFINITION. PUBLIC SECTION. "Public section property "Internal table structure TYPES: BEGIN OF ty_mara, matnr TYPE mara-matnr, ersda TYPE mara-ersda, ernam TYPE mara-ernam, mtart TYPE mara-mtart, END OF ty_mara. "Declaration of work area & internal table DATA: wa_mara TYPE ty_mara, it_mara TYPE STANDARD TABLE OF ty_mara. "Declaring Methods METHODS: met1. PROTECTED SECTION. PRIVATE SECTION.

"Protected section property "Private section property

ENDCLASS.

"cls1 DEFINITION

*----------------------------------------------------------------------* * CLASS cls1 IMPLEMENTATION *----------------------------------------------------------------------* * Class Implementation - method is defined here *----------------------------------------------------------------------* CLASS cls1 IMPLEMENTATION. "Implementing the method METHOD met1. SELECT matnr ersda ernam mtart FROM mara INTO TABLE it_mara WHERE matnr IN s_matnr. IF sy-subrc = 0. SORT it_mara BY matnr. LOOP AT it_mara INTO wa_mara. AT FIRST. WRITE: /3 'MATERIAL', 20 'DATE', 33 'NAME', 43 'TYPE'. ULINE. SKIP. ENDAT. WRITE: / wa_mara-matnr, wa_mara-ersda, wa_mara-ernam, wa_mara-mtart. ENDLOOP.

Rohini kumar

sap abap consultant

ELSE. MESSAGE 'No data found' TYPE 'I'. ENDIF. ENDMETHOD. ENDCLASS.

"met1 "cls1 IMPLEMENTATION

*-----Event Start of Selection-----------------------------------------* START-OF-SELECTION. "Declaring object / instance referenced to the class DATA: obj1 TYPE REF TO cls1. "Creating the object CREATE OBJECT: obj1. "Calling the method by object CALL METHOD: obj1->met1.

Ouput of Selection Screen:

By giving the proper value the List output is: Here Material No has been selected from 601010007 to 601010050. Since 601010050 is not in the database the last Material no is 601010049.

Rohini kumar

sap abap consultant

Object Oriented Classic for Multiple Tables We can have data from multiple tables. In object oriented approach those multiple internal tables have to be structured in the class definition part. In the implementation we can use multiple methods to select each database table. Here we can use "for all entries in" concept also. We are preparing the output table and output format in different methods. After that in start of selection event we can call them as well. Here is a program by which we can fetch the data from MARA table based on the Material Number selected in the Selection Screen. Then we can find out the Plant and Storage Location details from MARC and MARD table based on the material in the selection screen. These MARC and MARD table data will be selected FOR ALL ENTRIES IN selected MARA table.

Rohini kumar

REPORT

sap abap consultant

zsr_test.

*-------Declaration of Table for Select Option-------------------------* TABLES: mara. *----Event Initialization----------------------------------------------* INITIALIZATION. *----Selection screen with B1 block------------------------------------* SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001. "Mandatory selection range SELECT-OPTIONS s_matnr FOR mara-matnr OBLIGATORY. SELECTION-SCREEN END OF BLOCK b1. *----------------------------------------------------------------------* * CLASS cls1 DEFINITION *----------------------------------------------------------------------* * Class Definition *----------------------------------------------------------------------* CLASS cls1 DEFINITION. PUBLIC SECTION. "Public section property "Internal table structure TYPES: BEGIN OF ty_mara, matnr TYPE mara-matnr, ernam TYPE mara-ernam, mtart TYPE mara-mtart, END OF ty_mara, BEGIN OF ty_marc, matnr TYPE marc-matnr, werks TYPE marc-werks, END OF ty_marc, BEGIN OF ty_mard, matnr TYPE mard-matnr, werks TYPE mard-werks, lgort TYPE mard-lgort, END OF ty_mard, BEGIN OF ty_out1, matnr TYPE mara-matnr, ernam TYPE mara-ernam, mtart TYPE mara-mtart, werks TYPE mard-werks, lgort TYPE mard-lgort, END OF ty_out1. "Declaration of work area & DATA: wa_mara TYPE ty_mara, it_mara TYPE STANDARD wa_marc TYPE ty_marc, it_marc TYPE STANDARD wa_mard TYPE ty_mard, it_mard TYPE STANDARD wa_out1 TYPE ty_out1, it_out1 TYPE STANDARD "Declaration of methods

internal table TABLE OF ty_mara, TABLE OF ty_marc, TABLE OF ty_mard, TABLE OF ty_out1.

Rohini kumar

sap abap consultant

METHODS: m_mara, m_marc, m_mard, m_out1, m_display1. PROTECTED SECTION. "Protected section property PRIVATE SECTION. "Private section property ENDCLASS.

"cls1 DEFINITION

*----------------------------------------------------------------------* * CLASS cls1 IMPLEMENTATION *----------------------------------------------------------------------* * Class Implementation *----------------------------------------------------------------------* CLASS cls1 IMPLEMENTATION. "Implementing m_mara METHOD m_mara. SELECT matnr ernam mtart FROM mara INTO TABLE it_mara WHERE matnr IN s_matnr. IF sy-subrc = 0. SORT it_mara BY matnr. ELSE. MESSAGE 'No data found' TYPE 'I'. ENDIF. ENDMETHOD.

"m_mara

"Implementing m_marc METHOD m_marc. "Prerequisite of For all entries IF it_mara IS NOT INITIAL. SELECT matnr werks FROM marc INTO TABLE it_marc FOR ALL ENTRIES IN it_mara WHERE matnr = it_mara-matnr. IF sy-subrc = 0. SORT it_marc BY matnr. ENDIF. ENDIF. ENDMETHOD.

"m_marc

"Implementing m_mard METHOD m_mard. "Prerequisite of For all entries IF it_mara IS NOT INITIAL. SELECT matnr werks lgort FROM mard INTO TABLE it_mard FOR ALL ENTRIES IN it_mara WHERE matnr = it_mara-matnr. IF sy-subrc = 0. SORT it_mard BY matnr. ENDIF.

Rohini kumar

sap abap consultant

ENDIF. ENDMETHOD.

"m_mard

"Implementing m_out1 - Output table METHOD m_out1. IF it_marc IS NOT INITIAL. "Looping at plant table LOOP AT it_marc INTO wa_marc. wa_out1-werks = wa_marc-werks. "Looping at storage table - loop inside loop with where clause LOOP AT it_mard INTO wa_mard WHERE matnr = wa_marc-matnr. wa_out1-lgort = wa_mard-lgort. "Reading the master table since it has unique material READ TABLE it_mara INTO wa_mara WITH KEY matnr = wa_mard-matnr BINARY SEARCH. IF sy-subrc = 0. wa_out1-matnr = wa_mara-matnr. wa_out1-ernam = wa_mara-ernam. wa_out1-mtart = wa_mara-mtart. "Appending the output table APPEND wa_out1 TO it_out1. CLEAR: wa_out1, wa_mara, wa_marc, wa_mard. ENDIF. ENDLOOP. ENDLOOP. ENDIF. ENDMETHOD. "Implementing m_display1 METHOD m_display1. IF it_out1 IS NOT INITIAL. LOOP AT it_out1 INTO wa_out1. AT FIRST. "Control break statement WRITE: /3 'MATERIAL', 21 'NAME', 36 'TYPE', 48 'PLANT', 56 'STORAGE LOCATION'. ULINE. SKIP. ENDAT. WRITE: /3 wa_out1-matnr, 21 wa_out1-ernam, 36 wa_out1-mtart, 48 wa_out1-werks, 56 wa_out1-lgort. ENDLOOP. CLEAR wa_out1. ENDIF.

"m_out1

Rohini kumar

sap abap consultant

ENDMETHOD. ENDCLASS.

"m_display1 "cls1 IMPLEMENTATION

*-----Event Start of Selection-----------------------------------------* START-OF-SELECTION. "Declaring the object / instance referenced to class DATA: obj1 TYPE REF TO cls1. "Creating the object CREATE OBJECT: obj1. "Calling the methods by object CALL METHOD: obj1->m_mara, obj1->m_marc, obj1->m_mard, obj1->m_out1, obj1->m_display1.

The Selection Screen:

The Output is:

Rohini kumar

sap abap consultant

Purchase Order Header and Item Here is a program by which Purchase Order Header and Item are displayed separately by classic report format. We are taking proper Purchase Order (EBELN) from Selection Screen with Select Options and displaying the output by using two separated methods. REPORT

zsr_test NO STANDARD PAGE HEADING.

*-----Declaring tables for Select Option-------------------------------* TABLES: ekko. *-----Event Initialization---------------------------------------------* INITIALIZATION. SELECT-OPTIONS: s_ebeln FOR ekko-ebeln. *----------------------------------------------------------------------* * CLASS header DEFINITION *----------------------------------------------------------------------* * PO Header class definition *----------------------------------------------------------------------* CLASS header DEFINITION.

Rohini kumar

sap abap consultant

PUBLIC SECTION. "Declaring structures for work area & internal tables TYPES: BEGIN OF ty_ekko, ebeln TYPE ekko-ebeln, bukrs TYPE ekko-bukrs, aedat TYPE ekko-aedat, ernam TYPE ekko-ernam, END OF ty_ekko, BEGIN OF ty_ekpo, ebeln TYPE ekpo-ebeln, ebelp TYPE ekpo-ebelp, menge TYPE ekpo-menge, meins TYPE ekpo-meins, netpr TYPE ekpo-netpr, END OF ty_ekpo. "Declaring work areas & internal tables DATA: wa_ekko TYPE ty_ekko, it_ekko TYPE TABLE OF ty_ekko, wa_ekpo TYPE ty_ekpo, it_ekpo TYPE TABLE OF ty_ekpo, v_flag TYPE c. "Declaring methods METHODS: m_ekko, m_ekko_output, m_ekpo, m_ekpo_output. PROTECTED SECTION. PRIVATE SECTION. ENDCLASS.

"header DEFINITION

*----------------------------------------------------------------------* * CLASS header IMPLEMENTATION *----------------------------------------------------------------------* * PO Header class implementation *----------------------------------------------------------------------* CLASS header IMPLEMENTATION. "Method to select data from header table METHOD m_ekko. IF s_ebeln IS NOT INITIAL. SELECT ebeln bukrs aedat ernam FROM ekko INTO TABLE it_ekko WHERE ebeln IN s_ebeln. IF sy-subrc = 0. SORT it_ekko. ENDIF. ENDIF. ENDMETHOD.

"m_ekko

"Method to select data from item table METHOD m_ekpo. IF it_ekko IS NOT INITIAL. SELECT ebeln ebelp menge meins netpr FROM ekpo INTO TABLE it_ekpo FOR ALL ENTRIES IN it_ekko

Rohini kumar

sap abap consultant

WHERE ebeln = it_ekko-ebeln. IF sy-subrc = 0. SORT it_ekpo. ENDIF. ENDIF. ENDMETHOD.

"m_ekpo

"Method to display header output METHOD m_ekko_output. IF it_ekko IS NOT INITIAL. LOOP AT it_ekko INTO wa_ekko. AT FIRST. WRITE: 'Purchase Order' COLOR 1, 20 'Company Code' COLOR 1, 35 'Creation Date' COLOR 1, 50 'Created by' COLOR 1. ULINE. ENDAT. WRITE: / 20 35 50

wa_ekko-ebeln, wa_ekko-bukrs, wa_ekko-aedat, wa_ekko-ernam.

AT LAST. WRITE: / '~~~~~~~~~~~End of PO Header~~~~~~~~~~~'. SKIP. ENDAT. ENDLOOP. ENDIF. ENDMETHOD. "m_ekko_output "Method to display item wise output METHOD m_ekpo_output. IF it_ekpo IS NOT INITIAL. LOOP AT it_ekpo INTO wa_ekpo. AT FIRST. WRITE: / 'Purchase Order' COLOR 1, 20 'Item' COLOR 1, 35 'Quantity' COLOR 1, 45 'Unit' COLOR 1, 54 'Net Price' COLOR 1. ULINE. ENDAT. AT NEW ebeln. v_flag = 'X'. ENDAT. IF v_flag = 'X'. WRITE: / wa_ekpo-ebeln, 20 wa_ekpo-ebelp, 27 wa_ekpo-menge, 45 wa_ekpo-meins, 50 wa_ekpo-netpr. ELSE. WRITE: /20 wa_ekpo-ebelp, 27 wa_ekpo-menge, 45 wa_ekpo-meins,

Rohini kumar

sap abap consultant

50 wa_ekpo-netpr. ENDIF. AT END OF ebeln. SUM. WRITE: /27 '=================', 50 '=============='. WRITE: / 'Sub Total: ' COLOR 5, 27 wa_ekpo-menge, 50 wa_ekpo-netpr. SKIP. ENDAT. AT LAST. SUM. ULINE. WRITE: /

'Grand Total: ' COLOR 3, 27 wa_ekpo-menge, 50 wa_ekpo-netpr.

ENDAT. CLEAR: v_flag, wa_ekpo. ENDLOOP. ENDIF. ENDMETHOD. "m_ekpo_output ENDCLASS. "header IMPLEMENTATION *-----Event Start of Selection-----------------------------------------* START-OF-SELECTION. "Declaring object with the class type reference DATA: obj_header TYPE REF TO header. "Creating object CREATE OBJECT: obj_header. "Calling the methods via created object CALL METHOD: obj_header->m_ekko, obj_header->m_ekko_output, obj_header->m_ekpo, obj_header->m_ekpo_output.

Selection Screen:

The output is as follows:

Rohini kumar

sap abap consultant

Posted by SANDIP ROY at 2/11/2013 02:52:00 PM Email This

ALV Block List using OOPs The following program can show the list of Purchase Order Header and Item separately by using ALV Block List Display function module. Here the Programming concept has been done by OOPs. *&--------------------------------------------------------------------* *& Report ZSR_TEST *& *&--------------------------------------------------------------------* *&

Rohini kumar

sap abap consultant

*& *&--------------------------------------------------------------------* REPORT

zsr_test.

TYPE-POOLS: slis. TABLES: ekko, ekpo. DATA: v_repid TYPE sy-repid, v_user TYPE sy-uname, v_date TYPE char12, wa_top1 it_top1 wa_top2 it_top2

TYPE TYPE TYPE TYPE

slis_listheader, slis_t_listheader, slis_listheader, slis_t_listheader.

INITIALIZATION. v_repid = sy-repid. v_user = sy-uname. v_date = sy-datum. SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text001. SELECT-OPTIONS s_ebeln FOR ekko-ebeln OBLIGATORY. SELECTION-SCREEN END OF BLOCK b1. *---------------------------------------------------------------------* * CLASS cls1 DEFINITION *---------------------------------------------------------------------* * *---------------------------------------------------------------------* CLASS cls1 DEFINITION. PUBLIC SECTION. TYPES: BEGIN OF ty_ekko, ebeln TYPE ekko-ebeln, bukrs TYPE ekko-bukrs, lifnr TYPE ekko-lifnr, sel TYPE char1, END OF ty_ekko, BEGIN OF ty_ekpo, ebeln TYPE ekpo-ebeln, ebelp TYPE ekpo-ebelp, matnr TYPE ekpo-matnr, werks TYPE ekpo-werks,

Rohini kumar

sap abap consultant

lgort TYPE ekpo-lgort, menge TYPE ekpo-menge, meins TYPE ekpo-meins, sel TYPE char1, END OF ty_ekpo. DATA: wa_ekko it_ekko wa_ekpo it_ekpo

TYPE TYPE TYPE TYPE

wa_fcat1 it_fcat1 wa_fcat2 it_fcat2

ty_ekko, STANDARD TABLE OF ty_ekko, ty_ekpo, STANDARD TABLE OF ty_ekpo,

TYPE TYPE TYPE TYPE

slis_fieldcat_alv, slis_t_fieldcat_alv, slis_fieldcat_alv, slis_t_fieldcat_alv,

wa_layout TYPE slis_layout_alv, it_print TYPE slis_print_alv, wa_event1 wa_event2 it_event1 it_event2

TYPE TYPE TYPE TYPE

slis_alv_event, slis_alv_event, slis_t_event, slis_t_event.

METHODS: m_ekko, m_ekpo, m_fcat, m_layout, m_event1, m_event2, m_disp. PROTECTED SECTION. PRIVATE SECTION. ENDCLASS.

"cls1 DEFINITION

*---------------------------------------------------------------------* * CLASS cls1 IMPLEMENTATION *---------------------------------------------------------------------* * *---------------------------------------------------------------------* CLASS cls1 IMPLEMENTATION. METHOD m_ekko. SELECT ebeln bukrs lifnr FROM ekko INTO TABLE it_ekko WHERE ebeln IN s_ebeln. IF sy-subrc = 0. SORT it_ekko BY ebeln.

Rohini kumar

sap abap consultant

ELSE. MESSAGE 'Purchase Order doesn''t exist' TYPE 'I'. LEAVE LIST-PROCESSING. ENDIF. ENDMETHOD.

"m_ekko

METHOD m_ekpo. IF it_ekko IS NOT INITIAL. SELECT ebeln ebelp matnr werks lgort menge meins FROM ekpo INTO TABLE it_ekpo FOR ALL ENTRIES IN it_ekko WHERE ebeln = it_ekko-ebeln. ENDIF. ENDMETHOD.

"m_ekpo

METHOD m_fcat. CLEAR wa_fcat1. REFRESH it_fcat1. DATA: lv_col TYPE i VALUE 1. wa_fcat1-col_pos = wa_fcat1-fieldname wa_fcat1-tabname = wa_fcat1-seltext_l APPEND wa_fcat1 TO CLEAR wa_fcat1.

lv_col. = 'EBELN'. 'IT_EKKO'. = 'Purchase Order'. it_fcat1.

lv_col = 1 + lv_col. wa_fcat1-col_pos = lv_col. wa_fcat1-fieldname = 'BUKRS'. wa_fcat1-tabname = 'IT_EKKO'. wa_fcat1-seltext_l = 'Company Code'. APPEND wa_fcat1 TO it_fcat1. CLEAR wa_fcat1. lv_col = 1 + lv_col. wa_fcat1-col_pos = lv_col. wa_fcat1-fieldname = 'LIFNR'. wa_fcat1-tabname = 'IT_EKKO'. wa_fcat1-seltext_l = 'Vendor'. APPEND wa_fcat1 TO it_fcat1. CLEAR wa_fcat1. CLEAR wa_fcat2. REFRESH it_fcat2.

Rohini kumar

lv_col = 1. wa_fcat2-col_pos = wa_fcat2-fieldname wa_fcat2-tabname = wa_fcat2-seltext_l APPEND wa_fcat2 TO CLEAR wa_fcat2.

sap abap consultant

lv_col. = 'EBELN'. 'IT_EKPO'. = 'Purchase Order'. it_fcat2.

lv_col = 1 + lv_col. wa_fcat2-col_pos = lv_col. wa_fcat2-fieldname = 'EBELP'. wa_fcat2-tabname = 'IT_EKPO'. wa_fcat2-seltext_l = 'Item'. APPEND wa_fcat2 TO it_fcat2. CLEAR wa_fcat2. lv_col = 1 + lv_col. wa_fcat2-col_pos = lv_col. wa_fcat2-fieldname = 'MATNR'. wa_fcat2-tabname = 'IT_EKPO'. wa_fcat2-seltext_l = 'Material'. APPEND wa_fcat2 TO it_fcat2. CLEAR wa_fcat2. lv_col = 1 + lv_col. wa_fcat2-col_pos = lv_col. wa_fcat2-fieldname = 'WERKS'. wa_fcat2-tabname = 'IT_EKPO'. wa_fcat2-seltext_l = 'Plant'. APPEND wa_fcat2 TO it_fcat2. CLEAR wa_fcat2. lv_col = 1 + lv_col. wa_fcat2-col_pos = lv_col. wa_fcat2-fieldname = 'LGORT'. wa_fcat2-tabname = 'IT_EKPO'. wa_fcat2-seltext_l = 'Storage Location'. APPEND wa_fcat2 TO it_fcat2. CLEAR wa_fcat2. lv_col = 1 + lv_col. wa_fcat2-col_pos = lv_col. wa_fcat2-fieldname = 'MENGE'. wa_fcat2-tabname = 'IT_EKPO'. wa_fcat2-seltext_l = 'Quantity'. APPEND wa_fcat2 TO it_fcat2. CLEAR wa_fcat2. lv_col = 1 + lv_col.

Rohini kumar

wa_fcat2-col_pos = wa_fcat2-fieldname wa_fcat2-tabname = wa_fcat2-seltext_l APPEND wa_fcat2 TO CLEAR wa_fcat2. ENDMETHOD.

sap abap consultant

lv_col. = 'MEINS'. 'IT_EKPO'. = 'Unit'. it_fcat2. "m_fcat

METHOD m_layout. wa_layout-zebra = 'X'. wa_layout-colwidth_optimize = 'X'. wa_layout-box_fieldname = 'SEL'. ENDMETHOD.

"m_layout

METHOD m_event1. CALL FUNCTION 'REUSE_ALV_EVENTS_GET' * EXPORTING * I_LIST_TYPE = 0 IMPORTING et_events = it_event1 EXCEPTIONS list_type_wrong = 1 OTHERS = 2. IF sy-subrc = 0. CLEAR wa_event1. READ TABLE it_event1 INTO wa_event1 WITH KEY name = 'TOP_OF_PAGE'. IF sy-subrc = 0. wa_event1-form = 'TOP1'. MODIFY it_event1 FROM wa_event1 INDEX sy-tabix TRANSPORTING form. ENDIF. ENDIF. ENDMETHOD. "m_event1 METHOD m_event2. * *

CALL FUNCTION 'REUSE_ALV_EVENTS_GET' EXPORTING I_LIST_TYPE = 0 IMPORTING et_events = it_event2 EXCEPTIONS

Rohini kumar

list_type_wrong OTHERS

sap abap consultant

= 1 = 2.

IF sy-subrc = 0. CLEAR wa_event2. READ TABLE it_event2 INTO wa_event2 WITH KEY name = 'TOP_OF_PAGE'. IF sy-subrc = 0. wa_event2-form = 'TOP2'. MODIFY it_event2 FROM wa_event2 INDEX sy-tabix TRANSPORTING form. ENDIF. ENDIF. ENDMETHOD. "m_event2 METHOD m_disp.

* * *

* *

* *

CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_INIT' EXPORTING i_callback_program = v_repid I_CALLBACK_PF_STATUS_SET = ' ' I_CALLBACK_USER_COMMAND = ' ' IT_EXCLUDING = . CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_APPEND' EXPORTING is_layout = wa_layout it_fieldcat = it_fcat1 i_tabname = 'IT_EKKO' it_events = it_event1 IT_SORT = I_TEXT = ' ' TABLES t_outtab = it_ekko EXCEPTIONS program_error = 1 maximum_of_appends_reached = 2 OTHERS = 3. CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_APPEND' EXPORTING is_layout = wa_layout it_fieldcat = it_fcat2 i_tabname = 'IT_EKPO' it_events = it_event2 IT_SORT = I_TEXT = ' '

Rohini kumar

sap abap consultant

TABLES t_outtab EXCEPTIONS program_error maximum_of_appends_reached OTHERS

* * * * * * * * * * *

= it_ekpo = 1 = 2 = 3.

it_print-reserve_lines = 2. CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_DISPLAY' EXPORTING I_INTERFACE_CHECK = ' ' is_print = it_print I_SCREEN_START_COLUMN = 0 I_SCREEN_START_LINE = 0 I_SCREEN_END_COLUMN = 0 I_SCREEN_END_LINE = 0 IMPORTING E_EXIT_CAUSED_BY_CALLER = ES_EXIT_CAUSED_BY_USER = EXCEPTIONS PROGRAM_ERROR = 1 OTHERS = 2 . ENDMETHOD.

"m_disp

ENDCLASS.

"cls1 IMPLEMENTATION

START-OF-SELECTION. DATA: obj1 TYPE REF TO cls1. CREATE OBJECT obj1. CALL METHOD:

obj1->m_ekko, obj1->m_ekpo, obj1->m_fcat, obj1->m_layout, obj1->m_event1, obj1->m_event2, obj1->m_disp.

TOP-OF-PAGE. PERFORM top1. PERFORM top2. *&--------------------------------------------------------------------* *& Form TOP1 *&------------------------------------------------------------

Rohini kumar

sap abap consultant

---------* * text *---------------------------------------------------------------------* * --> p1 text * m_cls1, obj2->m_cls2.

Now we have set the debugger at the CALL METHOD. The method of first class can access public, protected & private variables and the global variable from that first class implementation.

Similarly when the program enters to the second class implementation, the method can access all variables of that class and the global variable.

When the program is not under any class then also global variable can be accessed.

Rohini kumar

sap abap consultant

The output is as follows.

Public, Protect and Private access in OOPs Class components will have to be declared under any of visibility sections like Public Section, Protected Section and Private Section. These visibility sections will have to be declared with this order. That means protected section or Private section cannot come before Public section. As per the requirement if the program only needs to contain Public section then there is no need to declare further section. This is similar to the Protected and Private section also. The data and methods declared in Public Section in a class can be and accessed by that class and any user, subclass of the program. When the data and methods are declared in Protected Section in a class then those will be accessed by that class and sub-classes only. Subclasses can be any child class of the main class. When the data and methods are declared in Private Section in a class then those will be accessed by only that class, not by any other class. Below is a program by which these 3 condition can be reflected: REPORT

zsr_test NO STANDARD PAGE HEADING.

*----------------------------------------------------------------------* * CLASS parent DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS parent DEFINITION. PUBLIC SECTION. DATA v_pub_parent TYPE char50 VALUE 'Parent class public data'. METHODS m_parent. PROTECTED SECTION. DATA v_pro_parent TYPE char50 VALUE 'Parent class protected data'.

Rohini kumar

sap abap consultant

PRIVATE SECTION. DATA v_pri_parent TYPE char50 VALUE 'Parent class private data'. ENDCLASS. "parent DEFINITION *----------------------------------------------------------------------* * CLASS child DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS child DEFINITION INHERITING FROM parent. PUBLIC SECTION. METHODS m_child. ENDCLASS. "child DEFINITION *----------------------------------------------------------------------* * CLASS parent IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS parent IMPLEMENTATION. METHOD m_parent. WRITE: / 'Accessing components from Parent class', / v_pub_parent, / v_pro_parent, / v_pri_parent. SKIP. ENDMETHOD. "m_parent ENDCLASS. "parent IMPLEMENTATION *----------------------------------------------------------------------* * CLASS child IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS child IMPLEMENTATION. METHOD m_child. WRITE: / 'Accessing components from Child class', / v_pub_parent, / v_pro_parent. SKIP. ENDMETHOD. "m_child ENDCLASS. "child IMPLEMENTATION DATA: obj_parent TYPE REF TO parent, obj_child TYPE REF TO child. START-OF-SELECTION. CREATE OBJECT: obj_parent, obj_child. CALL METHOD: obj_parent->m_parent, obj_child->m_child. WRITE: / 'Every user can access public data', / obj_parent->v_pub_parent.

Now at debugging level we shall see the accessing components of same class. Public, protected & private components are accessible from the same class.

Rohini kumar

sap abap consultant

From the child / sub class public & protected components are accessible.

From the user level only the public components are accessible.

Below is the output of this.

Rohini kumar

sap abap consultant

Inheritance Concept in OOPs Inheritance is a general property of OOPs. Class defined by Inheriting From statement denotes that this class is a sub class of a Super class. A class can have only a single super class where as it can have multiple sub classes. Sub class gets all of the Public and Protected components with visibility property of the super class. Sub class can add components as well in its declaration part. Here the super class will not access any of sub class's components. In the following program the Child class can have all the access of Public and Protected data and methods of its Parent class. Thus if a Parent class can have only the addition method then Child class will have that functionality for Inheritance property of OOPs. Here Child class can add multiply method as unique. Hence the Child has now two functionality Addition and Multiplication. The important point is that the Parent class will not have any access of this Multiplication. Below is a program of Inheritance. REPORT

zsr_test NO STANDARD PAGE HEADING.

*----------------------------------------------------------------------* * CLASS super_cls DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS super_cls DEFINITION. PUBLIC SECTION. DATA v_super_pub TYPE char40 VALUE 'Super public data'. METHODS m_super. PROTECTED SECTION. DATA v_super_pro TYPE char40 VALUE 'Super protected data'. PRIVATE SECTION.

Rohini kumar

sap abap consultant

DATA v_super_pri TYPE char40 VALUE 'Super private data'. ENDCLASS. "super_cls DEFINITION *----------------------------------------------------------------------* * CLASS parent DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS parent DEFINITION INHERITING FROM super_cls. PUBLIC SECTION. DATA v_parent_pub TYPE char40 VALUE 'Parent public data'. METHODS m_parent. PROTECTED SECTION. DATA v_parent_pro TYPE char40 VALUE 'Parent protected data'. PRIVATE SECTION. DATA v_parent_pri TYPE char40 VALUE 'Parent private data'. ENDCLASS. "parent DEFINITION *----------------------------------------------------------------------* * CLASS child DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS child DEFINITION INHERITING FROM parent. PUBLIC SECTION. DATA v_child_pub TYPE char40 VALUE 'Child public data'. METHODS m_child. PROTECTED SECTION. DATA v_child_pro TYPE char40 VALUE 'Child protected data'. PRIVATE SECTION. DATA v_child_pri TYPE char40 VALUE 'Child private data'. ENDCLASS. "child DEFINITION *----------------------------------------------------------------------* * CLASS super_cls IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS super_cls IMPLEMENTATION. METHOD m_super. WRITE: / 'Super Class' COLOR 3, / v_super_pub, / v_super_pro, / v_super_pri. SKIP. ENDMETHOD. "m_super ENDCLASS. "super_cls IMPLEMENTATION *----------------------------------------------------------------------* * CLASS parent IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS parent IMPLEMENTATION. METHOD m_parent. v_super_pub = 'Super public data from Parent'.

Rohini kumar

sap abap consultant

v_super_pro = 'Super protected data from Parent'. WRITE: / / / / / / / SKIP. ENDMETHOD. ENDCLASS.

'Parent Class' COLOR 3, v_super_pub, v_super_pro, '=====================', v_parent_pub, v_parent_pro, v_parent_pri. "m_parent "parent IMPLEMENTATION

*----------------------------------------------------------------------* * CLASS child IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS child IMPLEMENTATION. METHOD m_child. v_super_pub = 'Super public data from Child'. v_super_pro = 'Super protected data from Child'. v_parent_pub = 'Parent public data from Child'. v_parent_pro = 'Parent protected data from Child'. WRITE: / / / / / / / / / / SKIP. ENDMETHOD. ENDCLASS.

'Child Class' COLOR 3, v_super_pub, v_super_pro, '======================', v_parent_pub, v_parent_pro, '======================', v_child_pub, v_child_pro, v_child_pri. "m_child "child IMPLEMENTATION

START-OF-SELECTION. DATA: obj_super TYPE REF TO super_cls, obj_parent TYPE REF TO parent, obj_child TYPE REF TO child. CREATE OBJECT: obj_super, obj_parent, obj_child. CALL METHOD: obj_super->m_super, obj_parent->m_parent, obj_child->m_child.

Now we go to debug mode and analysis the behavior of inheritance. The super class doesn't have access to its sub classes. Hence we can't see the data from sub classes of the SUPER_CLS class.

Rohini kumar

sap abap consultant

Now we are going to parent class which is sub class of super_cls class. From here the public & protected components of super class are available. No private components will be available from the outside of that class. We can modify public & protected components of super class from the sub class.

Now again we are going to the sub class of parent class and that is child class. From there we shall have access of public & protected components of super_cls & parent class. Both are hierarchical super classes of the child sub class.

Rohini kumar

sap abap consultant

The output is as follows:

Posted by SANDIP ROY at 2/11/2013 04:28:00 PM

Class Definition Deferred concept One class can be refrerred within another class definition with defining the class. But that class must be defined later on.

Rohini kumar

sap abap consultant

Below is a program where we have defined class cls2. Inside there we are referring the object obj1 ref to class cls1 which has the definition deferred. Later on the class cls1 has been defined. *&--------------------------------------------------------------------* *& Report ZSR_TEST *& *&--------------------------------------------------------------------* *& *& *&--------------------------------------------------------------------* REPORT

zsr_test.

CLASS cls1 DEFINITION DEFERRED. *---------------------------------------------------------------------* * CLASS cls2 DEFINITION *---------------------------------------------------------------------* * *---------------------------------------------------------------------* CLASS cls2 DEFINITION. PUBLIC SECTION. DATA: obj1 TYPE REF TO cls1. ENDCLASS.

"cls2 DEFINITION

*---------------------------------------------------------------------* * CLASS cls1 DEFINITION *---------------------------------------------------------------------* * *---------------------------------------------------------------------* CLASS cls1 DEFINITION. PUBLIC SECTION. DATA: v_cls TYPE char40 VALUE 'This is class 1 Definition'.

Rohini kumar

ENDCLASS.

sap abap consultant

"cls1 DEFINITION

START-OF-SELECTION. DATA: obj2 TYPE REF TO cls2. CREATE OBJECT: obj2, obj2->obj1. WRITE: / obj2->obj1->v_cls. The output of this as follows:

Instantiating a Class within another Class's Implementation One class can be instantiated within the implementation of another class. Here the program contains two different classes cls1 and cls2. In the implementation of cls2 we are creating object obj1 ref to cls1 and then calling method of class 1 inside the 2nd class implementation.

REPORT

zsr_test NO STANDARD PAGE HEADING.

*----------------------------------------------------------------------* * CLASS cls1 DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls1 DEFINITION. PUBLIC SECTION. DATA v_cls1 TYPE char40 VALUE 'Class one data'. METHODS m_cls1. ENDCLASS. "cls1 DEFINITION *----------------------------------------------------------------------* * CLASS cls2 DEFINITION *----------------------------------------------------------------------* *

Rohini kumar

sap abap consultant

*----------------------------------------------------------------------* CLASS cls2 DEFINITION. PUBLIC SECTION. DATA v_cls2 TYPE char40 VALUE 'Class two data'. METHODS m_cls2. ENDCLASS. "cls2 DEFINITION *----------------------------------------------------------------------* * CLASS cls1 IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls1 IMPLEMENTATION. METHOD m_cls1. WRITE: / v_cls1. ENDMETHOD. "m_cls1 ENDCLASS. "cls1 IMPLEMENTATION *----------------------------------------------------------------------* * CLASS cls2 IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls2 IMPLEMENTATION. METHOD m_cls2. DATA obj1 TYPE REF TO cls1. CREATE OBJECT obj1. CALL METHOD obj1->m_cls1. WRITE: / v_cls2. ENDMETHOD. "m_cls2 ENDCLASS. "cls2 IMPLEMENTATION DATA obj2 TYPE REF TO cls2. START-OF-SELECTION. CREATE OBJECT: obj2. CALL METHOD obj2->m_cls2.

Now at debugging level we see at first the system will call the m_cls2 method. It holds the value of v_cls2 inside m_cls2.

Rohini kumar

sap abap consultant

After creation of the object of class cls1 the system gets access to call its method. Now it will call the method m_cls1 and the control will go to inside the class cls1.

After completing this method it will again go back to the second method m_cls2. The system will finish the rest of the work there.

The output is as follows:

Static Attribute Static attribute is declared with the statement CLASS-DATA. All the objects or instances can use the static attribute of the class. Validity of static attribute is not associated with the class

Rohini kumar

sap abap consultant

itself not with the instances of the class. Static attributes are accessed directly with the help of class name not interface name like cl_one=>cl_v_txt = 'SAP ABAP Dynpro'. In the following example we have declared two static attributes. We want to display the text from the method m_one. Inside the method the text is 'SAP ABAP Object Oriented'. After that we are calling this static attribute directly with the class and edit it again to 'SAP ABAP Dynpro'. After that we shall print this directly. REPORT

zsr_test NO STANDARD PAGE HEADING.

*----------------------------------------------------------------------* * CLASS cl_one DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_one DEFINITION. PUBLIC SECTION. CLASS-DATA: cl_v_txt TYPE char40, cl_v_cnt TYPE i. METHODS m_one. ENDCLASS. "cl_one DEFINITION *----------------------------------------------------------------------* * CLASS cl_one IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_one IMPLEMENTATION. METHOD m_one. cl_v_txt = 'SAP ABAP Object Oriented'. cl_v_cnt = 10. DO cl_v_cnt TIMES. WRITE: / sy-index, '>', cl_v_txt. ENDDO. SKIP. ENDMETHOD. "m_one ENDCLASS. "cl_one IMPLEMENTATION START-OF-SELECTION. DATA obj TYPE REF TO cl_one. CREATE OBJECT obj. CALL METHOD obj->m_one. cl_one=>cl_v_txt = 'SAP ABAP Dynpro'. cl_one=>cl_v_cnt = 5. DO cl_one=>cl_v_cnt TIMES. WRITE: / sy-index, '>', cl_one=>cl_v_txt. ENDDO.

Now we shall inspect at debugging level. We set breakpoint at CALL METHOD. There we can see cl_one=>cl_v_txt & cl_one=>cl_v_cnt hold nothing.

Rohini kumar

sap abap consultant

We enter into the method now and see the class data with respective values. We also can see the cl_one=>cl_v_txt holds the same value as inside the method.

Now after finishing the operation inside the method the program comes out. At this time the system doesn’t have access to the cl_v_txt but it holds value for cl_one=>cl_v_txt as per assignment.

Finally after finishing the operation here we get the output as follows.

Rohini kumar

sap abap consultant

Field Symbols in Class

Field symbol can be used in object oriented programming. The field symbol contains the value of any variable. The variable can be normal instance or any static variable in a class. In the following example we have declared an instance variable v_ins & a static variable v_cls in class cl_fs_test. Now we declare the field symbol at the global declaration part. After that we assign the variables to the field symbol one by one. REPORT

zsr_test NO STANDARD PAGE HEADING.

FIELD-SYMBOLS TYPE ANY. *----------------------------------------------------------------------* * CLASS cl_fs_test DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_fs_test DEFINITION. PUBLIC SECTION. DATA v_ins TYPE char50 VALUE 'Assign Instance variable to FS'. CLASS-DATA v_cls TYPE char50 VALUE 'Assign static variable to FS'. ENDCLASS. "cl_fs_test DEFINITION *----------------------------------------------------------------------* * CLASS cl_fs_test IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_fs_test IMPLEMENTATION. ENDCLASS. "cl_fs_test IMPLEMENTATION

Rohini kumar

sap abap consultant

START-OF-SELECTION. DATA obj TYPE REF TO cl_fs_test. CREATE OBJECT obj. ASSIGN obj->v_ins TO . WRITE / . ASSIGN obj->v_cls TO . WRITE / . cl_fs_test=>v_cls = 'Changing static variable to FS'. ASSIGN cl_fs_test=>v_cls TO . WRITE: / .

The output is as follows:

Method Calling by One Import Parameter When a method has only one import parameter then it can be called by different ways. If the method contains more than one import parameter then also it can be called by different ways. Here we have a program which can calculate temperature of Celsius Fahrenheit and Kelvin scale. The temperature will be calculated based on the input Parameter of Celsius and Fahrenheit. We have declared 3 methods with importing import parameter. After that we are implementing the methods with respective calculations. Like Celsius = (5/9)*(Fahrenheit-32) Fahrenheit = ((9* Celsius)/5)+32 and Kelvin = Celsius +273. Then in the data processing START-OF-SELECTION we are calling the methods with Exporting parameter by different ways. *&--------------------------------------------------------------------*

Rohini kumar

sap abap consultant

*& Report ZSR_TEST *& *&--------------------------------------------------------------------* *& *& *&--------------------------------------------------------------------* REPORT

zsr_test.

PARAMETERS: p_cel TYPE p DECIMALS 2, p_fer TYPE p DECIMALS 2. *---------------------------------------------------------------------* * CLASS cls1 DEFINITION *---------------------------------------------------------------------* * *---------------------------------------------------------------------* CLASS cls1 DEFINITION. PUBLIC SECTION. DATA: v_cel TYPE p DECIMALS 2, v_fer TYPE p DECIMALS 2, v_kel TYPE p DECIMALS 2. METHODS: m_cel IMPORTING i_fer LIKE v_fer, m_fer IMPORTING i_cel LIKE v_cel, m_kel IMPORTING i_cel LIKE v_cel. ENDCLASS.

"cls1 DEFINITION

*---------------------------------------------------------------------* * CLASS cls1 IMPLEMENTATION *---------------------------------------------------------------------* * *---------------------------------------------------------------------* CLASS cls1 IMPLEMENTATION. METHOD m_cel. v_cel = ( 5 / 9 ) * ( i_fer - 32 ). WRITE: / 'Celsius = ', v_cel. ENDMETHOD. "m_cel

Rohini kumar

sap abap consultant

METHOD m_fer. v_fer = ( ( 9 * i_cel ) / 5 ) + 32. WRITE: / 'Fahrenheit = ', v_fer. ENDMETHOD. "m_fer METHOD m_kel. v_kel = i_cel + 273. WRITE: / 'Kelvin = ', v_kel. ENDMETHOD. "m_kel ENDCLASS.

"cls1 IMPLEMENTATION

START-OF-SELECTION. DATA: obj1 TYPE REF TO cls1. CREATE OBJECT obj1. IF p_cel IS NOT INITIAL AND p_fer IS NOT INITIAL. CALL METHOD: obj1->m_fer EXPORTING i_cel = p_cel, obj1->m_kel( i_cel = p_cel ), obj1->m_cel( p_fer ). ELSEIF p_cel IS NOT INITIAL. CALL METHOD: obj1->m_fer EXPORTING i_cel = p_cel, obj1->m_kel( i_cel = p_cel ). ELSEIF p_fer IS NOT INITIAL. CALL METHOD: obj1->m_cel( p_fer ). ELSE. WRITE: / 'Enter a valid temperature'. ENDIF.

The output is as follows: When we give only Celsius temperature

Rohini kumar

sap abap consultant

Output:

___________________________________________________________ ______ When we give only Fahrenheit temperature

Output:

___________________________________________________________ ______ When we give both temperature

Rohini kumar

sap abap consultant

Output:

___________________________________________________________ ______ When we give nothing

Output:

Rohini kumar

sap abap consultant

Import Parameters Passed by Reference and Passed by Value We can pass parameters to a method in two ways - 1. By Reference & 2. By Value. When we pass by reference then that parameter cannot be changed in any way in the method but the parameter passed by value can be changed as per requirement as well. Below is a program where we have made a Calculator which can convert centimeter to Inches and Inches to centimeter. Here we are declaring two parameters as Inch and Cm. The method m_cm which produces the output from inches to centimeter is having the parameter i_inch that is passed by value. And the method m_inch which produces the output from centimeter to inches is having the parameter i_cm that is passed by reference. Now the parameter i_inch has been changed as per requirement but whenever we try to change the parameter i_cm then a syntax error is coming. Hence the parameter passed by reference cannot be changed.

*&--------------------------------------------------------------------* *& Report ZSR_TEST *& *&--------------------------------------------------------------------* *& *& *&--------------------------------------------------------------------* REPORT

zsr_test.

PARAMETERS: p_inch TYPE char40,

Rohini kumar

sap abap consultant

p_cm

TYPE p DECIMALS 2.

*---------------------------------------------------------------------* * CLASS cls1 DEFINITION *---------------------------------------------------------------------* * *---------------------------------------------------------------------* CLASS cls1 DEFINITION. PUBLIC SECTION. DATA: v_inch TYPE char40, v_cm TYPE p DECIMALS 2. METHODS: m_inch IMPORTING i_cm LIKE v_cm, m_cm IMPORTING value(i_inch) LIKE v_inch. ENDCLASS.

"cls1 DEFINITION

*---------------------------------------------------------------------* * CLASS cls1 IMPLEMENTATION *---------------------------------------------------------------------* * *---------------------------------------------------------------------* CLASS cls1 IMPLEMENTATION. METHOD m_inch. v_inch = '0.3937007874015748' * i_cm. WRITE: / 'Inch is = ', v_inch. "i_cm = 3. The field i_cm cannot be changed SKIP. ENDMETHOD. "m_inch METHOD m_cm. WRITE: / 'Before Change = ', i_inch. v_cm = '2.54' * i_inch. WRITE: / 'CM is = ', v_cm. i_inch = 'Passed by Value'. WRITE: / 'After Change = ', i_inch. ENDMETHOD. "m_cm ENDCLASS. START-OF-SELECTION.

"cls1 IMPLEMENTATION

Rohini kumar

sap abap consultant

DATA: obj1 TYPE REF TO cls1. CREATE OBJECT: obj1. IF p_inch IS NOT INITIAL AND p_cm IS NOT INITIAL. CALL METHOD: obj1->m_inch( p_cm ), obj1->m_cm( p_inch ). ELSEIF p_cm IS NOT INITIAL. CALL METHOD: obj1->m_inch EXPORTING i_cm = p_cm. ELSEIF p_inch IS NOT INITIAL. CALL METHOD obj1->m_cm( i_inch = p_inch ). ELSE. MESSAGE 'Please select Valid length' TYPE 'I'. LEAVE LIST-PROCESSING. ENDIF.

Below is the output:

After Executing:

Rohini kumar

sap abap consultant

Here we can see that the 5 CM is 1.968 Inches and that has come perfectly. The 10 Inches is 25.40 CM and that has come perfectly too. Here we are printing this inches before change as 10 and after change that is "Passed by Value".

Preferred Parameter in a Method When we want to use more than one optional import parameters in a method then we have to declare which one of them is the preferred import parameter. In this case we have to declare the statement PREFERRED PARAMETER after declaring all the optional import parameters. Below is a program where we have an input through input Parameter for Age. That particular age is passed to the optional import parameters i1, i2 & i3. A method is called for the validation of age in various condition. After having the proper validation the program is giving the required output. *&--------------------------------------------------------------------* *& Report ZSR_TEST *& *&--------------------------------------------------------------------* *& *& *&--------------------------------------------------------------------* REPORT

zsr_test.

PARAMETERS: p_age TYPE i. *---------------------------------------------------------------------* * CLASS cls1 DEFINITION *---------------------------------------------------------------------* * *---------------------------------------------------------------------* CLASS cls1 DEFINITION. PUBLIC SECTION.

Rohini kumar

sap abap consultant

METHODS: m1 IMPORTING i1 TYPE i OPTIONAL i2 TYPE i OPTIONAL i3 TYPE i OPTIONAL PREFERRED PARAMETER i1. ENDCLASS.

"cls1 DEFINITION

*---------------------------------------------------------------------* * CLASS cls1 IMPLEMENTATION *---------------------------------------------------------------------* * *---------------------------------------------------------------------* CLASS cls1 IMPLEMENTATION. METHOD m1. IF i1 LT 18. WRITE: / 'Age = ', i1, ' is not eligible for giving Vote.'. ELSE. WRITE: / 'Age = ', i1, ' is eligible for giving Vote.'. ENDIF. IF i2 LT 22. WRITE: / 'Age = ', i2, ' is not eligible for Jobs.'. ELSE. WRITE: / 'Age = ', i2, ' is eligible for Jobs.'. ENDIF. IF i3 GT 60. WRITE: / 'Age = ', i3, ' is Retirement age.'. ELSE. WRITE: / 'Age = ', i3, ' is not Retirement age.'. ENDIF. ENDMETHOD. ENDCLASS.

"m1 "cls1 IMPLEMENTATION

START-OF-SELECTION. DATA: obj1 TYPE REF TO cls1. CREATE OBJECT obj1. IF p_age IS NOT INITIAL. CALL METHOD: obj1->m1( i1 = p_age i2 = p_age i3 = p_age ).

Rohini kumar

sap abap consultant

ELSE. MESSAGE 'Please Enter an Age' TYPE 'I'. LEAVE LIST-PROCESSING. ENDIF.

Below is the Output.

Exporting and Changing Parameter in a Method We can change a parameter value by using Changing parameter and export a value by Export parameter. Below is a program where we have the total salary amount that is Gross salary as an Input parameter. We have a method which imports the gross salary, exports the income tax and change the value of net salary. The program has the function as follows: Income tax = gross salary * percentage of tax slab. Providend fund = 10% of gross salary. Net salary = gross salary - income tax - providend fund. Hence the net salary must be declared by changing parameter.

Rohini kumar

sap abap consultant

*&--------------------------------------------------------------------* *& Report ZSR_TEST *& *&--------------------------------------------------------------------* *& *& *&--------------------------------------------------------------------* REPORT

zsr_test.

DATA: v_sal TYPE p DECIMALS 4, v_tax TYPE p DECIMALS 4, pf TYPE p DECIMALS 4. PARAMETERS: p_sal TYPE p DECIMALS 4. *---------------------------------------------------------------------* * CLASS cls DEFINITION *---------------------------------------------------------------------* * *---------------------------------------------------------------------* CLASS cls DEFINITION. PUBLIC SECTION. METHODS: meth1 IMPORTING salary LIKE v_sal EXPORTING i_tax LIKE v_tax CHANGING net_sal LIKE v_sal. ENDCLASS.

"cls DEFINITION

*---------------------------------------------------------------------* * CLASS cls IMPLEMENTATION *---------------------------------------------------------------------* * *---------------------------------------------------------------------* CLASS cls IMPLEMENTATION. METHOD meth1. IF salary = 0.

Rohini kumar

sap abap consultant

MESSAGE 'Please Enter a valid Salary' TYPE 'I'. LEAVE LIST-PROCESSING. ELSEIF salary LT 200000. i_tax = 0. ELSEIF salary LT 500000. i_tax = salary * '0.1'. ELSEIF salary LT 1000000. i_tax = salary * '0.2'. ELSEIF salary GT 1000000. i_tax = salary * '0.3'. ENDIF. net_sal = salary - i_tax. pf = salary * '0.1'. net_sal = net_sal - pf. WRITE: / / / / ENDMETHOD. "meth1

'Gross Salary = ', salary, 'Per Annum', 'Tax Deduction = ', i_tax, 'Providend Fund = ', pf, 'Net Salary = ', net_sal, 'Per Annum'.

ENDCLASS.

"cls IMPLEMENTATION

START-OF-SELECTION. DATA obj TYPE REF TO cls. CREATE OBJECT obj. CALL METHOD: obj->meth1 EXPORTING salary = p_sal IMPORTING i_tax = v_tax CHANGING net_sal = v_sal.

Output is as follows:

Rohini kumar

sap abap consultant

Internal Table as Parameter of Method We can have an internal table as a parameter of a method. This case we have to Export the internal table at the declaration time of the method. And we shall import the internal table at the time of calling of the method. In the following program we are showing item wise header information of Purchase order in two different displays. The output will contain the header information at top and item information at below of the same screen. We are getting the Purchase Order number from the input parameter. REPORT

zsr_test NO STANDARD PAGE HEADING.

TABLES: ekko. TYPES: BEGIN OF ty_ekko, ebeln TYPE ekko-ebeln, lifnr TYPE ekko-lifnr, END OF ty_ekko, BEGIN OF ty_ekpo, ebeln TYPE ekpo-ebeln, ebelp TYPE ekpo-ebelp, matnr TYPE ekpo-matnr, werks TYPE ekpo-werks, lgort TYPE ekpo-lgort, mtart TYPE ekpo-mtart, END OF ty_ekpo. DATA: wa_ekko wa_ekpo it_ekko it_ekpo

TYPE TYPE TYPE TYPE

ty_ekko, ty_ekpo, STANDARD TABLE OF ty_ekko, STANDARD TABLE OF ty_ekpo.

Rohini kumar

sap abap consultant

INITIALIZATION. PARAMETERS p_ebeln TYPE ekko-ebeln. *----------------------------------------------------------------------* * CLASS cls *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls DEFINITION. PUBLIC SECTION. METHODS: m_ekko IMPORTING po TYPE ekko-ebeln EXPORTING lt TYPE ANY TABLE, m_ekpo. ENDCLASS.

"cls

*----------------------------------------------------------------------* * CLASS cls IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls IMPLEMENTATION. METHOD m_ekko. SELECT ebeln lifnr FROM ekko INTO TABLE lt WHERE ebeln = po. IF sy-subrc = 0. LOOP AT lt INTO wa_ekko. AT FIRST. WRITE: / 'Purchase Order Header:'. WRITE: / 'PO No.', 12 'Vendor', / '-----------------'. SKIP. ENDAT. WRITE: / wa_ekko-ebeln, 12 wa_ekko-lifnr. CLEAR wa_ekko. ENDLOOP. WRITE: / '-----------------'. SKIP 2. ENDIF. ENDMETHOD. "m_ekko METHOD m_ekpo. IF it_ekko IS NOT INITIAL. SELECT ebeln ebelp matnr werks lgort mtart FROM ekpo INTO TABLE it_ekpo FOR ALL ENTRIES IN it_ekko WHERE ebeln = it_ekko-ebeln. IF sy-subrc = 0. LOOP AT it_ekpo INTO wa_ekpo. AT FIRST. WRITE: / 'Purchase Order Item:'.

Rohini kumar

sap abap consultant

WRITE: / 12 20 40 48 57 ULINE. SKIP. ENDAT.

'PO No.', 'Item', 'Material', 'Plant', 'Storage', 'Type'.

WRITE: / wa_ekpo-ebeln, 12 wa_ekpo-ebelp, 20 wa_ekpo-matnr, 40 wa_ekpo-werks, 48 wa_ekpo-lgort, 57 wa_ekpo-mtart. CLEAR wa_ekpo. ENDLOOP. ULINE. ENDIF. ENDIF. ENDMETHOD. "m_ekpo ENDCLASS.

"cls IMPLEMENTATION

START-OF-SELECTION. DATA obj TYPE REF TO cls. CREATE OBJECT obj. CALL METHOD: obj->m_ekko EXPORTING po = p_ebeln IMPORTING lt = it_ekko, obj->m_ekpo.

The selection is based on the parameter of a purchase order.

The output is as follows.

Rohini kumar

sap abap consultant

Now we can achieve the same thing by using Selection range (select option). In the following program we have illustrated this. REPORT

zsr_test NO STANDARD PAGE HEADING.

TABLES: ekko. TYPES: BEGIN OF ty_ekko, ebeln TYPE ekko-ebeln, lifnr TYPE ekko-lifnr, END OF ty_ekko, BEGIN OF ty_ekpo, ebeln TYPE ekpo-ebeln, ebelp TYPE ekpo-ebelp, matnr TYPE ekpo-matnr, werks TYPE ekpo-werks, lgort TYPE ekpo-lgort, mtart TYPE ekpo-mtart, END OF ty_ekpo. DATA: wa_ekko wa_ekpo it_ekko it_ekpo

TYPE TYPE TYPE TYPE

ty_ekko, ty_ekpo, STANDARD TABLE OF ty_ekko, STANDARD TABLE OF ty_ekpo.

INITIALIZATION. SELECT-OPTIONS s_ebeln FOR ekko-ebeln. *----------------------------------------------------------------------* * CLASS cls *----------------------------------------------------------------------* *

Rohini kumar

sap abap consultant

*----------------------------------------------------------------------* CLASS cls DEFINITION. PUBLIC SECTION. METHODS: m_ekko IMPORTING po LIKE s_ebeln[] EXPORTING lt TYPE ANY TABLE, m_ekpo. ENDCLASS.

"cls

*----------------------------------------------------------------------* * CLASS cls IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls IMPLEMENTATION. METHOD m_ekko. SELECT ebeln lifnr FROM ekko INTO TABLE lt WHERE ebeln IN po. IF sy-subrc = 0. LOOP AT lt INTO wa_ekko. AT FIRST. WRITE: / 'Purchase Order Header:'. WRITE: / 'PO No.', 12 'Vendor', / '-----------------'. SKIP. ENDAT. WRITE: / wa_ekko-ebeln, 12 wa_ekko-lifnr. CLEAR wa_ekko. ENDLOOP. WRITE: / '-----------------'. SKIP 2. ENDIF. ENDMETHOD. "m_ekko METHOD m_ekpo. IF it_ekko IS NOT INITIAL. SELECT ebeln ebelp matnr werks lgort mtart FROM ekpo INTO TABLE it_ekpo FOR ALL ENTRIES IN it_ekko WHERE ebeln = it_ekko-ebeln. IF sy-subrc = 0. LOOP AT it_ekpo INTO wa_ekpo. AT FIRST. WRITE: / 'Purchase Order Item:'. WRITE: / 'PO No.', 12 'Item', 20 'Material', 40 'Plant', 48 'Storage', 57 'Type'. ULINE. SKIP.

Rohini kumar

sap abap consultant

ENDAT. WRITE: / wa_ekpo-ebeln, 12 wa_ekpo-ebelp, 20 wa_ekpo-matnr, 40 wa_ekpo-werks, 48 wa_ekpo-lgort, 57 wa_ekpo-mtart. CLEAR wa_ekpo. ENDLOOP. ULINE. ENDIF. ENDIF. ENDMETHOD. "m_ekpo ENDCLASS.

"cls IMPLEMENTATION

START-OF-SELECTION. DATA obj TYPE REF TO cls. CREATE OBJECT obj. CALL METHOD: obj->m_ekko EXPORTING po = s_ebeln[] IMPORTING lt = it_ekko, obj->m_ekpo.

Here we are using the selection screen with selection range.

The output is as follows.

Rohini kumar

sap abap consultant

Posted by SANDIP ROY at 3/19/2013 03:15:00 PM

Returning Parameter of Method Returning Parameter in a method is very important when we want to get some values from a method. Returning parameter has some prerequisites as follows: 1. Any Exporting & Changing parameter cannot be used in the method which uses Returning parameter. 2. Returning parameter can only be passed by value. Below is a program by which we can get Factorial value of any 3 numbers. Those 3 numbers are independent to each other and in one single screen we can get 3 outputs. Here we are using the Returning parameter at the declaration of a method and we are receiving that

Rohini kumar

sap abap consultant

parameter at the time of calling method. This calling has different syntax which have been shown in the program. We have also declared Importing parameter by reference and by value also as per factorial function requirement. The program can calculate Factorial value from 1 to 9 only. *&--------------------------------------------------------------------* *& Report ZSR_TEST *& *&--------------------------------------------------------------------* *& *& *&--------------------------------------------------------------------* REPORT

zsr_test.

DATA: v_num TYPE i. PARAMETERS: p_num1 TYPE i, p_num2 TYPE i, p_num3 TYPE i. *---------------------------------------------------------------------* * CLASS cls DEFINITION *---------------------------------------------------------------------* * *---------------------------------------------------------------------* CLASS cls DEFINITION. PUBLIC SECTION. METHODS m_fact IMPORTING i1 TYPE i value(i2) TYPE i RETURNING value(factorial) TYPE i. ENDCLASS.

"cls DEFINITION

*---------------------------------------------------------------------* * CLASS cls IMPLEMENTATION *---------------------------------------------------------------------* *

Rohini kumar

sap abap consultant

*---------------------------------------------------------------------* CLASS cls IMPLEMENTATION. METHOD m_fact. IF i1 0 AND i1 LT 10. factorial = i1. DO. i2 = i2 - 1. IF i2 = 1. EXIT. ENDIF. factorial = factorial * i2. ENDDO. ELSE. MESSAGE 'Value is too Big' TYPE 'I'. LEAVE LIST-PROCESSING. ENDIF. ENDMETHOD. "m_fact ENDCLASS.

"cls IMPLEMENTATION

START-OF-SELECTION. DATA obj TYPE REF TO cls. CREATE OBJECT obj. CALL METHOD: obj->m_fact EXPORTING i1 = p_num1 i2 = p_num1 RECEIVING factorial = v_num. WRITE: / 'Factorial of ', p_num1, '=', v_num. v_num = obj->m_fact( i1 = p_num2 i2 = p_num2 ). WRITE: / 'Factorial of ', p_num2, '=', v_num. MOVE obj->m_fact( i1 = p_num3 i2 = p_num3 ) TO v_num. WRITE: / 'Factorial of ', p_num3, '=', v_num.

The output is as follows:

Rohini kumar

sap abap consultant

Static Method of Class Static Method is the class method and it can access the static attributes and global attributes only. It cannot access any Instance attributes. When we declare a static method then we don't have to create any object to call that method. We can call a static method directly by the class where it has been declared. In the following example we have declared a static method in public section of a class. The method will need to access a variable which has been declared as static. The method can have the access of global variable as well. So we have declared a variable gv_time as global and a static variable cl_v_date in Public section of the class. Now the method has the access of those two variable and Write those as well. REPORT

zsr_test NO STANDARD PAGE HEADING.

DATA gv_time TYPE sy-uzeit. *----------------------------------------------------------------------* * CLASS cl_one DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_one DEFINITION. PUBLIC SECTION. DATA v_txt TYPE char40 VALUE 'SAP ABAP Object Oriented'.

Rohini kumar

sap abap consultant

CLASS-DATA: cl_v_date TYPE sy-datum. CLASS-METHODS cl_m_one. ENDCLASS. "cl_one DEFINITION *----------------------------------------------------------------------* * CLASS cl_one IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_one IMPLEMENTATION. METHOD cl_m_one. gv_time = sy-uzeit. cl_v_date = sy-datum. WRITE: / 'Today''s date: ', cl_v_date, / 'Time: ', gv_time. ENDMETHOD. "cl_m_one ENDCLASS. "cl_one IMPLEMENTATION START-OF-SELECTION. CALL METHOD cl_one=>cl_m_one.

Now we shall investigate in debugging mode. Set the breakpoint at CALL METHOD. The system will enter into the method. There is no need to instantiate the class as the method is the class method / static method.

We can see here the static method doesn’t have access to the general public component v_txt. To get access of this component we need to declare a normal method. The output is as follows.

Rohini kumar

sap abap consultant

Exception Handling by Method Method can raise exceptions as Function Module does. Depending on the value of sy-subrc the exception are handled after calling that method. Below is a Factorial program which has a class cls and a method meth. Now we are declaring the method with Import & Export parameter and with an Exception exc. We are getting the number as an input parameter. Here we make an exception exc. If the number is greater than 10 then it will call the exception and specified Information message will be displayed. *&--------------------------------------------------------------------* *& Report ZSR_TEST *& *&--------------------------------------------------------------------* *& *& *&--------------------------------------------------------------------* REPORT

zsr_test.

PARAMETERS p_num TYPE i. DATA factorial TYPE i. *---------------------------------------------------------------------* * CLASS cls DEFINITION *---------------------------------------------------------------------* * *---------------------------------------------------------------------* CLASS cls DEFINITION. PUBLIC SECTION. DATA: count TYPE i. METHODS meth IMPORTING num TYPE i EXPORTING fact TYPE i EXCEPTIONS exc. ENDCLASS. "cls DEFINITION *---------------------------------------------------------------------* * CLASS cls IMPLEMENTATION

Rohini kumar

sap abap consultant

*---------------------------------------------------------------------* * *---------------------------------------------------------------------* CLASS cls IMPLEMENTATION. METHOD meth. IF num GT 10. MESSAGE 'Factorial value is too Big' TYPE 'I' RAISING exc. ELSE. count = num. fact = num. DO. count = count - 1. fact = fact * count. IF count = 1. EXIT. ENDIF. ENDDO. ENDIF. ENDMETHOD. "meth ENDCLASS. "cls IMPLEMENTATION START-OF-SELECTION. DATA obj TYPE REF TO cls. CREATE OBJECT obj. CALL METHOD obj->meth EXPORTING num = p_num IMPORTING fact = factorial EXCEPTIONS exc = 1. IF sy-subrc 0. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ELSE. WRITE: / 'Factorial of ', p_num, ' =', factorial. ENDIF.

The output is as follows:

Rohini kumar

sap abap consultant

Factorial of 8 is as follows:

Method can be called from the same Method A method can call itself by Call Method statement inside the implementation of the method. Inside there method will have to be declared directly as CALL METHOD METH. That means object will not be mentioned here. Important point is that the method must have an EXIT point to get outside of the method. Otherwise the method will fall in an infinite calling/looping. Below is a program where we create a numerics table. Here we are getting the number by input parameter and multiplying it with the count. This count is initially 0 and it is gradually increased by 1. And the multiplied value is stored in a public variable and then it is written. Hence we are having the numerics table of input value. Inside the method we have called an Exit point. If the count is greater than 20 then it will get outside from the method otherwise it will continue for calling the method. *&--------------------------------------------------------------------* *& Report ZSR_TEST *& *&--------------------------------------------------------------------* *& *& *&------------------------------------------------------------

Rohini kumar

sap abap consultant

---------* REPORT

zsr_test.

PARAMETERS p_num TYPE i. *---------------------------------------------------------------------* * CLASS cls DEFINITION *---------------------------------------------------------------------* * *---------------------------------------------------------------------* CLASS cls DEFINITION. PUBLIC SECTION. DATA: count TYPE i, value TYPE i. METHODS m_tab. ENDCLASS. "cls DEFINITION *---------------------------------------------------------------------* * CLASS cls IMPLEMENTATION *---------------------------------------------------------------------* * *---------------------------------------------------------------------* CLASS cls IMPLEMENTATION. METHOD m_tab. count = count + 1. value = count * p_num. IF count GT 20. EXIT. ELSE. WRITE: / p_num, '*', count, '=', value. CALL METHOD m_tab. ENDIF. ENDMETHOD. "m_tab ENDCLASS. "cls IMPLEMENTATION START-OF-SELECTION. DATA obj TYPE REF TO cls. CREATE OBJECT obj. WRITE: /10 'Table of ', p_num, ':', /10 '-----------------------'. SKIP.

Rohini kumar

CALL METHOD obj->m_tab. Below is the Output:

ME in Method

sap abap consultant

Rohini kumar

sap abap consultant

When we declare a variable of any type in public section of a class then we can use it in any other implementation. Let us suppose we declare a variable of any type with a initial value in public section. Then we declare the variable again inside a method initiating with a different value. Now if we Write the variable inside the method, the system will print the changed value not the previous one. To reflect the previous value of the variable we have to use ME operator. Here is a program where we have delared a public variable v_text and initiate with a value. Then we have declared the same variable again but instantiate with different value. Inside the method we are writing that variable with ME operator and get the previously initiated value. By declaring directly we are getting the changed value. *&--------------------------------------------------------------------* *& Report ZSR_TEST *& *&--------------------------------------------------------------------* *& *& *&--------------------------------------------------------------------* REPORT

zsr_test.

*---------------------------------------------------------------------* * CLASS cls DEFINITION *---------------------------------------------------------------------* * *---------------------------------------------------------------------* CLASS cls DEFINITION. PUBLIC SECTION. DATA v_text TYPE char40 VALUE 'Class Attribute'. METHODS meth. ENDCLASS. "cls DEFINITION *---------------------------------------------------------------------* * CLASS cls IMPLEMENTATION *---------------------------------------------------------------------* * *-------------------------------------------------------------

Rohini kumar

sap abap consultant

---------* CLASS cls IMPLEMENTATION. METHOD meth. DATA v_text TYPE char40 VALUE 'Method Attribute'. WRITE: / me->v_text, / v_text. ENDMETHOD. "meth ENDCLASS. "cls IMPLEMENTATION START-OF-SELECTION. DATA obj TYPE REF TO cls. CREATE OBJECT obj. CALL METHOD obj->meth. Below is the Output:

Object Oriented ABAP - Constructor 1. Constructor gets Triggered when Object is Created 2. Import Parameter in Constructor 3. Constructor cannot have Export Parameter 4. Exception Handling in Constructor 5. Static Constructor Object Oriented ABAP - Inheritance 1. Child Class gets Access Public & Protected data-methods from Parent Class 2. Redefinition of Method by Child or Sub Class 3. Abstract Class cannot be Instantiated 4. Abstract Method 5. Final Class cannot have Subclass 6. Final Method cannot be redefined

Rohini kumar

sap abap consultant

7. Static Attributes exist only Once in Inheritance Tree 8. Sub Class Inherits Constructor of Super Class 9. Sub Class can Modify Constructor of Super Class 10. Static Constructor is Called Once in Program 11. Static Type and Dynamic Type 12. Static Type and Dynamic Type with New Components 13. Methods Hold Value of its Declared Class Object Oriented ABAP - Interface 1. Interface - Type of ABAP Objects 2. Interface is declared in Public Section of Class 3. All Methods of Interface must be Implemented 4. Interface Data are Valued by DATA VALUES 5. Final Method in Interface 6. Abstract Method in Interface 7. Interface can be Instantiated 8. Interface Static Attribute and Method are Accessed Directly 9. Nested Interface 10. Encapsulation by Interface Object Oriented ABAP - Friendship 1. Private Friends of Class 2. Sub Classes of Friend Class 3. Friendship is One Sided

Constructor gets Triggered when Object is Created Constructor is something which initializes the object. It provides the memory size to the object at the time of creation. In JAVA the name of constructor is the same with the class. Let us suppose we have a class named Test. So in JAVA we can create object in the following way.

Rohini kumar

sap abap consultant

Test objTest = new Test();

Here Test is the class, objTest is the name of the object. New is the keyword which indicates the creation of new object and Test() is the default constructor. Here the constructor name is similar with the class. Hence the constructor is created by automatically at the time of creation of object. Moreover we can say that when the object is created the constructor gets automatically triggered.

We can create our own constructor also. The name will be similar there.

Public Test(){

}

Inside the bracket {} we can write our custom function. This is also a default constructor because we are not passing any parameter but it is user driven. Whenever the object is created, it will get triggered. We also have parameterized constructor as follows.

Test objTest = new Test(9,10); OR Public Test(int a, int b){

}

Constructor doesn’t return anything. So its return type is nothing. Constructor initializes the object. Hence we can initialize any variable’s value inside the constructor.

The same concept is applied in ABAP. The name of the constructor is CONSTRUCTOR here; not the class name. It is defined with the METHODS statement. To execute the constructor there is no need to call the method. At the time of creating the object the constructor gets triggered.

In the following program we have defined a class cl_construct under which we have declared a constructor by methods statement. We also have a normal method here. At the implementation of this class we have implemented the constructor & normal method. Now under start of selection event we

Rohini kumar

sap abap consultant

create an object of that cl_construct class. We didn’t call the methods here. At the output we see the constructor gets triggered and the code has been executed.

REPORT

zsr_test.

*----------------------------------------------------------------------* * CLASS cl_construct DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_construct DEFINITION. PUBLIC SECTION. METHODS: m_meth, constructor. ENDCLASS. "cl_construct DEFINITION *----------------------------------------------------------------------* * CLASS cl_construct IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_construct IMPLEMENTATION. METHOD m_meth. WRITE: / 'General Method'. ENDMETHOD. "m_meth METHOD constructor. WRITE: / 'Constructor gets triggered object is created'. ENDMETHOD. "constructor ENDCLASS. "cl_construct IMPLEMENTATION START-OF-SELECTION. DATA obj TYPE REF TO cl_construct. CREATE OBJECT obj.

Below is the output:

Rohini kumar

sap abap consultant

Import Parameter in Constructor A constructor can have Import parameter and that parameter will get the value at the time of creating the object. Here we have a program where we have declared a constructor with Import parameter. Then we have implement the method constructor. After that we create the object and export a value to the import parameter.

REPORT

zsr_test.

PARAMETERS p_text TYPE char20. *----------------------------------------------------------------------* * CLASS cl_construct DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_construct DEFINITION. PUBLIC SECTION. METHODS constructor IMPORTING i_text TYPE char20. ENDCLASS. "cl_construct DEFINITION *----------------------------------------------------------------------* * CLASS cl_construct IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_construct IMPLEMENTATION. METHOD constructor. WRITE: /3 i_text, /3 'Date: ', sy-datum DD/MM/YYYY. ENDMETHOD. "constructor ENDCLASS. "cl_construct IMPLEMENTATION START-OF-SELECTION. DATA obj TYPE REF TO cl_construct. CREATE OBJECT obj EXPORTING i_text = p_text. Below is the output:

Rohini kumar

sap abap consultant

Constructor cannot have Export Parameter A constructor cannot have any Export parameter. If we declare Export parameter to constructor then it will create a syntax error. Below is the reflection:

Rohini kumar

sap abap consultant

Exception Handling in Constructor Constructor can have Exception parameter which will be declared at the time of declaration and instantiate at the time of creating the object. Exception parameter will take a constant value and sy-subrc will return that constant value if the exception raises. Here is a program which gives the output of Factorial value. The constructor method import the input parameter and has an exception parameter. In the method we declare the condition that if the input parameter is greater than 10 then exception will be raised otherwise the Factorial function will work. After the create object we check the value of sy-subrc and if exception raises then sy-subrc will return the constant value which is instantiated to exception parameter. REPORT

zsr_test.

PARAMETERS p_data TYPE i.

Rohini kumar

sap abap consultant

*----------------------------------------------------------------------* * CLASS cl_construct DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_construct DEFINITION. PUBLIC SECTION. METHODS constructor IMPORTING i_data TYPE i EXCEPTIONS exc. ENDCLASS. "cl_construct DEFINITION *----------------------------------------------------------------------* * CLASS cl_construct IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_construct IMPLEMENTATION. METHOD constructor. IF i_data GT 10. RAISE exc. ELSE. WRITE: / i_data, ' is less than 10'. ENDIF. ENDMETHOD. "constructor ENDCLASS. "cl_construct IMPLEMENTATION START-OF-SELECTION. DATA obj TYPE REF TO cl_construct. CREATE OBJECT obj EXPORTING i_data = p_data EXCEPTIONS exc = 1. IF sy-subrc 0. WRITE: / p_data, 'is greater than 10, so SY-SUBRC = ', sy-subrc. ENDIF.

Below is the Output:

Exception Raised:

Rohini kumar

sap abap consultant

Otherwise:

Static Constructor Static constructor is declared by CLASS-METHODS CLASS_CONSTRUCTOR statement. Here the naming convention must be followed. If we give any other name then that will not be the static constructor. Now static constuctor is called 1. before any other attributes and methods by using obj->var & obj->meth, 2. before any other static attributes and methods by using cls=>var & cls=>meth, 3. before creating of an object by using create object obj, 4. before registering an event handler method by using set handler cls=>meth

Rohini kumar

sap abap consultant

for the object obj. Below is a program where we have defined a static data and a static constructor. The static constructor is implemented in the method - endmethod portion. In the start of selection we write the static attribute but in the output we can see the static constructor function comes first.

REPORT

zsr_test.

*----------------------------------------------------------------------* * CLASS cl_construct DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_construct DEFINITION. PUBLIC SECTION. CLASS-DATA cl_v_text TYPE char40 VALUE 'SAP ABAP Object Oriented'. CLASS-METHODS class_constructor. ENDCLASS. "cl_construct DEFINITION *----------------------------------------------------------------------* * CLASS cl_construct IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_construct IMPLEMENTATION. METHOD class_constructor. WRITE: / 'Class Constructor gets triggered'. ENDMETHOD. "class_constructor ENDCLASS. "cl_construct IMPLEMENTATION START-OF-SELECTION. WRITE: / cl_construct=>cl_v_text. The following is the output.

Rohini kumar

sap abap consultant

In similar way if we write any statement after the start of selection, the static constructor will trigger first and then the system will go further, whatever the sequence is.

REPORT

zsr_test.

*----------------------------------------------------------------------* * CLASS cl_construct DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_construct DEFINITION. PUBLIC SECTION. CLASS-DATA cl_v TYPE char40 VALUE 'I am Class data'. CLASS-METHODS class_constructor. ENDCLASS. "cl_construct DEFINITION *----------------------------------------------------------------------* * CLASS cl_construct IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_construct IMPLEMENTATION. METHOD class_constructor. WRITE: / 'I am Class Constructor'. ENDMETHOD. "class_constructor ENDCLASS. "cl_construct IMPLEMENTATION START-OF-SELECTION. WRITE: / 'Hello', / cl_construct=>cl_v.

In the following program we are comparing class constructor with class method. The class constructor gets triggered first and then it comes for class method.

Rohini kumar

REPORT

sap abap consultant

zsr_test.

*----------------------------------------------------------------------* * CLASS cl_construct DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_construct DEFINITION. PUBLIC SECTION. CLASS-METHODS: class_meth, class_constructor. ENDCLASS. "cl_construct DEFINITION *----------------------------------------------------------------------* * CLASS cl_construct IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_construct IMPLEMENTATION. METHOD class_meth. WRITE: / 'Class Method gets triggered'. ENDMETHOD. "class_meth METHOD class_constructor. WRITE: / 'Class Constructor gets triggered'. ENDMETHOD. "class_constructor ENDCLASS. "cl_construct IMPLEMENTATION START-OF-SELECTION. CALL METHOD cl_construct=>class_meth.

Below is another version of the program where we are declaring an Import parameter to the static constructor element. We are instantiating the parameter with a value. But at the time of compilation we are getting syntactical error which says the Static Constructor cannot have any Import or Export parameter. Following is the reflection:

Rohini kumar

sap abap consultant

Child Class gets Access Public & Protected datamethods from Parent Class Inheritance is one of a concepts of Object Oriented programming where we have a parent class and a child class. Here thechild class has the access of all of its parent class attributes and methods declared in Public and Protected sections only. We have created a program where we define a Parent class. Under public section we declare an attribute and a method. Similarly we have created attribute & method in Protected section also. Now in the implementation we set some function for both of the methods. Then we have created the child class Inheriting from parent class. We declare

Rohini kumar

sap abap consultant

attribute & method in public section of the child class. Then we implement the class with having the functionality of the child class method. In the child class method we call the methods of parent class. After that in the start-of-selection we create an object for the child class. Here we are not creating object for parent class. Then we call the method of child class and we get all the data declared & implemented by parent class. Hence the child class has the access of all declared in public and protected section of Parent class.

REPORT

zsr_test NO STANDARD PAGE HEADING.

*----------------------------------------------------------------------* * CLASS cl_parent DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_parent DEFINITION. PUBLIC SECTION. "Accessible to child class DATA v_parent_pub TYPE char40. METHODS m_parent_pub. PROTECTED SECTION. "Accessible to child class DATA v_parent_pro TYPE char40. METHODS m_parent_pro. PRIVATE SECTION. "Not Accessible to child class DATA v_parent_pri TYPE char40. METHODS m_parent_pri. ENDCLASS. "cl_parent DEFINITION *----------------------------------------------------------------------* * CLASS cl_parent IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_parent IMPLEMENTATION. METHOD m_parent_pub. v_parent_pub = 'Parent Class Public data'. ENDMETHOD. "m_parent_pub METHOD m_parent_pro. v_parent_pro = 'Parent Class Protected data'. ENDMETHOD. "m_parent_pro METHOD m_parent_pri. v_parent_pri = 'Parent Class Private data'. ENDMETHOD. "m_parent_pri ENDCLASS. "cl_parent IMPLEMENTATION

Rohini kumar

sap abap consultant

*----------------------------------------------------------------------* * CLASS cl_child DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_child DEFINITION INHERITING FROM cl_parent. PUBLIC SECTION. DATA v_child_pub TYPE char40. METHODS m_child_pub. ENDCLASS. "cl_child DEFINITION *----------------------------------------------------------------------* * CLASS cl_child IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_child IMPLEMENTATION. METHOD m_child_pub. v_child_pub = 'Child Class Public data'. "Child can access public & protected methods CALL METHOD: m_parent_pub, m_parent_pro. "Child can access public & protected data WRITE: / v_parent_pub, / v_parent_pro, / v_child_pub. ENDMETHOD. "m_child_pub ENDCLASS. "cl_child IMPLEMENTATION START-OF-SELECTION. DATA obj TYPE REF TO cl_child. CREATE OBJECT obj. CALL METHOD obj->m_child_pub. Now we have some step by analysis in debugging mode. At first we set the break point at calling method and controller will enter into the method. Here we can see that the child class have access to all public & protected data of parent class. It doesn't have any access to it's private data.

Rohini kumar

So it populates all of those accordingly.

Below is the output:

sap abap consultant

Rohini kumar

sap abap consultant

Redefinition of Method by Child or Sub Class Subclass can redefine the inherited methods and can change its functionality as per requirement. Here we have to declare the methods in child class like this - METHODS METH REDEFINITION. We have a program where we have defined 2 classes - Parent and Child classes. In the Parent class we have declared data and method in the Public section as well as Protected section. Next we have implemented these two methods as well. Then we have declared the child class inheriting from parent class. There we have declared the same methods in Public and Protected section. The only difference is that we have Redefined these methods by the statement REDEFINITION. Here at the time of implementation we have changed the functionality of those methods as well. Now in the start of selection we just call the methods by instantiating the classes. In the output we can have the reflection of parent function as well as child function also. REPORT

zsr_test NO STANDARD PAGE HEADING.

*----------------------------------------------------------------------* * CLASS cl_parent DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_parent DEFINITION. PUBLIC SECTION. DATA v_par_pub TYPE char40. METHODS m_test_pub. PROTECTED SECTION. DATA v_par_pro TYPE char40. METHODS m_test_pro. ENDCLASS. "cl_parent DEFINITION *----------------------------------------------------------------------* * CLASS cl_parent IMPLEMENTATION *----------------------------------------------------------------------*

Rohini kumar

sap abap consultant

* *----------------------------------------------------------------------* CLASS cl_parent IMPLEMENTATION. METHOD m_test_pub. v_par_pub = 'Parent class Public data'. CALL METHOD m_test_pro. "Protected method cannot be called from user level WRITE: / v_par_pub, / v_par_pro. ENDMETHOD. "m_test_pub METHOD m_test_pro. v_par_pro = 'Parent class Protected data'. ENDMETHOD. "m_test_pro ENDCLASS. "cl_parent IMPLEMENTATION *----------------------------------------------------------------------* * CLASS cl_child DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_child DEFINITION INHERITING FROM cl_parent. PUBLIC SECTION. DATA v_chi_pub TYPE char40. "Same method of parent class is redefined METHODS m_test_pub REDEFINITION. PROTECTED SECTION. DATA v_chi_pro TYPE char40. "Same method of parent class is redefined METHODS m_test_pro REDEFINITION. ENDCLASS. "cl_child DEFINITION *----------------------------------------------------------------------* * CLASS cl_child IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_child IMPLEMENTATION. METHOD m_test_pub. v_chi_pub = 'Child class Public data'. CALL METHOD m_test_pro. "Protected method cannot be called from user level WRITE: / v_chi_pub, / v_chi_pro. ENDMETHOD. "m_test_pub

Rohini kumar

sap abap consultant

METHOD m_test_pro. v_chi_pro = 'Child class Protected data'. ENDMETHOD. "m_test_pro ENDCLASS. "cl_child IMPLEMENTATION START-OF-SELECTION. DATA: obj_par TYPE REF TO cl_parent, obj_chi TYPE REF TO cl_child. CREATE OBJECT: obj_par, obj_chi. "Calling the public methods only CALL METHOD: obj_par->m_test_pub. SKIP. CALL METHOD: obj_chi->m_test_pub. Below is the Output:

Abstract Class cannot be Instantiated Abstract class is something which contains at least one abstract method. Now abstract method is not implemented at the implementation of abstract class. To implement that abstract method we have to create a subclass of the abstract class and then we can implement it.

We cannot create any object (instance) for an abstract class. If we instantiate then syntax error will come. We can call normal methods defined in abstract class inside the subclass (child class). Now by instantiating the subclass we can have normal methods of abstract class.

Rohini kumar

sap abap consultant



In the following program we define an abstract class cl_abs.



We have a normal method m_normal and an abstract method m_abs.



At the implementation part we can implement normal methods only.



Now we define a subclass cl_normal of the abstract class cl_abs.



There we define a normal method m_child and redefine the abstract method m_abs.



Since we already have defined the abstract method m_abs, we have to redefine it.



At the implementation part of this subclass we can implement the abstract method m_abs.



Here we can call the normal method of abstract class.



Now at the start of selection we only can create object for the subclass because abstract class cannot be instantiated.

Rohini kumar

REPORT

sap abap consultant

zsr_test NO STANDARD PAGE HEADING.

*----------------------------------------------------------------------* * CLASS cl_abs DEFINITION *----------------------------------------------------------------------* * This Abstract class contains abstract & normal methods *----------------------------------------------------------------------* CLASS cl_abs DEFINITION ABSTRACT. PUBLIC SECTION. METHODS: m_normal, m_abs ABSTRACT. "It will not be implemented ENDCLASS. "cl_abs DEFINITION *----------------------------------------------------------------------* * CLASS cl_abs IMPLEMENTATION *----------------------------------------------------------------------* * Only the normal method will be implemented here *----------------------------------------------------------------------* CLASS cl_abs IMPLEMENTATION. METHOD m_normal. WRITE: /3 'Normal Method in Abstract class'. ENDMETHOD. "m_normal ENDCLASS. "cl_abs IMPLEMENTATION *----------------------------------------------------------------------* * CLASS cl_normal DEFINITION - Concrete Class *----------------------------------------------------------------------* * Creating child class to implement the abstract method *----------------------------------------------------------------------* CLASS cl_normal DEFINITION INHERITING FROM cl_abs. PUBLIC SECTION. METHODS: m_abs REDEFINITION, "Since it has already been declared m_child. ENDCLASS. "cl_normal DEFINITION *----------------------------------------------------------------------* * CLASS cl_normal IMPLEMENTATION *----------------------------------------------------------------------* * The predefined abstract method is implemented in Concrete Class *----------------------------------------------------------------------* CLASS cl_normal IMPLEMENTATION. METHOD m_abs. "Previously defined Abstract Method WRITE: /3 'Abstract method from child of Abstract class'. SKIP. ENDMETHOD. "m_abs METHOD m_child. CALL METHOD m_normal. WRITE: /3 'Normal child method'. ENDMETHOD. "m_child

Rohini kumar

ENDCLASS.

sap abap consultant

"cl_normal IMPLEMENTATION

START-OF-SELECTION. DATA: obj_normal TYPE REF TO cl_normal. CREATE OBJECT obj_normal. "Instantiating the Concrete class CALL METHOD: obj_normal->m_abs, obj_normal->m_child.

Abstract Method Abstract method can only be implemented in any of sub classes of the parent class by using REDEFINITION of the method. If we try to implement any abstract method then syntax error will come as follows:

Rohini kumar

sap abap consultant

We can implement the abstract method in any child or sub class by defining the method with REDEFINITION. Below is the same program but we have implemented the abstract method in the child class inherited from the parent. Before implementation we have defined the method by REDEFINITION. In this way we can use an abstract method in any program. REPORT

zsr_test NO STANDARD PAGE HEADING.

*----------------------------------------------------------------------* * CLASS cl_abs DEFINITION *----------------------------------------------------------------------* * This Abstract class contains abstract & normal methods *----------------------------------------------------------------------* CLASS cl_abs DEFINITION ABSTRACT. PUBLIC SECTION. METHODS: m_normal, m_abs ABSTRACT. "It will not be implemented ENDCLASS. "cl_abs DEFINITION *----------------------------------------------------------------------* * CLASS cl_abs IMPLEMENTATION

Rohini kumar

sap abap consultant

*----------------------------------------------------------------------* * Only the normal method will be implemented here *----------------------------------------------------------------------* CLASS cl_abs IMPLEMENTATION. METHOD m_normal. WRITE: /3 'Normal Method in Abstract class'. ENDMETHOD. "m_normal ENDCLASS. "cl_abs IMPLEMENTATION *----------------------------------------------------------------------* * CLASS cl_normal DEFINITION - Concrete Class *----------------------------------------------------------------------* * Creating child class to implement the abstract method *----------------------------------------------------------------------* CLASS cl_normal DEFINITION INHERITING FROM cl_abs. PUBLIC SECTION. METHODS: m_abs REDEFINITION, "Since it has already been declared m_child. ENDCLASS. "cl_normal DEFINITION *----------------------------------------------------------------------* * CLASS cl_normal IMPLEMENTATION *----------------------------------------------------------------------* * The predefined abstract method is implemented here *----------------------------------------------------------------------* CLASS cl_normal IMPLEMENTATION. METHOD m_abs. "Previously defined Abstract Method WRITE: /3 'Abstract method from child of Abstract class'. CALL METHOD m_normal. SKIP. ENDMETHOD. "m_abs METHOD m_child. WRITE: /3 'Normal child method'. ENDMETHOD. "m_child ENDCLASS. "cl_normal IMPLEMENTATION START-OF-SELECTION. DATA: obj_normal TYPE REF TO cl_normal. CREATE OBJECT obj_normal. "Instantiating the Concrete class CALL METHOD: obj_normal->m_abs, obj_normal->m_child. Below is the Output:

Rohini kumar

sap abap consultant

Final Class cannot have Sub Class Final class which is another type of class in OOPs concept cannot have any child or sub classes. If we create any sub class of final class then syntax error will come as follows:

Rohini kumar

sap abap consultant

Below is another version of the program where we have defined two Final classes and in the start of selection we are calling the methods declared in final classes.

*&---------------------------------------------------------------------* *& Report ZSR_TEST *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

zsr_test.

*----------------------------------------------------------------------* * CLASS cls1 DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls1 DEFINITION FINAL. PUBLIC SECTION. DATA v_txt TYPE char50. METHODS m_cls1. ENDCLASS. "cls1 DEFINITION *----------------------------------------------------------------------* * CLASS cls2 DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls2 DEFINITION FINAL. PUBLIC SECTION. METHODS m_cls2. ENDCLASS. "cls2 DEFINITION *----------------------------------------------------------------------* * CLASS cls1 IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls1 IMPLEMENTATION. METHOD m_cls1. v_txt = 'Final Class One'. WRITE / v_txt. ENDMETHOD. "m_cls1

Rohini kumar

ENDCLASS.

sap abap consultant

"cls1 IMPLEMENTATION

*----------------------------------------------------------------------* * CLASS cls2 IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls2 IMPLEMENTATION. METHOD m_cls2. WRITE / 'Final Class Two'. ENDMETHOD. "m_cls2 ENDCLASS. "cls2 IMPLEMENTATION START-OF-SELECTION. DATA: obj1 TYPE REF TO cls1, obj2 TYPE REF TO cls2. CREATE OBJECT: obj1, obj2. CALL METHOD: obj1->m_cls1, obj2->m_cls2.

Below is the Output:

Final Method cannot be redefined Final Method cannot be redefined. If we declare a method with FINAL statement then that method cannot be declared with REDEFINITION. In this case syntax error will come at the time of compilation as mentioned below:

Rohini kumar

sap abap consultant

Below we have created another version of the program where we have changed the name of the method in the child class. That's mean here the program has two different methods in parent and child class. Being a child/sub class it is inheriting all the data and methods from parent class.

*&---------------------------------------------------------------------* *& Report ZSR_TEST *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

zsr_test.

Rohini kumar

sap abap consultant

*----------------------------------------------------------------------* * CLASS parent DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS parent DEFINITION. PUBLIC SECTION. DATA v_txt TYPE char50. METHODS m_par FINAL. ENDCLASS. "parent DEFINITION *----------------------------------------------------------------------* * CLASS parent IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS parent IMPLEMENTATION. METHOD m_par. v_txt = 'Final Method in Parent Class'. WRITE / v_txt. ENDMETHOD. "m_par ENDCLASS. "parent IMPLEMENTATION *----------------------------------------------------------------------* * CLASS child DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS child DEFINITION INHERITING FROM parent. PUBLIC SECTION. METHODS m_chi FINAL. ENDCLASS. "child DEFINITION *----------------------------------------------------------------------* * CLASS child IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS child IMPLEMENTATION. METHOD m_chi. CALL METHOD m_par. v_txt = 'Final Method in Child Class'. WRITE / v_txt. ENDMETHOD. "m_chi ENDCLASS. "child IMPLEMENTATION START-OF-SELECTION. DATA: obj_chi TYPE REF TO child. CREATE OBJECT obj_chi.

Rohini kumar

sap abap consultant

CALL METHOD obj_chi->m_chi.

Below is the Output:

Static Attributes exist only Once in Inheritance Tree Here we have a program where we have defined a class cls1. In the public section we are declaring static data and method. Then we have implemented the class cls1 with the method. The method will write the static data. Next we declare second class cls2 and third class cls3 inheriting from cls1. In the start of selection we give value to the static data and call the static method. Here the point is that we give value to the cls3 class data and we call method of cls2. In spite of this we are getting the same value as an output because cls2 and cls3 are the sub classes of cls1. Hence we can say that the static attributes exist only once in each Inheritance tree. We can change the value of static data from the outside only one time and that change will reflect to all other sub classes. So they will be visible to other classes in the Inheritance tree.

*&---------------------------------------------------------------------* *& Report ZSR_TEST *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

zsr_test.

*----------------------------------------------------------------------* * CLASS cls1 DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls1 DEFINITION.

Rohini kumar

sap abap consultant

PUBLIC SECTION. CLASS-DATA v_txt TYPE char50. CLASS-METHODS m_cls1. ENDCLASS. "cls1 DEFINITION *----------------------------------------------------------------------* * CLASS cls1 IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls1 IMPLEMENTATION. METHOD m_cls1. WRITE / v_txt. ENDMETHOD. "m_cls1 ENDCLASS. "cls1 IMPLEMENTATION *----------------------------------------------------------------------* * CLASS cls2 DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls2 DEFINITION INHERITING FROM cls1. ENDCLASS. "cls2 DEFINITION *----------------------------------------------------------------------* * CLASS cls3 DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls3 DEFINITION INHERITING FROM cls1. ENDCLASS. "cls3 DEFINITION START-OF-SELECTION. cls3=>v_txt = 'SAP ABAP Object Oriented'. CALL METHOD cls2=>m_cls1.

Below is the Output:

Rohini kumar

sap abap consultant

This similar output can be produced in a different way also. Below is another version of the program and here we have declared 2 methods in the class cls1. In the 2nd method we are giving the value to the static data and then calling the 1st method. In the start of selection we just call the 2nd method by class cls1 and we are getting the similar output.

*&---------------------------------------------------------------------* *& Report

ZSR_TEST

*& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------*

REPORT

zsr_test.

*----------------------------------------------------------------------* *

CLASS cls1 DEFINITION

*----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls1 DEFINITION. PUBLIC SECTION. CLASS-DATA v_txt TYPE char50. CLASS-METHODS: m_cls1, m_cls2. ENDCLASS.

"cls1 DEFINITION

*----------------------------------------------------------------------* *

CLASS cls1 IMPLEMENTATION

*----------------------------------------------------------------------* *

Rohini kumar

sap abap consultant

*----------------------------------------------------------------------* CLASS cls1 IMPLEMENTATION. METHOD m_cls1. WRITE / v_txt. ENDMETHOD.

"m_cls1

METHOD m_cls2. v_txt = 'SAP ABAP Object Oriented'. CALL METHOD m_cls1. ENDMETHOD. ENDCLASS.

"m_cls2 "cls1 IMPLEMENTATION

*----------------------------------------------------------------------* *

CLASS cls2 DEFINITION

*----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls2 DEFINITION INHERITING FROM cls1. ENDCLASS.

"cls2 DEFINITION

*----------------------------------------------------------------------* *

CLASS cls3 DEFINITION

*----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls3 DEFINITION INHERITING FROM cls1. ENDCLASS.

START-OF-SELECTION.

"cls3 DEFINITION

Rohini kumar

sap abap consultant

CALL METHOD cls3=>m_cls2.

Output:

Sub Class Inherits Constructor of Super Class Constructor of Super class is Inherited by its Sub classes. Hence super class needs not to be instantiated in the data processing to fetch the functionality of the constructor. We have derived a program where we have defined a parent class and a constructor in the public section. Then we implement that constructor in the implementation portion. Next we define child class inheriting from parent class. Now in the start of selection when we create the object of child class then the constructor method is called automatically and we get the functionality implemented in that constructor. Hence we are not creating any object for the parent class but we can get its constructor. Here we are getting the constructor of super class by creating the object of sub class.

REPORT

zsr_test NO STANDARD PAGE HEADING.

*----------------------------------------------------------------------* * CLASS test DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS test DEFINITION. PUBLIC SECTION. DATA v_test TYPE char40. METHODS constructor. ENDCLASS. "test DEFINITION *----------------------------------------------------------------------* * CLASS test IMPLEMENTATION *----------------------------------------------------------------------* *

Rohini kumar

sap abap consultant

*----------------------------------------------------------------------* CLASS test IMPLEMENTATION. METHOD constructor. v_test = 'SAP ABAP Object Oriented'. WRITE: / v_test. ENDMETHOD. "constructor ENDCLASS. "test IMPLEMENTATION *----------------------------------------------------------------------* * CLASS child DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS child DEFINITION INHERITING FROM test. ENDCLASS. "child DEFINITION *----------------------------------------------------------------------* * CLASS child IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS child IMPLEMENTATION. ENDCLASS. "child IMPLEMENTATION START-OF-SELECTION. DATA obj TYPE REF TO child. CREATE OBJECT obj. Below is the output:

Sub Class can Modify Constructor of Super Class Subclass can modify the constructor method which has already been implemented by the super class. In this modification sub class can add some extra functionality as per requirement. And also the constructor of the super class has to be called by using CALL METHOD SUPER->CONSTRUCTOR statement inside the implementation in sub classes. Here REDEFINITION statement is not needed to modify the constructor. If we call the constructor directly then syntax error will be there.

Rohini kumar

sap abap consultant

We have created a program where we have defined a parent class then a child class (Inherited from parent class) and then a grandchild class (Inherited from child class). We have implemented a constructor in parent class and then re implemented it in the child class as well as in the grandchild class. Here we have to call the super class constructor method at first then we can modify the constructor. We have used the statement for calling the super class constructor - CALL METHOD SUPER->CONSTRUCTOR. Output will be coming in the order of Parent->Child->Grandchild. Here we have not used the REDEFINITION statement to modify the constructor. REPORT

zsr_test NO STANDARD PAGE HEADING.

*----------------------------------------------------------------------* * CLASS cl_grand DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_grand DEFINITION. PUBLIC SECTION. DATA v_grand TYPE char40. METHODS constructor.

Rohini kumar

ENDCLASS.

sap abap consultant

"cl_grand DEFINITION

*----------------------------------------------------------------------* * CLASS cl_grand IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_grand IMPLEMENTATION. METHOD constructor. v_grand = 'I am from Grand Parent class'. WRITE: /3 v_grand. ENDMETHOD. "constructor ENDCLASS. "cl_grand IMPLEMENTATION *----------------------------------------------------------------------* * CLASS cl_parent DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_parent DEFINITION INHERITING FROM cl_grand. PUBLIC SECTION. METHODS constructor. ENDCLASS. "cl_parent DEFINITION *----------------------------------------------------------------------* * CLASS cl_parent IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_parent IMPLEMENTATION. METHOD constructor. CALL METHOD super->constructor. v_grand = 'I am from Parent class'. WRITE: /3 v_grand. ENDMETHOD. "constructor ENDCLASS. "cl_parent IMPLEMENTATION *----------------------------------------------------------------------* * CLASS cl_child DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_child DEFINITION INHERITING FROM cl_parent. PUBLIC SECTION. METHODS constructor. ENDCLASS. "cl_child DEFINITION *----------------------------------------------------------------------* * CLASS cl_child IMPLEMENTATION *----------------------------------------------------------------------*

Rohini kumar

sap abap consultant

* *----------------------------------------------------------------------* CLASS cl_child IMPLEMENTATION. METHOD constructor. CALL METHOD super->constructor. v_grand = 'I am from Child class'. WRITE: /3 v_grand. ENDMETHOD. "constructor ENDCLASS. "cl_child IMPLEMENTATION START-OF-SELECTION. DATA obj_child TYPE REF TO cl_child. CREATE OBJECT obj_child. Below is the Output:

Static Constructor is Called Once in Program Static constructor of a class is called only one time in a program. In this matter when a sub class is accessed then its static constructor is called only once in that program. Here it needs to be remembered that whenever a static constructor is executed the system checks any of its super class constructor has been executed or not. Now if the super class constructor has not been executed then the system will execute that first then the sub class constructor will be executed. Here we have a program where we have defined a grand parent class with a static constructor method. We have implemented it with a specific functionality. Next we have created a parent class with having the same static constructor but we have implemented it differently. Similarly we have created a child class with having the same static constructor but we have implemented it more differently. The naming convention will be class_constructor. Now in the start of selection we have instantiated the child class only. In the output we can get both the functions of grandparent class and parent class and also child class static constructor methods. The output will come with this order: Grand Parent->Parent>Child. Here the object of parent and child class need not to be created to view the output. We have mentioned the previous program here and the constructor is

Rohini kumar

sap abap consultant

class_constructor. There is no need to call method separately for static constructor. It is called automatically by instantiating.

REPORT

zsr_test NO STANDARD PAGE HEADING.

*----------------------------------------------------------------------* * CLASS cl_grand DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_grand DEFINITION. PUBLIC SECTION. CLASS-DATA v_test TYPE char40. CLASS-METHODS class_constructor. ENDCLASS. "cl_grand DEFINITION *----------------------------------------------------------------------* * CLASS cl_grand IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_grand IMPLEMENTATION. METHOD class_constructor. v_test = 'Static Constructor - Grand Parent'. WRITE: /3 v_test. ENDMETHOD. "class_constructor ENDCLASS. "cl_grand IMPLEMENTATION *----------------------------------------------------------------------* * CLASS cl_parent DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_parent DEFINITION INHERITING FROM cl_grand. PUBLIC SECTION. CLASS-METHODS class_constructor. ENDCLASS. "cl_parent DEFINITION *----------------------------------------------------------------------* * CLASS cl_parent IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_parent IMPLEMENTATION. METHOD class_constructor. v_test = 'Static Constructor - Parent'. WRITE: /3 v_test. ENDMETHOD. "class_constructor ENDCLASS. "cl_parent IMPLEMENTATION

Rohini kumar

sap abap consultant

*----------------------------------------------------------------------* * CLASS cl_child DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_child DEFINITION INHERITING FROM cl_parent. PUBLIC SECTION. CLASS-METHODS class_constructor. ENDCLASS. "cl_child DEFINITION *----------------------------------------------------------------------* * CLASS cl_child IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_child IMPLEMENTATION. METHOD class_constructor. v_test = 'Static Constructor - Child'. WRITE: /3 v_test. ENDMETHOD. "class_constructor ENDCLASS. "cl_child IMPLEMENTATION START-OF-SELECTION. DATA obj_child TYPE REF TO cl_child. CREATE OBJECT obj_child. Below is the Output:

Static type and Dynamic type Static reference type cannot be changed further so it can point to a super class only. Dynamic reference type can be modified further so it can point to sub classes. Hence a static type of reference can point to super class only whereas a dynamic type of reference can point to any of its sub classes only. In the following program there two classes - parent & child. Here parent is the dynamic and child is static. Hence we can point parent class into child class. Here the child class is static and always points to parent class.

Rohini kumar

sap abap consultant

So, obj_parent3 = obj_child is correct. Because child class (static) can point to parent class and this parent class in pointing to its subclass. But obj_child = obj_parent1 is incorrect. Because parent class (dynamic) can not be converted to its subclass.

REPORT

zsr_test NO STANDARD PAGE HEADING.

*----------------------------------------------------------------------* * CLASS cl_parent DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_parent DEFINITION. PUBLIC SECTION. DATA v_test TYPE char40. METHODS m_parent. ENDCLASS. "cl_parent DEFINITION *----------------------------------------------------------------------* * CLASS cl_parent IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_parent IMPLEMENTATION. METHOD m_parent. v_test = 'I am from Parent class'. WRITE: /3 v_test. ENDMETHOD. "m_parent ENDCLASS. "cl_parent IMPLEMENTATION *----------------------------------------------------------------------* * CLASS cl_child DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_child DEFINITION INHERITING FROM cl_parent. PUBLIC SECTION. METHODS m_parent REDEFINITION. ENDCLASS. "cl_child DEFINITION *----------------------------------------------------------------------* * CLASS cl_child IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_child IMPLEMENTATION. METHOD m_parent. v_test = 'I am from Child class'. WRITE: /3 v_test.

Rohini kumar

sap abap consultant

ENDMETHOD. ENDCLASS. START-OF-SELECTION. DATA: obj_parent1 obj_parent2 obj_parent3 obj_child

"m_parent "cl_child IMPLEMENTATION

TYPE TYPE TYPE TYPE

REF REF REF REF

TO TO TO TO

cl_parent, cl_parent, cl_parent, cl_child.

CREATE OBJECT: obj_parent1, "Parent class object obj_parent2 TYPE cl_child, "Child class object obj_parent3, "Parent class object obj_child. "Child class object obj_parent3 = obj_child. "Parent converted into child class object CALL METHOD: obj_parent1->m_parent, obj_parent2->m_parent, obj_parent3->m_parent, obj_child->m_parent.

"Calling "Calling "Calling "Calling

parent method child method child method child method

Below is the output:

Static Type and Dynamic Type with New Components When a sub class contains new components which are not declared in the super class then calling the new components by creating a dynamic reference of super class will cause syntax error in the system. Here we have defined a parent class and a child class inheriting from parent class. In the parent class we have declared a method m_par. And in the child class we have redefined the method m_par and a new method m_chi. Now we have declared the object obj_par referenced by parent and object obj_chi referenced by child statically. At the time of object creation we have referenced obj_par of type child by dynamically. Now whenever we call the method m_chi by obj_par then syntax error will come. Here the error comes because the method m_chi is unknown to parent class though obj_par is dynamically referenced to child class. Hence the super class object obj_par has found the method

Rohini kumar

sap abap consultant

m_chi a new component which is not declared in the super class.

Below we have defined a modified version of the program where we have created static reference of parent and child class. Then we have called method from super class. After that we have called the same but redefined method from sub class by creating the dynamic reference of the super class. *&---------------------------------------------------------------------* *& Report ZSR_TEST *& *&---------------------------------------------------------------------* *& *&

Rohini kumar

sap abap consultant

*&---------------------------------------------------------------------* REPORT

zsr_test.

*----------------------------------------------------------------------* * CLASS parent DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS parent DEFINITION. PUBLIC SECTION. DATA v_txt TYPE char50. METHODS m_par. ENDCLASS. "parent DEFINITION *----------------------------------------------------------------------* * CLASS child DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS child DEFINITION INHERITING FROM parent. PUBLIC SECTION. METHODS m_par REDEFINITION. METHODS m_chi. ENDCLASS. "child DEFINITION *----------------------------------------------------------------------* * CLASS parent IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS parent IMPLEMENTATION. METHOD m_par. v_txt = 'Parent Class Method'. WRITE / v_txt. ENDMETHOD. "m_par ENDCLASS. "parent IMPLEMENTATION *----------------------------------------------------------------------* * CLASS child IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS child IMPLEMENTATION. METHOD m_par. v_txt = 'Parent Class Method from Child Class'. WRITE / v_txt. ENDMETHOD. "m_par METHOD m_chi.

Rohini kumar

sap abap consultant

v_txt = 'Child Class Method'. WRITE / v_txt. ENDMETHOD. "m_chi ENDCLASS. "child IMPLEMENTATION START-OF-SELECTION. DATA: obj_par TYPE REF TO parent, "Static Type obj_chi TYPE REF TO child. "Static Type CREATE OBJECT obj_par. CALL METHOD obj_par->m_par. "Call the method from Parent class SKIP. CREATE OBJECT: obj_par TYPE child, obj_chi. CALL METHOD: obj_par->m_par, "Call the Redefinition of m_par obj_chi->m_chi. The following is the Output:

Methods Hold Value of its Declared Class Method of a super class always holds super class attributes declared in public/protected/private sections. If the method is redefined in any of its sub class then only it can modify the value of its attributes. Otherwise the method always holds the value of that class where it has been declared. Here is a program where we have defined parent class where we have defined a method m_par in public and an attribute with value in private section. In the implementation of the method we are writing the private data. Then we have defined a child class inheriting from the parent class. There we have defined an attribute with value and a method in public section. Now at the time of implementation of the method we just call the parent class method. Finally we are creating the object of child class and calling the child class method. Here we are finding the output which holds the value of the parent class attribute, not the child class attribute. Hence we can say that the super class method holds super class attribute if the method is not redefined in the sub class.

Rohini kumar

sap abap consultant

*&---------------------------------------------------------------------* *& Report ZSR_TEST *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

zsr_test.

*----------------------------------------------------------------------* * CLASS parent DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS parent DEFINITION. PUBLIC SECTION. METHODS m_par. PRIVATE SECTION. DATA v_txt TYPE char50 VALUE 'Private Data from Parent Class'. ENDCLASS. "parent DEFINITION *----------------------------------------------------------------------* * CLASS child DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS child DEFINITION INHERITING FROM parent. PUBLIC SECTION. DATA v_txt TYPE char50 VALUE 'SAP ABAP OOPs'. METHODS m_chi. ENDCLASS. "child DEFINITION *----------------------------------------------------------------------* * CLASS parent IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS parent IMPLEMENTATION. METHOD m_par. WRITE / v_txt. ENDMETHOD. "m_par ENDCLASS. "parent IMPLEMENTATION *----------------------------------------------------------------------* * CLASS child IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------*

Rohini kumar

CLASS child IMPLEMENTATION. METHOD m_chi. CALL METHOD m_par. ENDMETHOD. ENDCLASS.

sap abap consultant

"m_chi "child IMPLEMENTATION

START-OF-SELECTION. DATA obj_chi TYPE REF TO child. CREATE OBJECT obj_chi. CALL METHOD obj_chi->m_chi.

In debugging mode we can see whenever the control goes to CALL METHOD obj_chi>m_chi then it goes to the child class method m_chi. At this point v_txt holds the data of child class, 'SAP ABAP OOPs'. But whenever it goes to CALL METHOD m_par inside the m_chi then the control goes to the parent class method m_par. Now herev_txt holds the data of parent class, 'Private Data from Parent Class'. Below is the Output:

Interface - Type of ABAP Objects Let us suppose we have two abstract classes, class A & class B. Now we know that the abstract class must contains at least one abstract method. Hence more than one abstract classes (here A & B) have abstract methods in each of those classes.

Now we want to implement those abstract methods. To do that we need to create a subclass of those abstract classes A & B. So a new class C will be created which is a subclass of A & B. This situation is called multiple inheritances. Unfortunately ABAP doesn’t support multiple inheritances.

To use this multiple inheritances functionality we need to use Interface.

Rohini kumar

sap abap consultant

Let us suppose we have two interfaces (A & B) rather than the classes. Now we can create a class C which implements these interfaces. When the relation is built by class to class, it needs inheritance. But when relation is built by interface to class it needs implements.

Now in this C class we implement all the abstract methods which have been declared in interfaces (A & B). After that we create an object of class C and then we can use it.

Most important point is that we can’t instantiate interface. It means we can’t define anything (like methods, constructor etc.) in interface. So to access the interface we need to create a normal class which implements those interfaces and then we can instantiate it.

Moreover we can refer the interface with the normal class. This is because to refer something there is no need to create object. Let’s take the example of JAVA.

We have interfaces A & B and we have normal class C. To instantiate the class C: C obj = new C( ); Here we are defining the default constructor C( ). But this will not happen for creating object of interface. Here we can refer the interface with the normal class C like: A objA = new C( ); B objB = new C( ); This is possible because we are using the memory of constructor C to create the reference of interface. It will work as a normal object. But this reference will have no access of class C’s methods. It can only use its own declared methods.

In the following program we have declared two interfaces i_a & i_b. Each of those contain own data and methods. Now we are declaring a normal class where we define normal data & methods. Then we declare the interfaces.

INTERFACES: i_a, i_b.

At the implementation point we define all the methods (own method and interface methods also). The interface method is defined like this.

Rohini kumar

sap abap consultant

METHOD i_a~m_inta. i_a~v_inta = 'I am from A Interface method'. WRITE: /3 i_a~v_inta. ENDMETHOD. "i_a~m_inta

After start of selection at the time of calling methods we need to call by this way.

CALL METHOD: obj->m_test, "Calling normal method obj->i_a~m_inta, "Calling interface method obj->i_b~m_intb. "Calling interface method

Whole program is as follows.

REPORT

zsr_test NO STANDARD PAGE HEADING.

*----------------------------------------------------------------------* * INTERFACE i_a *----------------------------------------------------------------------* * *----------------------------------------------------------------------* INTERFACE i_a. DATA v_inta TYPE char40. METHODS m_inta. ENDINTERFACE. "i_a *----------------------------------------------------------------------* * INTERFACE i_b *----------------------------------------------------------------------* * *----------------------------------------------------------------------* INTERFACE i_b. DATA v_intb TYPE char40. METHODS m_intb. ENDINTERFACE. "i_b *----------------------------------------------------------------------* * CLASS cl_c DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_c DEFINITION. PUBLIC SECTION. DATA v_test TYPE char40. METHODS m_test.

Rohini kumar

INTERFACES: i_a, i_b. ENDCLASS.

sap abap consultant

"cl_c DEFINITION

*----------------------------------------------------------------------* * CLASS cl_c IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_c IMPLEMENTATION. METHOD m_test. v_test = 'This is normal method'. WRITE: /3 v_test. ENDMETHOD. "m_test METHOD i_a~m_inta. i_a~v_inta = 'I am from A Interface method'. WRITE: /3 i_a~v_inta. ENDMETHOD. "i_a~m_inta METHOD i_b~m_intb. i_b~v_intb = 'I am from B Interface method'. WRITE: /3 i_b~v_intb. ENDMETHOD. "i_b~m_intb ENDCLASS. "cl_c IMPLEMENTATION START-OF-SELECTION. DATA obj TYPE REF TO cl_c. CREATE OBJECT obj. CALL METHOD: obj->m_test, obj->i_a~m_inta, obj->i_b~m_intb. SKIP. "Executing interface data WRITE: /3 'Interface Data: ',

The output is as follows:

"Calling normal method "Calling interface method "Calling interface method

obj->i_b~v_intb.

Rohini kumar

sap abap consultant

Interface is declared in Public Section of Class Interface can only be declared in the public section of any class. Any other section will cause syntax error in the system.

Here is a program where we have declared an Interface it. We have defined attribute and method in this interface. Next we have defined class where we have declared the interface in the protected section. After implementing the method we compile the program and a syntax error has come as follows.

Rohini kumar

sap abap consultant

Below is another version of the program where we have commented the protected section. And this time the program compiles successfully as produce the proper output.

*&---------------------------------------------------------------------* *& Report

ZSR_TEST

*& *&---------------------------------------------------------------------* *& *&

Rohini kumar

sap abap consultant

*&---------------------------------------------------------------------*

REPORT

zsr_test.

*----------------------------------------------------------------------* *

INTERFACE it

*----------------------------------------------------------------------* * *----------------------------------------------------------------------* INTERFACE it. DATA v_txt TYPE char50. METHODS meth. ENDINTERFACE.

"it

*----------------------------------------------------------------------* *

CLASS cls DEFINITION

*----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls DEFINITION. PUBLIC SECTION. DATA date TYPE sy-datum. INTERFACES it. ENDCLASS.

"cls DEFINITION

*----------------------------------------------------------------------* *

CLASS cls IMPLEMENTATION

*----------------------------------------------------------------------* *

Rohini kumar

sap abap consultant

*----------------------------------------------------------------------* CLASS cls IMPLEMENTATION. METHOD it~meth. date = sy-datum. WRITE: / it~v_txt, / 'Today''s Date is', date DD/MM/YYYY. ENDMETHOD. ENDCLASS.

"it~meth "cls IMPLEMENTATION

START-OF-SELECTION. DATA obj TYPE REF TO cls. CREATE OBJECT obj.

obj->it~v_txt = 'SAP ABAP Object Oriented'. CALL METHOD obj->it~meth.

The output is following:

All Methods of Interface must be Implemented A class which defines an Interface must implement all of its methods. If any method is missing in the implementation of that class then a compile error will come.

Rohini kumar

sap abap consultant

Below is a program where we have defined an Interface in which we declare attribute and methods meth1 and meth2. Now after defining the Interface in a class we implement only one method meth1. When we compile this program a syntax error comes with message implementation is missing for method meth2 as follows.

Here if we implement the second method then it successfully runs and produces the proper output.

*&---------------------------------------------------------------------*

Rohini kumar

*& Report

sap abap consultant

ZSR_TEST

*& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------*

REPORT

zsr_test.

*----------------------------------------------------------------------* *

INTERFACE it

*----------------------------------------------------------------------* * *----------------------------------------------------------------------* INTERFACE it. DATA v_txt TYPE char50. METHODS: meth1, meth2. ENDINTERFACE.

"it

*----------------------------------------------------------------------* *

CLASS cls DEFINITION

*----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls DEFINITION. PUBLIC SECTION. INTERFACES it. ENDCLASS.

"cls DEFINITION

Rohini kumar

sap abap consultant

*----------------------------------------------------------------------* *

CLASS cls IMPLEMENTATION

*----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls IMPLEMENTATION. METHOD it~meth1. it~v_txt = 'First Interface Method'. WRITE / it~v_txt. ENDMETHOD.

"it~meth1

METHOD it~meth2. it~v_txt = 'Second Interface Method'. WRITE / it~v_txt. ENDMETHOD.

"it~meth2

ENDCLASS.

"cls IMPLEMENTATION

START-OF-SELECTION. DATA obj TYPE REF TO cls. CREATE OBJECT obj. CALL METHOD: obj->it~meth1, obj->it~meth2.

The output is following:

Rohini kumar

sap abap consultant

Interface Data are Valued by DATA VALUES Interface attributes can be initiated with values at the time of Interface declaration in any class definition. If we give values like class attributes then compile error will come as follows:

Rohini kumar

sap abap consultant

Below is the program where we have initiated the Interface attribute values at the time of declaration by statement DATA VALUES. Now it successfully runs and produces the output.

*&---------------------------------------------------------------------* *& Report

ZSR_TEST

*& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------*

REPORT

zsr_test.

*----------------------------------------------------------------------* *

INTERFACE it

*----------------------------------------------------------------------* * *----------------------------------------------------------------------* INTERFACE it. DATA: v_txt1 TYPE char40, v_txt2 TYPE char40. METHODS mit. ENDINTERFACE.

"it

*----------------------------------------------------------------------* *

CLASS cls DEFINITION

*----------------------------------------------------------------------* *

Rohini kumar

sap abap consultant

*----------------------------------------------------------------------* CLASS cls DEFINITION. PUBLIC SECTION. INTERFACES it DATA VALUES v_txt1 = 'SAP ABAP' v_txt2 = 'Object Oriented'. ENDCLASS.

"cls DEFINITION

*----------------------------------------------------------------------* *

CLASS cls IMPLEMENTATION

*----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls IMPLEMENTATION. METHOD it~mit. WRITE: / it~v_txt1, / it~v_txt2. ENDMETHOD. ENDCLASS.

START-OF-SELECTION. DATA obj TYPE REF TO cls. CREATE OBJECT obj. CALL METHOD obj->it~mit.

The output is following:

"it~mit "cls IMPLEMENTATION

Rohini kumar

sap abap consultant

Final Method in Interface Final method is a method which cannot be redefined by sub classes. So as in Interface final method can be declared and implemented similar to a class final method. Here the interface needs to be declared in a class as INTERFACE it FINAL METHODS mit2 where mit2 is the final method.

In the following program we have defined one interface under which two methods have been declared. Inside the class we mention the second method is final. Again we have declared a subclass and redefined the first method. Now at the time of calling, we call two methods from two different objects of two classes.

REPORT

zsr_test NO STANDARD PAGE HEADING.

*----------------------------------------------------------------------* * INTERFACE i_interface *----------------------------------------------------------------------* * *----------------------------------------------------------------------* INTERFACE i_interface. DATA v_test TYPE char40. METHODS: m_normal, m_final. ENDINTERFACE. "i_interface *----------------------------------------------------------------------* * CLASS cl_parent DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_parent DEFINITION. PUBLIC SECTION. INTERFACES i_interface FINAL METHODS m_final. ENDCLASS. "cl_parent DEFINITION *----------------------------------------------------------------------*

Rohini kumar

sap abap consultant

* CLASS cl_parent IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_parent IMPLEMENTATION. METHOD i_interface~m_normal. i_interface~v_test = 'I am normal Method from Parent class'. WRITE: /3 i_interface~v_test. ENDMETHOD. "i_interface~m_normal METHOD i_interface~m_final. i_interface~v_test = 'I am final Method from Parent class'. WRITE: /3 i_interface~v_test. ENDMETHOD. "i_interface~m_final ENDCLASS. "cl_parent IMPLEMENTATION *----------------------------------------------------------------------* * CLASS cl_child DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_child DEFINITION INHERITING FROM cl_parent. PUBLIC SECTION. METHODS i_interface~m_normal REDEFINITION. ENDCLASS. "cl_child DEFINITION *----------------------------------------------------------------------* * CLASS cl_child IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cl_child IMPLEMENTATION. METHOD i_interface~m_normal. i_interface~v_test = 'I am redefined Method from Child class'. WRITE: /3 i_interface~v_test. ENDMETHOD. "i_interface~m_normal ENDCLASS. "cl_child IMPLEMENTATION START-OF-SELECTION. DATA: obj_parent TYPE REF TO cl_parent, obj_child TYPE REF TO cl_child. CREATE OBJECT: obj_parent, obj_child. CALL METHOD: obj_parent->i_interface~m_normal, obj_parent->i_interface~m_final, obj_child->i_interface~m_normal, obj_child->i_interface~m_final.

Rohini kumar

sap abap consultant

The following is the Output:

Abstract Method in Interface Abstract method is a method which cannot be implemented in the same class. Only sub class can implement an abstract method by redefinition. Important point is that the abstract method can be declared in an abstract class only. Now Interface can have an abstract method which must be declared in an abstract class as INTERFACES it ABSTRACT METHODS mit2 where mit2 is the abstract method. So mit2 will have to be implemented in any sub class by redefinition.

In the following program we have declared an Interface it where a text data and two methods mit1 and mit2 are there. Method mit2 is an abstract method so it will be declared in an abstract class. Now we are defining parent class as an abstract class where we are declaring the abstract interface method with the statement of INTERFACES it ABSTRACT METHODS mit2. In the implementation part we have implemented mit1 in parent class because abstract method mit2 cannot be implemented in the same class. So we have defined child class inheriting from parent class. We have redefined the abstract method as METHODS it~mit2 REDEFINITION in this child class. And then we can implement this properly. Finally in the start of selection we call these two methods by the child class object since abstract parent class cannot be instantiated.

*&---------------------------------------------------------------------* *& Report

ZSR_TEST

*& *&---------------------------------------------------------------------* *& *&

Rohini kumar

sap abap consultant

*&---------------------------------------------------------------------*

REPORT

zsr_test.

*----------------------------------------------------------------------* *

INTERFACE it

*----------------------------------------------------------------------* * *----------------------------------------------------------------------* INTERFACE it. DATA v_txt TYPE char50. METHODS: mit1, mit2. ENDINTERFACE.

"it

*----------------------------------------------------------------------* *

CLASS parent DEFINITION

*----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS parent DEFINITION ABSTRACT. PUBLIC SECTION. INTERFACES it ABSTRACT METHODS mit2. ENDCLASS.

"parent DEFINITION

*----------------------------------------------------------------------* *

CLASS parent IMPLEMENTATION

*----------------------------------------------------------------------* * *----------------------------------------------------------------------*

Rohini kumar

sap abap consultant

CLASS parent IMPLEMENTATION. METHOD it~mit1. it~v_txt = 'Abstract Parent Class - Normal Method'. WRITE / it~v_txt. ENDMETHOD. ENDCLASS.

"it~mit1 "parent IMPLEMENTATION

*----------------------------------------------------------------------* *

CLASS child DEFINITION

*----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS child DEFINITION INHERITING FROM parent. PUBLIC SECTION. METHODS it~mit2 REDEFINITION. ENDCLASS.

"child DEFINITION

*----------------------------------------------------------------------* *

CLASS child IMPLEMENTATION

*----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS child IMPLEMENTATION. METHOD it~mit2. it~v_txt = 'Normal Child Class - Abstract Method'. WRITE / it~v_txt. ENDMETHOD. ENDCLASS.

"it~mit2 "child IMPLEMENTATION

Rohini kumar

sap abap consultant

START-OF-SELECTION. DATA obj TYPE REF TO child. CREATE OBJECT obj. CALL METHOD: obj->it~mit1, obj->it~mit2.

The output is following:

Interface can be Instantiated Interface can be instantiated if the object is referenced to that interface. Here the class instance variable will have to be moved to the interface instance variable.

Following is a program where we have declared an interface containing constant, data and a method. Now the class definition contains only the interface in the public section. After that the method is implemented accordingly. In the start of selection we have created objects of the class. Here we have moved the class object variable to the interface variable and then we have called the method directly from the interface object variable.

*&---------------------------------------------------------------------* *& Report

ZSR_TEST

*& *&---------------------------------------------------------------------*

Rohini kumar

sap abap consultant

*& *& *&---------------------------------------------------------------------*

REPORT

zsr_test.

*----------------------------------------------------------------------* *

INTERFACE it

*----------------------------------------------------------------------* * *----------------------------------------------------------------------* INTERFACE it. CONSTANTS c_var TYPE char40 VALUE 'SAP ABAP Object Oriented'. DATA var TYPE char50. METHODS mit. ENDINTERFACE.

"it

*----------------------------------------------------------------------* *

CLASS cls DEFINITION

*----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls DEFINITION. PUBLIC SECTION. INTERFACES it. ENDCLASS.

"cls DEFINITION

*----------------------------------------------------------------------* *

CLASS cls IMPLEMENTATION

Rohini kumar

sap abap consultant

*----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls IMPLEMENTATION. METHOD it~mit. it~var = 'Interface Attribute'. WRITE: / 'Interface Method', / it~var, / it~c_var. ENDMETHOD.

"it~mit

ENDCLASS.

"cls IMPLEMENTATION

START-OF-SELECTION. DATA: obj_it TYPE REF TO it, obj

TYPE REF TO cls.

CREATE OBJECT obj. obj_it = obj. CALL METHOD obj_it->mit.

The following is the output:

Rohini kumar

sap abap consultant

Interface Static Attribute and Method are Accessed Directly Static attribute and method of Interface can be accessed directly in the data processing as of class. There is no need to create object for class.

Following program contains an interface where we have defined constants, static data and static method. The program contains a class which defines the interface and implements its method. Now in the start of selection we are valuing the class data directly by cls=>it~cls_var = 'Interface Data valued directly' statement and calling the method directly by call method cls=>it~cls_mit statement.

*&---------------------------------------------------------------------* *& Report

ZSR_TEST

*& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------*

REPORT

zsr_test.

*----------------------------------------------------------------------* *

INTERFACE it

*----------------------------------------------------------------------* * *----------------------------------------------------------------------* INTERFACE it. CONSTANTS c_var TYPE char40 VALUE 'SAP ABAP Object Oriented'. CLASS-DATA cls_var TYPE char50. CLASS-METHODS cls_mit.

Rohini kumar

sap abap consultant

ENDINTERFACE.

"it

*----------------------------------------------------------------------* *

CLASS cls DEFINITION

*----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls DEFINITION. PUBLIC SECTION. INTERFACES it. ENDCLASS.

"cls DEFINITION

*----------------------------------------------------------------------* *

CLASS cls IMPLEMENTATION

*----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls IMPLEMENTATION. METHOD it~cls_mit. WRITE: / 'Interface Method', / it~cls_var, / it~c_var. ENDMETHOD. ENDCLASS.

"it~cls_mit "cls IMPLEMENTATION

START-OF-SELECTION. cls=>it~cls_var = 'Interface Data valued directly'. CALL METHOD cls=>it~cls_mit.

Rohini kumar

sap abap consultant

The output is below:

Nested Interface We can create nested Interface by declaring one Interface in another Interface. We know to declare Interface in the class. The same syntax is followed here. In the class we only have to declare the Final Interface in public section. The following program contains Interface it1 and it2. It2 declares it1 inside. In the it1 we have two methods mit1 and mit2. Similarly in the it2 we have two methods mit1 and mit2. Now in mit1 of it1 we Select from EKKO and mit2 of it1 we Select from EKPO for all entries in internal table it_ekko. Next we have mit1 in it2 and mit2 in it2. In mit1 we are preparing the final output table whereas in mit2 we are preparing the Output. After all these in start of selection we create an object obj referenced to class cls. Next we call method mit1 and mit2 of it1 and mit1 and mit2 of it2 by object obj.

*&---------------------------------------------------------------------* *& Report ZSR_TEST *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

zsr_test.

TABLES: ekko, ekpo. SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001. SELECT-OPTIONS s_ebeln FOR ekko-ebeln OBLIGATORY. SELECTION-SCREEN END OF BLOCK b1.

Rohini kumar

sap abap consultant

*----------------------------------------------------------------------* * INTERFACE it1 *----------------------------------------------------------------------* * *----------------------------------------------------------------------* INTERFACE it1. METHODS: mit1, mit2. ENDINTERFACE. "it1 *----------------------------------------------------------------------* * INTERFACE it2 *----------------------------------------------------------------------* * *----------------------------------------------------------------------* INTERFACE it2. METHODS: mit1, mit2. INTERFACES it1. ENDINTERFACE. "it2 *----------------------------------------------------------------------* * CLASS cls DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls DEFINITION. PUBLIC SECTION. TYPES: BEGIN OF ty_ekko, ebeln TYPE ekko-ebeln, bukrs TYPE ekko-bukrs, lifnr TYPE ekko-lifnr, END OF ty_ekko, BEGIN OF ty_ekpo, ebeln TYPE ekpo-ebeln, ebelp TYPE ekpo-ebelp, matnr TYPE ekpo-matnr, werks TYPE ekpo-werks, lgort TYPE ekpo-lgort, END OF ty_ekpo, BEGIN OF ty_out, ebeln TYPE ekpo-ebeln, ebelp TYPE ekpo-ebelp, matnr TYPE ekpo-matnr, werks TYPE ekpo-werks, lgort TYPE ekpo-lgort, bukrs TYPE ekko-bukrs, lifnr TYPE ekko-lifnr, END OF ty_out.

Rohini kumar

DATA: wa_ekko wa_ekpo wa_out it_ekko it_ekpo it_out

sap abap consultant

TYPE TYPE TYPE TYPE TYPE TYPE

ty_ekko, ty_ekpo, ty_out, STANDARD TABLE OF ty_ekko, STANDARD TABLE OF ty_ekpo, STANDARD TABLE OF ty_out.

INTERFACES it2. ENDCLASS.

"cls DEFINITION

*----------------------------------------------------------------------* * CLASS cls IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls IMPLEMENTATION. METHOD it1~mit1. SELECT ebeln bukrs lifnr FROM ekko INTO TABLE it_ekko WHERE ebeln IN s_ebeln. IF sy-subrc = 0. SORT it_ekko BY ebeln. ELSE. MESSAGE 'Purchase Order Doesn''t Exist' TYPE 'I'. LEAVE LIST-PROCESSING. ENDIF. ENDMETHOD.

"it1~mit1

METHOD it1~mit2. SELECT ebeln ebelp matnr werks lgort FROM ekpo INTO TABLE it_ekpo FOR ALL ENTRIES IN it_ekko WHERE ebeln = it_ekko-ebeln. IF sy-subrc = 0. SORT it_ekpo BY ebeln ebelp. ENDIF. ENDMETHOD. METHOD it2~mit1. IF it_ekpo IS NOT INITIAL. LOOP AT it_ekpo INTO wa_ekpo. wa_out-ebeln = wa_ekpo-ebeln.

"it1~mit2

Rohini kumar

sap abap consultant

wa_out-ebelp wa_out-matnr wa_out-werks wa_out-lgort

= = = =

wa_ekpo-ebelp. wa_ekpo-matnr. wa_ekpo-werks. wa_ekpo-lgort.

READ TABLE it_ekko INTO wa_ekko WITH KEY ebeln = wa_ekpo-ebeln BINARY SEARCH. IF sy-subrc = 0. wa_out-bukrs = wa_ekko-bukrs. wa_out-lifnr = wa_ekko-lifnr. ENDIF. APPEND wa_out TO it_out. CLEAR: wa_out, wa_ekko, wa_ekpo. ENDLOOP. ENDIF. ENDMETHOD.

"it2~mit1

METHOD it2~mit2. IF it_out IS NOT INITIAL. LOOP AT it_out INTO wa_out. AT FIRST. WRITE: / 'Purchase Order', 18 'Item', 26 'Material', 48 'Plant', 55 'Storage', 65 'Company', 75 'Vendor'. ULINE. SKIP. ENDAT. WRITE: / 18 26 48 55 65 75 ENDLOOP. ENDIF.

wa_out-ebeln, wa_out-ebelp, wa_out-matnr, wa_out-werks, wa_out-lgort, wa_out-bukrs, wa_out-lifnr.

ENDMETHOD. ENDCLASS. START-OF-SELECTION. DATA obj TYPE REF TO cls.

"it2~mit2 "cls IMPLEMENTATION

Rohini kumar

CREATE OBJECT obj. CALL METHOD: obj->it1~mit1, obj->it1~mit2, obj->it2~mit1, obj->it2~mit2.

Below is the Output:

Output:

sap abap consultant

Rohini kumar

sap abap consultant

Encapsulation by Interface Encapsulation means one attribute and method can be modified in different classes. Hence data and method can have different form and logic and that can be hidden to separate class. Interface is used when we need to create one method with different functionality in different classes. Here the name of the method needs not to be changed. The same method will have to be implemented in different class implementations. The following program contains an Interface it. There we have declared attribute and a method like mit. Now we have defined two classes like cls1 and cls2. So we have to implement the method mit in both of the class implementation. We have implemented the method mit differently in different class. Now in the start of selection we create two objects obj1 and obj2 for two classes. Next we call the method by different objects and we are getting the function declared in separate class.

*&---------------------------------------------------------------------* *& Report ZSR_TEST

Rohini kumar

sap abap consultant

*& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

zsr_test.

*----------------------------------------------------------------------* * INTERFACE it *----------------------------------------------------------------------* * *----------------------------------------------------------------------* INTERFACE it. DATA v_txt TYPE char50. METHODS mit. ENDINTERFACE. "it *----------------------------------------------------------------------* * CLASS cls1 DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls1 DEFINITION. PUBLIC SECTION. INTERFACES it. ENDCLASS. "cls1 DEFINITION *----------------------------------------------------------------------* * CLASS cls2 DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls2 DEFINITION. PUBLIC SECTION. INTERFACES it. ENDCLASS. "cls2 DEFINITION *----------------------------------------------------------------------* * CLASS cls1 IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls1 IMPLEMENTATION. METHOD it~mit. it~v_txt = 'Class One Interface Method'. WRITE / it~v_txt. ENDMETHOD. "it~mit ENDCLASS. "cls1 IMPLEMENTATION

Rohini kumar

sap abap consultant

*----------------------------------------------------------------------* * CLASS cls2 IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls2 IMPLEMENTATION. METHOD it~mit. it~v_txt = 'Class Two Interface Method'. WRITE / it~v_txt. ENDMETHOD. "it~mit ENDCLASS. "cls2 IMPLEMENTATION START-OF-SELECTION. DATA: obj1 TYPE REF TO cls1, obj2 TYPE REF TO cls2. CREATE OBJECT: obj1, obj2. CALL METHOD: obj1->it~mit, obj2->it~mit.

The output is following:

Private Friends of a Class A class can be defined as a private friend of another class. The statement is like this: CLASS cls2 DEFINITION CREATE PRIVATE FRIENDS cls1. Here cls2 is the private friend class of cls1. Now the requisites are: 1. cls1 can use all protected and private components of cls2. 2. cls1 can instantiate cls2 in its method. Here we want to create a friend class and data and method in protected and private section. This friend class will be Instantiated from another class method and it will access all of its data and methods. The following program contains a class cls2 which is a private friend class of cls1. Now we have defined protected data and private data in class cls2. We also have defined a private method m_cls2 here. The method m_cls2 displays the protected and private data.

Rohini kumar

sap abap consultant

Now we have a public method m_cls1 of class cls1. In the implementation of m_cls1 we have instantiated friend class and called method m_cls2. Then we have changed the value of protected and private data and display those again. Finally in the start of selection we just have instantiated the class cls1 and called the method m_cls1. Hence we don't have to instantiate the friend class again as it has been instantiated before.

*&---------------------------------------------------------------------*

X Rohini Kumar sap abap consultant

*& Report f *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

zsr_test.

*----------------------------------------------------------------------* * CLASS cls1 DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls1 DEFINITION. PUBLIC SECTION. METHODS m_cls1. ENDCLASS. "cls1 DEFINITION *----------------------------------------------------------------------* * CLASS cls2 DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls2 DEFINITION CREATE PRIVATE FRIENDS cls1. PROTECTED SECTION. DATA v_pro TYPE char50. PRIVATE SECTION. DATA v_txt TYPE char50. METHODS m_cls2. ENDCLASS. "cls2 DEFINITION *----------------------------------------------------------------------*

Rohini kumar

sap abap consultant

* CLASS cls1 IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls1 IMPLEMENTATION. METHOD m_cls1. DATA obj2 TYPE REF TO cls2. CREATE OBJECT obj2. CALL METHOD obj2->m_cls2. obj2->v_pro = 'Protected Data of First Class'. obj2->v_txt = 'Private Data of First Class'. WRITE: / obj2->v_pro, / obj2->v_txt. ENDMETHOD. ENDCLASS. "cls1 IMPLEMENTATION

"m_cls1

*----------------------------------------------------------------------* * CLASS cls2 IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls2 IMPLEMENTATION. METHOD m_cls2. v_pro = 'Protected Data of Friend Class'. v_txt = 'Private Data of Friend Class'. WRITE: / 'Private Method:', / v_pro, / v_txt. SKIP. ENDMETHOD. "m_cls2 ENDCLASS. "cls2 IMPLEMENTATION START-OF-SELECTION. DATA obj1 TYPE REF TO cls1. CREATE OBJECT obj1. CALL METHOD obj1->m_cls1.

The output is as following:

Rohini kumar

sap abap consultant

Below is another version of the previous program and that will produce the same output. Here Definition Deferred has been used for defining cls1 class. The logic is same and we can use this procedure also. Here we have defined the friend class in another way like CLASS cls2 DEFINITION FRIENDS cls1.

*&---------------------------------------------------------------------* *& Report

ZSR_TEST

*& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------*

REPORT

zsr_test.

CLASS cls1 DEFINITION DEFERRED.

*----------------------------------------------------------------------* *

CLASS cls2 DEFINITION

*----------------------------------------------------------------------* * *----------------------------------------------------------------------*

Rohini kumar

sap abap consultant

CLASS cls2 DEFINITION FRIENDS cls1. PROTECTED SECTION. DATA v_pro TYPE char50. PRIVATE SECTION. DATA v_txt TYPE char50. METHODS m_cls2. ENDCLASS.

"cls2 DEFINITION

*----------------------------------------------------------------------* *

CLASS cls2 IMPLEMENTATION

*----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls2 IMPLEMENTATION. METHOD m_cls2. v_pro = 'Protected Data of Friend Class'. v_txt = 'Private Data of Friend Class'. WRITE: / 'Private Method:', / v_pro, / v_txt. SKIP. ENDMETHOD. ENDCLASS.

"m_cls2 "cls2 IMPLEMENTATION

*----------------------------------------------------------------------* *

CLASS cls1 DEFINITION

*----------------------------------------------------------------------* * *----------------------------------------------------------------------*

Rohini kumar

sap abap consultant

CLASS cls1 DEFINITION. PUBLIC SECTION. METHODS m_cls1. ENDCLASS.

"cls1 DEFINITION

*----------------------------------------------------------------------* *

CLASS cls1 IMPLEMENTATION

*----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls1 IMPLEMENTATION. METHOD m_cls1. DATA obj2 TYPE REF TO cls2. CREATE OBJECT obj2. CALL METHOD obj2->m_cls2.

obj2->v_pro = 'Protected Data of First Class'. obj2->v_txt = 'Private Data of First Class'. WRITE: / obj2->v_pro, / obj2->v_txt. ENDMETHOD. ENDCLASS.

"m_cls1 "cls1 IMPLEMENTATION

START-OF-SELECTION. DATA obj1 TYPE REF TO cls1. CREATE OBJECT obj1. CALL METHOD obj1->m_cls1.

Sub Classes of Friend Class

Rohini kumar

sap abap consultant

Sub classes of a friend class can access all of its class granted friendship. So sub classes can get all the protected and private elements of the class which is granting friendship of the super class. As an example if a class cls is granting friendship of parent class then child class will get access of all protected and private elements of class cls. In the following program we have defined a class cls which is a friend of parent class. We have declared protected and private data and a private method m_cls here. Now we are implementing the cls class by displaying the data in the method m_cls. Next we have defined parent class which holds a public method m_par. In the implementation part we have instantiated cls class and called method m_cls. Then we have modified the data and display those as well. Hence this method will display the previous version and also the current version of the output. Next we have defined a child class inheriting from parent and declared a public method m_chi. In the implementation part we have instantiated cls class again and called the parent class method m_par. Now we have called parent method m_par and modified the data again accordingly. After that we are displaying the current data. Hence the child method contains the display of parent method and child method. Finally in start of selection we just have instantiated the child object and then called the child method. The output will contain all previous versions of the protected and private data.

*&---------------------------------------------------------------------* *& Report ZSR_TEST *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

zsr_test.

CLASS parent DEFINITION DEFERRED. *----------------------------------------------------------------------* * CLASS cls DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls DEFINITION FRIENDS parent. PROTECTED SECTION. DATA v_pro TYPE char50. PRIVATE SECTION. DATA v_pri TYPE char50. METHODS m_cls.

Rohini kumar

ENDCLASS.

sap abap consultant

"cls DEFINITION

*----------------------------------------------------------------------* * CLASS cls IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls IMPLEMENTATION. METHOD m_cls. v_pro = 'Protected Data'. v_pri = 'Private Date'. WRITE: / 'Private Method:', / v_pro, / v_pri. SKIP. ENDMETHOD. "m_cls ENDCLASS. "cls IMPLEMENTATION *----------------------------------------------------------------------* * CLASS parent DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS parent DEFINITION. PUBLIC SECTION. METHODS m_par. ENDCLASS. "parent DEFINITION *----------------------------------------------------------------------* * CLASS parent IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS parent IMPLEMENTATION. METHOD m_par. DATA obj TYPE REF TO cls. CREATE OBJECT obj. CALL METHOD obj->m_cls. obj->v_pro = 'Protected - Accessed by Parent'. obj->v_pri = 'Private - Accessed by Parent'. WRITE: / 'Parent Method:', / obj->v_pro, / obj->v_pri. SKIP. ENDMETHOD. "m_par ENDCLASS. "parent IMPLEMENTATION *----------------------------------------------------------------------* * CLASS child DEFINITION

Rohini kumar

sap abap consultant

*----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS child DEFINITION INHERITING FROM parent. PUBLIC SECTION. METHODS m_chi. ENDCLASS. "child DEFINITION *----------------------------------------------------------------------* * CLASS child IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS child IMPLEMENTATION. METHOD m_chi. DATA obj TYPE REF TO cls. CREATE OBJECT obj. CALL METHOD m_par. obj->v_pro = 'Protected - Accessed by Child'. obj->v_pri = 'Private - Accessed by Child'. WRITE: / 'Child Method:', / obj->v_pro, / obj->v_pri. SKIP. ENDMETHOD. "m_chi ENDCLASS. "child IMPLEMENTATION START-OF-SELECTION. DATA obj_chi TYPE REF TO child. CREATE OBJECT obj_chi. CALL METHOD obj_chi->m_chi.

The output is following:

Rohini kumar

sap abap consultant

Friendship is one sided Friendship is one sided. That means if a class cls2 is defined with Friends of cls1 then cls1 can access all of protected and private components of cls2 but not vice versa. If cls2 want to access all components of cls1 then cls1 will have to be defined by Friends of cls2. Otherwise compile error will come in case of accessing cls1 components like below:

Rohini kumar

sap abap consultant

We have Modified this program by defining the cls1 class as Friends of cls2. Not the output is coming properly.

*&---------------------------------------------------------------------* *& Report ZSR_TEST *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

zsr_test.

CLASS cls1 DEFINITION DEFERRED. *----------------------------------------------------------------------* * CLASS cls2 DEFINITION

Rohini kumar

sap abap consultant

*----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls2 DEFINITION FRIENDS cls1. PUBLIC SECTION. METHODS meth2. PROTECTED SECTION. DATA v2_pro TYPE char50. PRIVATE SECTION. DATA v2_pri TYPE char50. METHODS m_cls2. ENDCLASS. "cls2 DEFINITION *----------------------------------------------------------------------* * CLASS cls1 DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls1 DEFINITION FRIENDS cls2. PUBLIC SECTION. METHODS meth1. PROTECTED SECTION. DATA v1_pro TYPE char50. PRIVATE SECTION. DATA v1_pri TYPE char50. METHODS m_cls1. ENDCLASS. "cls1 DEFINITION *----------------------------------------------------------------------* * CLASS cls2 IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls2 IMPLEMENTATION. METHOD meth2. DATA obj1 TYPE REF TO cls1. CREATE OBJECT obj1. CALL METHOD obj1->m_cls1. ENDMETHOD. "meth2 METHOD m_cls2. v2_pro = 'Protected Data Class 2'. v2_pri = 'Private Data Class 2'. WRITE: / 'Private Method of Class 2:', / v2_pro, / v2_pri. SKIP. ENDMETHOD. ENDCLASS. "cls2 IMPLEMENTATION

"m_cls2

Rohini kumar

sap abap consultant

*----------------------------------------------------------------------* * CLASS cls1 IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls1 IMPLEMENTATION. METHOD meth1. DATA obj2 TYPE REF TO cls2. CREATE OBJECT obj2. CALL METHOD obj2->m_cls2. ENDMETHOD. "meth1 METHOD m_cls1. v1_pro = 'Protected Data Class 1'. v1_pri = 'Private Data Class 1'. WRITE: / 'Private Method of Class 1:', / v1_pro, / v1_pri. SKIP. ENDMETHOD. ENDCLASS. "cls1 IMPLEMENTATION START-OF-SELECTION. DATA: obj1 TYPE REF TO cls1, obj2 TYPE REF TO cls2. CREATE OBJECT: obj1, obj2. CALL METHOD: obj2->meth2, obj1->meth1.

Below is the output:

Events with Handler Method in Same Class

"m_cls1

Rohini kumar

sap abap consultant

Event handler method can be used in the same class by declaring the event and the handler method with the triggering method. Here the triggering method will raise or trigger the event in the implementation part. The program below contains a class cls which contains data and constant in the public section. It contains events evnt1 and evnt2 here. And also it has event handler methods evnthand1 and evnthand2. It also contains triggering methods trig1 and trig2. Now at the time of implementation we implement the event handler methods evnthand1 and evnthand2 with general statement. After that we implement the triggering of event method trig1 and trig2 with RAISE EVENT statement. Finally in the start of selection we set handler method evnthand1 and evnthand2 by the object. Then we call triggering method trig1 and trig2. In the debugging mode when the program controller is at CALL METHOD: obj>trig1 then it calls the method trig1 and as the controller goes to RAISE EVENT evnt1 then it goes to event handler method evnthand1. There it follows the general statement. After finishing of that the controller comes back to the triggering method trig1 and end this method. Then it will go for the next triggering method trig2 and next steps will be similar. Here raise event will be evnt2 and event handler method is evnthand2.

*&---------------------------------------------------------------------* *& Report ZSR_TEST *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

zsr_test.

*----------------------------------------------------------------------* * CLASS cls DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls DEFINITION. PUBLIC SECTION. DATA v_txt TYPE char50. CONSTANTS c_das TYPE char50 VALUE '--------------------------------'. EVENTS: evnt1, evnt2. METHODS: evnthand1 FOR EVENT evnt1 OF cls, evnthand2 FOR EVENT evnt2 OF cls, trig1, trig2. ENDCLASS. "cls DEFINITION

Rohini kumar

sap abap consultant

*----------------------------------------------------------------------* * CLASS cls IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls IMPLEMENTATION. METHOD evnthand1. v_txt = 'First Event Handler Method'. WRITE / v_txt. SKIP 2. ENDMETHOD. "evnthand1 METHOD evnthand2. v_txt = 'Second Event Handler Method'. WRITE / v_txt. SKIP 2. ENDMETHOD.

"evnthand2

METHOD trig1. v_txt = 'First Event is being Triggered:'. WRITE: / v_txt, / c_das. RAISE EVENT evnt1. ENDMETHOD.

"trig1

METHOD trig2. v_txt = 'Second Event is being Triggered:'. WRITE: / v_txt, / c_das. RAISE EVENT evnt2. ENDMETHOD. ENDCLASS. "cls IMPLEMENTATION START-OF-SELECTION. DATA obj TYPE REF TO cls. CREATE OBJECT obj. SET HANDLER: obj->evnthand1 FOR obj, obj->evnthand2 FOR obj. CALL METHOD: obj->trig1, obj->trig2.

The output is following:

"trig2

Rohini kumar

sap abap consultant

Event with Handler Method in Different Class Event can be raised by event handler method which is declared in another class. Here we can metion an event in a class and want to handle that event in another class. In that case we need to declare the triggering method in the first class. In the following program we have defined a class cls1 where in the public section we have declared data v_cls1, event evnt and method trig. In the implementation part we have implemented the method trig with raising event evnt. We have to declare the triggering method trig in class cls1 because the event evnt is in cls1. Next we have declared another class cls2 and in the public section we are declaring a data and the event handler method evnthand for event evnt of class cls1. Now in the implementation part we have implemented the evnthand properly. Finally in the start of selection we have instantiated the class cls1 and cls2. Now we are calling set handler method evnthand for object of class cls1. Then we call the method trig by object of class cls1.

*&---------------------------------------------------------------------* *& Report ZSR_TEST *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

zsr_test.

*----------------------------------------------------------------------* * CLASS cls1 DEFINITION *----------------------------------------------------------------------*

Rohini kumar

sap abap consultant

* *----------------------------------------------------------------------* CLASS cls1 DEFINITION. PUBLIC SECTION. DATA v_cls1 TYPE char50. EVENTS evnt. METHODS trig. ENDCLASS. "cls1 DEFINITION *----------------------------------------------------------------------* * CLASS cls2 DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls2 DEFINITION. PUBLIC SECTION. DATA v_cls2 TYPE char50. METHODS evnthand FOR EVENT evnt OF cls1. ENDCLASS. "cls2 DEFINITION *----------------------------------------------------------------------* * CLASS cls1 IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls1 IMPLEMENTATION. METHOD trig. v_cls1 = 'Event Triggering Method in Class One'. WRITE / v_cls1. RAISE EVENT evnt. ENDMETHOD. "trig ENDCLASS. "cls1 IMPLEMENTATION *----------------------------------------------------------------------* * CLASS cls2 IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls2 IMPLEMENTATION. METHOD evnthand. v_cls2 = 'Event Handler Method in Class Two'. WRITE / v_cls2. ENDMETHOD. "evnthand ENDCLASS. "cls2 IMPLEMENTATION START-OF-SELECTION. DATA: obj1 TYPE REF TO cls1, obj2 TYPE REF TO cls2. CREATE OBJECT: obj1, obj2.

Rohini kumar

sap abap consultant

SET HANDLER obj2->evnthand FOR obj1. CALL METHOD obj1->trig.

The output is below:

Single Event can have Multiple Event Handler Methods One event can have more than one event handler method in the same class or in different classes. At the run time only one handler method will be triggered at a time. After triggering the one that has to be deactivated and then another handler method will have to be registered to be triggered. In this way system can register multiple event handler method to the same event.

The following program contains a class cls1 where in the public section we declare data v_cls1, event evnt, triggering method trig and event handler method evnthand1 for event evnt of class cls1. Now at the time of implementation we display a text in event handler method evnthand1 then we display another text in triggering method trig.

Next we have defined another class cls2 where in the public section we declare data and another event handler method evnthand2 for event evnt of class cls1. In the implementation we display a text in this method.

Finally in the start of selection we have created object obj1 for cls1 and obj2 for cls2. Now we set handler method evnthand1 for object obj1 and then call triggering method trig. After that we deactivate the registration of evnthand1 and then register again evnthand2 for obj1 by obj2. Then again we call the method trig. Hence initially we have registered the first handler method to the event and call the trigger. Then we have deactivated the registration to register the second handler method and called the same trigger again.

*&---------------------------------------------------------------------* *& Report ZSR_TEST *&

Rohini kumar

sap abap consultant

*&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

zsr_test.

*----------------------------------------------------------------------* * CLASS cls1 DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls1 DEFINITION. PUBLIC SECTION. DATA v_cls1 TYPE char50. EVENTS evnt. METHODS: trig, evnthand1 FOR EVENT evnt OF cls1. ENDCLASS. "cls1 DEFINITION *----------------------------------------------------------------------* * CLASS cls2 DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls2 DEFINITION. PUBLIC SECTION. DATA v_cls2 TYPE char50. METHODS evnthand2 FOR EVENT evnt OF cls1. ENDCLASS. "cls2 DEFINITION *----------------------------------------------------------------------* * CLASS cls1 IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls1 IMPLEMENTATION. METHOD evnthand1. v_cls1 = 'Event Handler in Class One'. WRITE / v_cls1. SKIP. ENDMETHOD. "evnthand1 METHOD trig. v_cls1 = 'Event Triggers in Class One'. WRITE / v_cls1. RAISE EVENT evnt. ENDMETHOD. "trig ENDCLASS. "cls1 IMPLEMENTATION

Rohini kumar

sap abap consultant

*----------------------------------------------------------------------* * CLASS cls2 IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls2 IMPLEMENTATION. METHOD evnthand2. v_cls2 = 'Event Handler in Class Two'. WRITE / v_cls2. SKIP. ENDMETHOD. "evnthand2 ENDCLASS. "cls2 IMPLEMENTATION START-OF-SELECTION. DATA: obj1 TYPE REF TO cls1, obj2 TYPE REF TO cls2. CREATE OBJECT: obj1, obj2. SET HANDLER obj1->evnthand1 FOR obj1. CALL METHOD obj1->trig. SET HANDLER: obj1->evnthand1 FOR obj1 ACTIVATION space, obj2->evnthand2 FOR obj1. CALL METHOD obj1->trig.

Below is the output:

Static Events are Triggered by Static Methods We can use the Static events in a class but the triggering methods must be static in this class. At the time of registration the FOR clause with object name is not required. The following program contains a class cls in which we have two static events evnt1 and evnt2 with two static triggering methods trig1 and trig2. We also have two handler methods evnthand1 and evnthand2. Now in the implementation we displaying some text

Rohini kumar

sap abap consultant

in handler methods as well as triggering methods. Next in the start of selection we have registered the handler method with object but there is no need to use the FOR clause with object name because this registration is for static events. Finally we call the two static triggering methods as well.

*&---------------------------------------------------------------------* *& Report ZSR_TEST *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

zsr_test.

*----------------------------------------------------------------------* * CLASS cls DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls DEFINITION. PUBLIC SECTION. DATA v_txt TYPE char50. CLASS-EVENTS: evnt1, evnt2. METHODS: evnthand1 FOR EVENT evnt1 OF cls, evnthand2 FOR EVENT evnt2 OF cls. CLASS-DATA v_cls TYPE char50. CLASS-METHODS: trig1, trig2. ENDCLASS. "cls DEFINITION *----------------------------------------------------------------------* * CLASS cls IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls IMPLEMENTATION. METHOD evnthand1. v_txt = 'Static Event - Handler Method One'. WRITE / v_txt. SKIP. ENDMETHOD. "evnthand1 METHOD evnthand2. v_txt = 'Static Event - Handler Method Two'. WRITE / v_txt. SKIP. ENDMETHOD.

"evnthand2

Rohini kumar

METHOD trig1. v_cls = 'Triggering Static Method One:'. WRITE / v_cls. RAISE EVENT evnt1. ENDMETHOD. METHOD trig2. v_cls = 'Triggering Static Method Two:'. WRITE / v_cls. RAISE EVENT evnt2. ENDMETHOD. ENDCLASS. "cls IMPLEMENTATION

sap abap consultant

"trig1

"trig2

START-OF-SELECTION. DATA obj TYPE REF TO cls. CREATE OBJECT obj. SET HANDLER: obj->evnthand1, obj->evnthand2. CALL METHOD: obj->trig1, obj->trig2.

Output is as follows:

Export Parameters in Event Event can have export parameters which are passed to the event handler method by that event. Here the handler methods need to be declared with importing parameters. The names of those importing parameters will be similar to the exporting parameters of the event. Triggering methods can only pass the value to those parameters by using RAISE EVENT statement. Hence at the time of raise event the triggering method can pass values to the parameters.

The following program contains a class cls in which we have data v_txt, events evnt1 with export parameters txt1 & txt2 and evnt2 with export parameters txt3 & txt4 under

Rohini kumar

sap abap consultant

public section. Then we have event handler methods hand1 for event evnt1 which is importing txt1 & txt2 and hand2 for events evnt2 which is importing txt3 & txt4. We also have triggering methods trig1 and trig2.

Now in the implementation part we have implemented the hand1 by displaying 3 text. Similarly we are implementing the hand2 method also. Then we are implementing the triggering method trig1 where we have raised event evnt1 with exporting txt1 and txt2 by passing value to them. Similarly we have implemented trig2 where we have raised event evnt2 with exporting txt3 and txt4 by passing value to them.

Finally in the start of selection we have set handler methods of hand1 and hand2 by the instance of class cls and then call methods trig1 and trig2.

*&---------------------------------------------------------------------* *& Report ZSR_TEST *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT

zsr_test.

*----------------------------------------------------------------------* * CLASS cls DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls DEFINITION. PUBLIC SECTION. DATA v_txt TYPE char50. EVENTS: evnt1 EXPORTING value(txt1) value(txt2) evnt2 EXPORTING value(txt3) value(txt4)

TYPE TYPE TYPE TYPE

char50 char50, char50 char50.

METHODS: hand1 FOR EVENT evnt1 OF cls IMPORTING txt1 txt2, hand2 FOR EVENT evnt2 OF cls IMPORTING txt3 txt4, trig1, trig2. ENDCLASS.

"cls DEFINITION

Rohini kumar

sap abap consultant

*----------------------------------------------------------------------* * CLASS cls IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls IMPLEMENTATION. METHOD hand1. v_txt = 'First Event Handler Method'. WRITE: / v_txt, / txt1, / txt2. SKIP. ENDMETHOD. "hand1 METHOD hand2. v_txt = 'Second Event Handler Method'. WRITE: / v_txt, / txt3, / txt4. SKIP. ENDMETHOD.

"hand2

METHOD trig1. v_txt = 'First Triggering Method'. WRITE / v_txt. RAISE EVENT evnt1 EXPORTING txt1 = '1st Variable of 1st Event' txt2 = '2nd Variable of 1st Event'. ENDMETHOD. "trig1 METHOD trig2. v_txt = 'Second Triggering Method'. WRITE / v_txt. RAISE EVENT evnt2 EXPORTING txt3 = '1st Variable of 2nd Event' txt4 = '2nd Variable of 2nd Event'. ENDMETHOD. "trig2 ENDCLASS. "cls IMPLEMENTATION START-OF-SELECTION. DATA obj TYPE REF TO cls. CREATE OBJECT obj. SET HANDLER: obj->hand1 FOR obj, obj->hand2 FOR obj. CALL METHOD: obj->trig1, obj->trig2.

The output is as following:

Rohini kumar

sap abap consultant

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF