In this section, you will implement the core logic of your custom microservice.
The goal is to create a microservice that returns key metric data, which can later be used to visualize business insights in SAP Sales Cloud and SAP Service Cloud Version 2.
Create a new file named app.js in the root folder of your project.
This file defines the logic of your microservice, including authentication, API communication, and endpoint handling.
Unlike the subcase example mentioned earlier, Developing a Custom Logic Microservice in SAP Build Code, this microservice does not trigger an action in SAP Sales Cloud and SAP Service Cloud Version 2. Instead, it provides a simple data service that returns predefined key performance indicators to SAP Sales Cloud and SAP Service Cloud Version 2.
The function associated with /customPieChart acts as a webhook endpoint that delivers static metric data as a JSON response. This makes it easy to test and visualize performance insights directly within SAP Sales Cloud and SAP Service Cloud Version 2. In a real-world scenario, the static data could be replaced with live data fetched from an external system or database, making the service dynamic and adaptable to real business use cases.
In the subcase microservice, the endpoint /onCreateSubCase was designed to react to an event coming from SAP Sales Cloud and SAP Service Cloud Version 2. It was triggered when a new case was created, and then performed an action in the backend. In contrast, the key metric microservice uses the endpoint /customPieChart, which serves data upon request. Instead of being triggered by an event, this endpoint is called directly by SAP Sales Cloud and SAP Service Cloud Version 2 as an API call and returns the predefined key performance indicators as JSON data.
This setup demonstrates how microservices encapsulate specific logic into functions that can be reused, extended, or replaced independently.
This microservice includes console.log statements that write runtime and request-related information to the Cloud Foundry application logs, which can be viewed using commands like cf logs <app-name> or directly in the SAP BTP cockpit (Go to your Global Account → Subaccount → Cloud Foundry → Spaces → Choose your Space → Choose your application → Logs)
The /health endpoint provides a simple availability check to verify that the microservice is running and reachable.
Insert the following code into your app.js file:
12345678910111213141516171819202122232425262728293031323334
const express = require('express');
const app = express();
app.use(express.json());
console.log('Start...');
app.post('/customPieChart', (req, res) => {
const response = {
total: 35.0,
data: [
{ name: 'Incomplete data', y: 7 },
{ name: 'Credit blocks', y: 18 },
{ name: 'Billing blocks', y: 10 }
]
};
console.log('Custom Pie Chart data requested');
res.status(200).json(response);
});
app.get('/health', (req, res) => {
res.status(200).send('OK');
});
const port = process.env.PORT || 4000;
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
As explained previously, Creating a Simple Webapp in SAP Build Code and Deploying It to the Cloud Foundry Runtime, use the cf login command in your terminal and provide your Cloud Foundry API endpoint, origin, organization, and space. For example, the following command can be used:
cf login --origin learningcx-platform -a https://api.cf.eu10-005.hana.ondemand.com -o "SAP - CX Extensibility_extension-c4h07i" -s dev
You may simply use cf login and then follow the instructions in the terminal if you’re using the default SAP Identity Provider.
Once logged in, you can deploy your application using the following command: cf push.
Once the deployment is complete, the terminal will display information about the deployed application, including its route. The route will later be used to integrate the key metric service into SAP Sales Cloud and SAP Service Cloud Version 2.
Summary
- A new microservice project was created including Node.js initialization, and manifest configuration.
- The microservice logic was implemented in app.js , where the /customPieChart endpoint was added to deliver predefined key metric data on request.
- Console log statements were included to support basic debugging and runtime monitoring, and a /health endpoint was added to verify that the microservice is running and reachable.
- After configuration, the service was deployed to the Cloud Foundry environment using cf push , generating a route that can be used for integration with SAP Sales Cloud and SAP Service Cloud Version 2.