Skip to Content

Create business partner in S/4HANA Cloud using ABAP Environment data

Requires Customer/Partner License
Create a business partner in an S/4HANA Cloud system based on a business user in SAP Business Technology Platform ABAP Environment.
You will learn
  • How to implement outbound service call from SAP BTP ABAP Environment to S/4 HANA Cloud service
  • How to retrieve data of logged in business user in SAP BTP ABAP Environment
  • Create business partner in S/4 HANA Cloud system based on data of business user in SAP BTP ABAP Environment (use Business Partner Integration Service in SAP S/4 HANA)​
  • Return business partner id after creation
niloofar-flothkoetterNiloofar FlothkoetterNovember 23, 2021
Created by
mervey45
September 14, 2018
Contributors
niloofar-flothkoetter
mervey45

Prerequisites

Create all ABAP artifacts with namespace Z... for local development.
Always replace XXX with your initials or group number.

  • Step 1
    1. Copy the service URL from communication arrangement on S/4 HANA Cloud system in a browser.

    2. Remove -api from your URL an add /$metadata at the end of the link and press enter. Login with your communication user and password.

      Download Service Metadata
    3. Right click the page and save the Metadata as a .edmx file.

      Save Service Metadata
  • Step 2

    Mark the steps 2 as completed by pressing Done if you have already created the package Z_Package_XXX (where XXX is your group number) in the previous tutorials.

    1. Open Eclipse and connect to your system.

    2. Right click the main package ZLOCAL and choose New > ABAP Package.

    3. Create your own ABAP development package Z_PACKAGE_XXX as a sub package of ZLOCAL.

      • Name: Z_PACKAGE_XXX
      • Description: my new package
    4. Click on Next.

      Create your own ABAP package
    5. Select package properties and press Next.

      • Software Component: ZLOCAL
      Create your own ABAP package
    6. Select a transport request and create your ABAP package with Finish.

      Select a Transport request

    You can add your package to Favorite Packages.

  • Step 3
    1. Mark your package under ZLOCAL or in Favorite Packages and click on File and choose New > Other… > Service Consumption Model.

      choose OData Proxy
    2. Click on Next.

      next
  • Step 4
    1. Enter a name for your service consumption model, a service definition will be generated as part of the service consumption model with this name.

    2. Enter a description and choose OData as Remote Consumption Mode.

    3. Click Next.

      Create Service Consumption Model
    4. Upload the .edmx file, which you saved before, in the Service Metadata File field.

    5. Click Next.

      upload file
  • Step 5
    1. Add the _XXX at the end of each name.

    2. Click on Next.

      change Artifact Names
  • Step 6
    1. Click on Next by artifact generation list.

      List
    2. Select a transport request.

    3. Click on Finish.

      Select transport request
  • Step 7

    Open service definitions in your package and make sure if your new service definition is created successfully.

    check service Definition
  • Step 8
    1. Right click your package and choose New > Other ABAP Repository Object > HTTP Service.

      Add HTTP Service
    2. Click Next.

      Add HTTP Service
  • Step 9
    1. Enter a name and a description.

      • Name: ZS4_BUPA_XXX
      • Description: call S/4 Business Partner
    2. Click Next.

      Create HTTP Service
    3. Select a transport request and click on Finish.

      Create HTTP Service
  • Step 10
    1. Go to the HTTP Service that you created in the last step.

    2. Open Handler Class.

      Open handler Class
    3. Copy the code in the class and change it with your data.

    i_service_instance_name the value of the service instance name property of the communication arrangement
    i_name the name of the destination
    i_authn_mode if_a4c_cp_service=>user_propagation
    iv_service_definition_name Data definition created as part of client proxy

    Retrieve Name of ZA_BusinessPartner_XXX Data Definition.

    ABAP
    Copy
    CLASS zcl_s4_bupa_xxx DEFINITION PUBLIC CREATE PUBLIC .
      PUBLIC SECTION.
        INTERFACES if_http_service_extension .
    ENDCLASS.
    
    CLASS zcl_s4_bupa_xxx IMPLEMENTATION.
      METHOD if_http_service_extension~handle_request.
        TRY.
            DATA(lo_destination) = cl_http_destination_provider=>create_by_cloud_destination(
                 i_name                  = 'S4BusinessPartnerOAuth2'
                 i_authn_mode =  if_a4c_cp_service=>user_propagation
             ).
    
            cl_web_http_client_manager=>create_by_http_destination(
              EXPORTING
                i_destination = lo_destination
              RECEIVING
                r_client = DATA(lo_http_client)
            ).
    
    **Relative service path**
            lo_http_client->get_http_request( )->set_uri_path( '/sap/opu/odata/sap/API_BUSINESS_PARTNER' ).
            lo_http_client->set_csrf_token( ).
    
            DATA(lo_client_proxy)  = cl_web_odata_client_factory=>create_v2_remote_proxy(
               iv_service_definition_name = 'ZS4_API_BUSINESS_PARTNER_XXX'
               io_http_client             = lo_http_client
               iv_relative_service_root   = '/sap/opu/odata/sap/API_BUSINESS_PARTNER' ).
    
    **Entity set A_BUSINESSPARTNER in business partner integration service**
            DATA(lo_create_request) = lo_client_proxy->create_resource_for_entity_set('A_BUSINESSPARTNER')->create_request_for_create( ).
    
            DATA(lv_userid) = cl_abap_context_info=>get_user_technical_name( ).
    
            SELECT SINGLE *
            FROM i_businessuser
                WITH PRIVILEGED ACCESS
            WHERE userid = @lv_userid INTO
            @DATA(ls_businessuser).
    
            IF sy-subrc <> 0.
              response->set_text( |Error retrieving business user { lv_userid }| ).
              RETURN.
            ENDIF.
    
            DATA(ls_bupa) = VALUE za_businesspartner_XXX(
              businesspartnercategory = '1'
              firstname = ls_businessuser-firstname
              lastname = ls_businessuser-lastname
            ).
    
            lo_create_request->set_business_data( ls_bupa ).
    
            DATA(lo_create_response) = lo_create_request->execute( ).
            lo_create_response->get_business_data( IMPORTING es_business_data = ls_bupa ).
    
            response->set_text( |Business partner { ls_bupa-businesspartner } was created| ).
    
          CATCH cx_root INTO DATA(lx_exception).
            response->set_text( lx_exception->get_text( ) ).
        ENDTRY.
      ENDMETHOD.
    ENDCLASS.
    
    
    1. Save and activate all objects that you created.
      activate
  • Step 11
    1. Navigate to HTTP service and click On URL.

      Copy the link
    2. Enter business user email and password and click Log on.

      business user requires the SAP_BR_BUPA_MASTER_SPECIALIST business role.
      This User has the same email-address for both S/4 HANA and SAP BTP ABAP Environment.

      Log On

      You see that a Business Partner is created.

    3. Copy the Business Partner number.
      Copy number
  • Step 12
    1. Open SAP Fiori Launchpad and login with the business user.

      open s/4
    2. Navigate to Business Partner Master > Maintain Business Partner.

      business partner master
  • Step 13

    Enter created Business Partner number in the related field and press enter.
    Check data of the created business service.

    verify
  • Step 14

    Where do you add your service metadata file?

Back to top