• No results found

A Wrapper to Call Dynamically Function via RFC

N/A
N/A
Protected

Academic year: 2021

Share "A Wrapper to Call Dynamically Function via RFC"

Copied!
13
0
0

Loading.... (view fulltext now)

Full text

(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)

(2)

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

(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

(4)

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.

(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:

(6)

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

(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 :

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

(8)

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 <fs_rfc_par_s>-par_value is initial .

* Read Internal Table

TRY .

CALL TRANSFORMATION ('ID')

SOURCE XML <fs_rfc_par_s>-par_value RESULT tab = <fs_any> .

* Read flat structure

CATCH cx_transformation_error .

lcl_conv := CL_ABAP_CONV_IN_CE=>CREATE( input = <fs_rfc_par_s>-par_value ).

lcl_conv->READ( importing data = ls_content len = lv_len ). <fs_any> := ls_content .

ENDTRY. endif.

ls_para-kind := <fs_rfc_par_s>-par_kind . ls_para-name := <fs_rfc_par_s>-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 <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 .

(9)

append ls_parameters to lt_parameters . 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.

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

(10)

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

(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

(12)

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

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

References

Related documents

Simplify deferring revenues or distributing expenses with the ability to make entries for future periods within General Ledger, Receivables Management, Payables Management,

Efforts to implement evidence-based policies in tobacco control over the past 40 years have changed social norms, countered tobacco industry marketing, enhanced tobacco

By coupling analysis of pathogens and pharmaceuticals in geographically dispersed samples in eThekwini, this study reveals a range of human and environmental health hazards in

makes no representations or warranties with respect to the accuracy or completeness of the contents of this document, and assumes no liability of any kind, including the

Neither the United States Government nor any agency Thereof, nor any of their employees, makes any warranty, express or implied, or assumes any legal liability or responsibility

Neither the United States Government nor any agency Thereof, nor any of their employees, makes any warranty, express or implied, or assumes any legal liability or responsibility

Association assumes no responsibility or liability for costs incurred by the bidder prior to the effective date of any contract resulting from this RFQ.. The bidder may be required

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