Understanding Different Types of RFC Calls in SAP ABAP

Estimated read time 14 min read

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.

ZBSP001_TAB – Table for BSP records

Row Field name Position Key Data element Domain Datatype Length Domain text

1MANDT1XMANDTMANDTCLNT3Client2FNAME2XVORNA0CHAR15CHAR15Employee’s first name3SNAME3XNACHNAMECHAR25CHAR25Employee’s last name4DOB4 GEBDATDATUMDATS8Date of birth5ADDRESS5 ADRNR_TXTBEZEI80CHAR80Full 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.

NOTE : While calling this FM, I’ll be using destination ‘NONE’ i.e., within same system. For external systems, kindly use SM59 RFC Destination.

1. Synchronous RFC (sRFC)

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:

Real-time data retrieval or updates where immediate feedback is required.Transactions requiring immediate acknowledgment of completion.

Considerations:

Can lead to performance bottlenecks if the remote function execution time is long.Network issues can directly impact the calling program’s execution.

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.

2. Asynchronous RFC (aRFC)

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:

Processes that can run in parallel without immediate dependency on the result.Long-running background processes.

Considerations:

Handling callback routines to process results once the remote function completes.Suitable for improving performance by parallelizing tasks.

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.

3. Transactional RFC (tRFC)

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:

Financial transactions or critical updates where execution must be guaranteed.Scenarios requiring a rollback mechanism in case of failure.

Considerations:

Requires explicit commit work to trigger the execution.Ensures data consistency and reliability.

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.

SM58 view:

4. Queued RFC (qRFC)

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:

Logistics or supply chain processes where sequential execution is crucial.Scenarios where dependent transactions must be executed in a specific order.

Considerations:

Maintains the order of execution, ensuring data integrity.Can introduce delays due to queue processing times.

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.

SMQ2 view:

SMQS and SMQR can be further used to manage and monitor the status of RFC queues.

Conclusion

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.

 

​ 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.ZBSP001_TAB – Table for BSP recordsRow Field name Position Key Data element Domain Datatype Length Domain text1MANDT1XMANDTMANDTCLNT3Client2FNAME2XVORNA0CHAR15CHAR15Employee’s first name3SNAME3XNACHNAMECHAR25CHAR25Employee’s last name4DOB4 GEBDATDATUMDATS8Date of birth5ADDRESS5 ADRNR_TXTBEZEI80CHAR80Full details of addressCode listing for function: ZFM_CREATE_ZBSP001_TABDescription: FM to create entry to table ZBSP001_TABFUNCTION 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.NOTE : While calling this FM, I’ll be using destination ‘NONE’ i.e., within same system. For external systems, kindly use SM59 RFC Destination.1. Synchronous RFC (sRFC)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:Real-time data retrieval or updates where immediate feedback is required.Transactions requiring immediate acknowledgment of completion.Considerations:Can lead to performance bottlenecks if the remote function execution time is long.Network issues can directly impact the calling program’s execution.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.2. Asynchronous RFC (aRFC)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:Processes that can run in parallel without immediate dependency on the result.Long-running background processes.Considerations:Handling callback routines to process results once the remote function completes.Suitable for improving performance by parallelizing tasks.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.3. Transactional RFC (tRFC)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:Financial transactions or critical updates where execution must be guaranteed.Scenarios requiring a rollback mechanism in case of failure.Considerations:Requires explicit commit work to trigger the execution.Ensures data consistency and reliability.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.SM58 view:4. Queued RFC (qRFC)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:Logistics or supply chain processes where sequential execution is crucial.Scenarios where dependent transactions must be executed in a specific order.Considerations:Maintains the order of execution, ensuring data integrity.Can introduce delays due to queue processing times.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.SMQ2 view:SMQS and SMQR can be further used to manage and monitor the status of RFC queues.ConclusionUnderstanding 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.   Read More Application Development Blog Posts articles 

#SAP

You May Also Like

More From Author

+ There are no comments

Add yours