In this CodeLab, you will enhance sample code to learn more about integrating the S/4HANA Cloud SDK into an application. You will investigate the sample code and perform your own additions to the implementation of a class to retrieve business partners.
In this step, you will investigate how to build queries to SAP S/4HANA from the Java backend.
First, you will retrieve the list of business partners for the list view in the application. Second, you will take a look at the query retrieving detailed data of a single business partner by ID.
Start the development of queries by looking into the class
BusinessPartnerServlet. This is the servlet exposing the business partner APIs.
Looking into the servlet, we can see that the main functionality is moved out into the commands
GetAllBusinessPartnersCommand should return a list of available business partners in SAP S/4HANA system.
The class is almost fully implemented, but the most important part - SAP S/4HANA integration - is left for you to complete.
Your task: Implement the execute method that queries SAP S/4HANA and returns a response.
Here are some hints to help you to understand the required code:
BusinessPartnerServicealready provides a method to retrieve all business partners. Type
serviceto see a list of all available methods. Use the method
getAllBusinessPartner()to fetch multiple business partner entities.
select()method on the result from step 1. Luckily, we do not have to know the exact names of these properties in the public API of S/4HANA. They are codified as static member of the class
BusinessPartner. We can select the business partner id by using
BusinessPartner.BUSINESS_PARTNER. Please, do the same for the first name and last name.
CATEGORY_PERSON. The method to filter is called
filter()and can be executed on the result from the previous step. The property
CATEGORY_PERSON. To express that use the methods provided by the object
execute()you finally execute the query and retrieve the result.
Hint: your final implementation should look similar to:
return service.getAllBusinessPartner().select(BusinessPartner.BUSINESS_PARTNER, BusinessPartner.FIRST_NAME, BusinessPartner.LAST_NAME) .filter(BusinessPartner.BUSINESS_PARTNER_CATEGORY.eq(CATEGORY_PERSON)) .execute();
Now, also take a look at the command
It was already implemented for you.
Can you find out how the OData "expand" method can be implemented using the Virtual Data Model of the SAP S/4HANA Cloud SDK?
Hint: addresses of business partners are retrieved using
To check whether the queries are implemented correctly, go to the integration-tests folder and remove the
@Ignore annotation for the following test:
Build and run test, on the srv folder and make sure that you get SUCCESS in all the categories shown in the console of WebIDE.
If the unignored test do not show errors, congratulations! You have successfully integrated SAP S/4HANA with your application.
To push your changes to the connected GitHub repository, choose "Git Pane" on the right, choose the files that you have modified, enter the message in the "Commit message" window, and choose commit and push: