Execute an Outbound Service from Custom Business Object Logic
- How to get needed service data from SAP Business Accelerator 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 - Custom Business Objects (ID: SAP_CORE_BC_EXT_CBO
), Communication Management (ID: SAP_CORE_BC_COM
) and Extensibility - Custom Communication Scenarios (ID: SAP_CORE_BC_EXT_CCS
) in your SAP S/4HANA Cloud system
- access to SAP Business Accelerator Hub.
Example Objects: Existence of custom business object Bonus Entitlement
as described in this tutorial (Blog), but without any special bonusplan-releasestatus
logic.
Knowledge: (optional) Tutorial: Tour the SAP Business Accelerator 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 SAP Translation Hub of SAP.
Be aware that the example is done with the SAP Business Accelerator Hub Sandbox system only. This shall only give an idea on how it works and cannot be used productively.
Tutorial feasibility last checked with SAP S/4HANA Cloud Release 2408
- Step 1
To get to know the SAP Translation Hub service first, you can try it out in SAP Business Accelerator Hub.
-
Go to Try out of SAP Translation Hub on SAP Business Accelerator Hub
-
Expand the Translate operations section .
-
Choose the POST operation /translate.
-
Switch to Body section of REQUEST.
-
Exchange the default body with this simplified example.
jsonCopy{ "sourceLanguage": "en", "targetLanguages": [ "es" ], "units": [ { "value": "Your text to be translated" } ] }
-
Hit the Run button.
-
The RESPONSE to the service call will appear.
-
- Step 2
To configure the connection to the system and the outbound scenario you will need the service’s end point.
After trying out the service in SAP Business Accelerator Hub, the response appears. Copy the Request URL - which is the end point - from the response section and paste it into a text editor for later use.
In order to authenticate during service call later you’ll need an API Key of SAP Business Accelerator Hub.
-
Still in SAP Business Accelerator 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 use.
-
- Step 3
In order to allow communication with the SAP Business Accelerator Hub Sandbox you have to create a communication system for it in your SAP S/4HANA Cloud System.
-
Enter your SAP S/4HANA Cloud system’s Fiori Launchpad.
-
Start typing Communication Systems in the Launchpad search and open the App from the results.
-
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 as Host Name
sandbox.api.sap.com
, which is the domain part of the service’s end point that you got in the previous step. -
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 directly in coding. -
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 SAP Business Accelerator Hub service as an available Communication Scenario.
-
Start typing Custom Communication Scenario in the Launchpad search and open the App from the results.
-
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 SAP_TRANSLATION_HUB
(prefixYY1_
is added automatically)Description Scenario for SAP Translation Hub
The details UI for the scenario opens.
-
Switch to the Outbound Service section
-
Press Add to start outbound service creation. A pop up opens.
Enter following data into the input fields
Field Label Field Value Description Outbound Service for SAP Translation Hub
Outbound Service ID OS_SAP_TRANSLATION_HUB
(prefixYY1_
and suffix_REST
are added automatically)URL Path /sth/translate
(service specific part of before gotten service’s end point) -
Press Create to finish the outbound service creation.
-
Another pop up opens and tells that only one instance of this communication scenario per client will be supported. Confirm with OK.
Both pop ups close. -
Press Save.
-
Press Publish to finish the custom communication scenario creation.
-
- Step 5
Create a Communication Arrangement to link the scenario with the communication system.
-
Start typing Communication Arrangements in the Launchpad search and open the App from the results.
-
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_SAP_TRANSLATION_HUB
Arrangement Name YY1_SAP_TRANSLATION_HUB_SANDBOX_API_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 6
Add fields to persists feedback at the custom business object
Bonus Entitlement
.-
Start typing Custom Business Objects in the Launchpad search and open the App from the results.
-
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 7
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.
-
Code HTTP client creation
In order to call an external service from there you need to create an HTTP client in your custom business object logic.
-
In the already existing coding go to the end but stay in front of the last 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_SAP_TRANSLATION_HUB' outbound_service = 'YY1_OS_SAP_TRANSLATION_HUB_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_SAP_TRANSLATION_HUB' outbound_service = 'YY1_OS_SAP_TRANSLATION_HUB_REST' ).
-
-
Code request body string
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. The XCO JSON module as part of the key user (KU) edition of the XCO library can be used to create the JSON string for the request body according to the required format.abapCopy* Create request body json string DATA(lo_json_builder) = xco_ku_json=>data->builder( ). lo_json_builder->begin_object( )->add_member( 'sourceLanguage' )->add_string( bonusentitlement-feedbackslanguage )->add_member( 'targetLanguages' )->begin_array( )->add_string( 'en' )->end_array( )->add_member( 'units' )->begin_array( )->begin_object( )->add_member( 'value' )->add_string( bonusentitlement-feedback )->end_object( )->end_array( )->end_object( ). DATA(lv_request_body) = lo_json_builder->get_data( )->to_string( ).
-
Code service request creation
Create the service request and set several properties
abapCopy* Creation of the service request DATA(request) = cl_ble_http_request=>create( ). request->set_method( if_ble_http_request=>co_method-post )->set_body( lv_request_body )->set_header_parameter( name = 'APIKey' value = '< YOUR API KEY >' )->set_content_type( 'application/json; charset=utf-8' ).
-
Code request sending and response processing
-
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_response_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 translate", "translationProvider": 1, "qualityIndex": 25 } ] } ] }
-
Implement getting the translation part from the JSON string by the help of the XCO JSON module
abapCopy* Get translation from response TYPES: BEGIN OF ts_translation, language TYPE c LENGTH 2, value TYPE string, translation_provider TYPE i, quality_index TYPE i, END OF ts_translation, BEGIN OF ts_unit, value TYPE string, translations TYPE STANDARD TABLE OF ts_translation WITH NON-UNIQUE DEFAULT KEY, END OF ts_unit, BEGIN OF ts_response, units TYPE STANDARD TABLE OF ts_unit WITH NON-UNIQUE DEFAULT KEY, END OF ts_response. DATA ls_response TYPE ts_response. xco_ku_json=>data->from_string( lv_response_body )->apply( VALUE #( ( xco_ku_json=>transformation->pascal_case_to_underscore ) ) )->write_to( REF #( ls_response ) ). bonusentitlement-feedbackinenglish = ls_response-units[ 1 ]-translations[ 1 ]-value.
The response of the service call is translated into a corresponding ABAP structure. With help of the built-in Camel case/Pascal case to underscore transformation the JSON data is adjusted to ABAP requirements.
-
Implement error handling
Consider a proper error handling by putting a TRY and CATCH block around the service call logic.
abapCopyTRY . " < 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 8
-
Start typing Bonus Entitlements in the Launchpad search and open the App from the results.
-
Open a
Bonus Entitlement
. -
Enter following data
Field Label Field Value Feedback Su texto a traducir
Feedback’s Language es
Please note that the language code must be entered in lower case to ensure that the service call is successful.
-
Save the Bonus Entitlement. The translation will get filled.
-
- Step 9
Which sequences are possible to execute an outbound service?
- Excursus - Try out the service in SAP Business Accelerator Hub
- Get service end point and API Key
- Create Communication System for Sandbox
- Create custom communication scenario for outbound service
- Create communication arrangement for outbound service
- Extend custom business object data structure
- Enhance custom business object logic
- Test the application
- Test yourself