A Wrapper to Call Dynamically Function via RFC

13
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 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: Oleg Zilin Company: Ozone Consulting JLLC, Republic of Belarus Created on: 20 August 2010 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)

description

A Wrapper to Call Dynamically Function via RFC

Transcript of 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 1

    A Wrapper to Call Dynamically

    Function via RFC

    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: Oleg Zilin

    Company: Ozone Consulting JLLC, Republic of Belarus

    Created on: 20 August 2010

    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)

  • 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 2

    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

  • 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 3

    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:

    We will transfer the parameters into our wrapper in this way:

    Input parameters -

    Internal tables - XML Xstring

    Flat structures - Xstring

    Output parameters -

    Internal tables - XML Xstring

    Flat structures - XML Xstring

  • 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

    We Create All Necessary Structures

    As we know, as it is written in ABAP keyword documentation, function can be dynamically called in this way:

    DATA: func TYPE string,

    ptab TYPE abap_func_parmbind_tab,

    ptab_line TYPE abap_func_parmbind,

    etab TYPE abap_func_excpbind_tab,

    etab_line TYPE abap_func_excpbind.

    CALL FUNCTION func

    PARAMETER-TABLE

    ptab

    EXCEPTION-TABLE

    etab.

    Types for the call are:

  • 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 5

    As we see, the basic difficulty for the use RFC-access is the type - ref to of data

    Therefore we will make our types:

    Finally, the type for the handling of the exceptions is:

  • 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

    We Make the Interface of our Function

    Now we create RFC-function Z_RFC_FUNC_CALL with the interface:

  • 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 7

    We Write the Wrapper

    This is the code of wrapper:

    FUNCTION Z_RFC_FUNC_CALL. *"---------------------------------------------------------------------- *"*"Local Interface: *" IMPORTING *" VALUE(IM_FNAME) TYPE RS38L_FNAM *" CHANGING *" VALUE(CH_PARAMETERS) TYPE ZRFC_PAR_T OPTIONAL *" VALUE(CH_ERR_MESSAGES) TYPE ZRFC_ERR_T OPTIONAL *" EXCEPTIONS *" FUNCTION_NOT_EXIST *" WRONG_DATA_IN_PARAMETERS *"---------------------------------------------------------------------- type-pools : abap . 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 : TYPE abap_func_parmbind , TYPE abap_func_excpbind , TYPE zrfc_par_s , TYPE zrfc_err_s , 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 . TRY. if -par_kind = abap_func_tables . CREATE DATA ld_dref TYPE STANDARD TABLE OF (-par_type). else. CREATE DATA ld_dref TYPE (-par_type). endif. unassign . ASSIGN ld_dref->* TO . ls_para-value := ld_dref .

  • 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 8

    if not -par_value is initial . * Read Internal Table TRY . CALL TRANSFORMATION ('ID') SOURCE XML -par_value RESULT tab = . * Read flat structure CATCH cx_transformation_error . lcl_conv := CL_ABAP_CONV_IN_CE=>CREATE( input = -par_value ). lcl_conv->READ( importing data = ls_content len = lv_len ). := ls_content . ENDTRY. endif. ls_para-kind := -par_kind . ls_para-name := -par_name . append ls_para to lt_para . 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 . ls_err-value := -err_value . ls_err-name := -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: , , . * ---------------------------- * Return result * ---------------------------- loop at lt_para assigning . unassign . clear ls_parameters. * field 'par_value' ASSIGN -value->* TO . CALL TRANSFORMATION ('ID') SOURCE tab = RESULT XML ls_parameters-par_value . * field 'par_kind' ls_parameters-par_kind := -kind . * field 'par_name' ls_parameters-par_name := -name . * field 'par_type' read table ch_parameters assigning with key par_name = -name . ls_parameters-par_type := -par_type .

  • 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 9

    append ls_parameters to lt_parameters . endloop. loop at lt_err assigning . * ERR_MESSAGE REF TO DATA (not yet at the moment). ls_err_messages-err_value := -value . ls_err_messages-err_name := -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 its possible to write own format of the conversion of table into Xstring.

    Example of Usage of the Wrapper

    We will try to call the function "READ_SPFLI_INTO_TABLE".

    In this function Processing Type is Normal Function Module.

    It means that directly RFC-access is impossible, we call this function via our wrapper.

    This is an example of an ABAP4 code sample:

    *&---------------------------------------------------------------------* *& Report Z_RFC_START_FUNCTION *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* 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 .

  • 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 10

    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. CALL TRANSFORMATION ('ID') SOURCE tab = lt_spfli RESULT XML ls_parameters-par_value . ls_parameters-par_type = 'SPFLI_TAB' . append ls_parameters to lt_parameters . * --------------------------- * The possible exceptions * --------------------------- ls_err_mess-err_name = 'NOT_FOUND'. ls_err_mess-err_value = 1. insert ls_err_mess into table lt_err_mess. ls_err_mess-err_name = 'OTHERS'. ls_err_mess-err_value = 4. insert ls_err_mess into table lt_err_mess. CALL FUNCTION 'Z_RFC_FUNC_CALL' destination lv_drfc EXPORTING IM_FNAME = lv_fname CHANGING CH_PARAMETERS = lt_parameters CH_ERR_MESSAGES = lt_err_mess EXCEPTIONS FUNCTION_NOT_EXIST = 1 WRONG_DATA_IN_PARAMETERS = 2 OTHERS = 3 . 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

  • 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 11

    with key par_name = 'ITAB' . TRY . CALL TRANSFORMATION ('ID') SOURCE XML ls_parameters-par_value RESULT tab = lt_spfli . 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.

  • 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 12

    Related Content

    SCN ABAP Articles

    For more information, visit the ABAP homepage.

  • 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

    Disclaimer and Liability Notice

    This document may discuss sample coding or other information that does not include SAP official interfaces and therefore is not supported by SAP. Changes made based on this information are not supported and can be overwritten during an upgrade.

    SAP will not be held liable for any damages caused by using or misusing the information, code or methods suggested in this document, and anyone using these methods does so at his/her own risk.

    SAP offers no guarantees and assumes no responsibility or liability of any type with respect to the content of this technical article or code sample, including any liability resulting from incompatibility between the content within this document and the materials and services offered by SAP. You agree that you will not hold, or seek to hold, SAP responsible or liable with respect to the content of this document.