its example to all sap abap porgrams importent program in sap abap you can pratice to own and learn the programming how its workFull description
sap abap book with oo abap nice book with pratical exposer here you can find new path to learning sap abap programming and question
HA400 - Abap Programming on Sap HanaFull description
Sap ABAPDescripción completa
Secure Programming – ABAP SAP R/3 4.6C TOC PASSWORD SECURITY SECURE STORE AND FORWARD MECHANISM (SSF) SECURITY LOGGING SAP VIRUS SCAN INTERFACE
abapFull description
SAP Webdynpro ABAP Guide, WebDynpro Guide ABAP
Descripción completa
This book cover the practical approces to sap abap programming language
Certification purposeDescripción completa
this book cover all pratical and inteview question answer nice book
rtrt
ABAP Consultant
Descripción: Tutorial sobre la declaración y el uso de las estructuras en ABAP
Those who are preparing for certification, this is for youFull description
Full description
Those who are preparing for certification, this is for youDescripción completa
hiFull description
Full description
Description complète
nice book with good examample and cover all interview question and answerFull 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.
/3 'Item', 17 'Quantity', 32 'Price'. WRITE / '=========================================='. SKIP. " Skipping one single line LOOP AT itab INTO wtab. 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.
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:
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.
/3 'Item', 13 'Quantity(KG)', 28 'Price(Rs)'. WRITE / '=========================================='. SKIP. " Skipping one single line LOOP AT itab INTO wtab. 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.
/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:
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:
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:
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.
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 '=========================================='.
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.
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.
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:
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
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
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:/
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.
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.
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
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.
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.
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',
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
"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.
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
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
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
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.
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,
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
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 = ' '
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: *&--------------------------------------------------------------------*
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
*&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM get_material . SELECT matnr ersda ernam mtart FROM mara INTO TABLE it_mara WHERE matnr IN s_matnr AND mtart = p_mtart. IF sy-subrc = 0. SORT it_mara BY matnr. ELSE. MESSAGE 'Material doesn''t exist' TYPE 'I'. ENDIF. ENDFORM. " GET_MATERIAL *&--------------------------------------------------------------------* *& Form GET_PLANT *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM get_plant . IF it_mara IS NOT INITIAL. 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. ENDIF. ENDIF. ENDFORM. " GET_PLANT *&------------------------------------------------------------
Rohini kumar
sap abap consultant
---------* *& Form GET_STORAGE *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM get_storage . IF it_marc IS NOT INITIAL. SELECT matnr werks lgort 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. ENDIF. ENDIF. ENDFORM. " GET_STORAGE *&--------------------------------------------------------------------* *& Form GET_DESCRIPTION *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM get_description . IF it_mara IS NOT INITIAL. SELECT matnr spras maktx FROM makt INTO TABLE it_makt FOR ALL ENTRIES IN it_mara WHERE matnr = it_mara-matnr AND spras = sy-langu. IF sy-subrc = 0. SORT it_makt BY matnr. ENDIF.
Rohini kumar
sap abap consultant
ENDIF. ENDFORM. " GET_DESCRIPTION *&--------------------------------------------------------------------* *& Form PREPARE_OUTPUT *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM prepare_output . IF it_mara IS NOT INITIAL AND it_marc IS NOT INITIAL AND it_mard IS NOT INITIAL. LOOP AT it_mara INTO wa_mara. wa_out-matnr = wa_mara-matnr. wa_out-ersda = wa_mara-ersda. wa_out-ernam = wa_mara-ernam. wa_out-mtart = wa_mara-mtart. READ TABLE it_makt INTO wa_makt WITH KEY matnr = wa_mara-matnr BINARY SEARCH. IF sy-subrc = 0. wa_out-maktx = wa_makt-maktx. ENDIF. 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. APPEND wa_out TO it_out. CLEAR: wa_out, wa_mara, wa_makt. CLEAR wa_mard. ENDLOOP. CLEAR wa_marc. ENDLOOP.
Rohini kumar
sap abap consultant
ENDLOOP. ENDIF. ENDFORM. " PREPARE_OUTPUT *&--------------------------------------------------------------------* *& Form PREPARE_FIELDCAT *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM prepare_fieldcat . CLEAR wa_fcat_out. REFRESH it_fcat_out. IF it_out IS NOT INITIAL. DATA: lv_col TYPE i VALUE 0. lv_col wa_fcat_out-col_pos wa_fcat_out-fieldname wa_fcat_out-tabname wa_fcat_out-seltext_l APPEND wa_fcat_out TO CLEAR wa_fcat_out.
ENDFORM. " PREPARE_FIELDCAT *&--------------------------------------------------------------------* *& Form ALV_LIST_DISPLAY *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text
Rohini kumar
sap abap consultant
*---------------------------------------------------------------------* FORM alv_list_display . IF it_out IS NOT INITIAL AND it_fcat_out IS NOT INITIAL.
ENDFORM. " ALV_LIST_DISPLAY *&--------------------------------------------------------------------* *& Form TOP_OF_PAGE *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM top_of_page . CLEAR wa_top. REFRESH it_top. CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL' EXPORTING date_internal = sy-datum IMPORTING date_external = v_date EXCEPTIONS date_internal_is_invalid = 1 OTHERS = 2. wa_top-typ = 'H'. wa_top-info = 'Material Details Report'. APPEND wa_top TO it_top. CLEAR wa_top. wa_top-typ = 'S'. wa_top-info = 'Report: '. CONCATENATE wa_top-info v_prog INTO wa_top-info. APPEND wa_top TO it_top. CLEAR wa_top. wa_top-typ = 'S'. wa_top-info = 'User Name: '. CONCATENATE wa_top-info v_name INTO wa_top-info.
Rohini kumar
sap abap consultant
APPEND wa_top TO it_top. CLEAR wa_top. wa_top-typ = 'S'. wa_top-info = 'Date: '. CONCATENATE wa_top-info v_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 * I_LOGO = * I_END_OF_LIST_GRID = * I_ALV_FORM = . ENDFORM. " TOP_OF_PAGE *&--------------------------------------------------------------------* *& Form PREPARE_LAYOUT *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM prepare_layout . wa_layout-zebra = 'X'. wa_layout-colwidth_optimize = 'X'. wa_layout-box_fieldname = 'SEL'. ENDFORM. The output is as following: Selection Screen:
" PREPARE_LAYOUT
Rohini kumar
Output Screen:
sap abap consultant
Rohini kumar
sap abap consultant
ALV Grid Interactive Report Here is an interactive report program which displays the basic output (First Screen) for Purchase Order header information. Then by double clicking the PO number we can go to secondary output which is the item display of that PO. On the Item screen if we double click on PO then ME23N Transaction will open with that particular PO. In this reports User Command comes to fetch the double click operation. The output is in ALV Grid format. *&--------------------------------------------------------------------* *& Report ZSR_TEST1 *& *&--------------------------------------------------------------------* *& *& *&--------------------------------------------------------------------* REPORT
zsr_test1.
TABLES: ekko, ekpo. TYPE-POOLS: slis. TYPES: BEGIN OF ty_ekko, ebeln TYPE ekko-ebeln, bukrs TYPE ekko-bukrs, lifnr TYPE ekko-lifnr, END OF ty_ekko, BEGIN OF ty_out_ekko, sel, ebeln TYPE ekko-ebeln, bukrs TYPE ekko-bukrs, lifnr TYPE ekko-lifnr, END OF ty_out_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, menge TYPE ekpo-menge, meins TYPE ekpo-meins, END OF ty_ekpo,
Rohini kumar
sap abap consultant
BEGIN OF ty_out_ekpo, sel, ebeln TYPE ekko-ebeln, ebelp TYPE ekpo-ebelp, matnr TYPE ekpo-matnr, werks TYPE ekpo-werks, lgort TYPE ekpo-lgort, menge TYPE ekpo-menge, meins TYPE ekpo-meins, END OF ty_out_ekpo. DATA: wa_ekko wa_ekpo it_ekko it_ekpo
TYPE TYPE TYPE TYPE
wa_out_ekko wa_out_ekpo it_out_ekko it_out_ekpo
ty_ekko, ty_ekpo, STANDARD TABLE OF ty_ekko, STANDARD TABLE OF ty_ekpo,
ty_out_ekko, ty_out_ekpo, STANDARD TABLE OF ty_out_ekko, STANDARD TABLE OF ty_out_ekpo, slis_fieldcat_alv, slis_fieldcat_alv, slis_t_fieldcat_alv, slis_t_fieldcat_alv,
wa_layout TYPE slis_layout_alv, wa_top_ekko wa_top_ekpo it_top_ekko it_top_ekpo
TYPE TYPE TYPE TYPE
wa_event TYPE wa_event_ekpo it_event TYPE it_event_ekpo
slis_alv_event, TYPE slis_alv_event, slis_t_event, TYPE slis_t_event,
r_ucomm TYPE sy-ucomm, rs_selfield TYPE slis_selfield, v_selfield TYPE slis_selfield-value, v_ebeln TYPE ekko-ebeln, v_prog TYPE sy-repid, v_name TYPE sy-uname. INITIALIZATION. v_prog = sy-repid. v_name = sy-uname.
Rohini kumar
sap abap consultant
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text001. SELECT-OPTIONS s_ebeln FOR ekko-ebeln OBLIGATORY. SELECTION-SCREEN END OF BLOCK b1. START-OF-SELECTION. PERFORM get_ekko. PERFORM fieldcat_ekko. PERFORM layout. PERFORM event_ekko. PERFORM grid_ekko. PERFORM ucomm_ekko USING r_ucomm CHANGING rs_selfield. TOP-OF-PAGE. PERFORM top_ekko. TOP-OF-PAGE DURING LINE-SELECTION. PERFORM top_ekpo. *&--------------------------------------------------------------------* *& Form ucomm_ekko *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * -->R_UCOMM text * -->RS_ESLFIELD text *---------------------------------------------------------------------* FORM ucomm_ekko USING r_ucomm_ekko TYPE sy-ucomm CHANGING rs_selfield_ekko TYPE slis_selfield. CASE r_ucomm_ekko. WHEN '&IC1'. IF rs_selfield_ekko-fieldname = 'EBELN'. CLEAR v_selfield. v_selfield = rs_selfield_ekko-value. PERFORM conversion_po. PERFORM get_ekpo. PERFORM fieldcat_ekpo. PERFORM layout. PERFORM event_ekpo. PERFORM grid_ekpo. PERFORM ucomm_ekpo USING r_ucomm CHANGING rs_selfield. ELSE. MESSAGE 'Invalid Field' TYPE 'S'.
Rohini kumar
sap abap consultant
ENDIF. ENDCASE. ENDFORM. "ucomm_ekko *&--------------------------------------------------------------------* *& Form GET_EKKO *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM get_ekko . REFRESH it_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. REFRESH it_out_ekko. LOOP AT it_ekko INTO wa_ekko. wa_out_ekko-ebeln = wa_ekko-ebeln. wa_out_ekko-bukrs = wa_ekko-bukrs. wa_out_ekko-lifnr = wa_ekko-lifnr. APPEND wa_out_ekko TO it_out_ekko. CLEAR: wa_out_ekko, wa_ekko. ENDLOOP. ELSE. MESSAGE 'Purchase Order doesn''t exist' TYPE 'I'. ENDIF. ENDFORM. " GET_EKKO *&--------------------------------------------------------------------* *& Form FIELDCAT_EKKO *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *-------------------------------------------------------------
Rohini kumar
sap abap consultant
---------* FORM fieldcat_ekko . CLEAR wa_fcat_ekko. REFRESH it_fcat_ekko. IF it_out_ekko IS NOT INITIAL. DATA lv_col TYPE i VALUE 0. lv_col wa_fcat_ekko-col_pos wa_fcat_ekko-fieldname wa_fcat_ekko-tabname wa_fcat_ekko-seltext_l APPEND wa_fcat_ekko TO CLEAR wa_fcat_ekko.
ENDFORM. " FIELDCAT_EKKO *&--------------------------------------------------------------------* *& Form LAYOUT *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM layout . wa_layout-zebra = 'X'.
Rohini kumar
sap abap consultant
wa_layout-colwidth_optimize = 'X'. wa_layout-box_fieldname = 'SEL'. ENDFORM. " LAYOUT *&--------------------------------------------------------------------* *& Form EVENT_EKKO *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM event_ekko . REFRESH it_event. * *
CALL FUNCTION 'REUSE_ALV_EVENTS_GET' EXPORTING I_LIST_TYPE = 0 IMPORTING et_events = it_event EXCEPTIONS list_type_wrong = 1 OTHERS = 2. IF it_event IS NOT INITIAL. CLEAR wa_event. READ TABLE it_event INTO wa_event WITH KEY name = 'USER_COMMAND'. IF sy-subrc = 0. wa_event-form = 'UCOMM_EKKO'. MODIFY it_event FROM wa_event INDEX sy-tabix TRANSPORTING form. ENDIF. ENDIF.
ENDFORM. " EVENT_EKKO *&--------------------------------------------------------------------* *& Form GRID_EKKO *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------*
Rohini kumar
sap abap consultant
* --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM grid_ekko . IF it_out_ekko IS NOT INITIAL AND it_fcat_ekko IS NOT INITIAL.
ENDFORM. " GRID_EKKO *&--------------------------------------------------------------------* *& Form top_ekko *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* FORM top_ekko. CLEAR wa_top_ekko. REFRESH it_top_ekko. DATA date TYPE char12. CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL' EXPORTING date_internal = sy-datum IMPORTING date_external = date EXCEPTIONS date_internal_is_invalid = 1 OTHERS = 2. wa_top_ekko-typ = 'H'. wa_top_ekko-info = 'Purchase Order Header'. APPEND wa_top_ekko TO it_top_ekko. CLEAR wa_top_ekko. wa_top_ekko-typ = 'S'. wa_top_ekko-info = 'Report: '. CONCATENATE wa_top_ekko-info v_prog INTO wa_top_ekko-info. APPEND wa_top_ekko TO it_top_ekko. CLEAR wa_top_ekko. wa_top_ekko-typ = 'S'. wa_top_ekko-info = 'User Name: '. CONCATENATE wa_top_ekko-info v_name INTO wa_top_ekko-info.
Rohini kumar
sap abap consultant
APPEND wa_top_ekko TO it_top_ekko. CLEAR wa_top_ekko. wa_top_ekko-typ = 'S'. wa_top_ekko-info = 'Date: '. CONCATENATE wa_top_ekko-info date INTO wa_top_ekko-info. APPEND wa_top_ekko TO it_top_ekko. CLEAR wa_top_ekko. CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE' EXPORTING it_list_commentary = it_top_ekko * I_LOGO = * I_END_OF_LIST_GRID = * I_ALV_FORM = . ENDFORM. "top_ekko *&--------------------------------------------------------------------* *& Form CONVERSION_PO *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM conversion_po . CLEAR v_ebeln. CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT' EXPORTING input = v_selfield IMPORTING output = v_ebeln. ENDFORM. " CONVERSION_PO *&--------------------------------------------------------------------* *& Form GET_EKPO *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------*
Rohini kumar
sap abap consultant
* --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM get_ekpo . IF v_ebeln IS NOT INITIAL. REFRESH it_ekpo. SELECT ebeln ebelp matnr werks lgort menge meins FROM ekpo INTO TABLE it_ekpo WHERE ebeln = v_ebeln. IF sy-subrc = 0. SORT it_ekpo BY ebelp. REFRESH it_out_ekpo. LOOP AT it_ekpo INTO wa_ekpo. AT NEW ebeln. wa_out_ekpo-ebeln = wa_ekpo-ebeln. ENDAT. wa_out_ekpo-ebelp = wa_ekpo-ebelp. wa_out_ekpo-matnr = wa_ekpo-matnr. wa_out_ekpo-werks = wa_ekpo-werks. wa_out_ekpo-lgort = wa_ekpo-lgort. wa_out_ekpo-menge = wa_ekpo-menge. wa_out_ekpo-meins = wa_ekpo-meins. APPEND wa_out_ekpo TO it_out_ekpo. CLEAR: wa_out_ekpo, wa_ekpo. ENDLOOP. ENDIF. ENDIF. ENDFORM. " GET_EKPO *&--------------------------------------------------------------------* *& Form FIELDCAT_EKPO *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM fieldcat_ekpo . CLEAR wa_fcat_ekpo. REFRESH it_fcat_ekpo.
Rohini kumar
sap abap consultant
IF it_out_ekpo IS NOT INITIAL. DATA lv_col TYPE i VALUE 0. lv_col wa_fcat_ekpo-col_pos wa_fcat_ekpo-fieldname wa_fcat_ekpo-tabname wa_fcat_ekpo-seltext_l APPEND wa_fcat_ekpo TO CLEAR wa_fcat_ekpo.
ENDFORM. " FIELDCAT_EKPO *&--------------------------------------------------------------------* *& Form EVENT_EKPO *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM event_ekpo . REFRESH it_event_ekpo. CALL FUNCTION 'REUSE_ALV_EVENTS_GET' * EXPORTING * I_LIST_TYPE = 0 IMPORTING et_events = it_event_ekpo EXCEPTIONS list_type_wrong = 1 OTHERS = 2. IF it_event_ekpo IS NOT INITIAL. CLEAR wa_event_ekpo. READ TABLE it_event_ekpo INTO wa_event_ekpo WITH KEY name = 'USER_COMMAND'. IF sy-subrc = 0. wa_event_ekpo-form = 'UCOMM_EKPO'. MODIFY it_event_ekpo FROM wa_event_ekpo INDEX sy-tabix TRANSPORTING form. ENDIF. ENDIF. ENDFORM. " EVENT_EKPO *&------------------------------------------------------------
Rohini kumar
sap abap consultant
---------* *& Form GRID_EKPO *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM grid_ekpo . IF it_out_ekpo IS NOT INITIAL AND it_fcat_ekpo IS NOT INITIAL.
*&--------------------------------------------------------------------* *& Form top_ekpo *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* FORM top_ekpo. CLEAR wa_top_ekpo. REFRESH it_top_ekpo. wa_top_ekpo-typ = 'H'. wa_top_ekpo-info = 'Purchase Order Item wise Display'. APPEND wa_top_ekpo TO it_top_ekpo. CLEAR wa_top_ekpo. CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE' EXPORTING it_list_commentary = it_top_ekpo * I_LOGO = * I_END_OF_LIST_GRID = * I_ALV_FORM = . ENDFORM. "top_ekpo *&--------------------------------------------------------------------* *& Form UCOMM_EKPO *&--------------------------------------------------------------------*
Rohini kumar
sap abap consultant
* text *---------------------------------------------------------------------* * -->P_R_UCOM text * <--P_RS_SELFIELD text *---------------------------------------------------------------------* FORM ucomm_ekpo USING r_ucomm_ekpo TYPE sy-ucomm CHANGING rs_selfield_ekpo TYPE slis_selfield. CASE r_ucomm_ekpo. WHEN '&IC1'. IF rs_selfield_ekpo-fieldname = 'EBELN'. SET PARAMETER ID 'BES' FIELD rs_selfield_ekpo-value. CALL TRANSACTION 'ME23N'. ELSE. MESSAGE 'Invalid Field' TYPE 'S'. ENDIF. ENDCASE. ENDFORM. The output is follows: Selection Screen:
First List (9 PO Headers):
" UCOMM_EKPO
Rohini kumar
Second List (by Double clicking on PO 3):
Third Output:
sap abap consultant
Rohini kumar
sap abap consultant
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. 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.
Rohini kumar
sap abap consultant
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. *------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,
Rohini kumar
sap abap consultant
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. 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.
Rohini kumar
sap abap consultant
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 *&---------------------------------------------------------------------* * 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.
Rohini kumar
sap abap consultant
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. 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.
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 -
2. Classical Display -
" TOP_OF_PAGE
Rohini kumar
sap abap consultant
ALV Grid Interactive with a Button Here is an ALD grid report where we can select one or multiple rows of PO on the basic list and then click on a button on Application toolbar. The program will display all the selected PO's item details on the second list. *&--------------------------------------------------------------------* *& Report ZSR_TEST *& *&--------------------------------------------------------------------* *& *& *&------------------------------------------------------------
Rohini kumar
sap abap consultant
---------* REPORT
zsr_test.
TABLES: ekko, ekpo. TYPE-POOLS: slis. TYPES: BEGIN OF ty_ekko, ebeln TYPE ekko-ebeln, bukrs TYPE ekko-bukrs, lifnr TYPE ekko-lifnr, END OF ty_ekko, BEGIN OF ty_out_ekko, sel, ebeln TYPE ekko-ebeln, bukrs TYPE ekko-bukrs, lifnr TYPE ekko-lifnr, END OF ty_out_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, menge TYPE ekpo-menge, meins TYPE ekpo-meins, END OF ty_ekpo, BEGIN OF ty_out_ekpo, sel, ebeln TYPE ekko-ebeln, ebelp TYPE ekpo-ebelp, matnr TYPE ekpo-matnr, werks TYPE ekpo-werks, lgort TYPE ekpo-lgort, menge TYPE ekpo-menge, meins TYPE ekpo-meins, END OF ty_out_ekpo, BEGIN OF ty_ebeln, ebeln TYPE ekpo-ebeln, END OF ty_ebeln. 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,
slis_alv_event, TYPE slis_alv_event, slis_t_event, TYPE slis_t_event,
r_ucomm TYPE sy-ucomm, rs_selfield TYPE slis_selfield, v_selfield TYPE slis_selfield-value, v_ebeln TYPE ekko-ebeln, v_prog TYPE sy-repid, v_name TYPE sy-uname. INITIALIZATION. v_prog = sy-repid. v_name = sy-uname. SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text001. SELECT-OPTIONS s_ebeln FOR ekko-ebeln OBLIGATORY. SELECTION-SCREEN END OF BLOCK b1. START-OF-SELECTION. PERFORM get_ekko. PERFORM fieldcat_ekko. PERFORM layout. PERFORM event_ekko. PERFORM grid_ekko. PERFORM ucomm_ekko USING r_ucomm CHANGING rs_selfield. *&------------------------------------------------------------
Rohini kumar
sap abap consultant
---------* *& Form GET_EKKO *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM get_ekko . REFRESH it_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. REFRESH it_out_ekko. LOOP AT it_ekko INTO wa_ekko. wa_out_ekko-ebeln = wa_ekko-ebeln. wa_out_ekko-bukrs = wa_ekko-bukrs. wa_out_ekko-lifnr = wa_ekko-lifnr. APPEND wa_out_ekko TO it_out_ekko. CLEAR: wa_out_ekko, wa_ekko. ENDLOOP. ELSE. MESSAGE 'Purchase Order doesn''t exist' TYPE 'I'. ENDIF. ENDFORM. " GET_EKKO *&--------------------------------------------------------------------* *& Form FIELDCAT_EKKO *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM fieldcat_ekko . CLEAR wa_fcat_ekko.
Rohini kumar
sap abap consultant
REFRESH it_fcat_ekko. IF it_out_ekko IS NOT INITIAL. DATA lv_col TYPE i VALUE 0. lv_col wa_fcat_ekko-col_pos wa_fcat_ekko-fieldname wa_fcat_ekko-tabname wa_fcat_ekko-seltext_l APPEND wa_fcat_ekko TO CLEAR wa_fcat_ekko.
ENDFORM. " FIELDCAT_EKKO *&--------------------------------------------------------------------* *& Form LAYOUT *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM layout . wa_layout-zebra = 'X'. wa_layout-colwidth_optimize = 'X'. wa_layout-box_fieldname = 'SEL'. ENDFORM.
" LAYOUT
Rohini kumar
sap abap consultant
*&--------------------------------------------------------------------* *& Form EVENT_EKKO *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM event_ekko . REFRESH it_event. * *
CALL FUNCTION 'REUSE_ALV_EVENTS_GET' EXPORTING I_LIST_TYPE = 0 IMPORTING et_events = it_event EXCEPTIONS list_type_wrong = 1 OTHERS = 2. IF it_event IS NOT INITIAL. CLEAR wa_event. READ TABLE it_event INTO wa_event WITH KEY name = 'USER_COMMAND'. IF sy-subrc = 0. wa_event-form = 'UCOMM_EKKO'. MODIFY it_event FROM wa_event INDEX sy-tabix TRANSPORTING form. ENDIF. ENDIF.
ENDFORM. " EVENT_EKKO *&--------------------------------------------------------------------* *& Form GRID_EKKO *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------*
Rohini kumar
sap abap consultant
FORM grid_ekko . IF it_out_ekko IS NOT INITIAL AND it_fcat_ekko IS NOT INITIAL.
*&--------------------------------------------------------------------* *& Form pf_status *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* FORM pf_status USING ut_extab TYPE slis_t_extab. SET PF-STATUS 'PF_EKKO'. ENDFORM. "pf_status *&--------------------------------------------------------------------* *& Form top_ekko *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* FORM top_ekko. CLEAR wa_top_ekko. REFRESH it_top_ekko. DATA date TYPE char12. CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL' EXPORTING date_internal = sy-datum IMPORTING date_external = date EXCEPTIONS date_internal_is_invalid = 1 OTHERS = 2. wa_top_ekko-typ = 'H'. wa_top_ekko-info = 'Purchase Order Header'. APPEND wa_top_ekko TO it_top_ekko. CLEAR wa_top_ekko. wa_top_ekko-typ = 'S'. wa_top_ekko-info = 'Report: '.
Rohini kumar
sap abap consultant
CONCATENATE wa_top_ekko-info v_prog INTO wa_top_ekko-info. APPEND wa_top_ekko TO it_top_ekko. CLEAR wa_top_ekko. wa_top_ekko-typ = 'S'. wa_top_ekko-info = 'User Name: '. CONCATENATE wa_top_ekko-info v_name INTO wa_top_ekko-info. APPEND wa_top_ekko TO it_top_ekko. CLEAR wa_top_ekko. wa_top_ekko-typ = 'S'. wa_top_ekko-info = 'Date: '. CONCATENATE wa_top_ekko-info date INTO wa_top_ekko-info. APPEND wa_top_ekko TO it_top_ekko. CLEAR wa_top_ekko. CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE' EXPORTING it_list_commentary = it_top_ekko * I_LOGO = * I_END_OF_LIST_GRID = * I_ALV_FORM = . ENDFORM. "top_ekko *&--------------------------------------------------------------------* *& Form UCOMM_EKKO *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * -->P_R_UCOMM text * <--P_RS_SELFIELD text *---------------------------------------------------------------------* FORM ucomm_ekko USING r_ucomm_ekko TYPE sy-ucomm CHANGING rs_selfield_ekko TYPE slis_selfield. CASE r_ucomm_ekko. WHEN 'DISP'. REFRESH: it_out_ekpo, it_ebeln. LOOP AT it_out_ekko INTO wa_out_ekko WHERE sel = 'X'. wa_ebeln-ebeln = wa_out_ekko-ebeln.
WHEN 'BACK' OR 'EXIT' OR 'CANCEL' OR 'E'. REFRESH it_out_ekko. LEAVE TO SCREEN 0. ENDCASE. ENDFORM. " UCOMM_EKKO *&--------------------------------------------------------------------* *& Form GET_EKPO *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM get_ekpo . IF it_ebeln IS NOT INITIAL. REFRESH it_ekpo. SELECT ebeln ebelp matnr werks lgort menge meins FROM ekpo INTO TABLE it_ekpo FOR ALL ENTRIES IN it_ebeln WHERE ebeln = it_ebeln-ebeln. IF sy-subrc = 0. SORT it_ekpo BY ebeln. LOOP AT it_ekpo INTO wa_ekpo. AT NEW ebeln. wa_out_ekpo-ebeln = wa_ekpo-ebeln. ENDAT. wa_out_ekpo-ebelp = wa_ekpo-ebelp. wa_out_ekpo-matnr = wa_ekpo-matnr. wa_out_ekpo-werks = wa_ekpo-werks. wa_out_ekpo-lgort = wa_ekpo-lgort. wa_out_ekpo-menge = wa_ekpo-menge.
Rohini kumar
sap abap consultant
wa_out_ekpo-meins = wa_ekpo-meins. APPEND wa_out_ekpo TO it_out_ekpo. CLEAR: wa_out_ekpo, wa_ekpo. ENDLOOP. ENDIF. ENDIF. ENDFORM. " GET_EKPO *&--------------------------------------------------------------------* *& Form FIELDCAT_EKPO *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM fieldcat_ekpo . CLEAR wa_fcat_ekpo. REFRESH it_fcat_ekpo. IF it_out_ekpo IS NOT INITIAL. DATA lv_col TYPE i VALUE 0. lv_col wa_fcat_ekpo-col_pos wa_fcat_ekpo-fieldname wa_fcat_ekpo-tabname wa_fcat_ekpo-seltext_l APPEND wa_fcat_ekpo TO CLEAR wa_fcat_ekpo.
ENDFORM. " FIELDCAT_EKPO *&--------------------------------------------------------------------* *& Form GRID_EKPO *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM grid_ekpo .
Rohini kumar
sap abap consultant
IF it_out_ekpo IS NOT INITIAL AND it_fcat_ekpo IS NOT INITIAL.
ENDFORM. " GRID_EKPO *&--------------------------------------------------------------------* *& Form top_ekpo *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* FORM top_ekpo. CLEAR wa_top_ekpo. REFRESH it_top_ekpo. wa_top_ekpo-typ = 'H'. wa_top_ekpo-info = 'Purchase Order Item Display'. APPEND wa_top_ekpo TO it_top_ekpo. CLEAR wa_top_ekpo. CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE' EXPORTING it_list_commentary = it_top_ekpo * I_LOGO = * I_END_OF_LIST_GRID = * I_ALV_FORM = . ENDFORM.
The Output is as follows: Selection Screen:
Basic List (PO Header):
"top_ekpo
Rohini kumar
Secondary List (PO Item details):
sap abap consultant
Rohini kumar
Click on BACK button to return Basic list:
sap abap consultant
Rohini kumar
Again select different POs to display Item details:
ALV List Display Report
sap abap consultant
Rohini kumar
sap abap consultant
Here is a report which displays the material details with plant, storage location and description. The output is of ALV List display. The looks of this report is different from classic view. Reuse_alv_list_display is the required function module which displays the proper output. ALV List display needs field catalog of structure slis_fieldcat_alv. Otherwise there will be no output. If we need to design the layout then we can pass layout work area of structure slis_layout_alv. Here we have mentioned the zebra layout with a selection box and optimized column design. If we need to mention some information on top of output page then we have to pass an event table. This event table holds the structure slis_t_event. We also need event work area of structure slis_alv_event. After having this data types we have to call function module Reuse_alv_events_get to prepare the event table. At the time of preparing the event table we have to call the top of page form name and have to modify the event table. Below is the coding: *&--------------------------------------------------------------------* *& 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,
Rohini kumar
sap abap consultant
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, 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
ty_mara, ty_marc, ty_mard, ty_makt, ty_out, STANDARD STANDARD STANDARD STANDARD STANDARD
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 declare_event. PERFORM alv_list_display. TOP-OF-PAGE. PERFORM top_of_page. *&--------------------------------------------------------------------* *& Form GET_MATERIAL *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM get_material . SELECT matnr ersda ernam mtart FROM mara INTO TABLE it_mara WHERE matnr IN s_matnr AND mtart = p_mtart. IF sy-subrc = 0. SORT it_mara BY matnr. ELSE.
Rohini kumar
sap abap consultant
MESSAGE 'Material doesn''t exist' TYPE 'I'. ENDIF. ENDFORM. " GET_MATERIAL *&--------------------------------------------------------------------* *& Form GET_PLANT *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM get_plant . IF it_mara IS NOT INITIAL. 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. ENDIF. ENDIF. ENDFORM. " GET_PLANT *&--------------------------------------------------------------------* *& Form GET_STORAGE *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM get_storage . IF it_marc IS NOT INITIAL. SELECT matnr werks lgort FROM mard INTO TABLE it_mard FOR ALL ENTRIES IN it_marc WHERE matnr = it_marc-matnr AND werks = it_marc-werks.
Rohini kumar
sap abap consultant
IF sy-subrc = 0. SORT it_mard BY matnr. ENDIF. ENDIF. ENDFORM. " GET_STORAGE *&--------------------------------------------------------------------* *& Form GET_DESCRIPTION *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM get_description . IF it_mara IS NOT INITIAL. SELECT matnr spras maktx FROM makt INTO TABLE it_makt FOR ALL ENTRIES IN it_mara WHERE matnr = it_mara-matnr AND spras = sy-langu. IF sy-subrc = 0. SORT it_makt BY matnr. ENDIF. ENDIF. ENDFORM. " GET_DESCRIPTION *&--------------------------------------------------------------------* *& Form PREPARE_OUTPUT *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM prepare_output . IF it_mara IS NOT INITIAL AND it_marc IS NOT INITIAL
Rohini kumar
sap abap consultant
AND it_mard IS NOT INITIAL. LOOP AT it_mara INTO wa_mara. wa_out-matnr = wa_mara-matnr. wa_out-ersda = wa_mara-ersda. wa_out-ernam = wa_mara-ernam. wa_out-mtart = wa_mara-mtart. READ TABLE it_makt INTO wa_makt WITH KEY matnr = wa_mara-matnr BINARY SEARCH. IF sy-subrc = 0. wa_out-maktx = wa_makt-maktx. ENDIF. 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. APPEND wa_out TO it_out. CLEAR: wa_out, wa_mara, wa_makt. CLEAR wa_mard. ENDLOOP. CLEAR wa_marc. ENDLOOP. ENDLOOP. ENDIF. ENDFORM. " PREPARE_OUTPUT *&--------------------------------------------------------------------* *& Form PREPARE_FIELDCAT *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM prepare_fieldcat . CLEAR wa_fcat_out.
Rohini kumar
sap abap consultant
REFRESH it_fcat_out. IF it_out IS NOT INITIAL. DATA: lv_col TYPE i VALUE 0. lv_col wa_fcat_out-col_pos wa_fcat_out-fieldname wa_fcat_out-tabname wa_fcat_out-seltext_l APPEND wa_fcat_out TO CLEAR wa_fcat_out.
ENDFORM. " PREPARE_FIELDCAT *&--------------------------------------------------------------------* *& Form DECLARE_EVENT *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM declare_event . CLEAR wa_event. REFRESH it_event. * *
CALL FUNCTION 'REUSE_ALV_EVENTS_GET' EXPORTING I_LIST_TYPE = 0 IMPORTING et_events = it_event EXCEPTIONS list_type_wrong = 1 OTHERS = 2. IF it_event IS NOT INITIAL. READ TABLE it_event INTO wa_event WITH KEY name = 'TOP_OF_PAGE'.
Rohini kumar
sap abap consultant
IF sy-subrc = 0. wa_event-form = 'TOP_OF_PAGE'. MODIFY it_event FROM wa_event INDEX sy-tabix TRANSPORTING form. ENDIF. ENDIF. ENDFORM. " DECLARE_EVENT *&--------------------------------------------------------------------* *& Form ALV_LIST_DISPLAY *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM alv_list_display . IF it_out IS NOT INITIAL AND it_fcat_out IS NOT INITIAL AND it_event IS NOT INITIAL.
ENDFORM. " TOP_OF_PAGE *&--------------------------------------------------------------------* *& Form PREPARE_LAYOUT *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text
Rohini kumar
sap abap consultant
*---------------------------------------------------------------------* FORM prepare_layout . wa_layout-zebra = 'X'. wa_layout-colwidth_optimize = 'X'. wa_layout-box_fieldname = 'SEL'. ENDFORM. The Output is as follows: Selection Screen:
Output Screen:
" PREPARE_LAYOUT
Rohini kumar
Another Selection:
sap abap consultant
Rohini kumar
sap abap consultant
Output:
Posted by SANDIP ROY at 6/21/2013 03:12:00 PM
ALV List Interactive Report Here is an interactive report program which displays the basic output (First Screen) for Purchase Order header information. Then by double clicking the PO number we can go to secondary output which is the item display of that PO. On the Item screen if we double click on PO then ME23N Transaction will open with that particular PO.
Rohini kumar
sap abap consultant
In this reports User Command comes to fetch the double click operation. The output is in ALV List format. *&--------------------------------------------------------------------* *& Report ZSR_TEST *& *&--------------------------------------------------------------------* *& *& *&--------------------------------------------------------------------* REPORT
zsr_test.
TABLES: ekko, ekpo. TYPE-POOLS: slis. TYPES: BEGIN OF ty_ekko, ebeln TYPE ekko-ebeln, bukrs TYPE ekko-bukrs, lifnr TYPE ekko-lifnr, END OF ty_ekko, BEGIN OF ty_out_ekko, sel, ebeln TYPE ekko-ebeln, bukrs TYPE ekko-bukrs, lifnr TYPE ekko-lifnr, END OF ty_out_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, menge TYPE ekpo-menge, meins TYPE ekpo-meins, END OF ty_ekpo, BEGIN OF ty_out_ekpo, sel, ebeln TYPE ekko-ebeln, ebelp TYPE ekpo-ebelp, matnr TYPE ekpo-matnr, werks TYPE ekpo-werks, lgort TYPE ekpo-lgort, menge TYPE ekpo-menge,
Rohini kumar
sap abap consultant
meins TYPE ekpo-meins, END OF ty_out_ekpo. DATA: wa_ekko wa_ekpo it_ekko it_ekpo
TYPE TYPE TYPE TYPE
wa_out_ekko wa_out_ekpo it_out_ekko it_out_ekpo
ty_ekko, ty_ekpo, STANDARD TABLE OF ty_ekko, STANDARD TABLE OF ty_ekpo,
r_ucomm TYPE sy-ucomm, rs_selfield TYPE slis_selfield, v_selfield TYPE slis_selfield-value, v_ebeln TYPE ekko-ebeln, v_prog TYPE sy-repid, v_name TYPE sy-uname. INITIALIZATION. v_prog = sy-repid. v_name = sy-uname. SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text001. SELECT-OPTIONS s_ebeln FOR ekko-ebeln OBLIGATORY. SELECTION-SCREEN END OF BLOCK b1. START-OF-SELECTION. PERFORM get_ekko. PERFORM fieldcat_ekko. PERFORM layout.
Rohini kumar
sap abap consultant
PERFORM event_ekko. PERFORM grid_ekko. PERFORM ucomm_ekko USING r_ucomm CHANGING rs_selfield. TOP-OF-PAGE. PERFORM top_ekko. TOP-OF-PAGE DURING LINE-SELECTION. PERFORM top_ekpo. *&--------------------------------------------------------------------* *& Form ucomm_ekko *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * -->R_UCOMM text * -->RS_ESLFIELD text *---------------------------------------------------------------------* FORM ucomm_ekko USING r_ucomm_ekko TYPE sy-ucomm CHANGING rs_selfield_ekko TYPE slis_selfield. CASE r_ucomm_ekko. WHEN '&IC1'. IF rs_selfield_ekko-fieldname = 'EBELN'. CLEAR v_selfield. v_selfield = rs_selfield_ekko-value. PERFORM conversion_po. PERFORM get_ekpo. PERFORM fieldcat_ekpo. PERFORM layout. PERFORM event_ekpo. PERFORM grid_ekpo. PERFORM ucomm_ekpo USING r_ucomm CHANGING rs_selfield. ELSE. MESSAGE 'Invalid Field' TYPE 'S'. ENDIF. ENDCASE. ENDFORM. "ucomm_ekko *&--------------------------------------------------------------------* *& Form GET_EKKO *&--------------------------------------------------------------------* * text
Rohini kumar
sap abap consultant
*---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM get_ekko . REFRESH it_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. REFRESH it_out_ekko. LOOP AT it_ekko INTO wa_ekko. wa_out_ekko-ebeln = wa_ekko-ebeln. wa_out_ekko-bukrs = wa_ekko-bukrs. wa_out_ekko-lifnr = wa_ekko-lifnr. APPEND wa_out_ekko TO it_out_ekko. CLEAR: wa_out_ekko, wa_ekko. ENDLOOP. ELSE. MESSAGE 'Purchase Order doesn''t exist' TYPE 'I'. ENDIF. ENDFORM. " GET_EKKO *&--------------------------------------------------------------------* *& Form FIELDCAT_EKKO *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM fieldcat_ekko . CLEAR wa_fcat_ekko. REFRESH it_fcat_ekko. IF it_out_ekko IS NOT INITIAL. DATA lv_col TYPE i VALUE 0.
Rohini kumar
sap abap consultant
lv_col wa_fcat_ekko-col_pos wa_fcat_ekko-fieldname wa_fcat_ekko-tabname wa_fcat_ekko-seltext_l APPEND wa_fcat_ekko TO CLEAR wa_fcat_ekko.
ENDFORM. " FIELDCAT_EKKO *&--------------------------------------------------------------------* *& Form LAYOUT *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM layout . wa_layout-zebra = 'X'. wa_layout-colwidth_optimize = 'X'. wa_layout-box_fieldname = 'SEL'. ENDFORM. " LAYOUT *&--------------------------------------------------------------------* *& Form EVENT_EKKO *&--------------------------------------------------------------------*
Rohini kumar
sap abap consultant
* text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM event_ekko . REFRESH it_event_ekko. * *
CALL FUNCTION 'REUSE_ALV_EVENTS_GET' EXPORTING I_LIST_TYPE = 0 IMPORTING et_events = it_event_ekko EXCEPTIONS list_type_wrong = 1 OTHERS = 2. IF it_event_ekko IS NOT INITIAL. CLEAR wa_event_ekko. READ TABLE it_event_ekko INTO wa_event_ekko WITH KEY name = 'USER_COMMAND'. IF sy-subrc = 0. wa_event_ekko-form = 'UCOMM_EKKO'. MODIFY it_event_ekko FROM wa_event_ekko INDEX sy-tabix TRANSPORTING form. ENDIF. CLEAR wa_event_ekko. READ TABLE it_event_ekko INTO wa_event_ekko WITH KEY name = 'TOP_OF_PAGE'. IF sy-subrc = 0. wa_event_ekko-form = 'TOP_EKKO'. MODIFY it_event_ekko FROM wa_event_ekko INDEX sy-tabix TRANSPORTING form. ENDIF. ENDIF.
ENDFORM. " EVENT_EKKO *&--------------------------------------------------------------------* *& Form GRID_EKKO *&--------------------------------------------------------------------* * text *-------------------------------------------------------------
Rohini kumar
sap abap consultant
---------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM grid_ekko . IF it_out_ekko IS NOT INITIAL AND it_fcat_ekko IS NOT INITIAL.
ENDFORM. " GRID_EKKO *&--------------------------------------------------------------------* *& Form top_ekko *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* FORM top_ekko. CLEAR wa_top_ekko. REFRESH it_top_ekko. DATA date TYPE char12. CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL' EXPORTING date_internal = sy-datum IMPORTING date_external = date EXCEPTIONS date_internal_is_invalid = 1 OTHERS = 2. wa_top_ekko-typ = 'H'. wa_top_ekko-info = 'Purchase Order Header'. APPEND wa_top_ekko TO it_top_ekko. CLEAR wa_top_ekko. wa_top_ekko-typ = 'S'. wa_top_ekko-info = 'Report: '. CONCATENATE wa_top_ekko-info v_prog INTO wa_top_ekko-info. APPEND wa_top_ekko TO it_top_ekko. CLEAR wa_top_ekko. wa_top_ekko-typ = 'S'. wa_top_ekko-info = 'User Name: '. CONCATENATE wa_top_ekko-info v_name INTO wa_top_ekko-info. APPEND wa_top_ekko TO it_top_ekko. CLEAR wa_top_ekko. wa_top_ekko-typ = 'S'. wa_top_ekko-info = 'Date: '. CONCATENATE wa_top_ekko-info date INTO wa_top_ekko-info. APPEND wa_top_ekko TO it_top_ekko.
Rohini kumar
sap abap consultant
CLEAR wa_top_ekko. CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE' EXPORTING it_list_commentary = it_top_ekko * I_LOGO = * I_END_OF_LIST_GRID = * I_ALV_FORM = . ENDFORM. "top_ekko *&--------------------------------------------------------------------* *& Form CONVERSION_PO *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM conversion_po . CLEAR v_ebeln. CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT' EXPORTING input = v_selfield IMPORTING output = v_ebeln. ENDFORM. " CONVERSION_PO *&--------------------------------------------------------------------* *& Form GET_EKPO *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM get_ekpo . IF v_ebeln IS NOT INITIAL. REFRESH it_ekpo.
Rohini kumar
sap abap consultant
SELECT ebeln ebelp matnr werks lgort menge meins FROM ekpo INTO TABLE it_ekpo WHERE ebeln = v_ebeln. IF sy-subrc = 0. SORT it_ekpo BY ebelp. REFRESH it_out_ekpo. LOOP AT it_ekpo INTO wa_ekpo. AT NEW ebeln. wa_out_ekpo-ebeln = wa_ekpo-ebeln. ENDAT. wa_out_ekpo-ebelp = wa_ekpo-ebelp. wa_out_ekpo-matnr = wa_ekpo-matnr. wa_out_ekpo-werks = wa_ekpo-werks. wa_out_ekpo-lgort = wa_ekpo-lgort. wa_out_ekpo-menge = wa_ekpo-menge. wa_out_ekpo-meins = wa_ekpo-meins. APPEND wa_out_ekpo TO it_out_ekpo. CLEAR: wa_out_ekpo, wa_ekpo. ENDLOOP. ENDIF. ENDIF. ENDFORM. " GET_EKPO *&--------------------------------------------------------------------* *& Form FIELDCAT_EKPO *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM fieldcat_ekpo . CLEAR wa_fcat_ekpo. REFRESH it_fcat_ekpo. IF it_out_ekpo IS NOT INITIAL. DATA lv_col TYPE i VALUE 0. lv_col wa_fcat_ekpo-col_pos wa_fcat_ekpo-fieldname wa_fcat_ekpo-tabname wa_fcat_ekpo-seltext_l
ENDIF. ENDFORM. " FIELDCAT_EKPO *&--------------------------------------------------------------------* *& Form EVENT_EKPO *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM event_ekpo . REFRESH it_event_ekpo. CALL FUNCTION 'REUSE_ALV_EVENTS_GET' * EXPORTING * I_LIST_TYPE = 0 IMPORTING et_events = it_event_ekpo EXCEPTIONS list_type_wrong = 1 OTHERS = 2. IF it_event_ekpo IS NOT INITIAL. CLEAR wa_event_ekpo. READ TABLE it_event_ekpo INTO wa_event_ekpo WITH KEY name = 'USER_COMMAND'. IF sy-subrc = 0. wa_event_ekpo-form = 'UCOMM_EKPO'. MODIFY it_event_ekpo FROM wa_event_ekpo INDEX sy-tabix TRANSPORTING form. ENDIF. CLEAR wa_event_ekpo. READ TABLE it_event_ekpo INTO wa_event_ekpo WITH KEY name = 'TOP_OF_PAGE'. IF sy-subrc = 0. wa_event_ekpo-form = 'TOP_EKPO'. MODIFY it_event_ekpo FROM wa_event_ekpo INDEX sy-tabix TRANSPORTING form. ENDIF. ENDIF.
Rohini kumar
sap abap consultant
ENDFORM. " EVENT_EKPO *&--------------------------------------------------------------------* *& Form GRID_EKPO *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM grid_ekpo . IF it_out_ekpo IS NOT INITIAL AND it_fcat_ekpo IS NOT INITIAL.
*&--------------------------------------------------------------------* *& Form top_ekpo *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* FORM top_ekpo. CLEAR wa_top_ekpo. REFRESH it_top_ekpo. wa_top_ekpo-typ = 'H'. wa_top_ekpo-info = 'Purchase Order Item wise Display'. APPEND wa_top_ekpo TO it_top_ekpo. CLEAR wa_top_ekpo. CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE' EXPORTING it_list_commentary = it_top_ekpo * I_LOGO = * I_END_OF_LIST_GRID = * I_ALV_FORM = . ENDFORM. "top_ekpo *&--------------------------------------------------------------------* *& Form UCOMM_EKPO *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * -->P_R_UCOM text * <--P_RS_SELFIELD text *---------------------------------------------------------------------* FORM ucomm_ekpo USING r_ucomm_ekpo TYPE sy-ucomm
Rohini kumar
sap abap consultant
CHANGING rs_selfield_ekpo
TYPE
slis_selfield. CASE r_ucomm_ekpo. WHEN '&IC1'. IF rs_selfield_ekpo-fieldname = 'EBELN'. SET PARAMETER ID 'BES' FIELD v_ebeln. CALL TRANSACTION 'ME23N'. ELSE. MESSAGE 'Invalid Field' TYPE 'S'. ENDIF. ENDCASE. ENDFORM. The output is follows: Selection Screen:
First List (11 PO Headers):
" UCOMM_EKPO
Rohini kumar
Second List (by Double clicking on PO 14):
Third Output:
sap abap consultant
Rohini kumar
sap abap consultant
F4 Help for Parameter and Select Option Here is a Program which defines the F4 help on Selection Screen. On the screen there are two fields 1. Company Code and 2. Project Definition. Here the company code is the Parameter and the project definition is the Select Option. In this program there is no manual entry. Every input will be given by using F4 help. The company codes will be coming uniquely by using F4 help. In database one company code contains lot of Projects. Hence unique company code is necessary while pressing the F4 button. Then the Project Definition Low field will also have to be selected by using F4 help. Here in the value table all project definition will not come. Only the projects which are under selected company code will come for the Low field. Other projects will not come in value table. Similarly the High field will have to be selected by using F4 help. High filed will show the value table data which are greater than the low field and under that selected company. Other projects will not come on this value table. REPORT
zabap_gui1.
*---------PROJ & PRPS Tables Declaration-------------------------------* *----------------------------------------------------------------------* TABLES: proj, prps. *---------Structure Declaration for different Internal Tables----------*
Rohini kumar
sap abap consultant
*----------------------------------------------------------------------* TYPES: "Structure to create F4 Help for Company Code BEGIN OF ty_vbukr, vbukr TYPE proj-vbukr, "Company code END OF ty_vbukr, "Structure to create F4 Help for Project definition BEGIN OF ty_pspid, pspid TYPE proj-pspid, "Project definition END OF ty_pspid. DATA: "Internal Tables of Value Table for F4 Help it_vbukr TYPE STANDARD TABLE OF ty_vbukr, it_pspid TYPE STANDARD TABLE OF ty_pspid, it_pspid1 TYPE STANDARD TABLE OF ty_pspid, "Internal Tables of Return Table for F4 Help wa_return1 TYPE ddshretval, wa_return2 TYPE ddshretval, wa_return3 TYPE ddshretval, it_return1 TYPE STANDARD TABLE OF ddshretval, it_return2 TYPE STANDARD TABLE OF ddshretval, it_return3 TYPE STANDARD TABLE OF ddshretval. CONSTANTS: "Constants for F4IF_INT_TABLE_VALUE_REQUEST c_vbukr TYPE dfies-fieldname VALUE 'P_VBUKR', c_pspid_l TYPE dfies-fieldname VALUE 'P_PSPID-LOW', c_pspid_h TYPE dfies-fieldname VALUE 'P_PSPID-HIGH', c_vorg TYPE char1 VALUE 'S'. *---------SELECTION SCREEN---------------------------------------------* *----------------------------------------------------------------------* SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001. PARAMETERS: p_vbukr LIKE proj-vbukr. SELECT-OPTIONS s_pspid FOR proj-pspid. SELECTION-SCREEN END OF BLOCK b1. *---------AT SELECTION-SCREEN ON VALUE-REQUEST for COMPANY CODE--------* *----------------------------------------------------------------------* AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_vbukr. SELECT DISTINCT vbukr FROM proj INTO TABLE it_vbukr. IF sy-subrc = 0. SORT it_vbukr BY vbukr. ENDIF. "Function Module to create F4 help CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST' EXPORTING retfield = c_vbukr value_org = c_vorg TABLES value_tab = it_vbukr return_tab = it_return1 EXCEPTIONS parameter_error = 1 no_values_found = 2 OTHERS = 3.
Rohini kumar
sap abap consultant
IF it_return1 IS NOT INITIAL. LOOP AT it_return1 INTO wa_return1. p_vbukr = wa_return1-fieldval. ENDLOOP. SELECT pspid FROM proj INTO TABLE it_pspid WHERE vbukr = p_vbukr. IF sy-subrc = 0. SORT it_pspid BY pspid. ENDIF. ENDIF. *-----AT SELECTION-SCREEN ON VALUE-REQUEST for PROJECT DEFINITION------* *----------------------------------------------------------------------* AT SELECTION-SCREEN ON VALUE-REQUEST FOR s_pspid-low. "Function Module to create F4 help CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST' EXPORTING retfield = c_pspid_l value_org = c_vorg TABLES value_tab = it_pspid return_tab = it_return2 EXCEPTIONS parameter_error = 1 no_values_found = 2 OTHERS = 3. IF it_return2 IS NOT INITIAL. LOOP AT it_return2 INTO wa_return2. s_pspid-low = wa_return2-fieldval. ENDLOOP. SELECT pspid FROM proj INTO TABLE it_pspid1 WHERE pspid GT s_pspid-low AND vbukr = p_vbukr. IF sy-subrc = 0. SORT it_pspid1 BY pspid. ENDIF. ENDIF. *-----AT SELECTION-SCREEN ON VALUE-REQUEST for PROJECT DEFINITION------* *----------------------------------------------------------------------* AT SELECTION-SCREEN ON VALUE-REQUEST FOR s_pspid-high. "Function Module to create F4 help CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST' EXPORTING retfield = c_pspid_h value_org = c_vorg TABLES value_tab = it_pspid1 return_tab = it_return3 EXCEPTIONS parameter_error = 1 no_values_found = 2 OTHERS = 3. IF it_return3 IS NOT INITIAL. LOOP AT it_return3 INTO wa_return3.
3. F4 on Project Definition Low Field (All projects are coming from company 0142): Total 2598 Entries
Rohini kumar
sap abap consultant
4. F4 on Project Definition High Field (All projects will be Greater than Low field with company 0142): Total 2589 Entries
Rohini kumar
5. Finally Data have been Selected:
sap abap consultant
Rohini kumar
sap abap consultant
Two ALV Grids in Single Screen using OOPs In a report we can create two ALV grids in a single screen by using two Custom Container in one screen. Here we shall have a standard screen (like screen 0100 or 1000). We must not modify that screen. We shall create a custom screen (screen 9000 or 9001), since custom screen number starts from 9000. After that we shall go to the LAYOUT of screen 9000. There we shall make two containers by drag and drop as following.
Rohini kumar
sap abap consultant
Now in PBO block we shall make custom PF status and custom title. Then after we shall create object for Container 1 and Container 2 which are referred to CL_GUI_CUSTOM_CONTAINER. CODE: CREATE OBJECT ob_custom1 EXPORTING container_name = 'CONTAINER1'. CREATE OBJECT ob_custom2
Rohini kumar
sap abap consultant
EXPORTING container_name = 'CONTAINER2'. After that ALV Grid 1 and Grid 2 objects shall be created for Container 1 and 2. The Grids are referred to CL_GUI_ALV_GRID. Here we shall assign grid 1 for container 1 by exporting i_parent to the specific container. Similarly grid 2 for container 2 must be assigned by exporting i_parent to container 2. CODE: CREATE OBJECT ob_grid1 EXPORTING i_parent = ob_custom1. CREATE OBJECT ob_grid2 EXPORTING i_parent = ob_custom2. After that Grid method SET_TABLE_FOR_FIRST_DISPLAY must be called with changing parameters. Here we have used field catalog (it_fieldcatalog) for selected custom fields and output table (it_outtab) for specific output table (Internal Table). CODE: CALL METHOD ob_grid1->set_table_for_first_display CHANGING it_fieldcatalog = it_fcat_ekko it_outtab = it_ekko. CALL METHOD ob_grid2->set_table_for_first_display CHANGING it_fieldcatalog = it_fcat_ekpo it_outtab = it_ekpo. In the PAI block we shall validate the BACK, EXIT and CANCEL button by user command. Here 2 custom containers and 2 grids must be freed and internal tables must be refreshed. Then we shall leave to screen 0. CODE: 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.
Rohini kumar
sap abap consultant
Detailed Coding of the Requirement: *&--------------------------------------------------------------------* *& Report ZSR_TEST *& *&--------------------------------------------------------------------* *& *& *&--------------------------------------------------------------------* REPORT
zsr_test NO STANDARD PAGE HEADING.
TABLES: ekko, ekpo. 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, lgort TYPE ekpo-lgort, sel TYPE char1, 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.
"Field Catalog structure and Tables DATA: wa_fcat_ekko TYPE lvc_s_fcat, wa_fcat_ekpo TYPE lvc_s_fcat, it_fcat_ekko TYPE STANDARD TABLE OF lvc_s_fcat, it_fcat_ekpo TYPE STANDARD TABLE OF lvc_s_fcat. DATA: "Object for Custom Container ob_custom1 TYPE REF TO cl_gui_custom_container, ob_custom2 TYPE REF TO cl_gui_custom_container, "Object for GRIDs
Rohini kumar
ob_grid1 ob_grid2
sap abap consultant
TYPE REF TO cl_gui_alv_grid, TYPE REF TO cl_gui_alv_grid.
INITIALIZATION. SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text001. SELECT-OPTIONS s_ebeln FOR ekko-ebeln. SELECTION-SCREEN END OF BLOCK b1. *---------------------------------------------------------------------* * CLASS purchase DEFINITION *---------------------------------------------------------------------* * *---------------------------------------------------------------------* CLASS purchase DEFINITION. PUBLIC SECTION. METHODS: get_ekko, "Selection from EKKO get_ekpo, "Selection from EKPO fieldcat_ekko, "Fieldcatalog for Header Table fieldcat_ekpo. "Fieldcatalog for Item Table ENDCLASS.
"purchase DEFINITION
*---------------------------------------------------------------------* * CLASS purchase IMPLEMENTATION *---------------------------------------------------------------------* * *---------------------------------------------------------------------* CLASS purchase IMPLEMENTATION. METHOD get_ekko. IF s_ebeln[] IS NOT INITIAL. 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 'PO doesn''t exist' TYPE 'I'. ENDIF.
Rohini kumar
sap abap consultant
ELSE. MESSAGE 'Please select a valid PO' TYPE 'I'. ENDIF. ENDMETHOD. "get_ekko METHOD get_ekpo. IF it_ekko IS NOT INITIAL. SELECT ebeln ebelp matnr werks lgort FROM ekpo INTO TABLE it_ekpo FOR ALL ENTRIES IN it_ekko WHERE ebeln = it_ekko-ebeln AND bukrs = it_ekko-bukrs. IF sy-subrc = 0. SORT it_ekpo BY ebeln. CALL METHOD: fieldcat_ekko, fieldcat_ekpo. CALL SCREEN 9000. ELSE. MESSAGE 'Item doesn''t exist' TYPE 'I'. ENDIF. ENDIF. ENDMETHOD. "get_ekpo METHOD fieldcat_ekko. CLEAR wa_fcat_ekko. REFRESH it_fcat_ekko. DATA: lv_col TYPE i VALUE 0. lv_col wa_fcat_ekko-col_pos wa_fcat_ekko-fieldname wa_fcat_ekko-tabname wa_fcat_ekko-reptext wa_fcat_ekko-col_opt APPEND wa_fcat_ekko TO CLEAR wa_fcat_ekko.
START-OF-SELECTION. DATA: purchase TYPE REF TO purchase. CREATE OBJECT purchase. CALL METHOD: purchase->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.
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.
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.
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.
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.
= 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
*& 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.
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 * <-- p2 text *---------------------------------------------------------------------* FORM leave . LEAVE TO SCREEN 0. LEAVE LIST-PROCESSING. ENDFORM. " LEAVE *&--------------------------------------------------------------------* *& Form DISPLAY *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM display . IF kna1-kunnr IS NOT INITIAL. SELECT SINGLE kunnr land1 name1 ort01 FROM kna1 INTO wa_kna1 WHERE kunnr = kna1-kunnr. IF sy-subrc = 0. CALL SCREEN 9002. ELSE. MESSAGE 'Customer Number doesn''t Exist' TYPE 'I'. ENDIF. ELSE. MESSAGE 'Invalid Customer Number' TYPE 'I'. LEAVE LIST-PROCESSING. ENDIF. ENDFORM.
" DISPLAY
*&--------------------------------------------------------------------* *& Form CANCEL
Rohini kumar
sap abap consultant
*&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM cancel . CLEAR: kna1-kunnr, country, name, city. LEAVE TO SCREEN 9001. LEAVE LIST-PROCESSING. ENDFORM. " CANCEL *&--------------------------------------------------------------------* *& Form LEAVE2 *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM leave2 . CLEAR: kna1-land1, kna1-name1, kna1-ort01. LEAVE TO SCREEN 0. LEAVE LIST-PROCESSING. ENDFORM. " LEAVE2 *&--------------------------------------------------------------------* *& Form PROCESS_LIST *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM process_list .
Rohini kumar
sap abap consultant
IF name = 'X'. kna1-name1 = wa_kna1-name1. ENDIF. IF country = 'X'. kna1-land1 = wa_kna1-land1. ENDIF. IF city = 'X'. kna1-ort01 = wa_kna1-ort01. ENDIF. ENDFORM.
" PROCESS_LIST
We have created two screens here as follows: PROCESS BEFORE OUTPUT. MODULE STATUS_9001. PROCESS AFTER INPUT. MODULE USER_COMMAND_9001. and PROCESS BEFORE OUTPUT. MODULE STATUS_9002. PROCESS AFTER INPUT. MODULE USER_COMMAND_9002. We have created a Transaction Code here and below is the output of first screen:
Rohini kumar
sap abap consultant
Then we have the first screen as follows:
Now we are entering the input value and check the boxes as per requirement:
Rohini kumar
sap abap consultant
Finally we click on the display button and then following output has come:
Rohini kumar
sap abap consultant
Now by clicking BACK button on the standard toolbar we come back to the first screen and then again by clicking BACK button we shall go to the home screen:
Select Options in Module Pool Program We can make Select Option for input element in the module pool program. Following are the steps. Step1 Step2
Go to SE80 and create a program (here it is zsandip_mod). Create top include with naming convention. Here the names are: INCLUDE zsandip_top global Data INCLUDE zsandip_o01 PBO-Modules INCLUDE zsandip_i01 PAI-Modules INCLUDE zsandip_f01 FORM-Routines
.
"
.
"
.
"
.
"
Rohini kumar
sap abap consultant
Step3 Step4
Create a screen that contains a number (here 9000).
Step5
In the Top Include mention the select option like below:
Inside the screen create a sub screen by using the sub screen tool bar. There is no need to create that sub screen separately.
TABLES mara. DATA: ok_code TYPE sy-ucomm. SELECTION-SCREEN BEGIN OF SCREEN 100 AS SUBSCREEN. SELECT-OPTIONS s_matnr FOR mara-matnr. SELECTION-SCREEN END OF SCREEN 100.
Step6
Now write the code in the flow logic of the screen 9000. In PBO we are calling sub screen 'SEL' which is including the program name with the sub screen number '100'. After that we have to call sub screen 'SEL' at first in the PAI block. If here it is not declared then the Select Option will remain blank. PROCESS BEFORE OUTPUT. MODULE status_9000. CALL SUBSCREEN sel INCLUDING sy-repid '100'. PROCESS AFTER INPUT. CALL SUBSCREEN sel. MODULE user_command_9000. Here in the Element List we have to declare the ok_code.
Step7
After that create the GUI status and PF status in the PBO module.
Step8
Next in PAI module mention the code shown below: CASE ok_code. WHEN 'BACK'. PERFORM leave. WHEN 'EXIT'. PERFORM leave. WHEN 'CANCEL'. PERFORM leave. ENDCASE.
Step9
Next declare the subroutine in INCLUDE zsandip_f01 and mention the following code:
Rohini kumar
sap abap consultant
LEAVE TO SCREEN 0. LEAVE LIST-PROCESSING. Step10
Finally create a transaction code (here zsr_mod) and run that by separately.
Layout is as follows (to view large click on the figure):
Output is as follows (to view large click on the figure):
Simple Table Control Table control is one of a ABAP displaying mechanisms. With the help of Table control we can see the output table in different visibility approach. Here we have developed a Module Pool program where a simple table
Rohini kumar
sap abap consultant
control has been incorporated. The table control will look like as follows from the layout. It is created from layout palate.
Here we have created a simple table control where we have two screens. First screen in the selection screen where we need to select a purchase order number and click on the display button. After that second screen will open and it contains the item wise detailed information. Following is the Steps for Table Control: Step 1: At first we are creating a module pool program with proper naming convention and create all the include programs as follows. INCLUDE INCLUDE INCLUDE INCLUDE
mz_sr_top mz_sr_o01 mz_sr_i01 mz_sr_f01
. . . .
" " " "
global Data PBO-Modules PAI-Modules FORM-Routines
Rohini kumar
sap abap consultant
Step 2: Next declare all the variables, structures, tables etc at top include. PROGRAM
sapmz_sr.
*-------Declaration of tables for screen fields------------------------* TABLES: ekko, ekpo. *------Declaration of required structures------------------------------* 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, 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. *-----Declaration of user command variables----------------------------* DATA: ok_code1 TYPE sy-ucomm, ok_code2 TYPE sy-ucomm. *-----Declaration of work area & table---------------------------------* DATA: wa_ekko TYPE ty_ekko, wa_ekpo TYPE ty_ekpo, it_ekpo TYPE TABLE OF ty_ekpo. *---------Declaration of Table Control---------------------------------* CONTROLS: tab_ctrl TYPE TABLEVIEW USING SCREEN 9002.
Step 3: Now create a screen 9001 which is the selection screen of purchase order.
Rohini kumar
In the elementary list we declare the sy-ucomm (ok_code).
Step 4: Write the flow logic as follows. PROCESS BEFORE OUTPUT. MODULE status_9001. PROCESS AFTER INPUT. MODULE user_command_9001.
Step 5:
sap abap consultant
Rohini kumar
sap abap consultant
Create the layout with required buttons and input fields.
Step 6: Create PBO and PAI module of screen 9001 as follows. MODULE status_9001 OUTPUT. SET PF-STATUS 'PF_PO_INP'. SET TITLEBAR 'PO_TITLE'. ENDMODULE.
" status_9001
OUTPUT
MODULE user_command_9001 INPUT. CASE ok_code1. WHEN 'DISP'. "Display button PERFORM get_po. WHEN 'CLR'. "Clear button CLEAR ekko-ebeln. WHEN 'BACK' OR 'EXIT' OR 'CANCEL'. LEAVE PROGRAM. ENDCASE. ENDMODULE.
" user_command_9001
INPUT
Step 7: Now we are going to create the functionality of Display button in the subroutine as follows. FORM get_po .
Rohini kumar
sap abap consultant
IF ekko-ebeln IS NOT INITIAL. REFRESH: it_ekpo. SELECT SINGLE ebeln bukrs ernam lifnr FROM ekko INTO wa_ekko WHERE ebeln = ekko-ebeln. IF sy-subrc = 0. SELECT ebeln ebelp matnr werks lgort FROM ekpo INTO TABLE it_ekpo WHERE ebeln = wa_ekko-ebeln. IF sy-subrc = 0. SORT it_ekpo. "Refreshing the table control to have updated data REFRESH CONTROL 'TAB_CTRL' FROM SCREEN 9002. CALL SCREEN 9002. ENDIF. ENDIF. ENDIF. ENDFORM.
" get_po
Step 8: After that create the screen 9002 which is the table control screen of PO item wise details.
Rohini kumar
sap abap consultant
Step 9: Write the flow logic of table control in PBO & PAI. PROCESS BEFORE OUTPUT. MODULE status_9002. LOOP AT it_ekpo INTO wa_ekpo WITH CONTROL tab_ctrl. MODULE table_control. ENDLOOP. PROCESS AFTER INPUT. LOOP AT it_ekpo. MODULE modify_table_control. ENDLOOP. MODULE user_command_9002.
Step 10: Now go to layout and create the table control from palate. The name must be TAB_CTRL.
Rohini kumar
sap abap consultant
Step 11: Click on the dictionary button and select the required fields which need to be displayed in the table control.
Step 12: After that the table control will look like this.
Rohini kumar
We can modify the visibility length also.
sap abap consultant
Rohini kumar
sap abap consultant
Step 13: Now we have to create the PBO module of table control as follows. MODULE status_9002 OUTPUT. SET PF-STATUS 'PF_PO_INP'. SET TITLEBAR 'PO_TITLE'. ENDMODULE.
" status_9002
OUTPUT
MODULE table_control OUTPUT. "Describing table to populate sy-dbcnt DESCRIBE TABLE it_ekpo LINES sy-dbcnt. "Current line populates the loop information in table control tab_ctrl-current_line = sy-loopc. "Lines are populated with number of table lines tab_ctrl-lines = sy-dbcnt. "Moving data ekpo-ebeln = ekpo-ebelp = ekpo-matnr = ekpo-werks = ekpo-lgort =
from work area to screen fields wa_ekpo-ebeln. wa_ekpo-ebelp. wa_ekpo-matnr. wa_ekpo-werks. wa_ekpo-lgort.
Rohini kumar
sap abap consultant
CLEAR wa_ekpo. ENDMODULE.
" table_control
OUTPUT
Step 14: Now create the PAI module as follows. MODULE user_command_9002 INPUT. CASE ok_code2. WHEN 'BACK' OR 'EXIT' OR 'CANCEL'. "Due to multiple clicks user command needs to be updated CLEAR ok_code2. LEAVE LIST-PROCESSING. LEAVE TO SCREEN 9001. ENDCASE. ENDMODULE.
" user_command_9002
INPUT
MODULE modify_table_control INPUT. "Readin the table with current line READ TABLE it_ekpo INTO wa_ekpo INDEX tab_ctrl-current_line. IF sy-subrc = 0. "Modifying the current line in table control MODIFY it_ekpo FROM wa_ekpo INDEX tab_ctrl-current_line. ENDIF. ENDMODULE.
" modify_table_control
INPUT
Step 15: Finally create a Transaction code and run it from SAP system. The selection screen which is the first screen will come and enter a PO number there.
Rohini kumar
sap abap consultant
Step 14: Click on Display button and we see the item wise details in table control.
Rohini kumar
Now we can scroll down like this.
sap abap consultant
Rohini kumar
sap abap consultant
Table Control Output by Select Option Input We have developed here a Module Pool program where Table Control has been used for output and Select Option for input. In the standard selection screen 100 we have maintained a select option for input like follows.
Rohini kumar
sap abap consultant
Here the Selection Screen is actually a sub screen and screen number 100 has been maintained by the program. The buttons DISPLAY and CLEAR have been maintained from the custom screen 9001. Hence the 9001 screen contains the selection screen as a sub screen and the sub screen 100 is maintained from the program. The layout of the 9001 screen is as follows:
Hence the logic of the selection screen is Calling the sub screen which is including program name and screen number 100. This logic has to be maintained in the PBO block. After that PAI block is
Rohini kumar
sap abap consultant
started. Here we have to Call sub screen and then we shall call the user command module. Here calling the sub screen in PBO is basically calling the selection screen and calling the sub screen in PAI is basically passing the value from the screen to the program. Hence if we don't call sub screen in PAI then input parameter and selection option will be blank in the main program. Now we have prepared second screen 9002 where the Table Control is there. In the PBO block of the 9002 screen we have to Loop at output table with table control
. Inside this loop we have created a module table_control which passes data of output table to the screen fields. After that in the PAI block we have to maintain the table control's next page which is triggered by Scroll down or by clicking the down arrow of table control. Hence we have Loop at output table again and inside the loop we have read output table with index Current Line. This Read is for checking if any further lines exist or not. So by checking sy-subrc value we have modified output table with current line index. The layout of Screen 9002 is as follows:
The steps are as follows.
Rohini kumar
sap abap consultant
Step 1: Create a module pool program with proper naming convention and activate all the include programs. INCLUDE INCLUDE INCLUDE INCLUDE
mz_test_top mz_test_o01 mz_test_i01 mz_test_f01
. . . .
" " " "
global Data PBO-Modules PAI-Modules FORM-Routines
Step 2: Declare all the important declarations at top include. PROGRAM
sapmz_test.
TABLES mara. TYPES: BEGIN OF ty_mara, matnr TYPE mara-matnr, ersda TYPE mara-ersda, ernam TYPE mara-ernam, laeda TYPE mara-laeda, aenam TYPE mara-aenam, pstat TYPE mara-pstat, mtart TYPE mara-mtart, mbrsh TYPE mara-mbrsh, matkl TYPE mara-matkl, meins TYPE mara-meins, bstme TYPE mara-bstme, END OF ty_mara.
"Material Number "Date "Name "Date of Last Change "Name of Person Who Changed Object "Maintenance status "Material Type "Industry sector "Material Group "Base Unit of Measure "Purchase Order Unit of Measure
DATA: wa_mara TYPE ty_mara, it_mara TYPE TABLE OF ty_mara. DATA: ok_code_sel TYPE sy-ucomm, ok_code_mat TYPE sy-ucomm. CONTROLS tab_ctrl TYPE TABLEVIEW USING SCREEN 9002. SELECTION-SCREEN BEGIN OF SCREEN 100 AS SUBSCREEN. SELECT-OPTIONS s_matnr FOR mara-matnr. SELECTION-SCREEN END OF SCREEN 100.
Step 3: Create a screen 9001 and set the ok code = ok_code_sel.
Rohini kumar
sap abap consultant
Step 4: Write the flow logic as follows. PROCESS BEFORE OUTPUT. MODULE status_9001. "Calling the Subscreen including program name & screen CALL SUBSCREEN sel INCLUDING sy-repid '100'. PROCESS AFTER INPUT. "Calling the Subscreen at PAI CALL SUBSCREEN sel. MODULE user_command_9001.
Step 5: Create the custom layout as follows. In the layout we need to create the sub screen by layout palate.
Rohini kumar
sap abap consultant
Step 6: Create the PBO module of screen 9001. MODULE status_9001 OUTPUT. SET PF-STATUS 'GUI_9001'. SET TITLEBAR 'TITLE_9001'. ENDMODULE.
" status_9001
OUTPUT
Step 7: Create the PAI module of screen 9001. MODULE user_command_9001 INPUT. CASE ok_code_sel. WHEN 'DISP'. "Display button PERFORM get_data_mara. WHEN 'CLR'. "Clear button CLEAR s_matnr. REFRESH s_matnr[]. WHEN 'BACK'. "Back button LEAVE PROGRAM. ENDCASE. ENDMODULE.
" user_command_9001
Step 8: Now we need to create the subroutine get_data_mara. FORM get_data_mara .
INPUT
Rohini kumar
sap abap consultant
IF s_matnr[] IS NOT INITIAL. SELECT matnr ersda ernam laeda aenam pstat mtart mbrsh matkl meins bstme FROM mara INTO TABLE it_mara WHERE matnr IN s_matnr. IF sy-subrc = 0. SORT it_mara. "Refreshing table control from the screen REFRESH CONTROL 'TAB_CTRL' FROM SCREEN 9002. "Calling the screen of table control CALL SCREEN 9002. ENDIF. ENDIF. ENDFORM.
" get_data_mara
Step 9: Now create the 9002 screen and set ok code = ok_code_mat.
Step 10: Write the flow logic of table control as follows. PROCESS BEFORE OUTPUT. MODULE status_9002.
Rohini kumar
sap abap consultant
"Looping the table with table control LOOP AT it_mara INTO wa_mara WITH CONTROL tab_ctrl. MODULE prepare_table_control. ENDLOOP. PROCESS AFTER INPUT. "Looping the table at PAI LOOP AT it_mara. MODULE modify_table_control. ENDLOOP. MODULE user_command_9002.
Step 11: Create the PBO & PAI module as follows. MODULE status_9002 OUTPUT. SET PF-STATUS 'GUI_9001'. SET TITLEBAR 'TITLE_9002'. ENDMODULE.
" status_9002
OUTPUT
MODULE user_command_9002 INPUT. CASE ok_code_mat. WHEN 'BACK'. CLEAR ok_code_mat. LEAVE LIST-PROCESSING. LEAVE TO SCREEN 9001. ENDCASE. ENDMODULE.
" user_command_9002
INPUT
Step 12: Now prepare the table control at PBO level as follows. MODULE prepare_table_control OUTPUT. "Describing table to populate sy-dbcnt DESCRIBE TABLE it_mara LINES sy-dbcnt. "Current line populates the loop information in table control tab_ctrl-current_line = sy-loopc. "Lines are populated with number of table lines tab_ctrl-lines = sy-dbcnt. "Moving data mara-matnr = mara-ersda = mara-ernam = mara-laeda = mara-aenam =
from work area to screen fields wa_mara-matnr. wa_mara-ersda. wa_mara-ernam. wa_mara-laeda. wa_mara-aenam.
Step 13: Create the PAI module of table control to modify the internal table. MODULE modify_table_control INPUT. "Reading the table with current line of table control READ TABLE it_mara INTO wa_mara INDEX tab_ctrl-current_line. IF sy-subrc = 0. "Modifying the table with table control current line MODIFY it_mara FROM wa_mara INDEX tab_ctrl-current_line. ENDIF. ENDMODULE.
" modify_table_control
INPUT
Step 14: Now create the T-code to execute this program.
Now entering the Material number range we are getting this details with table control.
Rohini kumar
Now we can scroll as follows.
sap abap consultant
Rohini kumar
Similarly we can scroll to view further column at right side.
sap abap consultant
Rohini kumar
sap abap consultant
Multiple Table Controls Here we are creating multiple table controls by using different screens. At first we shall have a selection screen where we are using select option (selection range) to enter the input of airline code. Since select option can only be used under a selection screen, we have declared a sub screen while creating the first screen 9001.
Rohini kumar
sap abap consultant
The first screen 9001 contains two push buttons ‘DISPLAY’ and ‘CLEAR’. After entering the selection range if DISPLAY button is clicked then the program will continue with second screen.
On the second screen 9002 we shall have the airline details with table control. In this table control we have declared a selection mark by which we can mark a specific row. We have a push button ‘FLIGHT NUMBER’ in this screen 9002. Now after selecting a row we can see flight number details by clicking that button.
Rohini kumar
sap abap consultant
Step – 1: At first we need to create all the include programs. INCLUDE INCLUDE INCLUDE INCLUDE
mz_testtop mz_testo01 mz_testi01 mz_testf01
. . . .
" " " "
global Data PBO-Modules PAI-Modules FORM-Routines
Step – 2: Next we are declaring the top include for global declaration. *&---------------------------------------------------------------------* *& Include MZ_TESTTOP Module Pool SAPMZ_TEST *& *&---------------------------------------------------------------------* PROGRAM
sapmz_test.
*-------Decalring the database tables for screen fields----------------* TABLES: scarr, spfli, sflight. TYPES: *------Airline structure-----------------------------------------------* BEGIN OF ty_scarr, carrid TYPE scarr-carrid, carrname TYPE scarr-carrname, currcode TYPE scarr-currcode, END OF ty_scarr, *------Airline structure for screen output-----------------------------* BEGIN OF ty_out_scarr,
Rohini kumar
sap abap consultant
mark TYPE char1, "Selection box carrid TYPE scarr-carrid, carrname TYPE scarr-carrname, currcode TYPE scarr-currcode, END OF ty_out_scarr, *------Flight schedule structure---------------------------------------* BEGIN OF ty_spfli, carrid TYPE spfli-carrid, connid TYPE spfli-connid, countryfr TYPE spfli-countryfr, cityfrom TYPE spfli-cityfrom, airpfrom TYPE spfli-airpfrom, cityto TYPE spfli-cityto, airpto TYPE spfli-airpto, END OF ty_spfli, *------Flight schedule structure for screen output---------------------* BEGIN OF ty_out_spfli, mark TYPE char1, carrid TYPE spfli-carrid, connid TYPE spfli-connid, countryfr TYPE spfli-countryfr, cityfrom TYPE spfli-cityfrom, airpfrom TYPE spfli-airpfrom, cityto TYPE spfli-cityto, airpto TYPE spfli-airpto, END OF ty_out_spfli. DATA: *-----Airline work areas & internal tables-----------------------------* lw_scarr TYPE ty_scarr, wa_scarr TYPE ty_out_scarr, lt_scarr TYPE TABLE OF ty_scarr, it_scarr TYPE TABLE OF ty_out_scarr, *-----Temporary internal table to store data for reusing---------------* it_temp_scarr TYPE TABLE OF ty_out_scarr, *-----To capture the Airline code for different use--------------------* v_carrid TYPE scarr-carrid, *-----Flight schedule work areas & internal tables---------------------* lw_spfli TYPE ty_spfli, wa_spfli TYPE ty_out_spfli, lt_spfli TYPE TABLE OF ty_spfli, it_spfli TYPE TABLE OF ty_out_spfli. DATA: ok_code1 TYPE sy-ucomm, "User command for screen 9001 ok_code2 TYPE sy-ucomm, "User command for screen 9002 ok_code3 TYPE sy-ucomm. "User command for screen 9003 *---------Declaring the selection screen as a subscreen of 100---------* SELECTION-SCREEN BEGIN OF SCREEN 100 AS SUBSCREEN. SELECT-OPTIONS: s_carrid FOR scarr-carrid. "Select option SELECTION-SCREEN END OF SCREEN 100. *---------Declaring the table controls for different screen------------* CONTROLS: tab_ctrl_scarr TYPE TABLEVIEW USING SCREEN 9002, tab_ctrl_spfli TYPE TABLEVIEW USING SCREEN 9003.
Rohini kumar
sap abap consultant
Here we have declared the selection screen as a sub screen of 100. Select option can only be called from a selection screen in module pool program. We need to create this sub screen at 9001 layout. We must not create the sub screen manually. In the layout there is an option of creating a sub screen.
Step – 3: Next we have to create the screen 9001. PROCESS BEFORE OUTPUT. "PBO for PF status & Title bar MODULE status_9001. "Calling the subscreen which is selection screen CALL SUBSCREEN sel INCLUDING sy-repid '100'. PROCESS AFTER INPUT. "Subscreen needs to be called at PAI level CALL SUBSCREEN sel. "PAI for different button functionality MODULE user_command_9001.
Step – 4: Then we need to create the PBO & PAI modules. MODULE status_9001 OUTPUT. SET PF-STATUS 'PF_SEL'.
Rohini kumar
sap abap consultant
SET TITLEBAR 'TI_SEL'. ENDMODULE.
" status_9001
OUTPUT
MODULE user_command_9001 INPUT. CASE ok_code1. "User command for 9001 WHEN 'DISP'. PERFORM get_airline. WHEN 'CLR'. REFRESH s_carrid. WHEN 'BACK' OR 'EXIT' OR 'CANCEL'. LEAVE PROGRAM. ENDCASE. ENDMODULE.
" user_command_9001
INPUT
Step – 5: After this we have to create the subroutine called in PAI. *&---------------------------------------------------------------------* *& Form get_airline *&---------------------------------------------------------------------* * Get data of Airline *----------------------------------------------------------------------* FORM get_airline . IF s_carrid[] IS NOT INITIAL. "It will refresh previous data when program moves with BACK REFRESH: lt_scarr. SELECT carrid carrname currcode FROM scarr INTO TABLE lt_scarr WHERE carrid IN s_carrid. IF sy-subrc = 0. REFRESH it_scarr. "Preparing the output table for screen fields LOOP AT lt_scarr INTO lw_scarr. wa_scarr-carrid = lw_scarr-carrid. wa_scarr-carrname = lw_scarr-carrname. wa_scarr-currcode = lw_scarr-currcode. APPEND wa_scarr TO it_scarr. CLEAR: wa_scarr, lw_scarr. ENDLOOP. IF it_scarr IS NOT INITIAL. SORT it_scarr. "Storing the data to a temp table for reusing it_temp_scarr = it_scarr. "Refreshing the table control to display new records REFRESH CONTROL 'TAB_CTRL_SCARR' FROM SCREEN 9002.
Rohini kumar
sap abap consultant
"Calling the next screen CALL SCREEN 9002. ENDIF. ELSE. MESSAGE 'Airline code doesn''t exist' TYPE 'S'. LEAVE LIST-PROCESSING. ENDIF. ELSE. MESSAGE 'Enter a valid Airline Code' TYPE 'I'. ENDIF. ENDFORM.
" get_airline
Step – 6: After that we have to create the next screen 9002. PROCESS BEFORE OUTPUT. "PBO for PF status & Title bar MODULE status_9002. "Populating the output table data in table control LOOP AT it_scarr INTO wa_scarr WITH CONTROL tab_ctrl_scarr. MODULE table_control_scarr. ENDLOOP. PROCESS AFTER INPUT. "Reseting the output table after it gets modified MODULE reset_scarr. "Modifying the output table with current line operation LOOP AT it_scarr. MODULE modify_scarr. ENDLOOP. "PAI for different button functionality MODULE user_command_9002.
Here we have prepared the table control’s MARK field with work area WA_SCARR-MARK.
Rohini kumar
sap abap consultant
Step – 7: Now we have to create the PBO and PAI modules. MODULE status_9002 OUTPUT. SET PF-STATUS 'PF_SEL'. SET TITLEBAR 'TI_AIR'. "The internal table needs to be reset with its previous data "The table has been modified with MARK "So it needs to be reset to use it again it_scarr = it_temp_scarr. ENDMODULE.
" status_9002
OUTPUT
Now we are putting the data from work area to screen fields of table control. MODULE table_control_scarr OUTPUT. CLEAR scarr. scarr-carrid
Now we need to reset the output internal table to its previous value. This is because the table will be reused for different sort of commands. MODULE reset_scarr INPUT. "Resetting the internal table to its previous data "It is needed to remove the modification with MARKs it_scarr = it_temp_scarr. ENDMODULE.
" reset_scarr
INPUT
Now we have to modify the internal table with its MARK fields for different sort of commands. MODULE modify_scarr INPUT. "When one row is selected the internal table is modified "with the MARK fields x coming from wa_scarr-mark IF wa_scarr-mark = 'X'. MODIFY it_scarr INDEX tab_ctrl_scarr-current_line FROM wa_scarr. ENDIF. "When the table control is scralled READ TABLE it_scarr INTO wa_scarr INDEX tab_ctrl_scarr-current_line. IF sy-subrc = 0. MODIFY it_scarr INDEX tab_ctrl_scarr-current_line FROM wa_scarr. ENDIF. ENDMODULE.
" modify_scarr
INPUT
Now we need to create the user commands for different buttons functionality. MODULE user_command_9002 INPUT. CASE ok_code2. "User command of screen 9002 WHEN 'FLNM'. PERFORM capture_carrid. PERFORM get_flight_number. WHEN 'BACK' OR 'EXIT' OR 'CANCEL'. "Previous user command needs to be cleared for further operation CLEAR ok_code1. LEAVE LIST-PROCESSING. LEAVE TO SCREEN 9001. ENDCASE. ENDMODULE.
" user_command_9002
INPUT
Rohini kumar
sap abap consultant
Step – 8: After this we have to create all the subroutines defined at user command. Here we need to capture the selected row data into a variable. *&---------------------------------------------------------------------* *& Form capture_carrid *&---------------------------------------------------------------------* * Capturing the selected rows into a table *----------------------------------------------------------------------* FORM capture_carrid . IF it_scarr IS NOT INITIAL. LOOP AT it_scarr INTO wa_scarr WHERE mark = 'X'. v_carrid = wa_scarr-carrid. CLEAR: wa_scarr. ENDLOOP. ENDIF. ENDFORM.
" capture_carrid
Now we have to get data from flight table based on the selected row data. *&---------------------------------------------------------------------* *& Form get_flight_number *&---------------------------------------------------------------------* * Get data from Flight schedule table *----------------------------------------------------------------------* FORM get_flight_number . IF v_carrid IS NOT INITIAL. "It will refresh previous data when program moves with BACK REFRESH: lt_spfli, it_spfli. SELECT carrid connid countryfr cityfrom airpfrom cityto airpto FROM spfli INTO TABLE lt_spfli WHERE carrid = v_carrid. "Preparing the output table for screen fields IF sy-subrc = 0. CLEAR v_carrid. "Clearing v_carrid for UAT - BUGs LOOP AT lt_spfli INTO lw_spfli. wa_spfli-carrid = lw_spfli-carrid. wa_spfli-connid = lw_spfli-connid. wa_spfli-countryfr = lw_spfli-countryfr. wa_spfli-cityfrom = lw_spfli-cityfrom. wa_spfli-airpfrom = lw_spfli-airpfrom. wa_spfli-cityto = lw_spfli-cityto. wa_spfli-airpto = lw_spfli-airpto. APPEND wa_spfli TO it_spfli. CLEAR: wa_spfli, lw_spfli. ENDLOOP. IF it_spfli IS NOT INITIAL.
Rohini kumar
sap abap consultant
SORT it_spfli. "Refreshing the table control to display new records REFRESH CONTROL 'TAB_CTRL_SPFLI' FROM SCREEN 9003. "Calling the next screen CALL SCREEN 9003. ENDIF. ELSE. CLEAR ok_code2. "Clearing OK_CODE2 for UAT - BUGs MESSAGE 'Flight doesn''t exist' TYPE 'I'. ENDIF. ELSE. CLEAR ok_code2. "Clearing OK_CODE2 for UAT - BUGs MESSAGE 'Select an Airline' TYPE 'I'. ENDIF. ENDFORM.
" get_flight_number
Step – 9: After this we need to create the screen 9003 (last screen). PROCESS BEFORE OUTPUT. "PBO for PF status & Title bar MODULE status_9003. "Populating the output table data in table control LOOP AT it_spfli INTO wa_spfli WITH CONTROL tab_ctrl_spfli. MODULE table_control_spfli. ENDLOOP. PROCESS AFTER INPUT. "Modifying the output table with current line operation LOOP AT it_spfli. MODULE modify_spfli. ENDLOOP. "PAI for different button functionality MODULE user_command_9003.
Rohini kumar
sap abap consultant
Step – 10: Then similarly we have to create all these modules. MODULE status_9003 OUTPUT. SET PF-STATUS 'PF_SEL'. SET TITLEBAR 'TI_SPFLI'. ENDMODULE.
" status_9003
OUTPUT
Here we are populating the screen fields of table control for flight number details. MODULE table_control_spfli OUTPUT. CLEAR spfli. spfli-carrid spfli-connid spfli-countryfr spfli-cityfrom spfli-airpfrom spfli-cityto spfli-airpto
Now we are again modifying the output table. Since this is the last screen of table control, there is no option to get marked row. MODULE modify_spfli INPUT. "When the table control is scralled
Rohini kumar
sap abap consultant
READ TABLE it_spfli INTO wa_spfli INDEX tab_ctrl_spfli-current_line. IF sy-subrc = 0. MODIFY it_spfli INDEX tab_ctrl_spfli-current_line FROM wa_spfli. ENDIF. ENDMODULE.
" modify_spfli
INPUT
Now we are preparing the buttons at user command for screen 9003. MODULE user_command_9003 INPUT. CASE ok_code3. WHEN 'BACK' OR 'EXIT' OR 'CANCEL'. "Previous user command needs to be cleared for further operation CLEAR: ok_code1, ok_code2. LEAVE LIST-PROCESSING. LEAVE TO SCREEN 9002. ENDCASE. ENDMODULE.
" user_command_9003
INPUT
The output screen 9001 is as follows:
Click on the Display after setting the selection range and the output is as follows:
Rohini kumar
We can scroll the table control and the output will be like follows:
sap abap consultant
Rohini kumar
sap abap consultant
Next we select a particular row like follows:
After that we want to see the Flight number details by clicking the Flight number button.
Simple Tabstrip Control
Rohini kumar
sap abap consultant
Tab strip control is a screen element which contains more than one tab. Each of these tabs is a sub screen. The tab strip control contains one main screen and n number of sub screen. This n number denotes the number of tabs. Tab strip is like follows.
When we click other tab then we can find different screen fields.
Rohini kumar
sap abap consultant
Each tab contains a tab title and page area. In the page area we can store the screen elements. This page area is actually the sub screen. Hence a tab strip is a combination of the main screen and all those sub screens. If there are many tabs and all of those tabs contain big tab titles then in one tab strip control area is not sufficient to display all those tabs. In this condition a scroll bar appears and then we can scroll to all of those tabs. Apart from that there will be a button which will display the list of all tabs. The tab title acts as a button in tab strip control. We can set the function code of any tab as we do for push button. We also can set a title and icon for that tab. We generally create all of these by the screen painter toolbar. We can customize the screen area of the main screen, tab strip screen and also the sub screens. We also can modify the position of these as per requirement. Now we have created here a tab strip which contains three tabs. The main screen contains the tab strip and material number input field. It also contains two push buttons ‘DISPALY’ and ‘CLEAR’. The functionality of this tab strip is to display the material details in tab1, material unit in tab2 and material sales data in tab3 based on the material input at the main screen when user clicks on the display button. The program will clear all of the screen fields if the clear button is clicked. Since we have only one main screen we only need one ok_code to capture the user command. Step – 1: At first we are creating the include programs as follows.
Rohini kumar
INCLUDE INCLUDE INCLUDE INCLUDE
mz_test_top mz_test_o01 mz_test_i01 mz_test_f01
sap abap consultant
. . . .
" " " "
global Data PBO-Modules PAI-Modules FORM-Routines
Now at top include we need to declare all the possible set of variables as follows. *-------Declaring DB tables to mention the screen fields---------------* TABLES: mara, makt, marm, mvke. *-----Declaring an work area for Material details----------------------* DATA: BEGIN OF wa_mara, matnr TYPE mara-matnr, ersda TYPE mara-ersda, ernam TYPE mara-ernam, mtart TYPE mara-mtart, END OF wa_mara. *-----Declaring an work area for Material description------------------* DATA: BEGIN OF wa_makt, matnr TYPE makt-matnr, maktx TYPE makt-maktx, END OF wa_makt. *-----Declaring an work area for Material units------------------------* DATA: BEGIN OF wa_marm, matnr TYPE marm-matnr, meinh TYPE marm-meinh, volum TYPE marm-volum, voleh TYPE marm-voleh, END OF wa_marm. *-----Declaring an work area for Material sales data-------------------* DATA: BEGIN OF wa_mvke, matnr TYPE mvke-matnr, vkorg TYPE mvke-vkorg, vtweg TYPE mvke-vtweg, END OF wa_mvke. *-----Declaring ok_code to capture user command------------------------* DATA: ok_code TYPE sy-ucomm. *--------Declaring the Tabstrip----------------------------------------* CONTROLS ts_mat TYPE TABSTRIP.
Here to declare the work area we have used the Pattern as follows.
Rohini kumar
sap abap consultant
Then after declaring the table name on the mentioned place press enter. Now select the required fields and press Copy button.
Enter the name of the work area which you want to declare and press enter.
Rohini kumar
sap abap consultant
In this way we can make any big structure with less amount of time. Step – 2: Next we are creating the screen 9001 which is the main screen. Here in the attributes the normal screen will be selected.
This screen will contain the OK_CODE for the user command.
Rohini kumar
sap abap consultant
Step – 3: After that we shall declare the PBO and PAI for this screen 9001.
In PBO we have the following. MODULE status_9001 OUTPUT. SET PF-STATUS 'PF_MAT'. SET TITLEBAR 'TI_MAT'. ENDMODULE.
The PF status is as follows.
" status_9001
OUTPUT
Rohini kumar
sap abap consultant
The title bar is as follows.
Step – 4: Now we shall go to the layout and this will trigger the screen painter by which we shall make the main screen of tab strip. Here we have a tab strip ts_mat which we have created by the tab strip button in the palate. Initially the system shows two tabs tab1 and tab2.
Rohini kumar
sap abap consultant
Double click on the tab strip ts_mat and by editing the tab title value we can increase the tab as per requirement.
Rohini kumar
sap abap consultant
Now we double click on a particular tab (tab1) and the following attributes will come. Here we have set the function code TAB1, the title text Material details and reference field SUB1. This SUB1 will be the name of the sub screen which needs to be created by specifying different screen number. In this way we are creating the other tabs also.
Now we just create a sub screen by dragging the mouse pointer after selecting the sub screen from palate. The name of this will be SUB1.
Rohini kumar
sap abap consultant
After that we are creating two buttons for display and clear the screen fields.
After customizing and adjusting the screen elements the final screen appears as follows.
Rohini kumar
sap abap consultant
Step – 5: Now we are creating the first sub screen SUB1 for material details. Here the screen type will be sub screen.
Rohini kumar
sap abap consultant
Since this is a sub screen no OK_CODE is there. It will take the main screen user command.
Step – 6: After that we need to create the screen elements for this sub screen as follows. In this way we are creating the rest of sub screens.
Rohini kumar
sap abap consultant
Similarly we are creating rest of the sub screens with different screen elements. Step – 7: Now we need to call these sub screens including program name and screen number at PBO level and PAI level. PROCESS BEFORE OUTPUT. *--------Creating the PF status & Title in PBO-------------------------* MODULE status_9001. *------Calling the sub screens with program name and screen number-----* CALL SUBSCREEN sub1 INCLUDING sy-repid '9002'. CALL SUBSCREEN sub2 INCLUDING sy-repid '9003'. CALL SUBSCREEN sub3 INCLUDING sy-repid '9004'. PROCESS AFTER INPUT. *--------Declaring the buttons functionality in PAI--------------------* MODULE user_command_9001. *------Calling the sub screens-----------------------------------------* CALL SUBSCREEN sub1. CALL SUBSCREEN sub2. CALL SUBSCREEN sub3.
Step – 8: Now in PAI we have to define the button functionality at user command.
Rohini kumar
sap abap consultant
MODULE user_command_9001 INPUT. *------Capturing the function code for different button----------------* CASE ok_code. WHEN 'TAB1'. "When user clicks on TAB1 button PERFORM tab1. WHEN 'TAB2'. "When user clicks on TAB1 button PERFORM tab2. WHEN 'TAB3'. "When user clicks on TAB1 button PERFORM tab3. WHEN 'DISP'. "When user clicks on Display button PERFORM get_material. PERFORM get_material_description. PERFORM get_material_unit. PERFORM get_material_sales_data. WHEN 'CLR'. "When user clicks on Clear button PERFORM clear_screen. WHEN 'BACK' OR 'EXIT' OR 'CANCEL'. "When user clicks on Standard toolbar buttons LEAVE PROGRAM. ENDCASE. ENDMODULE.
" user_command_9001
INPUT
Here the tab titles are treated as buttons. Step – 9: Now we need to define all of those tab related subroutines to activate the tabs. Here the purpose is to active the tab with tab title as follows.
Rohini kumar
sap abap consultant
Step – 10: Now we shall declare the subroutine for display button function as follows. *&---------------------------------------------------------------------* *& Form get_material *&---------------------------------------------------------------------* * Get data from MARA *----------------------------------------------------------------------* FORM get_material . IF mara-matnr IS NOT INITIAL. SELECT SINGLE matnr ersda ernam mtart FROM mara INTO wa_mara WHERE matnr = mara-matnr. IF sy-subrc = 0. "Passing the data from work area to screen fields of TAB1 mara-matnr = wa_mara-matnr. mara-ersda = wa_mara-ersda. mara-ernam = wa_mara-ernam. mara-mtart = wa_mara-mtart. CLEAR wa_mara. ENDIF. ENDIF.
Rohini kumar
sap abap consultant
ENDFORM. " get_material *&---------------------------------------------------------------------* *& Form get_material_description *&---------------------------------------------------------------------* * Get data from MAKT *----------------------------------------------------------------------* FORM get_material_description . IF mara-matnr IS NOT INITIAL. SELECT SINGLE matnr maktx FROM makt INTO wa_makt WHERE matnr = mara-matnr AND spras = 'EN'. IF sy-subrc = 0. "Passing the data from work area to screen fields of TAB1 makt-maktx = wa_makt-maktx. CLEAR wa_makt. ENDIF. ENDIF. ENDFORM. " get_material_description *&---------------------------------------------------------------------* *& Form get_material_unit *&---------------------------------------------------------------------* * Get data from MARM *----------------------------------------------------------------------* FORM get_material_unit . IF mara-matnr IS NOT INITIAL. SELECT SINGLE matnr meinh volum voleh FROM marm INTO wa_marm WHERE matnr = mara-matnr. IF sy-subrc = 0. "Passing the data from work area to screen fields of TAB2 marm-matnr = wa_marm-matnr. marm-meinh = wa_marm-meinh. marm-volum = wa_marm-volum. marm-voleh = wa_marm-voleh. CLEAR wa_marm. ENDIF. ENDIF. ENDFORM. " get_material_unit *&---------------------------------------------------------------------* *& Form get_material_sales_data *&---------------------------------------------------------------------* * Get data from MVKE *----------------------------------------------------------------------* FORM get_material_sales_data . IF mara-matnr IS NOT INITIAL. SELECT SINGLE matnr vkorg vtweg FROM mvke INTO wa_mvke WHERE matnr = mara-matnr. IF sy-subrc = 0. "Passing the data from work area to screen fields of TAB3
Step – 11: Now we shall create the clear function. Here the purpose will be to clear the screen fields whenever user clicks on the clear button. *&---------------------------------------------------------------------* *& Form clear_screen *&---------------------------------------------------------------------* * Refresh the screen fields data *----------------------------------------------------------------------* FORM clear_screen . "Clearing all the screen fields & work area CLEAR: mara, makt, marm, mvke, wa_mara, wa_makt, wa_marm, wa_mvke. ENDFORM.
" clear_screen
Now here is the main screen output:
Rohini kumar
sap abap consultant
Now we declare a material number here and click on the Display button. The system is displaying the material details with description on the first tab.
Now we click on the Material unit and the second tab will display required data with its screen fields. Here the tab title is working like a button.
Rohini kumar
sap abap consultant
Similarly when we click the Material Sales data we get the following output.
Rohini kumar
sap abap consultant
ALV Grid in Module Pool We can display ALV Grid by using Module Pool. Here for the Selection Screen we have to make a custom screen (Like 9001). The Grid display will be on another screen (Like 9002). The Selection Screen contains a sub screen (100) for the Select Option. On the selection screen we have created two button DISPLAY (FCT code = DISP) and CLEAR (FCT code = CLR). The sub screen 100 will contain the Parameter of Company code and Select option of Project Definition. Hence for the sub screen we call sub screen including program name and screen number (100) in PBO of screen 9001. Now in PAI we call the sub screen again to pass the value entered in the screen to the program. To display two ALV Grids we have created two Custom Containers which are referenced to CL_GUI_CUSTOM_CONTAINER class in PBO of screen 9002. We also have created two Grids, referenced to CL_GUI_ALV_GRID. After creating these objects we have to call method SET_TABLE_FOR_FIRST_DISPLAY by Grid object in PBO of screen 9002. In this method we have passed the Layout, Field catalog and the Internal Table. Here the Grid display is displayed on the screen 9002.
Rohini kumar
sap abap consultant
After displaying the Grid in screen 9002 if we click on BACK / EXIT / CANCEL button then the program will come to screen 9001 which is Selection Screen. Hence in the PAI of screen 9002, we have to call method REFRESH_TABLE_DISPLAY to refresh the ALV Grid and FREE to refresh the Custom Container. Here we also have cleared the screen fields and internal tables used. Following is the code of the program: Screen - 9001: PROCESS BEFORE OUTPUT. MODULE status_9001. "Calling the Sub Screen by using Program name & Screen CALL SUBSCREEN sub_sel INCLUDING sy-repid '100'. PROCESS AFTER INPUT. "Calling the Sub Screen to pass the value "from Sub Screen to Program CALL SUBSCREEN sub_sel. MODULE user_command_9001. Screen - 9002: PROCESS BEFORE OUTPUT. MODULE status_9002. PROCESS AFTER INPUT. MODULE user_command_9002. Include for PBO: *&--------------------------------------------------------------------* *& Include ZSR_O01 *&--------------------------------------------------------------------* *&--------------------------------------------------------------------* *& Module STATUS_9001 OUTPUT *&--------------------------------------------------------------------*
Rohini kumar
sap abap consultant
* text *---------------------------------------------------------------------* MODULE status_9001 OUTPUT. SET PF-STATUS 'GUI_9001'. SET TITLEBAR 'TITLE_9001'. ENDMODULE. " STATUS_9001 OUTPUT *&--------------------------------------------------------------------* *& Module STATUS_9002 OUTPUT *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* MODULE status_9002 OUTPUT. SET PF-STATUS 'GUI_9002'. SET TITLEBAR 'TITLE_9002'. PERFORM create_object. PERFORM grid_method. ENDMODULE.
" STATUS_9002
OUTPUT
Include for PAI: *&--------------------------------------------------------------------* *& Include ZSR_I01 *&--------------------------------------------------------------------* *&--------------------------------------------------------------------* *& Module USER_COMMAND_9001 INPUT *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* MODULE user_command_9001 INPUT. IF ok_code_sel IS NOT INITIAL. CASE ok_code_sel. WHEN 'BACK'. LEAVE PROGRAM. WHEN 'EXIT'.
Rohini kumar
sap abap consultant
LEAVE PROGRAM. WHEN 'CANCEL'. LEAVE PROGRAM. WHEN 'DISP'. PERFORM display_project_wbs. WHEN 'CLR'. PERFORM clear_screen. ENDCASE. ENDIF. ENDMODULE. " USER_COMMAND_9001 INPUT *&--------------------------------------------------------------------* *& Module USER_COMMAND_9002 INPUT *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* MODULE user_command_9002 INPUT. IF ok_code_pro IS NOT INITIAL. CASE ok_code_pro. WHEN 'BACK'. PERFORM leave_alv. WHEN 'EXIT'. PERFORM leave_alv. WHEN 'CANCEL'. PERFORM leave_alv. ENDCASE. ENDIF. ENDMODULE.
" USER_COMMAND_9002
INPUT
Include for Subroutine: *&--------------------------------------------------------------------* *& Include ZSR_F01 *&--------------------------------------------------------------------* *&--------------------------------------------------------------------* *& Form CLEAR_SCREEN *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------*
Rohini kumar
sap abap consultant
* --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM clear_screen . CLEAR: s_pspid, p_vbukr. REFRESH s_pspid[]. ENDFORM. " CLEAR_SCREEN *&--------------------------------------------------------------------* *& Form DISPLAY_PROJECT_WBS *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM display_project_wbs . TYPES: BEGIN OF ty_psphi, psphi TYPE prps-psphi, END OF ty_psphi. DATA: "Work Area & Table to get converted PSPHI "Project Definition Internal Version lw_psphi TYPE ty_psphi, lt_psphi TYPE STANDARD TABLE OF ty_psphi. SELECT pspnr pspid objnr ernam erdat vbukr vgsbr vkokr FROM proj INTO TABLE it_proj WHERE pspid IN s_pspid AND vbukr = p_vbukr. IF sy-subrc = 0. SORT it_proj BY pspid. REFRESH lt_psphi.
*
LOOP AT it_proj INTO wa_proj. CALL FUNCTION 'CONVERSION_EXIT_KONPD_INPUT' EXPORTING input = wa_proj-pspid IMPORTING output = lw_psphi PROJWA =
Rohini kumar
EXCEPTIONS not_found OTHERS
sap abap consultant
= 1 = 2.
IF lw_psphi IS NOT INITIAL. "Making the table of Project definition Internal Version APPEND lw_psphi TO lt_psphi. ENDIF. CLEAR: wa_proj, lw_psphi. ENDLOOP. ELSE. MESSAGE 'Project doesn''t exist' TYPE 'I'. ENDIF. IF lt_psphi IS NOT INITIAL. SELECT pspnr posid psphi poski pbukr pgsbr pkokr FROM prps INTO TABLE it_prps FOR ALL ENTRIES IN lt_psphi WHERE psphi = lt_psphi-psphi. IF sy-subrc = 0. SORT it_prps BY posid. CALL SCREEN 9002. ELSE. MESSAGE 'WBS Elements don''t exist' TYPE 'I'. ENDIF. ENDIF. ENDFORM. " DISPLAY_PROJECT_WBS *&--------------------------------------------------------------------* *& Form FCAT_PROJ *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM fcat_proj . CLEAR wa_fcat_proj. REFRESH it_fcat_proj. DATA lv_col TYPE i.
Rohini kumar
sap abap consultant
lv_col wa_fcat_proj-col_pos wa_fcat_proj-fieldname wa_fcat_proj-tabname wa_fcat_proj-reptext APPEND wa_fcat_proj TO CLEAR wa_fcat_proj.
ENDFORM. " FCAT_PROJ *&--------------------------------------------------------------------* *& Form FCAT_PRPS *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM fcat_prps . CLEAR wa_fcat_prps. REFRESH it_fcat_prps. DATA lv_col TYPE i VALUE 0. lv_col wa_fcat_prps-col_pos wa_fcat_prps-fieldname wa_fcat_prps-tabname wa_fcat_prps-reptext APPEND wa_fcat_prps TO CLEAR wa_fcat_prps.
ENDFORM. " FCAT_PRPS *&--------------------------------------------------------------------* *& Form LEAVE_ALV *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text
Rohini kumar
sap abap consultant
* <-- p2 text *---------------------------------------------------------------------* FORM leave_alv . "Calling method to refresh the ALV Grid with Container "Refresh_table_display refreshes the ALV Grid "FREE clears the Custom Container to generate new records CALL METHOD: obj_grid_proj->refresh_table_display, "FREE another method obj_grid_prps->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 * <-- p2 text *---------------------------------------------------------------------* FORM create_object . CLEAR: obj_cust_proj, obj_cust_prps, obj_grid_proj, obj_grid_prps. "Creating the Container object for Header PROJ CREATE OBJECT obj_cust_proj EXPORTING container_name = 'CONT_PROJ'. "Creating the Container object for Item PRPS CREATE OBJECT obj_cust_prps EXPORTING container_name = 'CONT_PRPS'. "Creating the ALV Grid Object for Header PROJ
Rohini kumar
sap abap consultant
CREATE OBJECT obj_grid_proj EXPORTING i_parent = obj_cust_proj. "Creating the ALV Grid Object for Item PRPS CREATE OBJECT obj_grid_prps EXPORTING i_parent = obj_cust_prps. ENDFORM. " CREATE_OBJECT *&--------------------------------------------------------------------* *& Form GRID_METHOD *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM grid_method . PERFORM fcat_proj. "Field Catalog for PROJ PERFORM alv_layout. "ALV Layout
ENDFORM. " GRID_METHOD *&--------------------------------------------------------------------* *& Form ALV_LAYOUT *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM alv_layout .
Top Include: *&--------------------------------------------------------------------* *& Include ZSR_TOP Module Pool ZSR_MOD *& *&--------------------------------------------------------------------* PROGRAM
zsr_mod.
TABLES: proj, prps. TYPES: BEGIN OF ty_proj, pspnr TYPE proj-pspnr, pspid TYPE proj-pspid, objnr TYPE proj-objnr, ernam TYPE proj-ernam, erdat TYPE proj-erdat, vbukr TYPE proj-vbukr, vgsbr TYPE proj-vgsbr, vkokr TYPE proj-vkokr, END OF ty_proj, BEGIN OF ty_prps, pspnr TYPE prps-pspnr, posid TYPE prps-posid, psphi TYPE prps-psphi, poski TYPE prps-poski, pbukr TYPE prps-pbukr, pgsbr TYPE prps-pgsbr, pkokr TYPE prps-pkokr, END OF ty_prps. DATA: ok_code_sel TYPE sy-ucomm, ok_code_pro TYPE sy-ucomm. DATA: wa_proj wa_prps it_proj it_prps
TYPE TYPE TYPE TYPE
ty_proj, ty_prps, STANDARD TABLE OF ty_proj, STANDARD TABLE OF ty_prps.
BEGIN OF SCREEN 100 AS SUBSCREEN. p_vbukr TYPE proj-vbukr. s_pspid FOR proj-pspid. END OF SCREEN 100.
The output is as follows: Screen - 9001:
lvc_s_layo, lvc_s_fcat, lvc_s_fcat, STANDARD TABLE OF lvc_s_fcat, STANDARD TABLE OF lvc_s_fcat.
Rohini kumar
sap abap consultant
Table Controls in Tab Strip Tab strip is a screen container where we can have multiple screen fields from different tables or same table. Tab contains two parts – tab title and page area. Here the tab title works like a push button. In the page area of tab we need to design a sub screen. Each tab is a container of each sub screen where we can show different fields. We can have table control also in that sub screen. We have to looping the internal table with the table control to create a table control in a tab strip. In the following example we have created a main screen where an input field of Airline code is there. Based on that input the program will fetch data from Airline, Flight schedule and Flight database table. After that the program will display the details inside the three tabs. Here at the first tab we are declaring the details of Airline. This is a normal display because we are selecting only one input of airline code. Now according to the function one airline can contain one or more flight schedule and flight details. So we are displaying those multiple data by using a table control created in different tabs. Step – 1: At first we need to create the different includes of the module pool. INCLUDE INCLUDE INCLUDE INCLUDE
mz_test_top mz_test_o01 mz_test_i01 mz_test_f01
. . . .
" " " "
global Data PBO-Modules PAI-Modules FORM-Routines
Rohini kumar
sap abap consultant
Step – 2: Next we are declaring the variables, structures and tables at the top include. *-------Declaring tables for screen fields-----------------------------* TABLES: scarr, spfli, sflight. TYPES: *------Airline internal structure--------------------------------------* BEGIN OF ty_scarr, carrid TYPE scarr-carrid, carrname TYPE scarr-carrname, currcode TYPE scarr-currcode, END OF ty_scarr, *------Flight schedule internal structure------------------------------* BEGIN OF ty_spfli, carrid TYPE spfli-carrid, connid TYPE spfli-connid, cityfrom TYPE spfli-cityfrom, airpfrom TYPE spfli-airpfrom, cityto TYPE spfli-cityto, airpto TYPE spfli-airpto, deptime TYPE spfli-deptime, arrtime TYPE spfli-arrtime, distance TYPE spfli-distance, END OF ty_spfli, *------Flight internal structure---------------------------------------* BEGIN OF ty_sflight, carrid TYPE sflight-carrid, connid TYPE sflight-connid, fldate TYPE sflight-fldate, price TYPE sflight-price, currency TYPE sflight-currency, seatsmax TYPE sflight-seatsmax, seatsocc TYPE sflight-seatsocc, END OF ty_sflight. *-----Work area & internal table declaration---------------------------* DATA: wa_scarr TYPE ty_scarr, wa_spfli TYPE ty_spfli, it_spfli TYPE TABLE OF ty_spfli, wa_sflight TYPE ty_sflight, it_sflight TYPE TABLE OF ty_sflight. DATA: ok_code TYPE sy-ucomm, "User command capturing variable v_carrid TYPE scarr-carrid. "Screen field variable CONTROLS: *---------Declaring the tab strip--------------------------------------* ts_air TYPE TABSTRIP, *---------Declaring the table controls---------------------------------* tc_spfli TYPE TABLEVIEW USING SCREEN 9003, tc_sflight TYPE TABLEVIEW USING SCREEN 9004.
Step – 3:
Rohini kumar
sap abap consultant
Now we are creating the main screen 9001. We shall have one single main screen which contains the tab strip. Several other tabs will contain several other sub screens. PROCESS BEFORE OUTPUT. *--------Calling the module for GUI status of PBO----------------------* MODULE status_9001. *------Calling the sub screens which contains the table control--------* CALL SUBSCREEN sub1 INCLUDING sy-repid '9002'. CALL SUBSCREEN sub2 INCLUDING sy-repid '9003'. CALL SUBSCREEN sub3 INCLUDING sy-repid '9004'. PROCESS AFTER INPUT. *------Calling the sub screens which contains the table control--------* CALL SUBSCREEN sub1. CALL SUBSCREEN sub2. CALL SUBSCREEN sub3. *--------Calling the module to capture the sy-ucomm--------------------* MODULE user_command_9001.
Here we have call other sub screens which belong to different tabs. The sub screens are 9002, 9003 & 9004. Step – 4: Now we are creating the GUI status for the main screen. MODULE status_9001 OUTPUT. SET PF-STATUS 'PF_MAIN_9001'. "GUI status
Rohini kumar
sap abap consultant
SET TITLEBAR 'TI_MAIN_9001'. ENDMODULE.
"GUI title
" status_9001
OUTPUT
Step – 5: After that we have to create the user command functionality (push button) in PAI. MODULE user_command_9001 INPUT. CASE ok_code. WHEN 'TAB1'. "TAB1 ts_air-activetab WHEN 'TAB2'. "TAB2 ts_air-activetab WHEN 'TAB3'. "TAB3 ts_air-activetab
tab title works like a push button = 'TAB1'. tab title works like a push button = 'TAB2'. tab title works like a push button = 'TAB3'.
WHEN 'DISP'. "Display push button PERFORM get_airline. PERFORM get_flight_schedule. PERFORM get_flight. WHEN 'CLR'. "Clear push button PERFORM clear_program. WHEN 'BACK' OR 'EXIT' OR 'CANCEL'. "GUI buttons LEAVE PROGRAM. ENDCASE. ENDMODULE.
" user_command_9001
INPUT
Since tab titles work as push buttons we have activated the tab strip with the tab name. Now for display command we have created three sub routines to fetch data from database tables. Step – 6: Now we are fetching data from database tables by using the sub routines. *&---------------------------------------------------------------------* *& Form get_airline *&---------------------------------------------------------------------* * Get data from Airline *----------------------------------------------------------------------* FORM get_airline . IF scarr-carrid IS NOT INITIAL. SELECT SINGLE carrid carrname currcode FROM scarr INTO wa_scarr WHERE carrid = scarr-carrid. IF sy-subrc = 0. "To avoid the selection screen field and display field "we are using different name for carrid v_carrid = wa_scarr-carrid. scarr-carrname = wa_scarr-carrname. scarr-currcode = wa_scarr-currcode. CLEAR wa_scarr.
Rohini kumar
sap abap consultant
ENDIF. ELSE. MESSAGE 'Please enter a valid Airline code' TYPE 'I'. ENDIF. ENDFORM. " get_airline *&---------------------------------------------------------------------* *& Form get_flight_schedule *&---------------------------------------------------------------------* * Get data from Flight schedule table *----------------------------------------------------------------------* FORM get_flight_schedule . IF scarr-carrid IS NOT INITIAL. SELECT carrid connid cityfrom airpfrom cityto airpto deptime arrtime distance FROM spfli INTO TABLE it_spfli WHERE carrid = scarr-carrid. ENDIF. ENDFORM. " get_flight_schedule *&---------------------------------------------------------------------* *& Form get_flight *&---------------------------------------------------------------------* * Get data from Flight table *----------------------------------------------------------------------* FORM get_flight . IF scarr-carrid IS NOT INITIAL. SELECT carrid connid fldate price currency seatsmax seatsocc FROM sflight INTO TABLE it_sflight WHERE carrid = scarr-carrid. ENDIF. ENDFORM.
" get_flight
Since we are selecting one single airline code to fetch details on tab 1 we have selected single record from airline table into work area. Rest of the selection has been done by using the internal tables which will be displayed on the table controls. Step – 7: After that we have created the CLEAR button functionality. *&---------------------------------------------------------------------* *& Form clear_program *&---------------------------------------------------------------------* * Clearing & refreshing all screen fields and tables *----------------------------------------------------------------------* FORM clear_program . CLEAR: scarr, spfli, sflight, v_carrid, wa_scarr, wa_spfli, wa_sflight. REFRESH: it_spfli, it_sflight. ENDFORM.
" clear_program
Rohini kumar
sap abap consultant
Step – 8: Since tab1 does not contain any user command functionality we haven’t created any PBO and PAI on that sub screen.
Here at tab2 (9003 sub screen) we are declaring the table control for flight schedule. Hence we have to loop internal table with table control at PBO and PAI. PROCESS BEFORE OUTPUT. * MODULE STATUS_9003. *------Applying the table control with the internal table--------------* LOOP AT it_spfli INTO wa_spfli WITH CONTROL tc_spfli. "Populating the table control with table data MODULE display_tc_spfli. ENDLOOP. PROCESS AFTER INPUT. *------Looping the output table for next lines at scrolling------------* LOOP AT it_spfli. "Modify the output table with current line MODULE modify_tc_spfli. ENDLOOP. * MODULE USER_COMMAND_9003.
Rohini kumar
sap abap consultant
Here we are not declaring any GUI status and button functionality since we don’t need that. Step – 9: Now we are creating the PBO module to populate the records at the table control. MODULE display_tc_spfli OUTPUT. *---------To activate the scrolling option of table control------------* PERFORM current_line_spfli. *-------Moving data from work area to screen fields--------------------* spfli-carrid = wa_spfli-carrid. spfli-connid = wa_spfli-connid. spfli-cityfrom = wa_spfli-cityfrom. spfli-airpfrom = wa_spfli-airpfrom. spfli-cityto = wa_spfli-cityto. spfli-airpto = wa_spfli-airpto. spfli-deptime = wa_spfli-deptime. spfli-arrtime = wa_spfli-arrtime. spfli-distance = wa_spfli-distance. ENDMODULE.
" display_tc_spfli
OUTPUT
Step – 10: Here we have declared a sub routine to activate the scrolling option in the table control. *&---------------------------------------------------------------------* *& Form current_line_spfli *&---------------------------------------------------------------------* * Scrolling operation in flight schedule table control *----------------------------------------------------------------------* FORM current_line_spfli . "Describing the internal table to populate the sy-dbcnt DESCRIBE TABLE it_spfli LINES sy-dbcnt. "Field current line of table control needs to be populated
Rohini kumar
sap abap consultant
"with sy-loopc - loop information in table control tc_spfli-current_line = sy-loopc. "Field lines is populated with the number of table lines "which has been processed yet tc_spfli-lines = sy-dbcnt. ENDFORM.
" current_line_spfli
Step – 11: Now we have to modify the internal table in table control at PAI. The scrolling function always works at PAI. MODULE modify_tc_spfli INPUT. READ TABLE it_spfli INTO wa_spfli INDEX tc_spfli-current_line. IF sy-subrc = 0. MODIFY it_spfli FROM wa_spfli INDEX tc_spfli-current_line. ENDIF. ENDMODULE.
" modify_tc_spfli
INPUT
Step – 12: Similarly we are creating another sub screen for another tab. The screen flow logic will be similar as follows. PROCESS BEFORE OUTPUT. * MODULE STATUS_9004. *------Applying the table control with the internal table--------------* LOOP AT it_sflight INTO wa_sflight WITH CONTROL tc_sflight. "Populating the table control with table data MODULE display_tc_sflight. ENDLOOP. PROCESS AFTER INPUT. *------Looping the output table for next lines at scrolling------------* LOOP AT it_sflight. "Modify the output table with current line MODULE modify_tc_sflight. ENDLOOP. * MODULE USER_COMMAND_9004.
Rohini kumar
sap abap consultant
Step – 13: Now the PBO module is as following. MODULE display_tc_sflight OUTPUT. *---------To activate the scrolling option of table control------------* PERFORM current_line_sflight. *-------Moving data from work area to screen fields--------------------* sflight-carrid = wa_sflight-carrid. sflight-connid = wa_sflight-connid. sflight-fldate = wa_sflight-fldate. sflight-price = wa_sflight-price. sflight-currency = wa_sflight-currency. sflight-seatsmax = wa_sflight-seatsmax. sflight-seatsocc = wa_sflight-seatsocc. ENDMODULE.
" display_tc_sflight
OUTPUT
Step – 14: Similarly we are creating the sub routine for scrolling option of table control. FORM current_line_sflight . "Describing the internal table to populate the sy-dbcnt DESCRIBE TABLE it_sflight LINES sy-dbcnt. "Field current line of table control needs to be populated "with sy-loopc - loop information in table control tc_sflight-current_line = sy-loopc. "Field lines is populated with the number of table lines "which has been processed yet tc_sflight-lines = sy-dbcnt. ENDFORM.
Step – 15:
" current_line_sflight
Rohini kumar
sap abap consultant
Similarly we are modifying the internal table with current line of table control. MODULE modify_tc_sflight INPUT. READ TABLE it_sflight INTO wa_sflight INDEX tc_sflight-current_line. IF sy-subrc = 0. MODIFY it_sflight FROM wa_sflight INDEX tc_sflight-current_line. ENDIF. ENDMODULE.
" modify_tc_sflight
INPUT
Step – 16: Finally we create a transaction code to run the module pool from the system. Now we have the output of this.
After giving the proper Airline code click the Display button.
Rohini kumar
Now click on the Flight Schedule tab.
sap abap consultant
Rohini kumar
After that click on the Flight Information tab.
Now we are scrolling down to the last row of this table.
sap abap consultant
Rohini kumar
sap abap consultant
Finally we click on the Clear button and all those screens will be cleared.
Rohini kumar
sap abap consultant
Database Table Update Delete Module pool program is an ABAP/4 program which consists a set of modules with different screens. In each and every screen a user can give input by entering data or clicking any button or check boxes. Several screens of module pool program must have a screen flow logic by which a user can interact with the program. It can be called screen programming or dialog programming also. Module pool program cannot be executed directly by pressing F8. We need to create Transaction code for a module pool program. Now we can execute the program by entering that t-code similar like SE38, SE11 etc.
1. 2.
3. 4.
A module pool program can have any number of screens. All those screens must have separate attribute, screen elements and flow logic. Since ABAP is event driven language, screens must have the events also. There are four types of events in module pool programming. PBO (Process Before Output) – When it triggers then it initializes the screen attribute, screen elements, PF status with all buttons of the screen and initial value of any fields if exists. PAI (Process After Input) – After displaying the screen when user enters any value or clicks any button on that screen then this event is triggered. After triggering this event it moves to the flow logic of the next screen. Hence preparing the next screen output. After doing this the system again triggers the PBO for next screen and then the next screen appears. POV (Process on Value request) – When user press the F4 button then it triggers this event and it displays the possible set of data records which need to be entered into that field. POH (Process on Help request) – When user press the F1 button then it triggers this event and it displays the help documentation (if exists) with a pop up screen. We design the screen by using Screen Painter (SE51) and PF status or menu bar by using Menu Painter (SE41). In module pool program we can directly go to screen painter inside the screen by clicking the LAYOUT button. We can display data records from database table into the screen by using module pool program. We also can modify data records by module pool program also. Here we have created a requirement where we have insert new data, update existing data and delete data by using module pool screen. We have a custom database table ‘zcustomers’. Now it contains fields of customer id, name and address.
Rohini kumar
sap abap consultant
Now we already have some records in this table as follows.
Now we have created this module pool program which has the first screen like this.
Rohini kumar
sap abap consultant
Here we already have entered the customer number ‘2’ and click the display button.
On the second screen we can see the customer details. The address is not updated there. We can check it from the database table also and we can confirm that the address has not been updated there. Now we shall enter an address on the address field and click on update button.
Rohini kumar
sap abap consultant
After updating the address of the customer we are generating a success message at message bar.
Now we can check the database again if the address has been updated or not.
Rohini kumar
sap abap consultant
We can also enter a new customer by using this program. We are going to the first screen and entering a new customer ID like ‘6’. There is a validation whether the ID is already existing or not. To do this we have to click the display button after entering the customer number. If it is not in database then the program will prompt an Information message like this.
Now we click on the create button and the program will move to the next screen.
Rohini kumar
sap abap consultant
The customer number which we have entered into the first screen, is carry forwarded to the second screen. And now we need to fill up the details of this customer.
After filling up the data we click on update button to update this new customer into the database. We shall again get a success message for that. Now we shall see the database table and it has been newly updated as well.
Rohini kumar
sap abap consultant
In this way if we delete any customer at the first screen level then it will be removed as well. Step – 1: Now we are coming to the coding part of this program. At the time of creation of this program we have four include programs as follows. We just need to create all of them and save those. INCLUDE INCLUDE INCLUDE INCLUDE
zsr_top zsr_o01 zsr_i01 zsr_f01
. . . .
" " " "
global Data PBO-Modules PAI-Modules FORM-Routines
Firstly we have to declare the data types, tables, variables etc in the global data declaration. PROGRAM
zsr_mod.
TABLES: zcustomers. TYPES: BEGIN OF ty_cust, id TYPE zcustomers-id, name TYPE zcustomers-name, address TYPE zcustomers-address, END OF ty_cust. DATA: wa_cust TYPE zcustomers, ok_code1 TYPE sy-ucomm, ok_code2 TYPE sy-ucomm.
Step – 2: Now we shall create the first screen 9001 and maintain the flow logic as follows. PROCESS BEFORE OUTPUT. MODULE status_9001.
Rohini kumar
sap abap consultant
PROCESS AFTER INPUT. MODULE user_command_9001.
Step – 3: Now we shall create the PBO – Module status_9001 as follows. MODULE status_9001 OUTPUT. SET PF-STATUS 'PF_CUST_INPUT'. SET TITLEBAR 'CUST_INPUT'. ENDMODULE.
" STATUS_9001
OUTPUT
Step – 4: After that we have to create the pf status and title bar by double clicking on there.
Step – 5: After that we shall create the PAI module user_command_9001 as follows: MODULE user_command_9001 INPUT. CASE ok_code1. WHEN 'DISP'. PERFORM get_customer. WHEN 'CRT'. PERFORM create_customer. WHEN 'CANC'. CLEAR: zcustomers-id. LEAVE LIST-PROCESSING. WHEN 'BACK' OR 'EXIT' OR 'CANCEL'.
Rohini kumar
sap abap consultant
LEAVE PROGRAM. ENDCASE. ENDMODULE.
" USER_COMMAND_9001
INPUT
Step – 6: Next we shall create all those subroutines as follows: FORM get_customer . IF zcustomers-id IS NOT INITIAL. SELECT SINGLE * FROM zcustomers INTO wa_cust WHERE id = zcustomers-id. IF sy-subrc = 0. CALL SCREEN 9002. ELSE. MESSAGE 'Customer doesn''t exist' TYPE 'I'. ENDIF. ELSE. MESSAGE 'Enter a Customer Number' TYPE 'I'. ENDIF. ENDFORM.
" get_customer
FORM create_customer . IF zcustomers-id IS NOT INITIAL. "Validation is needed if customer is already present SELECT SINGLE * FROM zcustomers INTO wa_cust WHERE id = zcustomers-id. IF sy-subrc = 0. "It means customer is already in database MESSAGE 'Customer already exist' TYPE 'I'. ELSE. "It means customer is not present in the database "Hence it needs to be created CLEAR: wa_cust, zcustomers-name, zcustomers-address. "Only zcustomers-id needs to be passed to screen 9002 CALL SCREEN 9002. ENDIF. ELSE. MESSAGE 'Enter a Customer Number' TYPE 'S'. ENDIF. ENDFORM.
" create_customer
Step – 7: Here we can see that the program is calling screen 9002 so we have to configure that now. On screen 9002 we have the following.
Rohini kumar
sap abap consultant
PROCESS BEFORE OUTPUT. MODULE status_9002. * PROCESS AFTER INPUT. MODULE user_command_9002.
Step – 8: Similarly we have to create the PBO for 9002 as follows. MODULE status_9002 OUTPUT. SET PF-STATUS 'PF_CUST_OUTPUT'. SET TITLEBAR 'CUST_OUTPUT'. IF ok_code1 = 'DISP'. PERFORM customer_output. ENDIF. ENDMODULE.
" status_9002
OUTPUT
Step – 9: Now after creating the pf status and title bar for screen 9002 we need to create the subroutine of customer_output. FORM customer_output . IF wa_cust IS NOT INITIAL. "Screen fields Work area fields zcustomers-id = wa_cust-id. zcustomers-name = wa_cust-name. zcustomers-address = wa_cust-address. ENDIF. ENDFORM.
" customer_output
Step – 10: Now we need to create the PAI for screen 9002 as follows. MODULE user_command_9002 INPUT. CASE ok_code2. WHEN 'UPDT'. PERFORM update_customer. WHEN 'DEL'. PERFORM delete_customer. WHEN 'BACK' OR 'EXIT' OR 'CANCEL'. LEAVE TO SCREEN 9001. LEAVE LIST-PROCESSING. ENDCASE. ENDMODULE.
" user_command_9002
INPUT
Step – 11: Next we need to create the subroutines to update and delete customer.
Rohini kumar
sap abap consultant
FORM update_customer . IF zcustomers-id IS NOT INITIAL. "Passing the values to the internal work area wa_cust-id = zcustomers-id. wa_cust-name = zcustomers-name. wa_cust-address = zcustomers-address. IF ok_code1 = 'CRT'. "Inserting new Customer into database while creating INSERT into zcustomers values wa_cust. ELSE. "Updating the database by using the work area UPDATE zcustomers FROM wa_cust. ENDIF. IF sy-subrc = 0. "If the update is successful MESSAGE 'Customer details updated successfully' TYPE 'S'. ELSE. "If the update is not successful MESSAGE 'Customer is not updated' TYPE 'E'. ENDIF. ENDIF. ENDFORM.
" update_customer
FORM delete_customer . IF zcustomers IS NOT INITIAL. "Work area fields wa_cust-id wa_cust-name wa_cust-address
"Delete data records from database table DELETE zcustomers FROM wa_cust. IF sy-subrc = 0. "If deletion is successful CLEAR: wa_cust, zcustomers. MESSAGE 'Data has been removed successfully' TYPE 'S'. LEAVE TO SCREEN 9001. ELSE. "If deletion is not successful MESSAGE 'Data has not been removed' TYPE 'E'. ENDIF. ENDIF. ENDFORM.
" delete_customer
Step – 12: Since module pool program cannot be executed directly we need to create a transaction code.
Rohini kumar
sap abap consultant
Now after doing this we have to activate the whole program and then check the t-code from the system.
Process on Value and Help Request Process on value request is an event and it is triggered when user press F4 for any particular field. It displays the possible set of values from the mentioned value table. The values will be displayed by a pop up window defined by SAP GUI. After declaring the event (process on value request) we need to declare a module followed by the specific field. That field confirms the particular field where the user put cursor and press F4. The module will contain the logic which is going to happen behind the help value pop up window. The syntax is as follows. PROCESS FIELD FIELD FIELD
Here we have declared three input fields in which we are creating the F4 value option.
Rohini kumar
sap abap consultant
Similarly we can create the help view for any particular field. We need to use the event process on help request which will be triggered when the user press F1 after having the cursor on that field. PROCESS ON HELP-REQUEST. FIELD inp1 MODULE help_customer.
In this example we are creating the help document for a field and similarly we are creating the logic under module. Now we are discussing the step by step approach for this process on value and help request. Step – 1: At first we have to create the include programs for the module pool. INCLUDE INCLUDE INCLUDE INCLUDE
mz_test_top mz_test_o01 mz_test_i01 mz_test_f01
. . . .
" " " "
global Data PBO-Modules PAI-Modules FORM-Routines
Step – 2: Now we are declaring the top include to mention every possible fields and tables. *------Structure for value table---------------------------------------* TYPES: BEGIN OF ty_kna1, kunnr TYPE kna1-kunnr, name1 TYPE kna1-name1, END OF ty_kna1. *-----Work area & internal table for help request----------------------* DATA: it_kna1 TYPE TABLE OF ty_kna1, wa_kna1_ret TYPE ddshretval, it_kna1_ret TYPE TABLE OF ddshretval. *-----Declaring OK code to capture user command------------------------* DATA: ok_code1 TYPE sy-ucomm. *-----Declaring DATA: inp1 TYPE inp2 TYPE inp3 TYPE
custom date & time field to capture from screen--------* char35, sy-datum, sy-uzeit.
Step – 3: Now we are creating a screen where we are maintaining all the events of module pool. PROCESS BEFORE OUTPUT. MODULE status_9001. PROCESS AFTER INPUT. MODULE user_command_9001. PROCESS FIELD FIELD FIELD
PROCESS ON HELP-REQUEST. FIELD inp1 MODULE help_customer.
Step – 4: After that we are preparing the layout.
Here for the date and time field we have to mention the attribute format DATS and TIMS respectively.
Rohini kumar
sap abap consultant
Step – 5: Now in PBO we are defining the GUI status as follows. MODULE status_9001 OUTPUT. SET PF-STATUS 'PF_SELECT'. SET TITLEBAR 'TI_SELECT'. ENDMODULE.
" status_9001
OUTPUT
Step – 6: After that in PAI we are creating the logic for user command. MODULE user_command_9001 INPUT. CASE ok_code1. WHEN 'CLR'. CLEAR: inp1, inp2, inp3. WHEN 'BACK'. LEAVE PROGRAM. ENDCASE. ENDMODULE.
" user_command_9001
INPUT
Step – 7: Now we have to create each and every module for process on value request. MODULE value_for_customer INPUT. "The system will show only customer no. & name after pressing F4 SELECT kunnr name1 FROM kna1 INTO TABLE it_kna1. "The table needs to be passed through this function module
Rohini kumar
* * * * * * * * * * * * * * * * *
* *
sap abap consultant
"for table value request CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST' EXPORTING DDIC_STRUCTURE = ' ' retfield = 'INP1' PVALKEY = ' ' DYNPPROG = ' ' DYNPNR = ' ' DYNPROFIELD = ' ' STEPL = 0 WINDOW_TITLE = VALUE = ' ' value_org = 'S' MULTIPLE_CHOICE = ' ' DISPLAY = ' ' CALLBACK_PROGRAM = ' ' CALLBACK_FORM = ' ' MARK_TAB = IMPORTING USER_RESET = TABLES value_tab = it_kna1 FIELD_TAB = return_tab = it_kna1_ret DYNPFLD_MAPPING = EXCEPTIONS parameter_error = 1 no_values_found = 2 OTHERS = 3 . IF sy-subrc <> 0. MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ELSE. LOOP AT it_kna1_ret INTO wa_kna1_ret. "The selected field needs to be passed to the screen field inp1 = wa_kna1_ret-fieldval. ENDLOOP. ENDIF.
ENDMODULE.
" value_for_customer
INPUT
MODULE capture_date INPUT.
* * * * * * *
"This function module will export the calendar and import date CALL FUNCTION 'F4_DATE' EXPORTING date_for_first_month = inp2 display = 'X' FACTORY_CALENDAR_ID = ' ' GREGORIAN_CALENDAR_FLAG = ' ' HOLIDAY_CALENDAR_ID = ' ' PROGNAME_FOR_FIRST_MONTH = ' ' IMPORTING select_date = inp2 SELECT_WEEK = SELECT_WEEK_BEGIN =
Rohini kumar
sap abap consultant
*
SELECT_WEEK_END = EXCEPTIONS calendar_buffer_not_loadable = 1 date_after_range = 2 date_before_range = 3 date_invalid = 4 factory_calendar_not_found = 5 holiday_calendar_not_found = 6 parameter_conflict = 7 OTHERS = 8 . IF sy-subrc <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. ENDMODULE.
Step – 8: Now before going to the process on help request event we need to create a help document by using Document Maintenance (SE61). We have created a test document ZTEST here with document class general text and English language.
Now we can write the help document inside the text area.
Rohini kumar
sap abap consultant
Step – 9: After creating this document we can create the help request event module. MODULE help_customer INPUT.
Here we have mentioned this function module where we need to pass the document class – TX for general text, system language and the document object name. Step – 10: Finally by creating a transaction code we can run the program.
Rohini kumar
sap abap consultant
By running the program we are getting the Customer name field where we can press F1 after having the cursor on there. The help request will show the following.
Now closing this we shall get back to the initial screen and then by pressing F4 we are getting the possible set of data as follows.
Rohini kumar
sap abap consultant
Similarly we are getting the calendar by pressing the F4 on the date field and then we can choose any date from the calendar. Here we have chosen 15th August, 2015.
Rohini kumar
Similarly we can enter time by pressing the F4 on the time field.
Finally we can prepare our screen by processing the value request event.
sap abap consultant
Rohini kumar
sap abap consultant
Now by clicking the Clear Customer button the screen will be cleared as follows.
We haven't prepared any function for Display Customer Button.
Disable Table Control Input Table control can be disabled in various ways. In the following example we have demonstrated a module pool program where we have mentioned a button which can disable table control to take an input. The table control output will be like following.
Rohini kumar
sap abap consultant
If we click the “Disable Table Control” button then the system will disable two fields – sales doc & item.
Rohini kumar
Next if we click the “Enable Table Control” button then it will be enabled again.
sap abap consultant
Rohini kumar
sap abap consultant
Now we shall discuss it step by step. Step – 1: Create a module pool program named SAPMZ_SR (as per naming convention it starts with SAPMZ). Create all the include programs as follows. INCLUDE INCLUDE INCLUDE INCLUDE
mz_sr_top mz_sr_o01 mz_sr_i01 mz_sr_f01
. . . .
" " " "
global Data PBO-Modules PAI-Modules FORM-Routines
Rohini kumar
sap abap consultant
Step – 2: Now create the TOP include where we need to declare every data members. PROGRAM
sapmz_sr.
TABLES: vbak, vbap. CONTROLS: tab_ctrl TYPE TABLEVIEW USING SCREEN 9001. TYPES: BEGIN OF ty_vbak, vbeln TYPE vbak-vbeln, erdat TYPE vbak-erdat, ernam TYPE vbak-ernam, vkorg TYPE vbak-vkorg, END OF ty_vbak. TYPES: BEGIN OF ty_vbap, vbeln TYPE vbap-vbeln, posnr TYPE vbap-posnr, matnr TYPE vbap-matnr, matkl TYPE vbap-matkl, END OF ty_vbap. DATA: wa_vbak TYPE ty_vbak, wa_vbap TYPE ty_vbap, it_vbap TYPE TABLE OF ty_vbap. DATA: ok_code TYPE sy-ucomm, disable TYPE c. Step – 3: Now create the screen 9001 and mention all details for the table control. PROCESS BEFORE OUTPUT. MODULE status_9001. LOOP AT it_vbap INTO wa_vbap WITH CONTROL tab_ctrl. MODULE prepare_table_control. ENDLOOP. PROCESS AFTER INPUT. LOOP AT it_vbap. MODULE modify_table_control. ENDLOOP. MODULE user_command_9001. Step – 4: Now create the layout for screen 9001. Here we have created one single screen where we have put the table control.
Rohini kumar
Step – 5: Mention the OK_CODE under Element List tab.
sap abap consultant
Rohini kumar
sap abap consultant
Step – 6: Now in PBO create the Module status 9001. MODULE status_9001 OUTPUT. "Setting PF status and Title SET PF-STATUS 'PF_9001'. SET TITLEBAR 'TI_9001'. "Preparing vbak-erdat vbak-ernam vbak-vkorg ENDMODULE.
the Sales Header information = wa_vbak-erdat. = wa_vbak-ernam. = wa_vbak-vkorg. " status_9001
OUTPUT
Step – 7: Next we create Module prepare table control. MODULE prepare_table_control OUTPUT. "Describing table to generate sy-dbcnt DESCRIBE TABLE it_vbap LINES sy-dbcnt. tab_ctrl-current_line = sy-loopc. tab_ctrl-lines = sy-dbcnt. "Preparing the Table Control data of Sales Doc Items
Rohini kumar
sap abap consultant
vbap-vbeln = wa_vbap-vbeln. vbap-posnr = wa_vbap-posnr. vbap-matnr = wa_vbap-matnr. vbap-matkl = wa_vbap-matkl. CLEAR wa_vbap. "If user clicks on the DISABLE button IF disable IS NOT INITIAL. LOOP AT SCREEN. CASE screen-name. WHEN 'VBAP-VBELN'. screen-input = '0'. "Input Disabled for VBELN MODIFY SCREEN. WHEN 'VBAP-POSNR'. screen-input = '0'. "Input Disabled for POSNR MODIFY SCREEN. ENDCASE. ENDLOOP. ENDIF. ENDMODULE.
" prepare_table_control
OUTPUT
Here we need to call the screen to disable the input of VBELN & POSNR and then modify the screen. Step – 8: Now we create the PAI module modify table control. MODULE modify_table_control INPUT. "Reading output table with current line to modify "when user scrolls down the table control READ TABLE it_vbap INTO wa_vbap INDEX tab_ctrl-current_line. IF sy-subrc = 0. MODIFY it_vbap FROM wa_vbap INDEX tab_ctrl-current_line. ENDIF. ENDMODULE.
" modify_table_control
Step – 9: Next create the module of user command 9001. MODULE user_command_9001 INPUT. CASE ok_code. WHEN 'DISP'. "Display Button PERFORM get_sales_data. WHEN 'REF'. "Refresh Button PERFORM refresh_sales_data. WHEN 'DISA'. "Disable Button
INPUT
Rohini kumar
sap abap consultant
disable = 'X'. WHEN 'ENA'. "Enable Button CLEAR disable. WHEN 'BACK'. "Back Button LEAVE LIST-PROCESSING. LEAVE PROGRAM. ENDCASE. ENDMODULE.
" user_command_9001
INPUT
Step – 10: Now we create the subroutines (performs) one by one. Firstly get sales data. FORM get_sales_data . IF vbak-vbeln IS NOT INITIAL. SELECT SINGLE vbeln erdat ernam vkorg FROM vbak INTO wa_vbak WHERE vbeln = vbak-vbeln. IF sy-subrc = 0. SELECT vbeln posnr matnr matkl FROM vbap INTO TABLE it_vbap WHERE vbeln = vbak-vbeln. IF sy-subrc = 0. SORT it_vbap. REFRESH CONTROL 'TAB_CTRL' FROM SCREEN 9001. CALL SCREEN 9001. ENDIF. ENDIF. ENDIF. ENDFORM.
" get_sales_data
Step – 11: Then refresh sales data. FORM refresh_sales_data . CLEAR: wa_vbak, vbak, vbap, ok_code. REFRESH it_vbap. ENDFORM.
" refresh_sales_data
Step – 12: Finally create a transaction code for that. In this case it is ZSR_TRAN.
Rohini kumar
Step – 13: Now we enter the transaction and get the following output.
sap abap consultant
Rohini kumar
sap abap consultant
1. Function Exit or Customer Exit 2. 3. The function exit is actually the customer exit. Here we need to use the statement CALL CUSTOMER-FUNCTION. Since we are writing our custom code inside a function module it is called function exit. This function module is a standard one so we can’t change anything in that function module. Inside this function module we have an include program starting with Z. We have to create this include and then we can write our own code. After that we need to implement this function module. Hence the function exit / customer exit is basically a function module which is provided by SAP and implemented by customer. 4.
Rohini kumar
sap abap consultant
5. The name of the function module contains EXIT_, then the program name and then the three digit indicator in function exit. We can search the exact function module exit by using the program name (the program name is called the Enhancement) in transaction SMOD. We implement the function exit by creating a specific project in CMOD. Now if one function exit is already implemented in one project then we cannot implement that function exit into a new project. This is the main draw back in function / customer exit. 6. 7. We have two standard DB tables like MODSAP & MODACT. Execute MODSAP by entering the enhancement name and we can get all function exits of this enhancement. Similarly execute MODACT by entering the project name and we get all the activated enhancement names. 8. 9. In the following example we are working with the transaction XK02 – change vendor data. We need to generate an information message if the country of the vendor is not India. To do this we are heading the following step by step approach. 10. 11. 1. Go to XK02.
12. 13. 2.
Mention a valid vendor and check Address.
Rohini kumar
sap abap consultant
14. 15. 3. Now we are in the address screen where the country is DE (Germany) for the existing vendor.
Rohini kumar
sap abap consultant
16. 17. 4. Now we modify the name by adding ‘Mr.’ and then save it. The vendor details have been saved successfully. At this stage we want this information pop up message.
Rohini kumar
sap abap consultant
18. 19. 5.
Stay on that screen where we need to customize and go to System > Status.
Rohini kumar
20. 21. 6.
Double click on the program name.
sap abap consultant
Rohini kumar
22. 23. 7.
Now we are in that program at SE38.
sap abap consultant
Rohini kumar
24. 25. 8.
Now find the statement CALL CUSTOMER-FUNCTION.
sap abap consultant
Rohini kumar
26. 27. 9.
28. 29.
Choose the program name.
sap abap consultant
Rohini kumar
sap abap consultant
30. 31. 10. Now we are in the global search screen where customer functions are visible. One program can have more than one customer functions for different purpose.
Rohini kumar
sap abap consultant
32. 33. 11. Now double click on CALL CUSTOMER-FUNCTION ‘001’ and go to the exact location of the program.
Rohini kumar
sap abap consultant
34. 35. 12. Now double click on the ‘001’ and then we go into the function module (SE37) as follows.
Rohini kumar
sap abap consultant
36. 37. 13. Another way to find the function exit is to go to the attribute of the program.
Rohini kumar
38. 39. 14. Note down the package.
sap abap consultant
Rohini kumar
sap abap consultant
40. 41. 15. Go to SMOD.
42. 43. 16. Enter the program name into the Enhancement input field and display.
Rohini kumar
sap abap consultant
44. 45. 17. Click on Components.
46. 47. 18. Now we can see the name of Function modules, Programs, screens etc.
Rohini kumar
sap abap consultant
48. 49. 19. Now we have to write our own code into that include program. To do this go to CMOD transaction.
Rohini kumar
50. 51. 20. Create a new project at first.
52. 53. 21. Give the description and save it in a package or local.
sap abap consultant
Rohini kumar
54. 55.
56. 57. 22. Click on Enhancement Assignment.
sap abap consultant
Rohini kumar
sap abap consultant
58. 59. 23. Now mention the program name into the enhancement and hit enter.
Rohini kumar
sap abap consultant
60. 61. 24. Then click on components. Then double click on Exit.
62. 63. 25. The function module screen will come and then double click on the include program.
Rohini kumar
64. 65. 26. Create the program.
sap abap consultant
Rohini kumar
sap abap consultant
66. 67. 27. Now we can write our custom code as follows. Activate the program.
68. 69. 28. Go back and activate the project. Here we have Activate as well as deactivate option.
70. 71. 29. Finally go to XK02 > mention vendor with address > change something (delete the ‘Mr.’ text in the name) > save it. We can see the pop up information message comes as required.
Rohini kumar
72.
SAP Script 1. SAP Script step by step 2. Create BAR code in SAP Script 3. Subroutine in SAP Script 4. Debug SAP Script
sap abap consultant
Rohini kumar
sap abap consultant
5. Working with Standard SAP Script 6. Convert SAP Script to PDF 7. Convert SAP Script to Text file
BAPI 1. BAPI Concept
SAP Script step by step
1. 2. 3. 4. 5.
Various business processes need a standard form by which the business can be run. Example of this kind of form is invoice, purchase orders, sales order details, delivery notes etc. We see that there are various formats of invoices between various retail shops. It means each and every organization uses a unique format of forms. SAP script is one of the tools of SAP which fulfills this requirement. There is another tool for this and that is SMARTFORMS. SAP script was the only one tool before release 4.6 and now from that release we have two options for the forms. We use SE71 transaction code to enter SAP script. SAP script contains a layout which is created by SE71 and a print program, created by SE38. In the program we call some predefined function modules as follows. OPEN_FORM START_FORM WRITE_FORM END_FORM CLOSE_FORM We can show multiple line items by looping the item table into work area and passing the work area into the WRITE_FORM.
1. 2. 3. 4.
There is some standard SAP script in the SAP system. Sales order confirmation – RVORDER01 Invoice – RVINVOICE01 Purchase order – MEDRUCK Packing list – RVDELNOTE etc. SAP script is client dependent whereas SMARTFORMS is client independent. Let us suppose we have created a script in 800 client which is development client. Now we want to test this script in another client 820. Since script is client dependent we won’t find any data in 820 client.
Rohini kumar
sap abap consultant
To fulfill this requirement let’s save the script in a package with a transport request in 800 client. Now go to SE09 and pick up the transport request number for that script.
Rohini kumar
sap abap consultant
Rohini kumar
sap abap consultant
After that go to 820 client and enter the transaction SCC1.
Mention the source client and that transport request number and then start immediately. We can run the script in another client by this way.
Rohini kumar
1.
sap abap consultant
Now script contains some development attributes as follows. Header – it contains two sections like administrative data and basic data. Now admin data contains the description, status, package, client, created by, date, languages etc. Basic data contains the basic settings of the script like set us page, default values of text formatting etc.
Rohini kumar
2.
sap abap consultant
Pages – it defines the pages of a form. Some forms contain more than one page. On the standard attribute we have to give unique page name if there is more than one page.
Rohini kumar
3.
sap abap consultant
Window – it is the various section of a page. We may have header & footer and the main body of the form. So we have to define a header window, footer window & a main window.
Rohini kumar
4.
sap abap consultant
Page Window – it defines which page contains which windows. Suppose we have three windows like header, footer & main. Now we want to put these windows in page1. So in page2 there must be some other or same window like footer and another content window. Inside the page window we can declare this very clearly.
Rohini kumar
5.
sap abap consultant
Paragraph Formats – it declares the format of the paragraph like left margin, right margin, alignment etc. We can use this paragraph format into any window. For different window we can design different paragraph format also.
Rohini kumar
6.
sap abap consultant
Character Formats – similar to paragraph format SAP script gives us the character format. We can customize the content character by using this. We can use different character formats for different window content.
Rohini kumar
7.
sap abap consultant
Documentation – SAP script provides the documentation in which we can write the technical documents like form name, pages used, windows used and elements used.
Rohini kumar
Now we are discussing about the step by step creation of SAP script. Step 1 – go to SE71 and create a form.
sap abap consultant
Rohini kumar
Step 2 – save it in package with transport request.
sap abap consultant
Rohini kumar
Step 3 – put a description at header.
sap abap consultant
Rohini kumar
Step 4 – create a page name PAGE1 in the page section.
sap abap consultant
Rohini kumar
Step 5 – create the windows as follows.
sap abap consultant
Rohini kumar
Step 6 – create a paragraph format P1.
sap abap consultant
Rohini kumar
Step 7 – create a character format C1.
sap abap consultant
Rohini kumar
sap abap consultant
Step 8 – go to page windows and configure the windows inside that page. Here go to edit and create element for that page.
Step 9 – double click on the window which you want to put inside this page.
Rohini kumar
Step 10 – In this way we have configured all the windows.
sap abap consultant
Rohini kumar
sap abap consultant
Step 11 – go to edit and text element to enter some content.
Step 12 – go to change editor and enter text as follows. Here the work areas should be declared between ‘&&’.
Rohini kumar
Step 13 – go to settings then form painter.
Step 14 – check the graphical form painter and enter.
sap abap consultant
Rohini kumar
sap abap consultant
Step 15 – now we can see the form layout and we also can edit the position and size of any windows. Step 16 – to enter a graphic right click on the layout > create graphic.
Rohini kumar
Step 17 – press F4 to get the name of the graphic.
sap abap consultant
Rohini kumar
Step 18 – select the required graphic from the list.
Step 19 – a graphic will be loaded like this.
sap abap consultant
Rohini kumar
sap abap consultant
Step 20 – to close this layout go to setting > form painter and uncheck the graphical form painter.
Step 21 – now we can check that the graphic has been added inside the page window also.
Rohini kumar
sap abap consultant
Step 22 – activate the form as follows.
Step 23 – now write a print program in SE38. (Mentioned below) Step 24 – execute that program and we can have the SAP script form as follows. REPORT
zsr_test.
*------Declaring structure for item table------------------------------* TYPES: BEGIN OF ty_ekpo, ebeln TYPE ekpo-ebeln, ebelp TYPE ekpo-ebelp, menge TYPE ekpo-menge, meins TYPE ekpo-meins, END OF ty_ekpo. *-----Declaring work area for PO header--------------------------------* DATA: BEGIN OF wa_ekko, ebeln TYPE ekko-ebeln, bukrs TYPE ekko-bukrs, ernam TYPE ekko-ernam, lifnr TYPE ekko-lifnr, END OF wa_ekko. *-----Declaring work area for Vendor Master----------------------------* DATA: BEGIN OF wa_lfa1,
Rohini kumar
sap abap consultant
lifnr TYPE lfa1-lifnr, land1 TYPE lfa1-land1, name1 TYPE lfa1-name1, ort01 TYPE lfa1-ort01, END OF wa_lfa1. *-----Declaring work area for Vendor Company Master--------------------* DATA: BEGIN OF wa_lfb1, lifnr TYPE lfb1-lifnr, bukrs TYPE lfb1-bukrs, erdat TYPE lfb1-erdat, ernam TYPE lfb1-ernam, akont TYPE lfb1-akont, END OF wa_lfb1. *-----Declaring work area & internal table for line item---------------* DATA: wa_ekpo TYPE ty_ekpo, it_ekpo TYPE TABLE OF ty_ekpo. *-----Event Initialization---------------------------------------------* INITIALIZATION. PARAMETERS: p_ebeln TYPE ekko-ebeln. *-----Event Start of Selection-----------------------------------------* START-OF-SELECTION. PERFORM get_purchase_order. PERFORM get_po_item. PERFORM display_script. *&---------------------------------------------------------------------* *& Form get_purchase_order *&---------------------------------------------------------------------* * Get data from Database table *----------------------------------------------------------------------* FORM get_purchase_order . IF p_ebeln IS NOT INITIAL. "Select single PO details SELECT SINGLE ebeln bukrs ernam lifnr FROM ekko INTO wa_ekko WHERE ebeln = p_ebeln. IF sy-subrc = 0. "Select single Vendor Master details SELECT SINGLE lifnr land1 name1 ort01 FROM lfa1 INTO wa_lfa1 WHERE lifnr = wa_ekko-lifnr. IF sy-subrc = 0. "Select single Vendor Company details SELECT SINGLE lifnr bukrs erdat ernam akont FROM lfb1 INTO wa_lfb1 WHERE lifnr = wa_lfa1-lifnr AND bukrs = wa_ekko-bukrs. ENDIF. ENDIF. ENDIF.
Rohini kumar
sap abap consultant
ENDFORM. " get_purchase_order *&---------------------------------------------------------------------* *& Form get_po_item *&---------------------------------------------------------------------* * Get data from item table to internal table *----------------------------------------------------------------------* FORM get_po_item. IF wa_ekko IS NOT INITIAL. SELECT ebeln ebelp menge meins FROM ekpo INTO TABLE it_ekpo WHERE ebeln = wa_ekko-ebeln. ENDIF. ENDFORM. "get_po_item *&---------------------------------------------------------------------* *& Form display_script *&---------------------------------------------------------------------* * Calling the SAP script function modules *----------------------------------------------------------------------* FORM display_script . "Opening the Form CALL FUNCTION 'OPEN_FORM' EXPORTING form EXCEPTIONS canceled device form OPTIONS unclosed mail_options archive_error invalid_fax_number more_params_needed_in_batch spool_error codepage OTHERS
= 'ZSCRIPT_TEST' = = = = = = = = = = = =
1 2 3 4 5 6 7 8 9 10 11 12.
IF sy-subrc <> 0. MESSAGE 'Form is not opened successfully' TYPE 'I'. ENDIF. "Starting the Form CALL FUNCTION 'START_FORM' EXPORTING form = 'ZSCRIPT_TEST' program = 'ZSR_TEST' EXCEPTIONS form = 1 format = 2 unended = 3 unopened = 4 unused = 5 spool_error = 6 codepage = 7 OTHERS = 8. IF sy-subrc <> 0.
Rohini kumar
sap abap consultant
MESSAGE 'Form is not started successfully' TYPE 'I'. ENDIF. "Writing the Form element one CALL FUNCTION 'WRITE_FORM' EXPORTING element = window = EXCEPTIONS element = function = type = unopened = unstarted = window = bad_pageformat_for_print = spool_error = codepage = OTHERS =
'E1' 'HEADER' 1 2 3 4 5 6 7 8 9 10.
IF sy-subrc <> 0. MESSAGE 'Form is not written for E1' TYPE 'I'. ENDIF. "Writing the Heading of PO Item CALL FUNCTION 'WRITE_FORM' EXPORTING element = 'HEAD' window = 'HEADING' EXCEPTIONS element = 1 function = 2 type = 3 unopened = 4 unstarted = 5 window = 6 bad_pageformat_for_print = 7 spool_error = 8 codepage = 9 OTHERS = 10. IF it_ekpo IS NOT INITIAL. LOOP AT it_ekpo INTO wa_ekpo. "Writing the line Items one by CALL FUNCTION 'WRITE_FORM' EXPORTING element = window = EXCEPTIONS element = function = type = unopened = unstarted = window = bad_pageformat_for_print = spool_error = codepage = OTHERS =
one 'ITEM' 'MAIN' 1 2 3 4 5 6 7 8 9 10.
Rohini kumar
sap abap consultant
ENDLOOP. ENDIF. "Writing the Form element two CALL FUNCTION 'WRITE_FORM' EXPORTING element = window = EXCEPTIONS element = function = type = unopened = unstarted = window = bad_pageformat_for_print = spool_error = codepage = OTHERS =
'COMP' 'COMPANY' 1 2 3 4 5 6 7 8 9 10.
IF sy-subrc <> 0. MESSAGE 'Form is not written for E2' TYPE 'I'. ENDIF. "Writing the Form element three CALL FUNCTION 'WRITE_FORM' EXPORTING element = 'E3' window = 'VENDOR' EXCEPTIONS element = 1 function = 2 type = 3 unopened = 4 unstarted = 5 window = 6 bad_pageformat_for_print = 7 spool_error = 8 codepage = 9 OTHERS = 10. IF sy-subrc <> 0. MESSAGE 'Form is not written for E3' TYPE 'I'. ENDIF.
* * * * * * * *
"Ending the Form CALL FUNCTION 'END_FORM' IMPORTING RESULT = EXCEPTIONS UNOPENED = BAD_PAGEFORMAT_FOR_PRINT = SPOOL_ERROR = CODEPAGE = OTHERS = . IF sy-subrc <> 0. MESSAGE 'Form is not ended' TYPE ENDIF.
1 2 3 4 5 'I'.
Rohini kumar
* * * * * * * * * * * *
sap abap consultant
"Closing the Form CALL FUNCTION 'CLOSE_FORM' IMPORTING RESULT = RDI_RESULT = TABLES OTFDATA = EXCEPTIONS UNOPENED = 1 BAD_PAGEFORMAT_FOR_PRINT = 2 SEND_ERROR = 3 SPOOL_ERROR = 4 CODEPAGE = 5 OTHERS = 6 . IF sy-subrc <> 0. MESSAGE 'Form is not closed' TYPE 'I'. ENDIF.
ENDFORM.
Selection Screen:
Printing Output device selection:
" display_script
Rohini kumar
The Script:
sap abap consultant
Rohini kumar
sap abap consultant
Rohini kumar
sap abap consultant
Create BAR code in SAP Script Now we know to create SAP script. We want to create a BAR code in the script. BAR code is actually a character format. It is generated from the character format. We can import bar code for some value like PO number, SO number, customer number like that. Here we are taking the script which has been created previously. We want to import the bar code against the vendor account number. Step 1: Create a new character format.
Step 2: Select the Bar Code in standard attribute. Press F4 help on the bar code input field.
Rohini kumar
sap abap consultant
Step 3: SAP system already contains some bar code. We can choose any one of them.
Rohini kumar
Step 4: Now the bar code has been selected.
sap abap consultant
Rohini kumar
sap abap consultant
Step 5: Go to Vendor Master page window where we need to create the bar code against vendor account.
Rohini kumar
sap abap consultant
Step 6: Go to change editor and put the newly created character format beside vendor account as follows.
Step 7: The bar code has been imported. So activate the script and run the print program.
Rohini kumar
Step 8: Select the output device for print.
Step 9: The form will look like the following.
sap abap consultant
Rohini kumar
sap abap consultant
In this example we have imported a bar code which has been defined in the system. We can generate our own bar code also. For this we need to create a new bar code. To do this we have to go to SE73 - SAP Script Font maintenance. Step 1: Go to SE73. Select system bar codes and click on Display.
Rohini kumar
sap abap consultant
Step 2: All system defined bar codes will be displayed here. We have to define our own bar code from here. Click on the new button.
Rohini kumar
sap abap consultant
Step 3: Choose bar technology New.
Step 4: Give a custom name starting with Z or Y and enter a description.
Step 5: Choose a bar code symbol. All five symbols are supported in this system.
Rohini kumar
Step 6: Choose the alignment.
Step 7: Mention the bar code parameters.
Step 8: Save it in a package and create transport request.
sap abap consultant
Rohini kumar
sap abap consultant
Step 9: Now the bar code has been created and the display list will be updated.
Step 10: Here we can see that our bar code has been populated in the list.
Rohini kumar
sap abap consultant
Step 11: We can test the bar code with print preview.
Step 12: Our created bar code is as follows.
Subroutine in SAP Script Let us go with the existing SAP script. We need to create a subroutine inside this script. As we know the usability of subroutine we want to create a subroutine inside this script. With the help of this subroutine we want to show the Bank details of existing vendor so LFBK table has been used. Now the actual coding part is taken care by SE38 where a subroutine pool program needs to be created. We are calling that program inside this form. To call this program we have to go to Text element with change editor. The steps are as follows. Step 1: Go to text element change editor. The code can be written there by using '/:' operator.
Rohini kumar
Step 2: Now create a subroutine pool program in SE38.
sap abap consultant
Rohini kumar
sap abap consultant
Step 3: This program will contain only the form and endform. We can declare nested forms also. The program is as follows. PROGRAM
zsubroutine_script.
*&---------------------------------------------------------------------* *& Form get_bank *&---------------------------------------------------------------------* * Subroutine by which getting data from vendor bank table LFBK *----------------------------------------------------------------------* FORM get_bank TABLES it_input STRUCTURE itcsy it_output STRUCTURE itcsy. *-Declaring input table and output table with structure ITCSY *-ITCSY is a standard structure which holds two fields *-Name & Value *-Here the name is field name & value is field value "Declaring bank details work area structure DATA: BEGIN OF wa_lfbk, lifnr TYPE lfbk-lifnr, banks TYPE lfbk-banks, bankl TYPE lfbk-bankl, END OF wa_lfbk. DATA: wa_var lv_var
TYPE itcsy, "work area for ITCSY TYPE lifnr. "variable for conversion
IF it_input IS NOT INITIAL.
Rohini kumar
sap abap consultant
"Reading the input table with key name READ TABLE it_input INTO wa_var WITH KEY name = 'WA_LFA1-LIFNR'. IF sy-subrc = 0. "Conversion for vendor account number CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT' EXPORTING input = wa_var-value IMPORTING output = lv_var. "Selecting the bank details SELECT SINGLE lifnr banks bankl FROM lfbk INTO wa_lfbk WHERE lifnr = lv_var. IF sy-subrc = 0. "Reading the output table with key name - first field READ TABLE it_output INTO wa_var WITH KEY name = 'WA_LFBK-BANKS'. IF sy-subrc = 0. wa_var-value = wa_lfbk-banks. "Bank country "Modifying the output table with values - bank country MODIFY it_output FROM wa_var INDEX sy-tabix. ENDIF. "Reading the output table with key name - second field READ TABLE it_output INTO wa_var WITH KEY name = 'WA_LFBK-BANKL'. IF sy-subrc = 0. wa_var-value = wa_lfbk-bankl. "Bank key "Modifying the output table with values - bank key MODIFY it_output FROM wa_var INDEX sy-tabix. ENDIF. ENDIF. ENDIF. ENDIF. ENDFORM.
"get_bank
Step 4: Finally in the form we can see the Bank details.
Rohini kumar
sap abap consultant
Rohini kumar
sap abap consultant
Debug SAP Script We can debug SAP script by using a different debugger. Let us assume the previous script which is needed to debug. The steps are as follows: Step 1: Go to SE71 and mention the script name. Then Utilities > Active Debugger.
Step 2: Then SAP script debugger will be activated as follows.
Step 3: Now go to print program and execute this.
Step 4: The debugger break point screen will come and click OK.
Rohini kumar
sap abap consultant
Step 5: Select the output device.
Step 6: Now the debugging has been started line by line. We can see the work areas, variables are changing with its values.
Rohini kumar
Step 7: We have single step, continue, execute options.
sap abap consultant
Rohini kumar
Step 8: Finally we can see the form.
sap abap consultant
Rohini kumar
sap abap consultant
Another way to debug SAP script is to execute the standard program RSTXDBUG. And we shall have the similar screen for activate debugger.
Working with Standard SAP Script
Rohini kumar
sap abap consultant
In recent time there is very less requirement of SAP script scratch development. We generally work with the standard script which is already available in the system. We don't modify any standard script. There is a process to have the client copy of the standard script. Hence we copy the standard one to custom and then we can modify it as per the requirement. Step 1: Go to NACE transaction to find out the name of the required standard script.
Step 2: Here we can see a lot of functional requirement and we have chosen the Billing. Then click on the Output type.
Rohini kumar
sap abap consultant
Step 3: Now we are in the display of output type. Click on the change button and then Position button.
Rohini kumar
sap abap consultant
Step 4: We shall need the output entry. Click on F4 help.
Step 5: All of the output types are displayed here. Now we have to choose the proper one.
Rohini kumar
sap abap consultant
Step 6: Now select the RD00 (invoice) for this case and then double click on Processing routines.
Rohini kumar
sap abap consultant
Step 7: Now we can see Program name, form name etc. If there is any smartform available then it will be mentioned under PDF/SmartForm.
Step 8: Now go to SE71 and type enter that form name (RVINVOICE01) for this case. Go to Utilities > 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.
*----------------------------------------------------------------------* * 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.
*----------------------------------------------------------------------* * 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
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 *& *&--------------------------------------------------------------------* *&
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,
*---------------------------------------------------------------------* * 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.
TOP-OF-PAGE. PERFORM top1. PERFORM top2. *&--------------------------------------------------------------------* *& Form TOP1 *&------------------------------------------------------------
Rohini kumar
sap abap consultant
---------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM top1 . CLEAR wa_top1. REFRESH it_top1. wa_top1-typ = 'H'. wa_top1-info = 'PURCHASE ORDER HEADER'. APPEND wa_top1 TO it_top1. CLEAR wa_top1. wa_top1-typ = 'S'. wa_top1-key = 'Date: '. CONCATENATE v_date+6(2) ':' v_date+4(2) ':' v_date(4) wa_top1-info INTO wa_top1-info. APPEND wa_top1 TO it_top1. CLEAR wa_top1. wa_top1-typ = 'S'. wa_top1-key = 'User: '. CONCATENATE v_user wa_top1-info INTO wa_top1-info. APPEND wa_top1 TO it_top1. CLEAR wa_top1. wa_top1-typ = 'S'. wa_top1-key = 'Report: '. CONCATENATE v_repid wa_top1-info INTO wa_top1-info. APPEND wa_top1 TO it_top1. CLEAR wa_top1. CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE' EXPORTING it_list_commentary = it_top1 * I_LOGO = * I_END_OF_LIST_GRID = * I_ALV_FORM = . ENDFORM. " TOP1 *&------------------------------------------------------------
Rohini kumar
sap abap consultant
---------* *& Form TOP2 *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* * --> p1 text * <-- p2 text *---------------------------------------------------------------------* FORM top2 . CLEAR wa_top2. REFRESH it_top2. wa_top2-typ = 'H'. wa_top2-info = 'PURCHASE ORDER ITEM DOCUMENT'. APPEND wa_top2 TO it_top2. CLEAR wa_top2. CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE' EXPORTING it_list_commentary = it_top2 * I_LOGO = * I_END_OF_LIST_GRID = * I_ALV_FORM = . ENDFORM. TOP2
The Selection Screen is below where we are selecting PO 1 to 4.
The output will be like below:
"
Rohini kumar
sap abap consultant
Global Data in OOPs Concept Global data is accessible from any class in a program. We know that the visibility depends on the public, protected or private section. The globally declared variable is visible from anywhere in the program. We have created a program where the global variable is v_globe and in the debugging mode we can see its value from any of classes. REPORT
zsr_test NO STANDARD PAGE HEADING.
DATA: v_globe TYPE char40 VALUE 'This is Global Data'. *----------------------------------------------------------------------* * CLASS cls1 DEFINITION
Rohini kumar
sap abap consultant
*----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls1 DEFINITION. PUBLIC SECTION. DATA v_pub_1 TYPE char40 VALUE 'Class 1 public data'. METHODS m_cls1. PROTECTED SECTION. DATA v_pro_1 TYPE char40 VALUE 'Class 1 protected data'. PRIVATE SECTION. DATA v_pri_1 TYPE char40 VALUE 'Class 1 private data'. ENDCLASS. "cls1 DEFINITION *----------------------------------------------------------------------* * CLASS cls2 DEFINITION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls2 DEFINITION. PUBLIC SECTION. DATA v_pub_2 TYPE char40 VALUE 'Class 2 public data'. METHODS m_cls2. PROTECTED SECTION. DATA v_pro_2 TYPE char40 VALUE 'Class 2 protected data'. PRIVATE SECTION. DATA v_pri_2 TYPE char40 VALUE 'Class 2 private data'. ENDCLASS. "cls2 DEFINITION *----------------------------------------------------------------------* * CLASS cls1 IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls1 IMPLEMENTATION. METHOD m_cls1. WRITE: / v_pub_1, / v_pro_1, / v_pri_1. ENDMETHOD. "m_cls1 ENDCLASS. "cls1 IMPLEMENTATION *----------------------------------------------------------------------* * CLASS cls2 IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls2 IMPLEMENTATION. METHOD m_cls2. WRITE: / v_pub_2, / v_pro_2, / v_pri_2. ENDMETHOD. "m_cls2 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
CALL METHOD: obj1->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.
*----------------------------------------------------------------------* * 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.
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. *&--------------------------------------------------------------------*
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.
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.
*---------------------------------------------------------------------* * 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.
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.
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:'.
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.
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'.
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.
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.
*----------------------------------------------------------------------* * 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.
*----------------------------------------------------------------------* * 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.
*----------------------------------------------------------------------* * 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.
*----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls2 DEFINITION INHERITING FROM cls1. ENDCLASS.
*----------------------------------------------------------------------* * *----------------------------------------------------------------------* 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.
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.
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 *& *&---------------------------------------------------------------------* *& *&
*----------------------------------------------------------------------* * 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.
*----------------------------------------------------------------------* * 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.
*----------------------------------------------------------------------* * 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.
*----------------------------------------------------------------------* * *----------------------------------------------------------------------* INTERFACE it. DATA v_txt TYPE char50. METHODS meth. ENDINTERFACE.
*----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls DEFINITION. PUBLIC SECTION. DATA date TYPE sy-datum. INTERFACES it. ENDCLASS.
*----------------------------------------------------------------------* 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.
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.
*----------------------------------------------------------------------* * *----------------------------------------------------------------------* INTERFACE it. DATA v_txt TYPE char50. METHODS: meth1, meth2. ENDINTERFACE.
*----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls DEFINITION. PUBLIC SECTION. INTERFACES it. ENDCLASS.
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.
*----------------------------------------------------------------------* * *----------------------------------------------------------------------* INTERFACE it. DATA: v_txt1 TYPE char40, v_txt2 TYPE char40. METHODS mit. ENDINTERFACE.
*----------------------------------------------------------------------* CLASS cls DEFINITION. PUBLIC SECTION. INTERFACES it DATA VALUES v_txt1 = 'SAP ABAP' v_txt2 = 'Object Oriented'. 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.
*----------------------------------------------------------------------* * *----------------------------------------------------------------------* INTERFACE it. DATA v_txt TYPE char50. METHODS: mit1, mit2. ENDINTERFACE.
*----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS parent DEFINITION ABSTRACT. PUBLIC SECTION. INTERFACES it ABSTRACT METHODS mit2. ENDCLASS.
*----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS child DEFINITION INHERITING FROM parent. PUBLIC SECTION. METHODS it~mit2 REDEFINITION. ENDCLASS.
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.
*----------------------------------------------------------------------* * *----------------------------------------------------------------------* INTERFACE it. CONSTANTS c_var TYPE char40 VALUE 'SAP ABAP Object Oriented'. DATA var TYPE char50. METHODS mit. ENDINTERFACE.
*----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls DEFINITION. PUBLIC SECTION. INTERFACES it. ENDCLASS.
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.
*----------------------------------------------------------------------* * *----------------------------------------------------------------------* INTERFACE it. CONSTANTS c_var TYPE char40 VALUE 'SAP ABAP Object Oriented'. CLASS-DATA cls_var TYPE char50. CLASS-METHODS cls_mit.
*----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS cls DEFINITION. PUBLIC SECTION. INTERFACES it. ENDCLASS.
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.
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.
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.
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.
*----------------------------------------------------------------------* * 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.
*& 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.
*----------------------------------------------------------------------* * *----------------------------------------------------------------------* 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.
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.
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.
*----------------------------------------------------------------------* * 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
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.
*----------------------------------------------------------------------* * 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.
*----------------------------------------------------------------------* * 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.
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.