CREATING CAPM APPLICATION WITH NODE.JS BY USING S/4 HANA DATABASE – Part 2

Estimated read time 13 min read

Hello Buddies!!

Have you Subscribed to all our pre-requisites for the development?

No 😐!!!

Don’t worry let’s set it up. Follow the link below!!

https://community.sap.com/t5/welcome-corner-blog-posts/creating-capm-application-with-node-js-by-using-s-4-hana-database/ba-p/14208638

Welcome to next episode of CREATING CAPM APPLICATION WITH NODE.JS BY USING S/4 HANA DATABASE.

In this Episode Lets Create an Capm Application by Using a Template, So that deployment configurations will be added automatically without manual errors.

Step1: Click on the light icon and select Project from Template > Cap Project. 

 

Step2: Click on next and Fill the details like Project name, Runtime environment. I named my project as zcapm_hanadb.

 

 

 

 

Click on Finish. Hurray our app gets generated in few seconds.

Note: Check whether you are subscribed to Hana cloud, which is already there in prerequisites blog, We have also created a instance for that.

Now make sure your Hana db instance is running.

It would take around 10 to 15 minutes to get to running status.

Now let move to SAP BAS where our application got created.

Step3: Run the application as Right click on app> Open in integrated Terminal > Enter command CDS Watch, check whether all the node modules installed or not. Enter Command

npm install

 

Step4: Next enter the command npm install -g hana-cli. In this we are installing hana cloud Command line interface where we need to push our data into.

 

Now lets work on our Project before we connect with Data Base.

Step5: In folder structure under db >data-model.cds(I have re-named it on my own interest).Name should be filename.cds 

 

Lets create our Structure, In this there should be one Unique key for every entity you have created.

Code:

 

namespace zcapm_hanadb;

entity Products {

@title : ‘Sales

Order Number’

key id: Integer;

@title : ‘Product Name’

name: String;

@title : ‘Price’

price: Decimal;

@title : ‘Description’

description: String;

@title : ‘Stock left’

stock: Integer;
}

 

Step 6: In folder structure under srv >cat-service.cds(I have re-named it on my own interest).Name should be filename.cds

 

In this case lets declare our function calls for get and Post. Just function names are declared in this section where we are about to call them in another file.

Important Note: In defining the cds function please remember two main things,

a) function(Get Call)b) action (Post Call)

Code: 

 

using zcapm_hanadb from ‘../db/data-model’;
service CatalogService{
entity Products as projection on zcapm_hanadb.Products;
// get service call
function fnCatalogGetService(name: String) returns String;
// Post service call
action fnCreateRecord(id: Integer, name: String, price: Decimal, description:String, stock: Integer) returns String;
}

 

Step 7: In this let’s create one more file under srv>cds-service.js (filename.js). 

In this file we will write our node js logic for functions we have declared in cat-service.cds.

Code: 

 

