Technology Blog Posts by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
Divya_Mandava
Explorer
0 Kudos
550

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/technology-blog-posts-by-members/creating-capm-application-with-node-js...

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. 

Divya_Mandava_55-1757916882746.png

 

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

Divya_Mandava_56-1757916882749.png

 

Divya_Mandava_57-1757916882750.png

 

Divya_Mandava_58-1757916882752.png

 

 

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.

Divya_Mandava_59-1757916882754.png

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

Divya_Mandava_60-1757916882756.png

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

Divya_Mandava_61-1757916882759.png

 

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.

Divya_Mandava_62-1757916882763.png

 

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 

Divya_Mandava_63-1757916882764.png

 

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

Divya_Mandava_64-1757916882765.png

 

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,

  1. a) function(Get Call)
  2. 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)

Divya_Mandava_65-1757916882768.png

command: cds watch (Run the app).

Divya_Mandava_66-1757916882772.png

 

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

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

Divya_Mandava_67-1757916882775.png

 

Enter Stop port and select below one.

Divya_Mandava_68-1757916882778.png

 

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

Divya_Mandava_69-1757916882784.png

 

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.

Divya_Mandava_70-1757916882785.png

 

Step 9: Expand our project.

Divya_Mandava_71-1757916882788.png

 

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).

Divya_Mandava_72-1757916882790.png

 

  • Bind to an HDI container

Divya_Mandava_73-1757916882792.png

 

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

Divya_Mandava_74-1757916882793.png

Divya_Mandava_75-1757916882795.png

 

 

Note: Make sure your Hana Cloud is running

Divya_Mandava_76-1757916882799.png

 

 

Divya_Mandava_77-1757916882802.png

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

Divya_Mandava_78-1757916882803.png

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

Divya_Mandava_79-1757916882805.png

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

cf services

Divya_Mandava_80-1757916882809.png

 

Divya_Mandava_81-1757916882811.png

 

 

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

Divya_Mandava_82-1757916882812.png

 

 

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.

Divya_Mandava_84-1757916882816.png

 

Our deployment looks like below screen.

Divya_Mandava_85-1757916882820.png

 

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

Divya_Mandava_86-1757916882822.png

 

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

Divya_Mandava_87-1757916882823.png

 

 

Divya_Mandava_88-1757916882826.png

 

 

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

Divya_Mandava_89-1757916882827.png

 

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

Divya_Mandava_90-1757916882830.png

 

 

Divya_Mandava_91-1757916882831.png

 

 

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

Divya_Mandava_92-1757916882833.png

 

Divya_Mandava_93-1757916882834.png

Once you click on Review, this looks like below

Divya_Mandava_94-1757916882835.png

Click on import to database

Divya_Mandava_95-1757916882837.png

 

 

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

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

Divya_Mandava_96-1757916882838.png

 

 

Divya_Mandava_97-1757916882841.png

 

 

Now right click and click on Open Data. 

Divya_Mandava_98-1757916882842.png

 

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.

Divya_Mandava_99-1757916882845.png

 

 

 

Divya_Mandava_100-1757916882848.png

Error: Data is not loaded let's 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.

Divya_Mandava_101-1757916882850.png

 

Divya_Mandava_102-1757916882851.png

 

 

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

Command: cds bind -2 <instanceName>:SharedDevKey   

Divya_Mandava_103-1757916882853.png

 

 

Divya_Mandava_104-1757916882854.png

 

 

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

Step 14: Next run cds watch --profile hybrid

Divya_Mandava_105-1757916882856.png

 

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

Divya_Mandava_106-1757916882857.png

 

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.