Developing a Custom Key Metric Microservice in SAP Build Code

Objective

After completing this lesson, you will be able to build a microservice using SAP Build Code to serve a custom key metric, suitable for display within SAP Sales Cloud and SAP Service Cloud Version 2.

Creation of a New Project and Configuration of the Manifest File

In this lesson, you will learn how to develop a simple microservice that provides custom key metric data for SAP Sales Cloud and SAP Service Cloud Version 2. By the end of this lesson, you will have successfully deployed a Node.js based microservice that delivers key metric data, ready to be integrated into the SAP Sales Cloud and SAP Service Cloud Version 2 environment.

You'll follow the same process you learned earlier Developing a Custom Logic Microservice in SAP Build Code. If you need a reminder, refer to that lesson for detailed steps on how to create a new project, initialize Node.js, and configure the manifest.yaml file.

Start by opening your SAP Build Code Dev Space. Create a new project from a template and choose Basic Multitarget Application. In the next screen, provide a name for your project, for example: btp-microservice-keymetric.

This will create a new multitarget application structure in your workspace.

Once the project is created, open a terminal and initialize the Node.js project environment. Execute the following commands:

  • npm init -y
  • npm install express

Caution

Be aware that the setup differs slightly from the subcase microservice. For the key metric microservice, only the express library is installed because the microservice returns the sample data synchronously and does not call SAP Sales and SAP Service Cloud Version 2 APIs. Therefore, the axios library is not required.

These commands prepare the project for Node.js development by generating the package.json and package-lock.json files, as well as the node_modules folder.

In the SAP Business Application Studio, the npm init and npm install commands executed in the terminal prepare the Node.js project development by generating the package.json and package-lock.json files, as well as the node_modules folder.

Next, create a new file named manifest.yaml in the root directory of your project. This file defines the configuration for building and deploying your service to the Cloud Foundry environment within your BTP subaccount. In this manifest, no credentials are required because the key metric microservice does not call back into SAP Sales and SAP Service Cloud Version 2. It is only invoked by the tenant and returns predefined data, unlike the subcase microservice which needs credentials to actively create objects in the tenant.

This screenshot shows the open manifest.yaml file in SAP Business Application Studio, showing deployment settings for the Node.js microservice and its defined parameters.

Use the following code snippet as a template for your reference — you may use it as provided or adapt it as needed to meet your specific requirements.

YAML
1234567891011
--- applications: - name: btp-microservice-keymetric command: node app.js instances: 1 memory: 128M default-route: true buildpack: nodejs_buildpack env: OPTIMIZE_MEMORY: "true"

Implementation and Deployment of the Microservice Logic

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 screenshot shows the open app.js file in SAP Business Application Studio, showing the implementation of the Key Metric microservice. The /customPieChart endpoint is highlighted.

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:

JavaScript
12345678910111213141516171819202122232425262728293031323334
const express = require('express'); const app = express(); app.use(express.json()); console.log('Start...'); // --- Webhook endpoint for custom key metrics --- 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); }); // --- Minimal health check --- app.get('/health', (req, res) => { res.status(200).send('OK'); }); // --- Start server --- 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.