Receiving Results from an Asynchronous RFC

To receive results from an asynchronously called function, use the following syntax:

CALL FUNCTION Remotefunction

   STARTING NEW TASK Taskname

   PERFORMING RETURN_FORM ON END OF TASK.

Once the called function is completed, the next dialog step in the calling program (such as AT USER-COMMAND) guides the system into the FORM routine that checks for results. This FORM routine consists of a special syntax and must be called with a using parameter that refers to the name of the task:

Client System

CALL FUNCTION Remotefunction

   STARTING NEW TASK Taskname

   DESTINATION Dest

   PERFORMING RETURN_FLIGHT ON END OF TASK.

...

FORM RETURN_FLIGHT USING TASKNAME.

   RECEIVE RESULTS FROM FUNCTION Remotefunction

   IMPORTING   F1 = a1

   EXCEPTIONS  SYSTEM_FAILURE MESSAGE SYSTEM_MSG.

   SET USER-COMMAND ‘OKCD’.

ENDFORM.

§         If a function module returns no result, the addition PERFORMING RETURN_FORM ON END OF TASK can be omitted.

§         If an asynchronous call calls several consecutive function modules with the same destination, you must assign a different task name to each.

§         A calling program which starts an asynchronous RFC with PERFORMING cannot switch roll areas or change to an internal mode. This is because the asynchronous function module call reply cannot be passed on to the relevant program. You can perform a roll area switch with SUBMIT or CALL TRANSACTION.

§         If the calling program which has executed the asynchronous call is terminated, despite the fact that it is expecting replies, these replies from the asynchronous call cannot be delivered.

§         You can use the WAIT statement with PERFORMING form ON END OF TASK to wait for the reply to a previously started asynchronous call. In this case, WAIT must be in the same program context.

§         The program processing continues after WAIT if either the condition of a logical expression was satisfied by the subroutine that performs the task in question, or a specified time period has been exceeded. For more information on the WAIT statement, see the online help in the ABAP editor.

§         The key word RECEIVE occurs only with the function module call CALL FUNCTION Remotefunction STARTING NEW TASK Taskname. If the function module returns no results, this part need not be defined.

§         The effect of the SET USER-COMMAND ‘OKCD’ statement is exactly as if the user had entered the function in the command field and pressed ENTER. This means that the current positioning of the list and the cursor is taken into account.

No call-backs are supported.

The SET USER-COMMAND ‘OKCD’ statement replaces the REFRESH SCREEN command. REFRESH SCREEN is no longer maintained and should therefore not be used.

DATA: INFO LIKE RFCSI,

* Result of RFC_SYSTEM_INFO function module

  MSG(80) VALUE SPACE.

* Exception handling

CALL FUNCTION ‘RFC_SYSTEM_INFO’

  STARTING NEW TASK ‘INFO’

  PERFORMING RETURN_INFO ON END OF TASK

  EXCEPTIONS

      COMMUNICATION_FAILURE   = 1 MESSAGE MSG

      COMMUNICATION_FAILURE   = 2.MESSAGE MSG.

IF SY-SUBRC = 0.

  WRITE: ‘Wait for reply’.

  ELSE.

  WRITE MSG

  ENDIF.

...

AT USER-COMMAND.

* Return from FORM routine RETURN_INFO via SET USER-COMMAND

      IF SY-UCOMM = ‘OKCD’.

         IF MSG = SPACE.

             WRITE: ‘Destination =‘, INFO-RFCDEST.

           ELSE.

             WRITE MSG.

           ENDIF.

        ENDIF.

...

FORM RETURN_INFO USING TASKNAME.

RECEIVE RESULTS FROM FUNCTION ‘RFC_SYSTEM_INFO’

    IMPORTING RFCSI_EXPORT = INFO

    EXCEPTIONS

      COMMUNICATION_FAILURE   = 1 MESSAGE MSG

      SYSTEM_FAILURE          = 2 MESSAGE MSG.

  SET USER-COMMAND ‘OKCD’.       “Set OK-code

ENDFORM.

Keeping the Remote Context