Search code examples
s4sdk

How do I access OData service using the SAP S/4HANA Cloud SDK in existing application?


I have a Java application on SAP Cloud Platform Cloud Foundry that integrates with SAP S/4HANA Cloud (my company's ERP system) by calling APIs (OData services) in that system. I heard about the SAP S/4HANA Cloud SDK and that it makes such scenarios much easier.

How can I leverage the SAP S/4HANA Cloud SDK? Currently, my code to call SAP S/4HANA looks like this (simplified and joined together) for the scenario of retrieving product master data. I have created the S4Product class myself as representation of the response. The baseUrland authHeader are determined before by talking to the destination service on SAP Cloud Platform.

StringBuilder url = new StringBuilder(baseUrl);
url.append("/sap/opu/odata/sap/API_PRODUCT_SRV/A_Product");
url.append("&$select=Product,CreationDate");
url.append("&$filter=ProductType eq '1'");
url.append("&$top=10");

URL urlObj = new URL(url.toString());
HttpURLConnection connection = (HttpURLConnection) urlObj.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Authorization",authHeader);

connection.setDoInput(true);

final InputStreamReader in = new InputStreamReader(connection.getInputStream());
String response = CharStreams.toString(in);

List<S4Product> result = Arrays.asList(new Gson().fromJson(response, S4Product[].class));

Now I'm asked to do something similar with business partners. How do I do this for the business partner OData service, using the SDK? Do I have to create a new application if I want to use the SDK?


Solution

  • With the Java virtual data model of the SAP S/4HANA Cloud SDK, your code would be replaced by something like the following.

    final List<Product> products = new DefaultProductMasterService()
        .getAllProduct()
        .select(Product.PRODUCT, Product.CREATION_DATE)
        .filter(Product.PRODUCT_TYPE.eq("1"))
        .top(10)
        .execute();
    

    This handles everything you have done before manually, in a fluent and type-safe API. In this case, the class Product is provided by the SAP S/4HANA Cloud SDK, no need to create that yourself. It offers a Java representation of the entity type, with all fields, which we are using to define the select and filter query options.

    And for your question about business partners, it would look quite similar to this.

    final List<BusinessPartner> businessPartners = new DefaultBusinessPartnerService()
        .getAllBusinessPartner()
        .select(BusinessPartner.BUSINESS_PARTNER /* more fields ... */)
        // example filter
        .filter(BusinessPartner.BUSINESS_PARTNER_CATEGORY.eq("1"))
        .execute();
    

    BTW, this also covers talking to the destination service and applying authentication headers - you no longer need to do this manually.

    You can use the SAP S/4HANA Cloud SDK in any Java project. Just include the dependencies com.sap.cloud.s4hana.cloudplatform:scp-cf (for Cloud Foundry) and com.sap.cloud.s4hana:s4hana-all.