
Remote Function Call (RFC) is a powerful feature in SAP ABAP, enabling communication between SAP systems, as well as between SAP systems and external systems. RFCs facilitate remote function execution in a seamless and efficient manner. In this blog, we will explore the different types of RFC calls in SAP ABAP, their use cases, and key considerations.
Let's create a sample RFC function module to modify a custom table.
Row Field name Position Key Data element Domain Datatype Length Domain text
1 | MANDT | 1 | X | MANDT | MANDT | CLNT | 3 | Client |
2 | FNAME | 2 | X | VORNA0 | CHAR15 | CHAR | 15 | Employee's first name |
3 | SNAME | 3 | X | NACHNAME | CHAR25 | CHAR | 25 | Employee's last name |
4 | DOB | 4 | GEBDAT | DATUM | DATS | 8 | Date of birth | |
5 | ADDRESS | 5 | ADRNR_TXT | BEZEI80 | CHAR | 80 | Full details of address |
Code listing for function: ZFM_CREATE_ZBSP001_TAB
Description: FM to create entry to table ZBSP001_TAB
FUNCTION zfm_create_zbsp001_tab. *"---------------------------------------------------------------------- *"*"Local Interface: *" IMPORTING *" VALUE(IM_STRUCT) TYPE ZBSP001_TAB *" EXPORTING *" VALUE(EX_FLG) TYPE CHAR1 *"---------------------------------------------------------------------- * Global data declarations IF im_struct IS NOT INITIAL. MODIFY zbsp001_tab FROM im_struct. IF sy-subrc = 0. ex_flg = abap_true. ELSE. ex_flg = abap_false. ENDIF. ENDIF. ENDFUNCTION.
Definition: Synchronous RFC is the most straightforward type of RFC call. It is a blocking call, meaning the calling program waits until the called remote function module has finished executing and returns the result. No COMMIT WORK needed.
Use Cases:
Considerations:
Sample Program:
*&---------------------------------------------------------------------* *& Report ZTEST_RFC_PROGRAM *&---------------------------------------------------------------------* REPORT ztest_rfc_program. DATA : lv_tid TYPE arfctid, lv_fnum TYPE qretstate-qrfnum, lv_flg TYPE char1, ls_struct TYPE zbsp001_tab. ************************************************** * sRFC ************************************************** ls_struct-mandt = '110'. ls_struct-fname = 'Semual'. ls_struct-sname = 'Stain'. ls_struct-dob = '19620815'. ls_struct-address = 'NewYork'. CALL FUNCTION 'ZFM_CREATE_ZBSP001_TAB' DESTINATION 'NONE' EXPORTING im_struct = ls_struct IMPORTING "importing allowed ex_flg = lv_flg. IF lv_flg = abap_true. WRITE : 'sRFC' COLOR COL_HEADING. WRITE : 'ENTRY CREATED SUCCESSFULLY' COLOR COL_KEY. ELSE. WRITE : 'ENTRY CREATION FAILED' COLOR COL_BACKGROUND. ENDIF. CLEAR : lv_tid, lv_fnum, lv_flg, ls_struct.
Definition: Asynchronous RFC is a non-blocking call, allowing the calling program to continue processing without waiting for the remote function to complete. No COMMIT WORK needed for the call itself, but the called function module may need a commit if it performs updates.
Use Cases:
Considerations:
Sample Program:
*&---------------------------------------------------------------------* *& Report ZTEST_RFC_PROGRAM *&---------------------------------------------------------------------* REPORT ztest_rfc_program. ************************************************** * aRFC ************************************************** DATA : lv_tid TYPE arfctid, lv_fnum TYPE qretstate-qrfnum, lv_flg TYPE char1, ls_struct TYPE zbsp001_tab. ls_struct-mandt = '110'. ls_struct-fname = 'Pablo'. ls_struct-sname = 'Cuelho'. ls_struct-dob = '19620815'. ls_struct-address = 'Oregon'. CALL FUNCTION 'ZFM_CREATE_ZBSP001_TAB' DESTINATION 'NONE' STARTING NEW TASK 'TASK1' PERFORMING return_code ON END OF TASK EXPORTING im_struct = ls_struct. IF sy-subrc = 0. WRITE : 'aRFC call successful' COLOR COL_HEADING. ENDIF. AT USER-COMMAND. IF sy-ucomm = 'OKCD'. IF lv_flg = abap_true. WRITE : 'aRFC' COLOR COL_HEADING. WRITE : 'ENTRY CREATED SUCCESSFULLY' COLOR COL_KEY. CLEAR ls_struct. ELSE. WRITE : 'ENTRY CREATION FAILED' COLOR COL_BACKGROUND. CLEAR ls_struct. ENDIF. ENDIF. CLEAR : lv_tid, lv_fnum, lv_flg, ls_struct.
*&---------------------------------------------------------------------* *& Form return_code *&---------------------------------------------------------------------* FORM return_code USING taskname. RECEIVE RESULTS FROM FUNCTION 'ZFM_CREATE_ZBSP001_TAB' IMPORTING ex_flg = lv_flg. SET USER-COMMAND 'OKCD'. ENDFORM.
Definition: Transactional RFC ensures that the remote function call is executed exactly once, even if there are network issues. It uses a transactional mechanism to guarantee the execution but not in a predefined order. Requires COMMIT WORK to ensure that the function module execution is committed to the database.
Use Cases:
Considerations:
Sample Program:
*&---------------------------------------------------------------------* *& Report ZTEST_RFC_PROGRAM *&---------------------------------------------------------------------* REPORT ztest_rfc_program. DATA : lv_tid TYPE arfctid, lv_fnum TYPE qretstate-qrfnum, lv_flg TYPE char1, ls_struct TYPE zbsp001_tab.
************************************************** * tRFC ************************************************** * Process Once via SM58 ************************************************** ls_struct-mandt = '110'. ls_struct-fname = 'Peter'. ls_struct-sname = 'Semon'. ls_struct-dob = '19620815'. ls_struct-address = 'NewYork'. CALL FUNCTION 'ZFM_CREATE_ZBSP001_TAB' IN BACKGROUND TASK DESTINATION 'NONE' EXPORTING im_struct = ls_struct. * IMPORTING "Background TASK doesn't support IMPORTING * ex_flg = lv_flg. IF sy-subrc = 0. CALL FUNCTION 'ID_OF_BACKGROUNDTASK' EXPORTING dest = 'NONE' IMPORTING tid = lv_tid fnum = lv_fnum. IF lv_tid IS NOT INITIAL. WRITE : 'tRFC' COLOR COL_HEADING. WRITE : 'ENTRY CREATED SUCCESSFULLY - TID : ', lv_tid COLOR COL_KEY. CLEAR ls_struct. COMMIT WORK. "Important to get TID in SM58 view ELSE. WRITE : 'ENTRY CREATION FAILED' COLOR COL_BACKGROUND. CLEAR ls_struct.
ROLLBACK WORK. ENDIF. ENDIF. CLEAR : lv_tid, lv_fnum, lv_flg, ls_struct.
Definition: Queued RFC is an extension of tRFC that processes calls in a predefined order. It ensures that the sequence of function calls is maintained, which is critical for scenarios where order matters. Requires COMMIT WORK to ensure that the function module execution is committed and processed in the correct order.
Use Cases:
Considerations:
Sample Program:
*&---------------------------------------------------------------------* *& Report ZTEST_RFC_PROGRAM *&---------------------------------------------------------------------* REPORT ztest_rfc_program. DATA : lv_tid TYPE arfctid, lv_fnum TYPE qretstate-qrfnum, lv_flg TYPE char1, ls_struct TYPE zbsp001_tab.
************************************************** * qRFC ************************************************** * SMQ1 - Process Outbound Once in Sequence * SMQ2 - Process Inbound Once in Sequence
* For destination 'NONE', check SMQ2 only as SMQ1 will be processed automatically.
" For external systems, once you process SMQ1 - SMQ2 will appear in another system DATA: p_in TYPE trfcqnam VALUE 'ZTEST02_IN', p_out TYPE trfcqnam VALUE 'ZTEST02_OUT', p_rfc TYPE rfcdest VALUE 'NONE'. WRITE : 'qRFC' COLOR COL_HEADING. DO 2 TIMES. CASE sy-index. WHEN 1. ls_struct-mandt = '110'. ls_struct-fname = 'John'. ls_struct-sname = 'Carter'. ls_struct-dob = '19650815'. ls_struct-address = 'Washington'. WHEN 2. ls_struct-mandt = '110'. ls_struct-fname = 'Dr.'. ls_struct-sname = 'Strange'. ls_struct-dob = '19670815'. ls_struct-address = 'Seattle'. ENDCASE. CALL FUNCTION 'TRFC_SET_QIN_PROPERTIES' EXPORTING qout_name = p_out qin_name = p_in EXCEPTIONS invalid_queue_name = 1 OTHERS = 2. IF sy-subrc <> 0. MESSAGE e001(00) WITH 'TRFC_SET_QIN_PROPERTIES error'. ENDIF. call function 'ZFM_CREATE_ZBSP001_TAB' IN BACKGROUND TASK AS SEPARATE UNIT DESTINATION 'NONE' EXPORTING im_struct = ls_struct. IF sy-subrc = 0. CALL FUNCTION 'ID_OF_BACKGROUNDTASK' EXPORTING dest = 'NONE' IMPORTING tid = lv_tid fnum = lv_fnum. IF lv_tid IS NOT INITIAL. WRITE : 'ENTRY CREATED SUCCESSFULLY - TID : ', lv_tid COLOR COL_KEY. CLEAR ls_struct. ELSE. WRITE : 'ENTRY CREATION FAILED' COLOR COL_BACKGROUND. CLEAR ls_struct. ENDIF. ENDIF. ENDDO. COMMIT WORK. IF sy-subrc <> 0. MESSAGE e001(00) WITH 'Commit error with return code:' sy-subrc. ENDIF. CLEAR : lv_tid, lv_fnum, lv_flg, ls_struct.
Understanding the different types of RFC calls in SAP ABAP is essential for designing efficient and reliable applications. Each type of RFC has its own strengths and ideal use cases. By leveraging the appropriate type of RFC call, developers can optimize performance, ensure data consistency, and enhance the robustness of their SAP solutions.
Whether it's synchronous or asynchronous communication, transactional guarantees, or maintaining execution order, RFCs provide a versatile framework for remote function execution in the SAP ecosystem.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
5 | |
4 | |
2 | |
2 | |
2 | |
2 | |
2 | |
2 | |
1 | |
1 |