0% found this document useful (0 votes)
143 views5 pages

Lab 7 (Event and Subroutine) - Ans

The document provides instructions and code examples for exercises on using events and subroutines in ABAP. The exercises demonstrate how to: 1. Create a message class and use error messages in a program 2. Create a program that uses a macro to populate and append records to an internal table 3. Create a program that calls a subroutine to perform a calculation and return the result 4. Create another program that calls the subroutine from the previous program externally

Uploaded by

mebele8069
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
143 views5 pages

Lab 7 (Event and Subroutine) - Ans

The document provides instructions and code examples for exercises on using events and subroutines in ABAP. The exercises demonstrate how to: 1. Create a message class and use error messages in a program 2. Create a program that uses a macro to populate and append records to an internal table 3. Create a program that calls a subroutine to perform a calculation and return the result 4. Create another program that calls the subroutine from the previous program externally

Uploaded by

mebele8069
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Hong Kong Institute of Vocational Education

IT114105 HD in Software Engineering


ITP4512 Enterprise Software
Topic 6: Event and Modularization
Lab 7 – Event and Subroutine

Intended Learning Outcomes


Upon completion of this tutorial/lab, you should be able to:
o Identify how to handle event in an SAP R/3 system.
o Identify how to use subroutine in ABAP programs.
o Create simple ABAP programs to use error messages define in message class.
o Create simple ABAP programs to use subroutines for specific functions.

Exercise1

1. Use SE91 to create a new message class called Z_MESSAGE and add at least one message like
‘Data not found’.
Answer:

2. Create a program called Z_07_MESSAGE with below selection screen:

3. In the program, capture data from table SPFLI with the selection criteria.
4. Show data (carrid, connid, cityfrom and cityto) if found but show pop-up error message if not
found. (sy-subrc = 4)

1
5. Error message should make use of the message class Z_MESSAGE.
Answer:
*&---------------------------------------------------------------------*
*& Report Z_07_MESSAGE *
*& *
*&---------------------------------------------------------------------*
*& *
*& *
*&---------------------------------------------------------------------*

REPORT Z_07_MESSAGE MESSAGE-ID z_message .


TABLES spfli.
DATA wa TYPE spfli.
SELECT-OPTIONS airline FOR spfli-carrid obligatory.
SELECT * FROM spfli INTO WA WHERE carrid IN airline.
WRITE: / wa-carrid, wa-connid, wa-cityfrom, wa-cityto.
ENDSELECT.
IF sy-subrc = 4.
MESSAGE i002 WITH 'SPFLI'.
ELSEIF sy-subrc <> 0.
MESSAGE i000.
ENDIF.

Exercise 2

1. Create a program called Z_07_MACRO.


2. Copy the source code in program Z_05_INT_TABLE into Z_07_MACRO.
REPORT Z_05_INT_TABLE .
TYPES: BEGIN OF int_rec,
customer TYPE c LENGTH 5,
lname TYPE c LENGTH 15,
fname TYPE c LENGTH 15,
END OF int_rec.
DATA: int_tab TYPE TABLE OF int_rec,
int_wa TYPE int_rec.

*** create the MACRO here and then call the MACRO***

WRITE: / 'Customer ', 'Last Name ', 'First Name'.


ULINE.
LOOP AT int_tab INTO int_wa.
WRITE : / int_wa-customer UNDER 'Customer ',
int_wa-lname UNDER 'Last Name ',
int_wa-fname UNDER 'First Name'.
ENDLOOP.

3. Modify the codes as follows:


a. Create a Macro named append_fields, which will fill up the work area (int_wa) using
parameters, and append the work area to the internal table (int_tab).
b. Do the actual appending by calling the Macro append_fields 4 times.
c. Write the data in the internal table as usual.

2
Answer:
*&---------------------------------------------------------------------*
*& Report Z_07_MACRO *
*& *
*&---------------------------------------------------------------------*
*& *
*& *
*&---------------------------------------------------------------------*

REPORT Z_07_MACRO .
TYPES: BEGIN OF int_rec,
customer TYPE c LENGTH 5,
lname TYPE c LENGTH 15,
fname TYPE c LENGTH 15,
END OF int_rec.

DATA: int_tab TYPE TABLE OF int_rec,


int_wa TYPE int_rec.

DEFINE append_fields.
CLEAR int_wa.
int_wa-customer = &1.
int_wa-lname = &2.
int_wa-fname = &3.
APPEND int_wa TO int_tab.
END-OF-DEFINITION.

append_fields: '12345' 'Porter' 'John',


'12346' 'Porter' 'May',
'12347' 'Carter' 'John',
'12348' 'Carter' 'Peter'.

WRITE: / 'Customer ', 'Last Name ', 'First Name'.


ULINE.

LOOP AT int_tab INTO int_wa.


WRITE : / int_wa-customer UNDER 'Customer ',
int_wa-lname UNDER 'Last Name ',
int_wa-fname UNDER 'First Name'.
ENDLOOP.

3
Exercise 3

1. Create a program called Z_07_SB_DIVIDE which has two integer parameters.


2. In the program, create a subroutine called DIVIDE_NUM to handle ‘number 1 divided by number
2’, return the result to the main program.
3. After the subroutine is called, show the result as output like below:

Answer:
*&---------------------------------------------------------------------*
*& Report Z_07_SB_DIVIDE *
*& *
*&---------------------------------------------------------------------*
*& *
*& *
*&---------------------------------------------------------------------*

REPORT Z_07_SB_DIVIDE MESSAGE-ID Z_MESSAGE .

DATA f_result TYPE p DECIMALS 2.


DATA result TYPE f.
PARAMETERS Number1 TYPE i.
PARAMETERS Number2 TYPE i.

PERFORM divide_num USING Number1 Number2


CHANGING result.
MOVE result TO f_result.

WRITE: 'Result: ', f_result.

FORM divide_num USING num1 TYPE i


num2 TYPE i
CHANGING resu TYPE f.

IF num2 = 0.
MESSAGE i003.
ELSE.
COMPUTE resu = num1 / num2.
ENDIF.

ENDFORM.

4
Exercise 4

1. Write another program named Z_07_SB_DIVIDE1 which call the subroutine externally. That is,
the program will call the subroutine DIVIDE_NUM in program Z_07_SB_DIVIDE.

Answer:
*&---------------------------------------------------------------------*
*& Report Z_07_SB_DIVIDE1 *
*& *
*&---------------------------------------------------------------------*
*& *
*& *
*&---------------------------------------------------------------------*

REPORT Z_07_SB_DIVIDE1 MESSAGE-ID Z_MESSAGE .


DATA f_result TYPE p DECIMALS 2.
DATA result TYPE f.
PARAMETERS Number1 TYPE i.
PARAMETERS Number2 TYPE i.

PERFORM divide_num in program Z_07_SB_DIVIDE USING Number1 Number2


CHANGING result.
MOVE result TO f_result.

WRITE: 'Result: ', f_result.

END.

You might also like