Skip to Content

The following steps will explain how to incorporate the S/4HANA Cloud SDK in a project developed in the SAP Web IDE. If you want to follow this tutorial, we highly recommend checking out the first part of this blog series.

Note: This post is part of a series. For a complete overview visit the SAP S/4HANA Cloud SDK Overview.

Goal of this blog post

This tutorial will cover your first steps when incorporating the S/4 HANA Cloud SDK into a project developed in the SAP Web IDE (https://www.sap.com/developer/topics/sap-webide.html). Specifically, the following steps will be explained

  1. Creating a sample project from template in the Web IDE
  2. Adapting the projects dependencies and MTA descriptor to work with the SDK
  3. Adding a servlet class that uses the SDK to read business partners from an S/4 HANA system

Note: This tutorial requires access to an SAP S/4HANA system (see prerequisites below).

Prerequisites

In order to execute this tutorial successfully, we assume a working and reachable system of SAP S/4HANA on-premise or S/4HANA Cloud. Please refer to the prerequisites part of Step 4 – Calling an Odata Service for further information.

Generate a Multi-Target Application in the Web IDE

To use Web IDE, you need a trial account on SAP Cloud Platform as described in Step 3 – Hello World on SCP Cloud Foundry in the section Setup for CloudFoundry. With your SCP trial account, you also get a trial account in the NEO environment. After logging in to SCP, choose NEO Trial:

Select Services from the navigation bar to the left and find the service Web IDE Full-Stack. (You might want to bookmark the URL for later use):

Click on the service. Then click on Take Action -> Go to Service. The SAP Web IDE will open up in a new tab, with the home view open:

Switch to the workspace view the second icon from the top in the left sidebar.

Choose File > New > Project from Template from the menu and select Multi-Target-Application. Enter an project ID, go to the next screen, enter an application ID (e.g. sdk_tutorial) and press finish. In the following, we will assume that project and application are named sdk_tutorial.

Now right click on your project in the workspace area on the left and select New > Java Module. Enter tutorial as the module name and press next. Enter the group ID as com.sap.cloud.sdk and press finish.

Right-click your project in the workspace and install the builder via Settings > Cloud Foundry > Install Builder:

After the builder has been installed, right-click on your project folder in the navigation area and select Build:

When the build is finished, right-click the folder Workspace and refresh it. Then right-click on the mtar-file in the folder mta-archives > sdk_tutorial (not on your project folder) and select Deploy to Cloud Foundry:

Choose your Cloud Foundry Endpoint, Organization and Space and press Deploy:

If you now go to the Applications section of your Cloud Foundry account, you should see the application tutorial running. Click on the application and the URL, and you see the entry page for the application:

Adapt Your Project for Usage of the SDK

To use the SDK, the associated Java libraries have to be added as dependencies to your project. Open the file pom.xml in folder tutorial of your project.

Add the following lines directly after the closing </properties>-tag:

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.sap.cloud.s4hana</groupId>
                <artifactId>sdk-bom</artifactId>
                <version>1.9.4</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
</dependencyManagement>

Add the following lines into the <dependencies>-section:

		<dependency>
			<groupId>com.sap.cloud.s4hana.cloudplatform</groupId>
			<artifactId>scp-cf</artifactId>
		</dependency>
		<dependency>
			<groupId>com.sap.cloud.s4hana</groupId>
			<artifactId>s4hana-all</artifactId>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.12</version>
		</dependency>

Note: As new versions of the SDK are being released regularly, please refer to the relase notes if you want to use the most up-to-date version: S/4HANA Cloud SDK Release Notes.

 

Add Class BusinessPartnerServlet to Your Project

Right-click on the folder your HelloWorldServlet-class resides in and choose New > Java Class. Name the new class BusinessPartnerServlet. Copy the following code into your class:

package com.sap.cloud.sdk.tutorial;

import com.google.gson.Gson;
import org.slf4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

import com.sap.cloud.sdk.cloudplatform.logging.CloudLoggerFactory;
import com.sap.cloud.sdk.odatav2.connectivity.ODataException;

import com.sap.cloud.sdk.s4hana.datamodel.odata.helper.Order;
import com.sap.cloud.sdk.s4hana.datamodel.odata.namespaces.businesspartner.BusinessPartner;
import com.sap.cloud.sdk.s4hana.datamodel.odata.services.DefaultBusinessPartnerService;

@WebServlet("/businesspartners")
public class BusinessPartnerServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
    private static final Logger logger = CloudLoggerFactory.getLogger(BusinessPartnerServlet.class);

    private static final String CATEGORY_PERSON = "1";

    @Override
    protected void doGet(final HttpServletRequest request, final HttpServletResponse response)
            throws ServletException, IOException {
        try {
            final List<BusinessPartner> businessPartners =
                    new DefaultBusinessPartnerService()
                            .getAllBusinessPartner()
                            .select(BusinessPartner.BUSINESS_PARTNER,
                                    BusinessPartner.LAST_NAME,
                                    BusinessPartner.FIRST_NAME,
                                    BusinessPartner.IS_MALE,
                                    BusinessPartner.IS_FEMALE,
                                    BusinessPartner.CREATION_DATE)
                            .filter(BusinessPartner.BUSINESS_PARTNER_CATEGORY.eq(CATEGORY_PERSON))
                            .orderBy(BusinessPartner.LAST_NAME, Order.ASC)
                            .execute();

            response.setContentType("application/json");
            response.getWriter().write(new Gson().toJson(businessPartners));

        } catch (final ODataException e) {
            logger.error(e.getMessage(), e);
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            response.getWriter().write(e.getMessage());
        }
    }
}

 

Hint: If you chose a different package during creation of the Java Module, adapt the package name.

Save your class. The code snippet you just copied tries to establish a connection to an S/4HANA system and select all business partners from that system using the S/4HANA Cloud SDK. For more information please refer to Step 4 – Calling an Odata Service.

Setting up the connection to an S/4HANA System

To connect to an SAP S/4HANA system, you have to inform Cloud Foundry about the location of your ERP endpoint. As described in more depth in Step 4 – Calling an Odata Service, the recommended approach is to do this using the destination service.

Open the file mta.yml in your project folder and switch to the code editor.

In your mta.yml proceed as given in one of the options below (option 1 with the destination service is recommended).

Connecting to an S/4 HANA System Using the Destination Service on SAP Cloud Platform Cloud Foundry

For the recommended approach of using the destination service to configure the connection to your SAP S/4HANA system, proceed as follows. Note that this requires version 1.6.0 or higher of the SAP S/4HANA Cloud SDK.

In your multi-target application, you can setup the application to automatically create the necessary destination and xsuaa service and bind them to your application. To do so, open up the file mta.yml in your project folder. Add the following lines to the end of the file:

    properties:
      TARGET_RUNTIME: tomee 
   requires:
      - name: my-xsuaa
      - name: my-destination
         
resources:
  - name: my-destination
    type: destination
    description: Destination Service
  - name: my-xsuaa
    type: com.sap.xs.uaa

The lines following resources: will create the services with the given parameters upon deployment to Cloud Foundry, while the lines following requires: will bind them to your application.

Maintain your ERP endpoint destination as explained in Step 4 – Calling an Odata Service in chapter Configure Destinations.

Deploy to Cloud Foundry and Display Business Partners

Build and deploy your project again. Open the URL and add /businesspartners to it. You should now see a list of businesspartners retrieved from the S/4HANA system:

 

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply