a wrapper to call dynamically function via rfc

Upload: hemendra-sabharwal

Post on 12-Oct-2015

18 views

Category:

Documents


1 download

DESCRIPTION

A Wrapper to Call Dynamically Function via RFC

TRANSCRIPT

  • 5/21/2018 A Wrapper to Call Dynamically Function via RFC

    1/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 DynamicallyFunction via RFC

    Applies to:

    Any integration with SAP R/3 system and external applications.

    For more information, visit theABAP 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, VisualBasic, Delphi, and Java and long ago with FORTRAN for mainframes. He has been workingwith the SAP solutions since 2004. He has the experience in IT Services, Automotive,Electronics, Consumer Products, Manufacturing industries. At present he works for thecompany Ozone Consulting JLLC in Republic of Belarus as programmer analyst (ABAP4Developer)

    http://www.sdn.sap.com/irj/sdn/abaphttp://www.sdn.sap.com/irj/sdn/abaphttp://www.sdn.sap.com/irj/sdn/abaphttp://www.sdn.sap.com/irj/sdn/abap
  • 5/21/2018 A Wrapper to Call Dynamically Function via RFC

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

  • 5/21/2018 A Wrapper to Call Dynamically Function via RFC

    3/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 differentERP-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

  • 5/21/2018 A Wrapper to Call Dynamically Function via RFC

    4/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 4

    We Create All Necessary Structures

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

    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:

  • 5/21/2018 A Wrapper to Call Dynamically Function via RFC

    5/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 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:

  • 5/21/2018 A Wrapper to Call Dynamically Function via RFC

    6/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 6

    We Make the Interface of our Function

    Now we create RFC-function Z_RFC_FUNC_CALL with the interface:

  • 5/21/2018 A Wrapper to Call Dynamically Function via RFC

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

    We Write the Wrapper

    This is the code of wrapper:

    FUNCTIONZ_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 TYPEabap_func_parmbind_tab ,ls_para TYPEabap_func_parmbind ,lt_err TYPEabap_func_excpbind_tab ,ls_err TYPEabap_func_excpbind ,ld_dref TYPEREFTOdata ,ld_err TYPEREFTOdata ,

    ls_content TYPEstring ,lcl_conv TYPEREFTOcl_abap_conv_in_ce ,lv_len TYPEi ,lt_parameters TYPEzrfc_par_t ,ls_parameters TYPEzrfc_par_s ,lt_err_messages TYPEzrfc_err_t ,ls_err_messages TYPEzrfc_err_s .

    field-symbols : TYPEabap_func_parmbind , TYPEabap_func_excpbind , TYPEzrfc_par_s , TYPEzrfc_err_s , TYPEany .

    * ----------------------------* Check Name Of Function* ----------------------------CALLFUNCTION'FUNCTION_EXISTS'EXPORTINGFUNCNAME = IM_FNAME

    EXCEPTIONSFUNCTION_NOT_EXIST = 1OTHERS = 2.

    IFSY-SUBRC 0.messagee651(fl) withim_fname raisingfunction_not_exist.

    ENDIF.* -----------------------------* Create Data according to type

    * -----------------------------loopatCH_PARAMETERS assigning .TRY.

    if-par_kind = abap_func_tables .CREATEDATAld_dref TYPESTANDARDTABLEOF(-par_type).

    else.CREATEDATAld_dref TYPE(-par_type).

    endif.unassign .ASSIGNld_dref->* TO .ls_para-value:= ld_dref .

  • 5/21/2018 A Wrapper to Call Dynamically Function via RFC

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

    ifnot-par_value isinitial.* Read Internal Table

    TRY.CALLTRANSFORMATION ('ID')

    SOURCE XML -par_valueRESULT tab = .

    * Read flat structureCATCHcx_transformation_error .lcl_conv := CL_ABAP_CONV_IN_CE=>CREATE( input= -

    par_value ).lcl_conv->READ( importingdata= ls_content len = lv_len ). := ls_content .

    ENDTRY.endif.ls_para-kind := -par_kind .ls_para-name := -par_name .appendls_para tolt_para .

    CATCHcx_sy_create_data_error.message'Parameters have a wrong data!'

    type'E'raisingwrong_data_in_parameters .

    ENDTRY.endloop.

    * Set Table Of ErrorsloopatCH_ERR_MESSAGES assigning .ls_err-value := -err_value .ls_err-name := -err_name .insertls_err intotablelt_err .

    endloop.* ----------------------------* Start of Function* ----------------------------

    callfunctionIM_FNAMEPARAMETER-TABLElt_para

    EXCEPTION-TABLElt_err.

    unassign: , , .

    * ----------------------------* Return result* ----------------------------

    loopatlt_para assigning .unassign .clearls_parameters.

    * field 'par_value'ASSIGN-value->* TO .CALLTRANSFORMATION ('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'

    readtablech_parametersassigning

    withkeypar_name = -name .ls_parameters-par_type := -par_type .

  • 5/21/2018 A Wrapper to Call Dynamically Function via RFC

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

    appendls_parameters tolt_parameters .

    endloop.loopatlt_err assigning .

    * ERR_MESSAGE REF TO DATA (not yet at the moment).ls_err_messages-err_value := -value.ls_err_messages-err_name := -name .appendls_err_messages tolt_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 TYPErs38l_fnam value'READ_SPFLI_INTO_TABLE' ,lv_drfc TYPErfcdisplay-rfctytext value'type_our_destin' ,

    lt_parameters TYPEzrfc_par_t ,ls_parameters TYPEzrfc_par_s ,lt_err_mess TYPEzrfc_err_t ,ls_err_mess TYPEzrfc_err_s ,lv_carrier TYPEstring ,lt_spfli TYPEspfli_tab ,ls_content TYPEstring ,lcl_conv TYPEREFTOcl_abap_conv_in_ce ,lv_len TYPEi ,ls_carrid TYPEspfli-carrid .

    start-of-selection .

    * -------------------------* Set parameter 1 - 'ID'* -------------------------lv_carrier = 'LH'.ls_parameters-par_name = 'ID'.ls_parameters-par_kind = ABAP_FUNC_EXPORTING.CALLFUNCTION'SCMS_STRING_TO_XSTRING'EXPORTINGTEXT = lv_carrier

    IMPORTINGBUFFER = ls_parameters-par_value .

  • 5/21/2018 A Wrapper to Call Dynamically Function via RFC

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

    ls_parameters-par_type = 'SPFLI-CARRID'.appendls_parameters tolt_parameters .

    * --------------------------* Set parameter 2 - 'ITAB'* --------------------------ls_parameters-par_name = 'ITAB'.ls_parameters-par_kind = ABAP_FUNC_IMPORTING.

    CALLTRANSFORMATION ('ID')

    SOURCE tab = lt_spfliRESULT XML ls_parameters-par_value .

    ls_parameters-par_type = 'SPFLI_TAB'.appendls_parameters tolt_parameters .* ---------------------------* The possible exceptions* ---------------------------ls_err_mess-err_name = 'NOT_FOUND'.ls_err_mess-err_value = 1.insertls_err_mess intotablelt_err_mess.

    ls_err_mess-err_name = 'OTHERS'.ls_err_mess-err_value = 4.insertls_err_mess intotablelt_err_mess.

    CALLFUNCTION'Z_RFC_FUNC_CALL'destination lv_drfcEXPORTINGIM_FNAME = lv_fname

    CHANGINGCH_PARAMETERS = lt_parametersCH_ERR_MESSAGES = lt_err_mess

    EXCEPTIONSFUNCTION_NOT_EXIST = 1WRONG_DATA_IN_PARAMETERS = 2OTHERS = 3 .

    IFSY-SUBRC 0.MESSAGEIDSY-MSGID

    TYPESY-MSGTYNUMBERSY-MSGNO

    WITHSY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.ENDIF.* --------------------------* Read parameter 1 - 'ID'* --------------------------readtablelt_parameters

    intols_parameters

    withkeypar_name = 'ID'.TRY.CALLTRANSFORMATION ('ID')

    SOURCE XML ls_parameters-par_valueRESULT tab = ls_carrid .

    CATCHcx_transformation_error .message'Parameters have a wrong data!'TYPE'I'.

    Endtry.

    * --------------------------* Read parameter 2 - 'ITAB'* --------------------------readtablelt_parameters

    intols_parameters

  • 5/21/2018 A Wrapper to Call Dynamically Function via RFC

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

    withkeypar_name = 'ITAB'.TRY.CALLTRANSFORMATION ('ID')

    SOURCE XML ls_parameters-par_valueRESULT tab = lt_spfli .

    CATCHcx_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.

  • 5/21/2018 A Wrapper to Call Dynamically Function via RFC

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

    Related Content

    SCN ABAP Articles

    For more information, visit theABAP homepage.

    http://www.sdn.sap.com/irj/scn/articles-abap-allhttp://www.sdn.sap.com/irj/scn/articles-abap-allhttp://www.sdn.sap.com/irj/sdn/abaphttp://www.sdn.sap.com/irj/sdn/abaphttp://www.sdn.sap.com/irj/sdn/abaphttp://www.sdn.sap.com/irj/sdn/abaphttp://www.sdn.sap.com/irj/scn/articles-abap-all
  • 5/21/2018 A Wrapper to Call Dynamically Function via RFC

    13/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 13

    Disclaimer and Liability Notice

    This document may discuss sample coding or other information that does not include SAP official interfaces and therefore is notsupported 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 orcode sample, including any liability resulting from incompatibility between the content within this document and the materials andservices offered by SAP. You agree that you will not hold, or seek to hold, SAP responsible or liable with respect to the content of thisdocument.