Co-author
prasanthpadmanabhan.menon
Introduction
Let’s look at the customer scenario in Two-Tier ERP where Subsidiary is on Cloud and Headquarter is on ECC or SAP S/4HANA. There is need to have real time data exchange between the two system to take a business decision. For example, there could be a requirement where in 2EJ scope item customer want to check the status of the sales order document and based on status allow change of Purchase Order. This blog will provide detailed information on how data is exchanged in real time between two systems.
Technical set up
According to the requirement we have, for making the status check, an API call should be made to the SAP S/4HANA On Premise system from the SAP S/4HANA Cloud system.
This requires some prerequisite setup. This includes,
- Set up a virtual host for the SAP S/4HANA On Premise API via SAP Cloud Connector.
- Create a communication system to connect to the BTP subaccount
- Enable the communication arrangement SAP_COM_0200 in the S/4HANA Cloud system
- Create a custom communication scenario to call the SAP S/4HANA On Premise API via the virtual host
- Create a communication system with and Outbound User having the credentials of the SAP S/4HANA On Premise API
- Create a communication arrangement for the custom communication arrangement which uses the communication system created in step 5
1. Set up a virtual host for the SAP S/4HANA On Premise API via SAP Cloud Connector
- After setting up SAP Cloud connector in your system as explained in the blog, setup the On- Premise hostname to Virtual hostname map on the cloud connector as shown.
Make sure that the cloud connector is connected to your SAP BTP subaccount and is in operational status by selecting the Subaccount.
If the internal host has an http redirection in place, maintain the mapping as follows
After the cloud connector setup is finished, you can log in to your SAP BTP account and go to the section Connectivity -> SAP Cloud Connector to verify that SAP BTP is connected to the SAP Cloud Connector
2. Create a Communication System to connect to the BTP Subaccount
This can be done via the following steps
- Login to the SAP S/4HANA Cloud system and select the tile Communication Systems
- Click on new and provide a system name, for example ‘BTP_CONNECTION’ and click on create
- Under the Technical Data section, Provide the Host Name and UI Host Name as hana.ondemand.com under General (You can provide hanatrial.ondemand.com in case you are connecting to a BTP trial account)
- Scroll down to Users for Outbound Connection. Click on Add User and maintain the Username and Password for the BTP Subaccount
3. Enable the communication arrangement SAP_COM_0200 in the S/4HANA Cloud system
This can be done via the following steps
- Login to the SAP S/4HANA Cloud system and select the tile Communication Arrangements
- Click on new and search for the Communication Scenario SAP_COM_0200
- Select this Communication Scenario and click on Create
- Under Additional Properties, maintain the Account Name as the Subaccount Technical Name
- Save the Communication Arrangement
Add a new service channel for On-Premise to Cloud of type ABAP Cloud System in the Cloud connector from Step 1 after the communication arrangement is activated
Local Instance Number can be any number between 0-99
4. Create a custom communication scenario to call the SAP S/4HANA On Premise API via the virtual host
This can be done via the following steps
- Login to the SAP S/4HANA Cloud system and select the tile Custom Communication Scenarios
- Click on New and Provide a name for the Communication Scenario, for example ‘STATUS_READ_SO’ and click on New
Go to the Outbound Services Tab and click on Add. In the pop-up, provide a description, for example ‘Sales Order Read’, and give the URL Path as ‘/sap/opu/odata/sap/API_SALES_ORDER_SRV/’
- Publish the Custom Communication Scenario
- Upon Publish, the status of the Custom Communication Scenario will change to Published
5. Create a communication system with and Outbound User having the credentials of the SAP S/4HANA On Premise API
This can be done via the following steps
- Login to the SAP S/4HANA Cloud system and select the tile Communication Systems
- Click on new and provide a system name, for example ‘<SID>_OP and click on create
- Under the Technical Data section, Provide the Host Name and UI Host Name as the hostname for the virtual host created in SAP Cloud connector
Maintain the SCC Location ID as the Location ID of the SAP Cloud Connector
- Click on Create and click on Save to Save the Communication system
6. Create a communication arrangement for the custom communication arrangement which uses the communication system created in step 5
This can be done via the following steps
- Login to the SAP S/4HANA Cloud system and select the tile Communication Arrangements
- Click on new and search for the Communication Scenario YY1_STATUS_READ_SO
- Select this Communication Scenario and click on Create
- Provide the Communication System created in Step 5 as the communication System and maintain the Port ID as the Port ID created for the virtual host in step 1
- Save and click on check connection to test if the connection is successful.
BADI
Once the communication arrangement is set up, we will see how to call the API in PO BADI.
We will use the sales order API to read the item status
Below is the sales order with overall status as completed, we want to fetch the status of the sales order on cloud.
API Call
We will implement the cloud BADI as below
* Structure for messages
DATA: ls_message LIKE LINE OF messages.
*Implement a check if the outbound service is available
CHECK cl_ble_http_client=>is_service_available(
communication_scenario = 'YY1_STATUS_READ_SO'
outbound_service = 'YY1_CIAPI_REST'
) = abap_true.
*Implement creation of HTTP client
DATA(lo_client) = cl_ble_http_client=>create(
communication_scenario = 'YY1_STATUS_READ_SO'
outbound_service = 'YY1_CIAPI_REST'
).
DATA lv_request_body TYPE string.
* Creation of the service request
DATA(request) = cl_ble_http_request=>create( ).
* method that is used for the service call
request->set_method( 'GET' ).
*Implement error handling
TRY .
* Send a request and receive a response.
DATA(response) = lo_client->send( request ).
* Get the body of the response.
DATA(lv_body) = response->get_body( ).
CATCH cx_ble_http_exception INTO DATA(lx).
* The http status code can be checked.
CASE lx->status_code.
WHEN 404.
* Error handling
WHEN OTHERS.
* Error handling
ENDCASE.
ENDTRY.
Once the data is available in lv_body we can extract the status and write the code in Badi to check the status and display relevant warning/error message
If BTP is on Cloud Foundry, then you would need to use communication arrangement SAP_COM_0574 instead of SAP_COM_0200. We will soon Publish the blog for same.
Conclusion
This blog post should help you to understand in detail about how to make an API call from Cloud to SAP S/4HANA to retrieve information on real time.
Thanks for reading this blog post, hopefully the blog post was informative.
For more information
https://developers.sap.com/tutorials/abap-extensibility-cbo-execute-outbound-service.html