Skip to Content

Execute an Outbound Service from Custom Business Object Logic

Requires Customer/Partner License
Call an external service of SAP Business Accelerator Hub from inside the logic implementation of a custom business object.
You will learn
  • 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
peterpersielPeter PersielDecember 12, 2024

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.

    Try out of SAP Translation Hub on SAP Business Accelerator Hub
    1. Go to Try out of SAP Translation Hub on SAP Business Accelerator Hub

    2. Expand the Translate operations section .

    3. Choose the POST operation /translate.

    4. Switch to Body section of REQUEST.

    5. Exchange the default body with this simplified example.

      json
      Copy
      {
          "sourceLanguage": "en",
          "targetLanguages": [
              "es"
          ],
          "units": [
              {
                  "value": "Your text to be translated"
              }
          ]
      }
      
    6. Hit the Run button.

    7. 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.

    Service End Point: Request URL in response section

    In order to authenticate during service call later you’ll need an API Key of SAP Business Accelerator Hub.

    1. Still in SAP Business Accelerator Hub, scroll to top and press Show API Key

      Button to show API Key of in SAP Business Accelerator Hub

      A pop up opens.

    2. Press Copy Key and Close to save the key to your clipboard.

      Pop Up to Copy API Key
    3. 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.

    1. Enter your SAP S/4HANA Cloud system’s Fiori Launchpad.

    2. Start typing Communication Systems in the Launchpad search and open the App from the results.

      Communication Systems application from search results
    3. Execute the action to create a New Communication System.

    4. Enter following Data into the input fields.

      Field Label Field Value
      System ID SANDBOX_API_SAP_COM
      System Name SANDBOX_API_SAP_COM
      Pop Up to create New Communication System
    5. Press Create

      The Details screen of the new Communication System opens.

    6. 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.

    7. 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.

      Pop Up to create New Outbound
    8. Press Create to finish the outbound user creation. The pop up closes.

    9. Press Save to finish the communication system creation.

  • Step 4

    Define the external SAP Business Accelerator Hub service as an available Communication Scenario.

    1. Start typing Custom Communication Scenario in the Launchpad search and open the App from the results.

      Custom Communication Scenario application from search results
    2. Execute the action to create a New Custom Communication Scenario.

      A pop up opens.

    3. Enter following data into the input fields and press the New button

      Field Label Field Value
      Communication Scenario ID SAP_TRANSLATION_HUB (prefix YY1_ is added automatically)
      Description Scenario for SAP Translation Hub
      Scenario creation pop up

      The details UI for the scenario opens.

    4. Switch to the Outbound Service section

      Switch to outbound services in scenario maintenance
    5. Press Add to start outbound service creation. A pop up opens.

      Pop Up to create outbound service

      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 (prefix YY1_ and suffix _REST are added automatically)
      URL Path /sth/translate (service specific part of before gotten service’s end point)
    6. Press Create to finish the outbound service creation.

    7. 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.

    8. Press Save.

    9. Press Publish to finish the custom communication scenario creation.

  • Step 5

    Create a Communication Arrangement to link the scenario with the communication system.

    1. Start typing Communication Arrangements in the Launchpad search and open the App from the results.

      Custom Communication Arrangements application from search results
    2. Execute the action to create a New Custom Communication Arrangement.

      A pop up opens.

    3. Select or Enter following data.

      Field Label Field Value
      Scenario YY1_SAP_TRANSLATION_HUB
      Arrangement Name YY1_SAP_TRANSLATION_HUB_SANDBOX_API_SAP_COM
    4. Press Create.

      Communication Creation Pop Up

      The pop up closes and the Arrangement’s Detail Page opens.

    5. Select Communication System SANDBOX_API_SAP_COM

      Communication System Selection in Communication Arrangement
    6. Save the Arrangement.

  • Step 6

    Add fields to persists feedback at the custom business object Bonus Entitlement.

    1. Start typing Custom Business Objects in the Launchpad search and open the App from the results.

      Custom Business Objects application from search results
    2. Open the business object Bonus Entitlement.

    3. Start Edit Mode by executing the Edit Draft action.

    4. Switch to Fields section.

    5. Add following New fields

      Field Label Field Identifier Field Type Field Properties
      FeedbackFeedback Text Length: 255
      Feedback's language FeedbacksLanguage Text Length: 2
      Feedback in english FeedbackInEnglish Text Length: 255
    6. 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 and CX_BLE_HTTP_EXCEPTION to enable you to work with HTTP requests.

    1. Switch to Logic section.

    2. Enter the After Modification Event Logic.

      Enter After Modification logic
    3. 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

        abap
        Copy
        * 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

        abap
        Copy
        * 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'
        ).
        
    4. 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.

      json
      Copy
      {
          "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 and value 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.

      abap
      Copy
      * 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( ).
      
    5. Code service request creation

      Create the service request and set several properties

      abap
      Copy
      * 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' ).
      
    6. Code request sending and response processing

      1. Implement sending the request by the use of the before created HTTP client and receive the response.

        abap
        Copy
        * Send a request and receive a response.
        DATA(response) = lo_client->send( request ).
        
      2. Implement getting the response body from the response.

        abap
        Copy
        * Get the body of the response.
        DATA(lv_response_body) = response->get_body( ).
        
      3. The response body in JSON format will look like this

        json
        Copy
        {
            "units": [
                {
                    "value": "Su texto a traducir",
                    "translations": [
                        {
                            "language": "en",
                            "value": "Your text to translate",
                            "translationProvider": 1,
                            "qualityIndex": 25
                        }
                    ]
                }
            ]
        }
        
      4. Implement getting the translation part from the JSON string by the help of the XCO JSON module

        abap
        Copy
        * 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.

      5. Implement error handling

        Consider a proper error handling by putting a TRY and CATCH block around the service call logic.

        abap
        Copy
        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.
        
    7. Publish the After Modification logic.

  • Step 8
    1. Start typing Bonus Entitlements in the Launchpad search and open the App from the results.

      Bonus Entitlements application from search results
    2. Open a Bonus Entitlement.

    3. 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.

    4. Save the Bonus Entitlement. The translation will get filled.

  • Step 9

    Which sequences are possible to execute an outbound service?

Back to top