Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
former_member182455
Active Contributor

Summary:

The purpose of this document is to explain and provide a step-by-step walkthrough to implement Exchange rate files (CSV) Scenario using ABAP code in PI. Also this would give a clear picture how to write the ABAP code in PI.

Introduction: This document will gives clear idea about How to get a CSV file from Web page using ABAP Code in SAP PI. At sender side Customer bank team is updating Exchange rate file in their web page then PI will pick the data and send to ECC.

For this interface, there is no need of creating any sender adapter while there is a need of creation of receiver XI (Proxy) adapter..

Business Scenario: We need to retrieve daily exchange rates from the web page and upload them into
SAP ECC using Proxy communication

Pre-Requisites:

Basic knowledge of XI/PI, XML and ABAP etc…

Assumption      SLD configuration is with Technical System and Business System is done. Product and SWC is created in SLD.

  •     SWC is imported in ESR.
  •     Communication between webpage to ABAP Program and ABAP program to PI need to create.
  •   Create the RFC destination to connect to Web page:

Using Tr Code: SM58 to create the RFC Destination (BANK to PI)

              Provide the details below:

    •   Connection type: G
    • Target host: specify the bank host name
    • Path prefix: Specify the bank path prefix

  • Create the one more RFC Destination to connect to ABAP program to PI and specify the below details
    • Connection type: G
    • Target Host: SAP PI host
    • Service No  : SAP PI Service No
    • Path perfix : /sap/xi/adapter_plain?namespace=XXXXXXXXX&interface=XXXX&service=XXXX&qos=EO       

ABAP Code


REPORT ZTRIGGER_CNB_INTERFACE.

tables RFCDES.data w_rc like sy-subrc.data w_message type string.data w_client type ref to if_http_client.data w_subrc type sy-subrc.data w_wsubrc(10).data w_sytabix(4).data w_errortext type string.data w_timeout type i.data w_response type ref to cl_HTTP_RESPONSE.data w_xrdata type xstring.data w_pclient type ref to if_http_client.
PARAMETERS: p_dest  like rfcdes-rfcdest  obligatory default 'RFC Destination'
                               
matchcode object HTTPDESTINATION.PARAMETERS: p_ldest like rfcdes-rfcdest  obligatory default 'RFC Destination'
                               
matchcode object HTTPDESTINATION.
*at selection-screen on p_dest.
 
select single * from RFCDES where RFCDEST = p_dest
                               
and RFCTYPE = 'G'.
 
if sy-subrc <> 0.
   
message i001(am) with 'HTTP destination' p_dest 'is not correct!'.
 
endif.
at selection-screen on p_ldest.
 
select single * from RFCDES where RFCDEST = p_ldest
                               
and RFCTYPE = 'G'.
 
if sy-subrc <> 0.
    message i001(am) with 'HTTP destination' p_ldest 'is not correct!'.

 
endif.
*start-of-selection.
  w_timeout
= 300.
* If the HTTP connection to CNB website failes, try again => max 10 times
 
do 10 times.
   
write sy-index to w_sytabix.
   
concatenate 'GETTING DATA FROM CNB WEBSITE. Attempt:-' w_sytabix
   
into w_message.
   
write:/ w_message.
   
perform get_data_from_CNB.
   
IF w_rc = 0.
      w_message
= 'XML data from CNB web is fetched successfully'.
     
write:/ w_message.
     
exit.
   
else.
      w_message
= 'XML data from CNB web could not be fetched'.
     
write:/ w_message color col_negative.
     
exit.
   
ENDIF.
 
enddo.

   
write sy-index to w_sytabix.
   
concatenate 'POSTING DATA TO HTTP ADAPTER. Attempt:-' w_sytabix
   
into w_message.
   
write:/ w_message.
   
perform send_data_to_http.
   
IF w_rc = 0.
      w_message
= 'XML data is successfully sent to HTTP adapter'.
     
write:/ w_message.
     
exit.
   
else.
      w_message
= 'XML data could not be sent to HTTP adapter'.
     
write:/ w_message color col_negative.
     
exit.
   
ENDIF.
 
enddo.

form get_data_from_CNB.

 
clear w_rc.* open connection
 
call method cl_http_client=>create_by_destination
   
EXPORTING
      destination             
= p_dest
   
IMPORTING
     
client                  = w_client
   
EXCEPTIONS
      destination_not_found   
= 1
      internal_error         
= 2
      argument_not_found     
= 3
      destination_no_authority
= 4
      plugin_not_active       
= 5
     
others                  = 6.

 
if sy-subrc <> 0.
    w_rc
= sy-subrc.
   
write sy-subrc to w_wsubrc.
   
concatenate
   
'HTTP Connection with CNB website failed. Return code = ' w_wsubrc
     
into w_message separated by space.
   
write:/ w_message color col_negative.
   
exit.
 
else.
    w_message
= 'HTTP Connection with CNB website established'.
   
write:/ w_message.
 
endif.
* Send request - empty
 
call method w_client->send
   
EXPORTING
      timeout                   
= w_timeout
   
EXCEPTIONS
      http_communication_failure
= 1
      http_invalid_state       
