Execute an Outbound Service from Custom Business Object Logic
- How to get needed service data from SAP API Business Hub Sandbox
- How to configure outbound service connection in SAP S/4HANA Cloud system
- How to call and process an outbound service in custom business object logic
Prerequisites
Authorizations: Your user needs
- (a) business role(s) with business catalogs Extensibility (ID: SAP_CORE_BC_EXT
) and Communication Management (ID: SAP_CORE_BC_COM
) in your S/4HANA Cloud system
- access to SAP API Business Hub.
Example Objects: Existence of custom business object Bonus Entitlement
as described in this tutorial (Blog)
Knowledge: (optional) Tutorial: Getting started with the SAP API Business Hub
The example application of Bonus Entitlement
will be enhanced by a feedback functionality. The manager’s feedback will be translated automatically into English by calling the externally available service Machine Translation API of SAP.
Be aware that the example is done with SAP API Business Hub Sandbox system only. This shall only give an idea on how it works and cannot be used productively.
Additional Information
- SAP S/4HANA Cloud Release (tutorial’s last update): 1805
- Step 1
To get to know the Machine Translation API service first, you can try it out in SAP API Business Hub.
-
Below API References click the Link to try out the service.
-
If you are not logged in yet, a pop appears to do so. Press Log On
-
Copy to clipboard this example code for a request body of the machine translation service
jsonCopy{ "sourceLanguage": "en", "targetLanguages": [ "es" ], "units": [ { "value": "Your text to be translated" } ] }
-
Scroll down to Parameters and paste the code into the Value input field of Parameter
body
-
Scroll down to the Try Out button and press it
The response to the service call will appear below.
- Step 2
To configure the connection to the system and the outbound scenario you
will need the service’s end point.After having tried out the service in SAP API Business Hub, the response had appeared.
Copy the Request URL - which is the end point - from the response section and paste it into a text editor for later usageIn order to authenticate during service call later you’ll need an API Key of SAP API Business Hub.
-
Still in SAP API Business Hub, scroll to top and press Show API Key
A pop up opens.
-
Press Copy Key and Close to save the key to your clipboard.
-
Paste the application key into a text editor for later usage.
-
- Step 3
In order to allow communication with the SAP API Business Hub Sandbox you have to create a communication system for it in your SAP S/4HANA System.
-
Enter your SAP S/4HANA system’s Fiori Launchpad.
-
Open application Communication Systems from Communication Management Launchpad group.
-
Execute the action to create a New Communication System.
-
Enter following Data into the input fields.
Field Label Field Value System ID SANDBOX_API_SAP_COM
System Name SANDBOX_API_SAP_COM
-
Press Create
The Details screen of the new Communication System opens.
-
Enter
sandbox.api.sap.com
which is the domain specific part of the before gotten service’s end point as Host Name -
Scroll down to the Outbound Users section and press the + button to add an outbound user.
Select the Authentication Method option
None
as authentication will be done via the API Key. -
Press Create to finish the outbound user creation. The pop up closes.
-
Press Save to finish the communication system creation.
-
- Step 4
Define the external service as an available Communication Scenario.
-
Go to Home of your SAP S/4HANA system’s Fiori Launchpad.
-
Open application Custom Communication Scenario from Extensibility Launchpad group
-
Execute the action to create a New Custom Communication Scenario.
A pop up opens.
-
Enter following data into the input fields and press the New button
Field Label Field Value Communication Scenario ID MACHINE_TRANSLATION_API
(prefixYY1_
is added automatically)Description Scenario for SAP Machine Translation API
The details UI for the scenario opens.
-
- Step 5
Set the service path as outbound service.
-
Being in the object page of the custom communication scenario, switch to the Outbound Service section
-
Press + to start outbound service creation
A pop up opensEnter following data into the input fields
Field Label Field Value Description Outbound Service for SAP Machine Translation API
Outbound Service ID OS_MACHINE_TRANSLATION_API
(prefixYY1_
and suffix_REST
are added automatically)URL Path /ml/translation/translation
(service specific part of before gotten service’s end point) -
Press Create to finish the outbound service creation.
The pop up closes.
-
Press Publish to finish the custom communication scenario creation.
-
- Step 6
Create a Communication Arrangement to link the scenario with the communication system.
-
Go to Home of your SAP S/4HANA system’s Fiori Launchpad.
-
Open application Custom Communication Arrangements from Communication Management Launchpad group.
-
Execute the action to create a New Custom Communication Arrangement.
A pop up opens.
-
Select or Enter following data.
Field Label Field Value Scenario YY1_MACHINE_TRANSLATION_API
Arrangement Name YY1_MACHINE_TRANSLATION_API_SANDBOX_SAP_COM
-
Press Create.
The pop up closes and the Arrangement’s Detail Page opens.
-
Select Communication System
SANDBOX_API_SAP_COM
-
Save the Arrangement.
-
- Step 7
Add fields to persists feedback at the custom business object
Bonus Entitlement
.-
Go to Home of your SAP S/4HANA system’s Fiori Launchpad.
-
Open Custom Business Objects application in Extensibility launchpad group.
-
Open the business object
Bonus Entitlement
. -
Start Edit Mode by executing the Edit Draft action.
-
Switch to Fields section.
-
Add following New fields
Field Label Field Identifier Field Type Field Properties Feedback
Feedback
Text
Length: 255
Feedback's language
FeedbacksLanguage
Text
Length: 2
Feedback in english
FeedbackInEnglish
Text
Length: 255
-
Publish the business object.
-
- Step 8
Now as the business object has just been published, the logic can be enhanced by the translation functionality. ABAP for key users was enhanced by the classes
CL_BLE_HTTP_CLIENT
,CL_BLE_HTTP_REQUEST
andCX_BLE_HTTP_EXCEPTION
to enable you to work with HTTP requests.-
Switch to Logic section.
-
Enter the After Modification Event Logic.
-
- Step 9
- In the already existing coding go to the end but before this snippet abapCopy
ELSE. RETURN. ENDIF. ENDIF.
- Implement a check if the outbound service is available abapCopy
* Check if the outbound service is available CHECK cl_ble_http_client=>is_service_available( communication_scenario = 'YY1_MACHINE_TRANSLATION_API' outbound_service = 'YY1_OS_MACHINE_TRANSLATION_API_REST' ) = abap_true.
- Implement creation of HTTP client abapCopy
* Create HTTP client to access the outbound service DATA(lo_client) = cl_ble_http_client=>create( communication_scenario = 'YY1_MACHINE_TRANSLATION_API' outbound_service = 'YY1_OS_MACHINE_TRANSLATION_API_REST' ).
- In the already existing coding go to the end but before this snippet
- Step 10
Implement the creation of the Request Body.
As the aim is to translate every other language than english into english, the target language is set to english. The source language and the to be translated feedback are gotten from the corresponding fields of the custom business object.
The request body in JSON format looks this way.jsonCopy{ "sourceLanguage": "es", "targetLanguages": [ "en" ], "units": [ { "value": "Su texto a traducir" } ] }
In the custom business object logic you have to supply this request as string. The
sourceLanguage
andvalue
values have to be replaced with variables by string concatenation.abapCopyDATA lv_request_body TYPE string. CONCATENATE '{"sourceLanguage": "' bonus_entitlement-feedbackslanguage '","targetLanguages": ["en"],"units": [{"value": "' bonus_entitlement-feedback '"}]}' INTO lv_request_body.
- Step 11
Create the service request and set several properties
abapCopy* Creation of the service request DATA(request) = cl_ble_http_request=>create( * method that is used for the service call )->set_method( if_ble_http_request=>co_method-post )->set_body( lv_request_body )->set_header_parameter( EXPORTING name = 'apikey' value = '<YOUR API KEY>' "the key you got with Step 1 )->set_header_parameter( EXPORTING name = 'Content-Type' "Content type the bodies of request and response are formatted as value = 'application/json' ).
- Step 12
-
Implement sending the request by the use of the before created HTTP client and receive the response.
abapCopy* Send a request and receive a response. DATA(response) = lo_client->send( request ).
-
Implement getting the response body from the response.
abapCopy* Get the body of the response. DATA(lv_body) = response->get_body( ).
The response body in JSON format will look like this
jsonCopy{ "units": [ { "value": "Su texto a traducir", "translations": [ { "language": "en", "value": "Your text to be translated" } ] } ] }
-
Implement getting the translation part from the JSON string by the help of string operations.
abapCopy* Get translation from response DATA(lv_translation) = substring_before( val = substring_after( val = lv_body sub = '"en","value":"') sub = '"}]}]}' ). bonus_calculation-feedbackinenglish = lv_translation.
- Implement error handling abapCopy
TRY . " CODING PARTS OF THIS STEP FROM BEFORE TO BE PLACED HERE 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.
- Publish the After Modification logic.
-
- Step 13
-
Go to Home of your SAP S/4HANA system’s Fiori Launchpad.
-
Open the Bonus Entitlement application from Extensibility Launchpad group.
-
Open a
Bonus Entitlement
. -
Enter following data
Field Label Field Value Feedback Su texto a traducir
Feedback’s Language es
-
Save the Bonus Entitlement. The translation will get filled.
-
- Step 14
Which sequences are possible to execute an outbound service?
- Try out the service in SAP API Business Hub
- Get service end point and API Key
- Create Communication System for Sandbox
- Create custom communication scenario for external service
- Create outbound service in scenario
- Create communication arrangement for external service
- Extend custom business object with feedback fields
- Open custom business object logic
- Code HTTP client creation
- Code request body string
- Code service request creation
- Code request sending and response processing
- Test the application
- Test yourself