Entering content frameMessage Handling in Function Module Modules Locate the document in its SAP Library structure

In order for a message to be sent from a function module context module, the function module must contain the necessary exception handling. In other words, the function module must send the message using the MESSAGE ... RAISING statement. If the exception handling in the function module is programmed using the RAISE statement, the system reacts with a runtime error. For more information about exception handling in function modules, see Function Modules.

The system sends the message specified in the MESSAGE... RAISING statement. Messages specified for function modules in the Modules table are ignored.

When you query dependent data from context instances using the DEMAND statement, you can decide whether the system should handle the user message or whether you want to handle it yourself in the program.

Message Handling - System

If you want the system to handle the message, use the basic form of the DEMAND statement without the MESSAGES option. The system will then behave as though the MESSAGE statement in the function module occurred after the DEMAND statement. Note that the system reaction to the various message types depends on the current user dialog (dialog screen, selection screen or list).

Message Handling - Program

If you want to catch the message in your program, use the DEMAND statement using the option MESSAGES INTO <itab>. To do this, you need to define an internal table <itab> with the structure SYMSG. The system deletes the contents of <itab> table at the beginning of the DEMAND statement. Whilst it is processing the context, the system does not output or react to any messages, but writes their ID to <itab>. If there are messages in <itab> following the DEMAND statement, the system sets the return code SY-SUBRC to a value unequal to zero.

This enables you to prevent automatic message handling with contexts and allows you to program your own using the entries in <itab>. This is important, for example, if you want to make particular fields on an entry screen ready for input again. In this case, you must base your message handling on the fields (using the FIELDS statement in screen flow logic, or the ABAP statement AT SELECTION-SCREEN for selection screens), and not on fields in the context.

Example

The function module PERCENT has the following source code:

FUNCTION PERCENT.

RESULT = V1 - V2.

IF V1 = 0.
  MESSAGE ID 'AT' TYPE 'E' NUMBER '012' RAISING DIV_ZERO.
ELSE.
  RESULT = RESULT * 100 / V1.
ENDIF.

ENDFUNCTION.

Context DOCU_TEST4 uses this function module as its only module, with key fields MAX and OCC for input parameters V1 and V2 and the dependent field PERCENT for the output parameter RESULT.

To demonstrate system message handling, execute the following program:

REPORT  RSGCON04.

DATA: INPUT_1 TYPE I,
      INPUT_2 TYPE I,
      PERCENTAGE TYPE I.

CONTEXTS DOCU_TEST4.

DATA: CONTEXT_INST TYPE CONTEXT_DOCU_TEST4.

SUPPLY MAX = 00
       OCC = 20
       TO CONTEXT CONTEXT_INST.

DEMAND PERCENT = PERCENTAGE
       FROM CONTEXT CONTEXT_INST.

WRITE: / 'Percentage: ', PERCENTAGE.

The program terminates with the following error message:

This is an error message

To demonstrate program message handling, execute the following program:

REPORT  RSGCON05.

DATA: INPUT_1 TYPE I,
      INPUT_2 TYPE I,
      PERCENTAGE TYPE I.

CONTEXTS DOCU_TEST4.

DATA: CONTEXT_INST TYPE CONTEXT_DOCU_TEST4.

DATA ITAB LIKE SYMSG OCCURS 0 WITH HEADER LINE.

SUPPLY MAX = 00
       OCC = 20
       TO CONTEXT CONTEXT_INST.

DEMAND PERCENT = PERCENTAGE
       FROM CONTEXT CONTEXT_INST MESSAGES INTO ITAB.

WRITE: / 'Percentage: ', PERCENTAGE.

LOOP AT ITAB.
  WRITE: / ITAB-MSGTY, ITAB-MSGID, ITAB-MSGNO,
           ITAB-MSGV1, ITAB-MSGV2.
ENDLOOP.

The program outputs the following:

PERCENTAGE:  0

E  AT  012

Instead of terminating with an error message, the program stores the message in the internal table ITAB, where it is available for you to process further.

 

 

 

 

Leaving content frame