4

In all the documentation I found examples for ODataQueryBuilder. But do you also have an example how to use the Create, Update and Delete methods of the package com.sap.cloud.sdk.odatav2.connectivity:

  • ODataCreateRequestBuilder
  • ODataDeleteRequestBuilder
  • ODataUpdateRequestBuilder

How is the CSRF token handled?

Please provide a working example?

1 Answers1

1

CSRF tokens are fetched with a HEAD request on the metadata endpoint of the OData service.

Some notes:

  • The following examples assume that you have a destination named "DestinationName" configured in the SAP Cloud Platform cockpit.
  • Please keep in mind that the S/4HANA virtual data model is usually the easier alternative.

ODataCreateRequestBuilder

Map<String, Object> body = new HashMap<>();
body.put("FirstName", "John");
body.put("LastName", "Doe");
body.put("BusinessPartnerCategory", "1");

ODataCreateRequest createRequest =
    ODataCreateRequestBuilder
        .withEntity("/sap/opu/odata/sap/API_BUSINESS_PARTNER", "A_BusinessPartner")
        .withBodyAsMap(body)
        .build();

createRequest.execute("DestinationName");                 

ODataUpdateRequestBuilder

Map<String, Object> keys = new HashMap<>();
keys.put("BusinessPartner", "12345");

Map<String, Object> params = new HashMap<>();
params.put("FirstName", "John");
params.put("MiddleName", "D.");
params.put("LastName", "Doe");
params.put("BusinessPartnerCategory", "1");

final ODataUpdateRequest updateRequest =
    ODataUpdateRequestBuilder
        .withEntity("/sap/opu/odata/sap/API_BUSINESS_PARTNER", "A_BusinessPartner", keys)
        .withBodyAsMap(params)
        .build();

updateRequest.execute("DestinationName");      

ODataDeleteRequestBuilder

Map<String, Object> keys = new HashMap<>();
keys.put("BusinessPartner", "12345");
keys.put("AddressID", "98765");

ODataDeleteRequest deleteRequest =
    ODataDeleteRequestBuilder
        .withEntity("/sap/opu/odata/sap/API_BUSINESS_PARTNER", "A_BusinessPartnerAddress", keys)
        .build();

deleteRequest.execute("DestinationName");
Sander Wozniak
  • 650
  • 8
  • 27
  • 4
    Please note regarding "CSRF tokens are fetched with a HEAD request on the metadata endpoint": older versions of the underlying library com.sap.cloud.servicesdk:connectivity-odatav2 such as version 1.10.2 (used in S/4SDK 1.9.2) use a GET request instead. – Henning Heitkötter Feb 23 '18 at 07:51
  • 1
    Is there a possibility to do a partial dataset update (PATCH/MERGE) with ODataUpdateRequestBuilder ? – user3170440 Feb 26 '18 at 08:37
  • 1
    As of today, this functionality is not supported. – Sander Wozniak Feb 26 '18 at 10:55