Does RFC function modules work only once in a loop?

By Sarves Sombhatla, Yash Technologies

Sometimes, when we are trying to transfer the data from one SAP system to another SAP system by looping the RFC function module, only the data which is looped first will be uploaded and the remaining data will not be uploaded, why?  

The reason why this happens is when we establish a connection from one system to another SAP system using an RFC, the global data of the function group remains, as the connection remains active the global data will not be cleared, to clear the global data we should close the connection and re-establish it. By doing this the global data will be freed and we can continue upload the data completely from one system to another by looping the RFC function module.  

To have a clear insight about this topic, check the below example:  

Consider there are 2 SAP systems

  1. ABC
  2. XYZ

Assumptions:

  1. An RFC destination is created between both the systems ABC & XYZ.  

In ABC we have a database table and we want to upload the data from system XYZ to ABC.  

In the first sap system ABC: 

1. Create a table (ZRFC_TABLE) with 2 fields

  • ID
  • Name  

2. Now create an RFC function module to upload the data into the table through another SAP system:

 

Import parameter: ITAB type ZRFC_TABLE which is created above.  

Define a flag variable in the Global data of the function module.  

GOTO à Global Data  

 

In the source code of the function module write the code to update the database table. 

Save the function module and activate it.  

Now LOGON to another SAP system (XYZ) from where we need to send data to the database table in the other SAP system (ABC).  

Write a report program with the following code.  

REPORT ZRFC_REPORT.
TYPES: 
BEGIN OF type_table,
        mandt TYPE mandt,
        id TYPE numc2,
        name TYPE char20,
      END OF type_table.
DATA: 
itab TYPE TABLE OF type_table,
      fs TYPE type_table.
DATA: 
id TYPE numc2 VALUE '4'.
DO 5 TIMES.
  fs-id = id + 1.
  fs-name = 'Name'.
  APPEND fs TO itab.
  id = fs-id.
ENDDO.
LOOP AT itab INTO fs.
  CALL FUNCTION 'ZRFC' DESTINATION 'ABC'
    EXPORTING
      itab = fs.
ENDLOOP.

In the above report we have not used RFC_CONNECTION_CLOSE function module. So if we try with the above code the flag variable which has been declared in the global data of the function module will be set to ‘X’ and the second time loop will not update the database as the flag is not cleared.  

REPORT ZRFC_REPORT.
TYPES: 
      BEGIN OF type_table,
        mandt TYPE mandt,
        id TYPE numc2,
        name TYPE char20,
      END OF type_table.
DATA: 
      itab TYPE TABLE OF type_table,
      fs TYPE type_table.
DATA: 
      id TYPE numc2 VALUE '40'.
DO 5 TIMES.
  fs-id = id + 1.
  fs-name = 'sarves'.
  APPEND fs TO itab.
  id = fs-id.
ENDDO.
LOOP AT itab INTO fs.
  CALL FUNCTION 'YH1317_RFC' DESTINATION 'R3XCLNT100'
    EXPORTING
      itab = fs.
  CALL FUNCTION 'RFC_CONNECTION_CLOSE'
   EXPORTING
     destination                = 'R3XCLNT100'
*     TASKNAME                   =
*   EXCEPTIONS
*     DESTINATION_NOT_OPEN       = 1
*     OTHERS                     = 2
            .
  IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.
ENDLOOP.

When we use the RFC_CONNECTION_CLOSE function module then the connection will be closed and the global data flag will be cleared.  

This will allow you to update the database table with complete records.  

In the same way when we try to upload data using a standard BAPI/RFC, it is a good practice to close the connection and rebuild it as the global data of the function group will be cleared and we can upload error free data into the SAP system.

Please send us your feedback/suggestions at webmaster@SAPTechnical.COM 

HomeContribute About Us Privacy Terms Of Use • Disclaimer • SafeCompanies: Advertise on SAPTechnical.COM | Post JobContact Us  

Graphic Design by Round the Bend Wizards

footer image footer image