Show TOC

Message Handling in Function Module ModulesLocate this document in the navigation structure

 Contexts are obsolete and should not be used. Contexts were introduced in Release 4.0 for high-performance access to frequently required data. Since the introduction of ABAP Objects for Release 4.5, contexts have not been developed further. Since Release 6.40, contexts can be replaced by shared objects .

For a message to be sent from a function module context module, the function module must contain the necessary exception handling. Exception handling in the function module must be programmed with the statement MESSAGE... RAISING. If 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 handles 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 response to the various message types depends on the current user dialog (dynpro, selection screen, or list).

Message Handling - Program

If you want to catch the message in your program, use the DEMANDstatement with 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 the itab  table at the beginning of the DEMAND statement. While it is processing the context, the system does not display or respond to any messages, but writes their ID to itab. If there are messages in the table following the DEMAND statement, the system sets the return code sy-subrc to a value not 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 these fields (using the FIELDS statement in the dynpro flow logic, or the ABAP statement AT SELECTION-SCREEN for selection screens), and not on fields in the context.

Tip

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.

The context docu_test4 uses this function module as its only module, with key fields max and occ for input parameters v1 and v2and the dependent field percentfor the output parameter result.

To demonstrate system message handling, execute the following program:

REPORT  demo_context_message_3.

DATApercentage 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  demo_context_message_4.

DATA: percentage TYPE i.

CONTEXTS docu_test4.

DATA: context_inst TYPE context_docu_test4.

DATAitab TYPE TABLE OF symsg 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.   "#EC NOTEXT

IF sy-subrc NE 0.  LOOP AT itab.    WRITE: / itab-msgty, / itab-msgid, / itab-msgno,           / itab-msgv1, / itab-msgv2.  ENDLOOP.ENDIF.

The program displays 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.