Skip to Content
Technical Articles

Unboxing Custom Business Object Continued: Calling REST API inside logic

In Continuation to  blog post “Unboxing Custom Business Objects in S/4HANA Cloud where we described some of the key observations while using Custom Business Object. Here we will cover how you can call REST API from CBO logic to read / post any data. Sometimes we may have requirement where we need to update field value of CBO based on other field / in case I need to perform certain action based on the data. Example I can update Sales Order based on action upon CBO data. We might have to use REST APIs to fulfill such requirements. So, can I call REST API from CBO? Answer is yes; we can call REST API from CBO logic.

For using REST API call from CBO logic; you will need Communication arrangement set up in S/4HANA Cloud system with Outbound service in this case.
• Create Communication user using app “Maintain Communication Users
• Create Communication scenario ‘YY1_TEST_CBO’ using app “Custom Communication       Scenario” having Outbound Service ‘YY1_TEST_CBO_REST’ having URL path as   /sap/opu/odata/sap
• Create Communication system “INT_CBO” using app “Communication System” where you need    to give the host name of your system and assign Communication user to the communication       system.Maintain the Outbound user under “Users for Outbound Communication”
• Create Communication Arrangement using app “Communication Arrangement” that uses Communication system INT_CBO AND Communication user created above.
Once the Communication Arrangement is in place and check connection is successful ; you are good to go ahead with the code in CBO logic .

This is sample code on how you can call REST API from Custom field logic/BO once the communication arrangement is in place. Here we have Project Id as field in CBO which is entered by the user. Based on the Project Id we get the Project Name from REST API and fill in the CBO field Project Name
1. First check if the Communication arrangement for REST API we are trying to call exists?
DATA l_v_api TYPE string.
cl_ble_http_client=>is_service_available(
EXPORTING
communication_scenario = ‘YY1_TEST_CBO’
outbound_service = ‘YY1_TEST_CBO_REST’
RECEIVING available = DATA(l_v_data)
) .

2. Create HTTP client to access the outbound service
DATA(lo_client) = cl_ble_http_client=>create(
communication_scenario = ‘YY1_ TEST_CBO’
outbound_service = ‘YY1_ TEST_CBO _REST’
).

3. Creation of the service request–Call to REST API which you plan to use
CONCATENATE ‘/sap/opu/odata/cpd/SC_EXTERNAL_SERVICES_SRV/ProjectSet(‘
”” projectid-projectid1”’)’ INTO l_v_api .

4. Call the REST API by setting the header parameters
DATA(request) = cl_ble_http_request=>create( ).
request->set_header_parameter(
EXPORTING
name = ‘Content-Type’ “Content type the bodies of request and response are formatted as
value = ‘application/json’
)->set_method( ‘GET’ )->set_resource_extension( l_v_api ).

TRY .
DATA(response) = lo_client->send( request ).
CATCH cx_ble_http_exception INTO DATA(l_v_error).
ENDTRY.

5. Get the status code to check if call was success / failure. Status code 200 is okay
This step is optional; in case you want to check status code ; you can use this
response->get_header_parameter( EXPORTING NAME = ‘~status_code’ RECEIVING value =          data(l_v_status) ).

6. Get the response
DATA(l_v_body) = response->get_body( ).

7. Find the value of project Name and fill in the value in CBO Project name field
DATA(l_v_prjtname) = substring_after( val = l_v_body sub = ‘ProjectName>’).
l_v_prjtname = substring_before( val = l_v_prjtname sub = ‘</d:ProjectName’ ).
projectid-projectname = l_v_prjtname.

Summary: In this we explained how REST API can be called from CBO logic in case Communication arrangement is in place And the value returned by REST API can be used to fill in the CBO field value.As we move forward ; In next blog  we will discuss on the options for testing APIs.

Regards
Taranam

1 Comment
You must be Logged on to comment or reply to a post.