In a basic TypeScript program, read the Business Partner API service using SAP Cloud SDK.
Prerequisites
For the complete execution of current exercise, you must execute the following activities first:
- Execute the following exercises:
- Creating your Pay-As-You-Go Account in SAP BTP
- Activating the APIs in SAP S/4HANA Cloud: this exercise activates the required API in the S/4HANA Cloud tenant.
In case you want to run this exercise in SAP Business Application Studio, just skip this step.
In case you want to run this exercise in Visual Studio Code, make sure that the following software is installed on your laptop:
Software Version Type Node.js and npm latest application Git latest application Visual Studio Code latest application - Retrieve the following information you will need for the exercise:
Information How to get it API Key - Go to https://api.sap.com/api/API_BUSINESS_PARTNER/overview
- Log-in with your user and password.
- Choose Show API Key.
SAP S/4HANA Cloud tenant URL Use the same tenant URL where you executed the Activating the APIs in SAP S/4HANA Cloud exercise. Service user name The service user you created in the previous exercise, for example ADDRESS_MANAGER_###. Password The password you created in the previous exercise, for example WelcomeToTheClouds1!. You now create the initial application project: Open your IDE (SAP Business Application Studio), then clone the course GitHub repository to a local folder.
Use the following information:
Information Value GitHub repository URL https://github.com/SAP-samples/cloud-sdk-learning-journey.git choose Clone from Git, enter the repository URL, open the cloned repository
Steps
In your IDE (SAP Business Application Studio), create and run a TypeScript Hello World application.
Switch to Git branch
tsminimal_v1.0_hello
. You choose the branch in the left corner of the bottom bar, or you open a Terminal and run:Review the source code in the
app.ts
file. Review thepackage.json
file.To build the application, open a Terminal and run:
To run the application, choose F5 (Run → Start Debugging) or run the command:
Now we have a basic TypeScript project. Generate the SAP Cloud SDK client for the Business Partner (A2X) API.
In the terminal run
In this branch, we modified the project in the following way:
File Changes package.json we included a devDependency to @sap-cloud-sdk/generator
.API_BUSINESS_PARTNER.edmx - We created a folder
service-specifications
at the root of the project. - We downloaded the EDMX file for the business partner service from the API Business Hub.
- We copied the
API_BUSINESS_PARTNER.edmx file
into theservice-specifications
folder. - To adjust file encoding problems, we right-clicked on the file textual content and have chosen Format Document.
options-per-service.json We created an
options-per-service.json
file in theservice-specifications
folder with the following content:Code snippetExpand{ "service-specifications/API_BUSINESS_PARTNER.edmx": { "packageName": "business-partner-service", "directoryName": "business-partner-service", "basePath": "/sap/opu/odata/sap/API_BUSINESS_PARTNER" } }
- We created a folder
Generate the SAP Cloud SDK client running the following commands in the terminal:
Code snippetExpandnpm install npx generate-odata-client --input service-specifications --outputDir services --optionsPerService service-specifications/options-per-service.json
With this command, the SAP Cloud SDK client is created within the
services/business-partner-service
folder.
Modify the project so that the
app.ts
program reads the service using the SAP Cloud SDK. Extract the BUSINESS_PARTNER, FIRST_NAME, LAST_NAME, ADDRESS_ID, COUNTRY, CITY_NAME for all the Business Partners whose last name is Smith.In the terminal run
In this branch, we modified the project in the following way:
File Changes app.ts We included code to:
- Import the BusinessPartner entity and the businessPartnerService function.
- Query Business Partner information, including the detail of required columns, filtering criteria, destination service
- Convert the returned information to JSON and write it to the standard output.
Review the new code:
Along with OData Business Partner entities, we can see that there were generated methods to fetch the data. Based on the provided metadata, helpers to query the data were created.
Code snippetExpandimport { BusinessPartner, businessPartnerService, } from "../services/business-partner-service";
BusinessPartner API provides data for thousands of partners. We would like to filter these entries and select only the fields we are interested in. For example, we would like to find partners that have last name "Smith" and display their full name and address. For this, we use the generated businessPartnerApi and businessPartnerAddressApi.
In
app.ts
, find the following line:Enter your API Key (replace
>>>YOUR_API_KEY<<<
).Build and run the application.
Modify the project to query your S/4HANA Cloud system, accessible via the following information (replace ### with your user number):
Information Value URL Your S/4HANA Client tenant URL, for example https://my000000.s4hana.ondemand.com Service user name ADDRESS_MANAGER_### Password WelcomeToTheClouds1! In the terminal run
In this branch, we modified the project in the following way:
File Changes app.ts We change the API with the new URL, User and password.
We remove the assignment of the API Key, since it is not required to access an SAP S/4HANA Cloud system.
Review the changed source file.
In
app.ts
, enter your values for URL, User and Password in the following lines.Code snippetExpand.execute({ url: "https://my000000.s4hana.ondemand.com", username: "ADDRESS_MANAGER_###", password: "S4HANA_PASSWORD!", });
Build and run the application.