Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Applies to:
Any integration with SAP R/3 system and external applications.
For more information, visit the ABAP homepage.
Summary
This article explains how to write a wrapper to call dynamically any function via RFC.
Author Bio
Oleg Zilin started his IT career more than 30 years ago. He programmed with COBOL, Visual
Basic, Delphi, and Java and long ago with FORTRAN for mainframes. He has been working
with the SAP solutions since 2004. He has the experience in IT Services, Automotive,
Electronics, Consumer Products, Manufacturing industries. At present he works for the
company „Ozone Consulting JLLC‟ in Republic of Belarus as programmer analyst (ABAP4
Developer)
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 1
A Wrapper to Call Dynamically Function via RFC
Table of Contents
Introduction ......................................................................................................................................................... 3
We Create All Necessary Structures .............................................................................................................. 4
We Make the Interface of our Function ........................................................................................................... 6
We Write the Wrapper..................................................................................................................................... 7
Example of Usage of the Wrapper .................................................................................................................. 9
Conclusion ........................................................................................................................................................ 11
Related Content ................................................................................................................................................ 12
Disclaimer and Liability Notice .......................................................................................................................... 13
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 2
A Wrapper to Call Dynamically Function via RFC
Introduction
Once for me it was necessary to develop the program, this had to collect and process data from different
ERP-systems.
Here I used calls of functions via RFC. All functions had different interfaces.
In order to facilitate the development, I decided to write a RFC wrapper for the dynamic call of functions.
You can see the diagram of the wrapper below:
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 3
A Wrapper to Call Dynamically Function via RFC
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 4
A Wrapper to Call Dynamically Function via RFC
As we see, the basic difficulty for the use RFC-access is the type - “ref to of data”
Therefore we will make our types:
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 5
A Wrapper to Call Dynamically Function via RFC
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 6
A Wrapper to Call Dynamically Function via RFC
data :
lt_para TYPE abap_func_parmbind_tab ,
ls_para TYPE abap_func_parmbind ,
lt_err TYPE abap_func_excpbind_tab ,
ls_err TYPE abap_func_excpbind ,
ld_dref TYPE REF TO data ,
ld_err TYPE REF TO data ,
ls_content TYPE string ,
lcl_conv TYPE REF TO cl_abap_conv_in_ce ,
lv_len TYPE i ,
lt_parameters TYPE zrfc_par_t ,
ls_parameters TYPE zrfc_par_s ,
lt_err_messages TYPE zrfc_err_t ,
ls_err_messages TYPE zrfc_err_s .
field-symbols :
<fs_para_s> TYPE abap_func_parmbind ,
<fs_err_s> TYPE abap_func_excpbind ,
<fs_rfc_par_s> TYPE zrfc_par_s ,
<fs_rfc_err_s> TYPE zrfc_err_s ,
<fs_any> TYPE any .
* ----------------------------
* Check Name Of Function
* ----------------------------
CALL FUNCTION 'FUNCTION_EXISTS'
EXPORTING
FUNCNAME = IM_FNAME
EXCEPTIONS
FUNCTION_NOT_EXIST = 1
OTHERS = 2.
IF SY-SUBRC <> 0.
message e651(fl) with im_fname raising function_not_exist.
ENDIF.
* -----------------------------
* Create Data according to type
* -----------------------------
loop at CH_PARAMETERS assigning <fs_rfc_par_s> .
TRY.
if <fs_rfc_par_s>-par_kind = abap_func_tables .
CREATE DATA ld_dref TYPE STANDARD TABLE OF (<fs_rfc_par_s>-par_type).
else.
CREATE DATA ld_dref TYPE (<fs_rfc_par_s>-par_type).
endif.
unassign <fs_any> .
ASSIGN ld_dref->* TO <fs_any> .
ls_para-value := ld_dref .
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 7
A Wrapper to Call Dynamically Function via RFC
CATCH cx_sy_create_data_error.
message 'Parameters have a wrong data!'
type 'E'
raising wrong_data_in_parameters .
ENDTRY.
endloop .
* Set Table Of Errors
loop at CH_ERR_MESSAGES assigning <fs_rfc_err_s> .
ls_err-value := <fs_rfc_err_s>-err_value .
ls_err-name := <fs_rfc_err_s>-err_name .
insert ls_err into table lt_err .
endloop .
* ----------------------------
* Start of Function
* ----------------------------
call function IM_FNAME
PARAMETER-TABLE
lt_para
EXCEPTION-TABLE
lt_err.
unassign:
<fs_rfc_par_s> ,
<fs_rfc_err_s> ,
<fs_any> .
* ----------------------------
* Return result
* ----------------------------
loop at lt_para assigning <fs_para_s> .
unassign <fs_any> .
clear ls_parameters.
* field 'par_value'
ASSIGN <fs_para_s>-value->* TO <fs_any> .
CALL TRANSFORMATION ('ID')
SOURCE tab = <fs_any>
RESULT XML ls_parameters-par_value .
* field 'par_kind'
ls_parameters-par_kind := <fs_para_s>-kind .
* field 'par_name'
ls_parameters-par_name := <fs_para_s>-name .
* field 'par_type'
read table ch_parameters
assigning <fs_rfc_par_s>
with key par_name = <fs_para_s>-name .
ls_parameters-par_type := <fs_rfc_par_s>-par_type .
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 8
A Wrapper to Call Dynamically Function via RFC
endloop.
loop at lt_err assigning <fs_err_s> .
* ERR_MESSAGE REF TO DATA (not yet at the moment).
ls_err_messages-err_value := <fs_err_s>-value .
ls_err_messages-err_name := <fs_err_s>-name .
append ls_err_messages to lt_err_messages .
endloop.
CH_PARAMETERS[] := lt_parameters[] .
CH_ERR_MESSAGES[] := lt_err_messages[].
ENDFUNCTION.
Note: We used here „Id‟ transformation, but it‟s possible to write own format of the conversion of table into Xstring.
REPORT Z_RFC_START_FUNCT.
type-pools : abap .
data :
lv_fname TYPE rs38l_fnam value 'READ_SPFLI_INTO_TABLE' ,
lv_drfc TYPE rfcdisplay-rfctytext value 'type_our_destin' ,
lt_parameters TYPE zrfc_par_t ,
ls_parameters TYPE zrfc_par_s ,
lt_err_mess TYPE zrfc_err_t ,
ls_err_mess TYPE zrfc_err_s ,
lv_carrier TYPE string ,
lt_spfli TYPE spfli_tab ,
ls_content TYPE string ,
lcl_conv TYPE REF TO cl_abap_conv_in_ce ,
lv_len TYPE i ,
ls_carrid TYPE spfli-carrid .
start-of-selection .
* -------------------------
* Set parameter 1 - 'ID'
* -------------------------
lv_carrier = 'LH'.
ls_parameters-par_name = 'ID'.
ls_parameters-par_kind = ABAP_FUNC_EXPORTING.
CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
EXPORTING
TEXT = lv_carrier
IMPORTING
BUFFER = ls_parameters-par_value .
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 9
A Wrapper to Call Dynamically Function via RFC
ls_parameters-par_type = 'SPFLI-CARRID' .
append ls_parameters to lt_parameters .
* --------------------------
* Set parameter 2 - 'ITAB'
* --------------------------
ls_parameters-par_name = 'ITAB'.
ls_parameters-par_kind = ABAP_FUNC_IMPORTING.
ls_err_mess-err_name = 'OTHERS'.
ls_err_mess-err_value = 4.
insert ls_err_mess into table lt_err_mess.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID
TYPE SY-MSGTY
NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
* --------------------------
* Read parameter 1 - 'ID'
* --------------------------
read table lt_parameters
into ls_parameters
with key par_name = 'ID' .
TRY .
CALL TRANSFORMATION ('ID')
SOURCE XML ls_parameters-par_value
RESULT tab = ls_carrid .
CATCH cx_transformation_error .
message 'Parameters have a wrong data!' TYPE 'I'.
Endtry.
* --------------------------
* Read parameter 2 - 'ITAB'
* --------------------------
read table lt_parameters
into ls_parameters
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 10
A Wrapper to Call Dynamically Function via RFC
CATCH cx_transformation_error .
message 'Parameters have a wrong data!' TYPE 'I'.
Endtry.
The result of calling you can see in the internal table ‘lt_spfli’.
Conclusion
Thus we can call dynamically practically any function.
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 11
A Wrapper to Call Dynamically Function via RFC
Related Content
SCN ABAP Articles
For more information, visit the ABAP homepage.
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 12
A Wrapper to Call Dynamically Function via RFC
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 13