Entering content frame

Message Handling in Function Module Modules Locate the document in its SAP Library structure

Caution Contexts are obsolete and should not be used. Contexts were introduced for 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 Structure linkshared objects.


In order 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 DEMANDstatement, 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 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 DEMANDstatement. 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 the table following the DEMANDstatement, 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 these fields (using the FIELDS statement in the screen flow logic, or the ABAP statement AT SELECTION-SCREEN for selection screens), and not on fields in the context.


The function module percent has the following source code:

FUNCTION percent.

result = v1 - v2.

IF v1 = 0.
  result = result * 100 / v1.



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  demo_context_message_3.

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

DATA: 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 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.


The program outputs the following:


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