const cds = require(‘@sap/cds’);
const {Products} = cds.entities(‘zcapm_hanadb’);
module.exports = cds.service.impl(async function() {
//Get call
this.on(‘fnCatalogGetService’, async (req) => {
const { name } = req.data;
console.log(name, “Hello World”);
// Post call
this.on(‘fnCreateRecord’, async (req) => {
const { id, name, price, description, stock } = req.data;
console.log(“Hello World is working fine”);
})
});

 

Now we have created our very basic structure of entity and has given a service name under it. In order to utilize our service lets deploy our app to S/4 Hana DB and call data from it.

Command: cds build (Find there any errors in the terminal)

command: cds watch (Run the app).

 

Note: If you found error message like in above image, then follow below steps.

First enter Ctrl + C (To End the process)Click = > View > Command Palette.

 

Enter Stop port and select below one.

 

Once it is done, now again enter the cds watch. You will find the screen like below where you you can find your service.

 

Step 8:  Lets connect to our app to S/4 Hana DB.

On the top-left most corner  where our project we have opened, there when you minimize it you will find SAP HANA PROJECTS. Open them.

 

Step 9: Expand our project.

 

Step 10: Under Database Connections we can find the same project name.

Click on the Green socket symbol to bind our project to HDI container (i.e., Hana DB container).

 

Bind to an HDI container

 

Click on new service instance, it will suggest you a name(you can rename it or use it as it is)

 

 

Note: Make sure your Hana Cloud is running

 

 

On clicking on top of Database Connections click on bind all.

Next Click on Hana HD Container symbol which is on the top of project.

Mean while if you want to know what are all services got generated. Enter the command 

cf services

 

 

 

We can see our instance got reflected in sap BTP as well.

 

 

Application gets open here you can find your container with in built database with the instance we have created.

 

 

Step 11: Our service got binded in the HDI Container and let’s deploy our app.

Click on the deploy icon(Rocket icon) under hana projects > your project.

 

Our deployment looks like below screen.

 

Under project > db you can observe all our hana database files got created.

 

Navigate to Hana db explorer, we see our app got deployed. Refresh the page.

 

 

 

 

As our structure is ready let’s add some data in it.

 

Step 12: Right click on your entity and select import data. Filename should be same as entity name which we have maintained in db>data-model.cds

File Data I Have Uploaded Here..

Id

name

price

Description

stock

1

Widget A

19.99

A useful widget for various tasks

100

2

Widget B

29.99

An advanced widget with additional features

50

3

Widget C

39.99

A premium widget designed for expert users

25

 

 

 

 

In Step3 it automatically reads the table from the data imported.

 

Once you click on Review, this looks like below

Click on import to database

 

 

Hurray! we don’t have any errors and it’s successfully imported.

Now import it to our database table Review > Import data.

 

 

 

 

Now right click and click on Open Data. 

 

As our database is having the data now, we need to have scope to access it in our application.

Lets run the application command: cds watch –profile  hybrid.

Here after we will be using this command as will compile the runtime dependencies.

 

 

 

Error: Data is not loaded its fix it up!!!

We need to add the shared key access for the cloud foundry

Step 13: Go to btp cockpit and the service instance that we have created during deployment process, Copy it.

 

 

 

Copy the Bold letters as an Instance name and type in with the command in your app

Command: cds bind -2 <instanceName>:SharedDevKey   

 

 

 

 

Instance key also gets appeared on top of hana DB explorer.

Step 14: Next run cds watch –profile hybrid

 

Click on Products and Check your metadata is loaded or not.

 

Hurray!!! My data got loaded successfully.

In next session lets perform CRUD Operations on S/4 Hana data from SAP Fiori app as Front end…Stay Tuned😉

References:

https://fioriappslibrary.hana.ondemand.com/sap/fix/externalViewer/

https://community.sap.com/t5/technology-q-a/creating-capm-application-with-node-js-by-using-s-4-hana…

https://help.sap.com/fiori_bs2013/helpdata/en/a7/1564526ba1f25fe10000000a423f68/content.htm?no_cache…

Hope it’s helpful 😊

Thanks,
Divya MANDAVA.

 

 

​ Hello Buddies!!Have you Subscribed to all our pre-requisites for the development?No 😐!!!Don’t worry let’s set it up. Follow the link below!!https://community.sap.com/t5/welcome-corner-blog-posts/creating-capm-application-with-node-js-by-using-s-4-hana-database/ba-p/14208638Welcome to next episode of CREATING CAPM APPLICATION WITH NODE.JS BY USING S/4 HANA DATABASE.In this Episode Lets Create an Capm Application by Using a Template, So that deployment configurations will be added automatically without manual errors.Step1: Click on the light = icon and select Project from Template > Cap Project.  Step2: Click on next and Fill the details like Project name, Runtime environment. I named my project as zcapm_hanadb.    Click on Finish. Hurray our app gets generated in few seconds.Note: Check whether you are subscribed to Hana cloud, which is already there in prerequisites blog, We have also created a instance for that.Now make sure your Hana db instance is running.It would take around 10 to 15 minutes to get to running status.Now let move to SAP BAS where our application got created.Step3: Run the application as Right click on app> Open in integrated Terminal > Enter command CDS Watch, check whether all the node modules installed or not. Enter Commandnpm install Step4: Next enter the command npm install -g hana-cli. In this we are installing hana cloud Command line interface where we need to push our data into. Now lets work on our Project before we connect with Data Base.Step5: In folder structure under db >data-model.cds(I have re-named it on my own interest).Name should be filename.cds  Lets create our Structure, In this there should be one Unique key for every entity you have created.Code: namespace zcapm_hanadb;

entity Products {

@title : ‘Sales

Order Number’

key id: Integer;

@title : ‘Product Name’

name: String;

@title : ‘Price’

price: Decimal;

@title : ‘Description’

description: String;

@title : ‘Stock left’

stock: Integer;
} Step 6: In folder structure under srv >cat-service.cds(I have re-named it on my own interest).Name should be filename.cds In this case lets declare our function calls for get and Post. Just function names are declared in this section where we are about to call them in another file.Important Note: In defining the cds function please remember two main things,a) function(Get Call)b) action (Post Call)Code:  using zcapm_hanadb from ‘../db/data-model’;
service CatalogService{
entity Products as projection on zcapm_hanadb.Products;
// get service call
function fnCatalogGetService(name: String) returns String;
// Post service call
action fnCreateRecord(id: Integer, name: String, price: Decimal, description:String, stock: Integer) returns String;
} Step 7: In this let’s create one more file under srv>cds-service.js (filename.js). In this file we will write our node js logic for functions we have declared in cat-service.cds.Code:  const cds = require(‘@sap/cds’);
const {Products} = cds.entities(‘zcapm_hanadb’);
module.exports = cds.service.impl(async function() {
//Get call
this.on(‘fnCatalogGetService’, async (req) => {
const { name } = req.data;
console.log(name, “Hello World”);
// Post call
this.on(‘fnCreateRecord’, async (req) => {
const { id, name, price, description, stock } = req.data;
console.log(“Hello World is working fine”);
})
}); Now we have created our very basic structure of entity and has given a service name under it. In order to utilize our service lets deploy our app to S/4 Hana DB and call data from it.Command: cds build (Find there any errors in the terminal)command: cds watch (Run the app). Note: If you found error message like in above image, then follow below steps.First enter Ctrl + C (To End the process)Click = > View > Command Palette. Enter Stop port and select below one. Once it is done, now again enter the cds watch. You will find the screen like below where you you can find your service. Step 8:  Lets connect to our app to S/4 Hana DB.On the top-left most corner  where our project we have opened, there when you minimize it you will find SAP HANA PROJECTS. Open them. Step 9: Expand our project. Step 10: Under Database Connections we can find the same project name.Click on the Green socket symbol to bind our project to HDI container (i.e., Hana DB container). Bind to an HDI container Click on new service instance, it will suggest you a name(you can rename it or use it as it is)  Note: Make sure your Hana Cloud is running  On clicking on top of Database Connections click on bind all.Next Click on Hana HD Container symbol which is on the top of project.Mean while if you want to know what are all services got generated. Enter the command cf services   We can see our instance got reflected in sap BTP as well.  Application gets open here you can find your container with in built database with the instance we have created.  Step 11: Our service got binded in the HDI Container and let’s deploy our app.Click on the deploy icon(Rocket icon) under hana projects > your project. Our deployment looks like below screen. Under project > db you can observe all our hana database files got created. Navigate to Hana db explorer, we see our app got deployed. Refresh the page.    As our structure is ready let’s add some data in it. Step 12: Right click on your entity and select import data. Filename should be same as entity name which we have maintained in db>data-model.cdsFile Data I Have Uploaded Here..IdnamepriceDescriptionstock1Widget A19.99A useful widget for various tasks1002Widget B29.99An advanced widget with additional features503Widget C39.99A premium widget designed for expert users25    In Step3 it automatically reads the table from the data imported. Once you click on Review, this looks like belowClick on import to database  Hurray! we don’t have any errors and it’s successfully imported.Now import it to our database table Review > Import data.    Now right click and click on Open Data.  As our database is having the data now, we need to have scope to access it in our application.Lets run the application command: cds watch –profile  hybrid.Here after we will be using this command as will compile the runtime dependencies.   Error: Data is not loaded its fix it up!!!We need to add the shared key access for the cloud foundryStep 13: Go to btp cockpit and the service instance that we have created during deployment process, Copy it.   Copy the Bold letters as an Instance name and type in with the command in your appCommand: cds bind -2 <instanceName>:SharedDevKey       Instance key also gets appeared on top of hana DB explorer.Step 14: Next run cds watch –profile hybrid Click on Products and Check your metadata is loaded or not. Hurray!!! My data got loaded successfully.In next session lets perform CRUD Operations on S/4 Hana data from SAP Fiori app as Front end…Stay Tuned😉References:https://fioriappslibrary.hana.ondemand.com/sap/fix/externalViewer/https://community.sap.com/t5/technology-q-a/creating-capm-application-with-node-js-by-using-s-4-hana…https://help.sap.com/fiori_bs2013/helpdata/en/a7/1564526ba1f25fe10000000a423f68/content.htm?no_cache…Hope it’s helpful 😊Thanks,Divya MANDAVA.    Read More Technology Blog Posts by Members articles 

#SAP

#SAPTechnologyblog

You May Also Like

More From Author