= 2
      http_processing_failed   
= 3
     
others                    = 4.

 
if sy-subrc <> 0.
   
call method w_client->get_last_error
     
IMPORTING
       
code    = w_subrc
       
message = w_errortext.

   
write w_subrc to w_wsubrc.
   
concatenate
   
'Communication error while sending request. Return code = ' w_wsubrc
     
'message: ' w_errortext
     
into w_message separated by space.
   
write:/ w_message color col_negative.
    w_rc
= w_subrc.
   
exit.
 
else.
    w_message
= 'Communication with CNB website established'.
   
write:/ w_message.
 
endif.

 
call method w_client->receive
   
EXCEPTIONS
      http_communication_failure
= 1
      http_invalid_state       
= 2
      http_processing_failed   
= 3
     
others                    = 4.

 
if sy-subrc <> 0.
   
call method w_client->get_last_error
     
IMPORTING
       
code    = w_subrc
       
message = w_errortext.

   
write w_subrc to w_wsubrc.
   
concatenate
   
'Communication error while receiving response from CNB website.'
    ' Return code = ' w_wsubrc
      'message: '
w_errortext
     
into w_message separated by space.
   
write:/ w_message color col_negative.
    w_rc
= w_subrc.
   
exit.
 
else.
    w_message = 'Data from CNB website is fetched'.

   
write:/ w_message.
 
endif.
* access response
 
call method w_client->response->if_http_entity~get_data
    RECEIVING
     
DATA = w_xrdata.
* close connection
 
call method w_client->close
   
EXCEPTIONS
      http_invalid_state
= 1
     
others            = 2.

 
if w_xrdata is initial.
    w_rc
= 4.
    w_message
= 'Exchange rate data from CNB website could not be fetched'.
   
write:/ w_message color col_negative.
   
exit.
 
endif.
endform.                    "get_data_from_CNB
FORM send_data_to_http .

 
clear w_rc.
 
call method cl_http_client=>create_by_destination
   
EXPORTING
      destination             
= p_ldest
   
IMPORTING
     
client                  = w_pclient
   
EXCEPTIONS
      destination_not_found   
= 1
      internal_error         
= 2
      argument_not_found     
= 3
      destination_no_authority
= 4
      plugin_not_active       
= 5
     
others                  = 6.

 
IF SY-SUBRC <> 0.
    w_rc
= sy-subrc.
   
write sy-subrc to w_wsubrc.
   
concatenate
   
'HTTP Connection to post data to adapter failed. Return code = ' w_wsubrc
     
into w_message separated by space.
   
write:/ w_message color col_negative.
   
exit.
 
else.
    w_message
= 'Connection with HTTP adapter to post data established'.
   
write:/ w_message.
 
ENDIF.
* request
 
call method w_pclient->REQUEST->if_http_entity~set_data
   
EXPORTING
     
DATA = w_xrdata.
* send data
 
CALL METHOD w_pclient->SEND
   
EXPORTING
      TIMEOUT                   
= w_timeout
   
EXCEPTIONS
      HTTP_COMMUNICATION_FAILURE
= 1
      HTTP_INVALID_STATE       
= 2
      HTTP_PROCESSING_FAILED   
= 3
      HTTP_INVALID_TIMEOUT     
= 4
     
others                    = 5.

 
IF SY-SUBRC <> 0.
   
call method w_pclient->get_last_error
     
IMPORTING
       
code    = w_subrc
       
message = w_errortext.

    w_rc
= w_subrc.
   
write w_subrc to w_wsubrc.
   
concatenate
   
'Communication error while posting data to HTTP adapter. Return code = ' w_wsubrc
     
'message: ' w_errortext
     
into w_message separated by space.
   
write:/ w_message color col_negative.
   
exit.
 
else.

   
COMMIT WORK AND WAIT.

    w_message
= 'Data successfully posted to HTTP adapter'.
   
write:/ w_message.
 
ENDIF.
* close connection
 
call method w_pclient->close
   
EXCEPTIONS
      http_invalid_state
= 1
     
others            = 2.
ENDFORM.                    " send_data_to_http

Integration Repository (IR):

Sender DT: create the DT as per the CSV file

Create the MT and assign the DT type to message type

Create the SI and assign the MT type to Service interface

Create the Target DT, MT and SI

Note: If you need to convert from CSV to XML in the mapping than create the JAVA mapping.

Create the Mapping:

Create the OM

Save and active the IR

ID :

Go to ID part and create the Scenario name and create the Receiver Agreement, Receiver Determination and Interface Determination and receiver channel

Note: no need to create the sender CC ( communication channel and sender agreement ) data is comming directly in to SXMB_MONI inbound messages.

Receiver Agreement: Create this corresponding to Receiver Business service and inbound interface combination and specify communication channel.

Receiver Determination: Configure Receivers for corresponding Sender Service and Outbound Interface combination.

Interface Determination: Specify corresponding Inbound Interface and Interface Mapping for combination of Sender Service and Outbound Interface w.r.t specified Receiver in Receiver Determination.

Go to change list and activate all the ID objects. Integration Directory part is completed

3 Comments
Labels in this area