Programming

How to Develop a Simple Backend Extension Application for SAP S/4HANA Cloud

When it comes to programming, hands-on practice is always a great way to learn. With that in mind, let’s go ahead and develop a simple backend extension application for SAP S/4HANA Cloud.

 

However, we understand that not everyone has access to an SAP S/4HANA Cloud system but may still want to test out the extension feature. For any kind of backend extension, our primary target is to build an OData service from various other sources.

 

This new OData service can then be consumed by any UI, e.g., SAP Fiori. We plan to use SAP S/4HANA Cloud SDK, which has been integrated with SAP Web IDE Full Stack. Let’s walk through the process step-by-step.

 

Defining a Destination

As a first step, you’ll define a destination in your SAP Cloud Platform Cockpit that points to your SAP S/4HANA cloud system. As a prerequisite, you must create a communication user, a communication system, and a communication arrangement in your SAP S/4HANA system.

 

Once communication details have been set up in SAP S/4HANA Cloud, navigate back to your SAP Cloud Platform Cockpit and create a destination called “ErpQueryEndpoint.” This destination can be used by default if you do not reference any other destination name when calling an API from a destination. You can also pass the destination name as a part of the extension logic.

 

Since you may not have access to an SAP S/4HANA Cloud system, we’ll create a mock server and refer to that system as a destination in SAP Cloud Platform Cockpit. More complete details are documented in https://sap.github.io/cloud-s4-sdk-book/pages/mock-odata.html.

 

Creating a Business Application in SAP Web IDE Full Stack

Create a business application in SAP Web IDE Full Stack by using the template shown below. Make sure that you maintain your Cloud Foundry instance details and install the builder in the SAP Web IDE preferences.

 

SAP Cloud Platform Business Application
 

Setting Up the SAP S/4HANA Cloud SDK

If you expand your project files in the SAP Web IDE, you’ll find the pom.xml file in the srv folder. This file includes all the required dependencies for developing SAP S/4HANA extension applications, as shown below.

 

Setting up the SAP S/4HANA Cloud SDK
 

Service Definition Based on SAP API Business Hub or an SAP S/4HANA Cloud System

Right-click on the srv folder and select New Data _ Model from External Service. Now, select either Service Catalog or SAP API Business Hub to find your required service. Selecting Service Catalog will allow you to pull the services available in a specific destination, e.g., ErpQueryEndpoint. In our case, we’ll select a service from the SAP API Business Hub. This step will create the required csn and edmx metadata files.

 

Service Definition
 

Defining the Data Model

Navigate to your project’s srv folder and replace my-service.cds content with the content shown below.

 

using API_BUSINESS_PARTNER as bp from './external/csn/

ODataServiceforBusinessPartner';

 

service QueryService{

 

@cds.persistence.skip

Entity BusinessPartner as projection on bp.A_BusinessPartnerType{

BusinessPartner,

LastName,

FirstName,

BusinessPartnerCategory

};

}

 

If you can’t find the my-service.cds file, then you might have renamed it something else previously. If you get any syntax error after putting in the logic shown above, then make sure that you’re using right name after the csn folder path in the first line of your my-service.cds file. This name should be same as the JSON file name, without an extension, under the csn folder.

 

You can now build the service module by right-clicking on the srv folder and then selecting Build > Build. After the build is successful, run the Java application by rightclicking on the srv folder and then selecting Run > Java Application from the context menu.

 

Now, you should see the OData base URL and can review its metadata, as shown in the next two figures. However, since no connection to the backend exists (via a destination), the entity set will return an error message.

 

Run Extending SAP S/4HANA Cloud
 
Code Listing
 

Adding Custom Handlers to Query the OData Service

Create a Java class using the context menu of the srv folder, which will call the business partner OData service and return the four fields defined in your service definition. Here’s the logic:

 

package my.scpbook;

 

import com.sap.cloud.sdk.service.prov.api.operations.Create;

import com.sap.cloud.sdk.service.prov.api.operations.Query;

import com.sap.cloud.sdk.service.prov.api.operations.Update;

import com.sap.cloud.sdk.service.prov.api.request.CreateRequest;

import com.sap.cloud.sdk.service.prov.api.request.QueryRequest;

import com.sap.cloud.sdk.service.prov.api.request.UpdateRequest;

import com.sap.cloud.sdk.service.prov.api.response.CreateResponse;

import com.sap.cloud.sdk.service.prov.api.response.ErrorResponse;

import com.sap.cloud.sdk.service.prov.api.response.QueryResponse;

import com.sap.cloud.sdk.service.prov.api.response.UpdateResponse;

import com.sap.cloud.sdk.s4hana.connectivity.ErpConfigContext;

import com.sap.cloud.sdk.s4hana.datamodel.odata.namespaces.businesspartner.BusinessPartner;

import com.sap.cloud.sdk.s4hana.datamodel.odata.services.DefaultBusinessPartnerService;

import com.sap.cloud.sdk.odatav2.connectivity.ODataException;

 

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

 

import java.util.List;

 

public class QueryOdata {

 

private final Logger logger = LoggerFactory.getLogger(this.getClass());

 

@Query(serviceName = "QueryService", entity = "BusinessPartner")

public QueryResponse queryCustomers(QueryRequest qryRequest) throws

ODataException {

 

List<BusinessPartner> result = new DefaultBusinessPartnerService().

getAllBusinessPartner()

.select(BusinessPartner.BUSINESS_PARTNER, BusinessPartner.LAST_NAME,

BusinessPartner.FIRST_NAME)

.filter(BusinessPartner.BUSINESS_PARTNER_CATEGORY.eq("1")).execute();

return QueryResponse.setSuccess().setData(result).response();

}

}

 

Make sure that you use the correct service name we previously defined. Once you build the service module and run the Java application, you’ll be able to run the OData service and display the content by using the entity set for business partners.

 

Deploying to SAP Cloud Platform on the Cloud Foundry Environment

Once you finalize the code, you can build the entire project, which will create an .mtar file under the folder mta_archives. You can then deploy this file to Cloud Foundry, as shown below.

 

Deploying to SAP Cloud Platform
 

Conclusion

As you can see, even a simple backend extension application for SAP S/4HANA Cloud takes some time to program. The results are definitely worth the time it takes, however. If you’ve read all the way through, you learned how to define a destination, create a business application, set up the SAP S/4HANA Cloud SDK, create a service definition, define the data model, add custom handlers, and deploy to SAP Cloud Platform.

 

Editor’s note: This post has been adapted from a section of the book SAP Cloud Platform: Cloud-Native Development by Gairik Acharya, Govind Bajaj, Avijit Dhar, Anup Ghosh, and Asidhara Lahiri.

Recommendation

SAP Cloud Platform: Cloud-Native Development
SAP Cloud Platform: Cloud-Native Development

Learn to build cloud applications from the ground up using SAP Cloud Platform. Explore the Neo and Cloud Foundry development environments; pick your backend language from a selection including Java, Node.js, and ABAP; and create a frontend with SAPUI5 and SAP Fiori, and more. Once your app is ready to run, secure, test, and monitor it before delivery and implementation. Then find out how to integrate essential SAP Cloud Platform services like the SAP Leonardo toolset. Take your apps to the next level!

Learn More
SAP PRESS
by SAP PRESS

SAP PRESS is the world's leading SAP publisher, with books on ABAP, SAP S/4HANA, SAP C/4HANA, SAP Leonardo, SAP Cloud Platform, and more!

Comments

Latest Blogs