Skip to Content
Technical Articles
Author's profile photo Shubham Singhal

How to Extend SAP S/4HANA Business Address Services (or any other SAP S/4HANA application) with Serverless Functions on Kyma Part-II

In this blog post you will learn…

 

 

Overview:

  • We will learn about how we will create serverless function in Kyma Runtime and how they are deployed (i.e. how URLs are created to get which can be used to run serverless functions) .
  • We will also learn about how to use Google APIs to get Geocodes of any inputted address .
  • To Know what is serverless function and need of serverless function refer to previous blog post of this blog series

Prerequisites/Skills:

  1. Access to SAP BTP Cockpit
    You need access to a productive account of SAP BTP Cockpit
    Note:
    This service is available in Trial version of SAP BTP Cockpit
  2. Node.js/JavaScript
    Functions are written in JavaScript for Node.js runtime.
    As such, some knowledge is helpful, however, most of the tutorials can be followed without any knowledge.
  3. ABAP.
  4. Kyma Environment Setup explained in Part-I of this Blog Series

Implementation Steps:

After successfully setting up the Kyma Runtime Environment as described in the steps above, we now must define the Kyma Namespace. This is where all our functions will be defined. The steps below are to set up your Kyma Namespace and get started with the Kyma environment.

1. Open Your Subaccount in SAP BTP Cockpit.

2. Click on Instances and Subscription.

3. Click on Environments.

 

4. Click on the Actions button in your Kyma environment and navigate to ‘Go to  Dashboard’.

5. On the Dashboard click on ‘Select Namespace’ and choose your namespace.

 

Creating Serverless Function

  • Click on ‘Functions’ on left hand pane and then click on ‘Create Function’ to create a new function.
  • Enter name of your choice and press Enter. You will see the screen shown below.
  • There are two sections under code tab:
    • First section will contain our source code to call the Google API.
    • Second section will contain dependencies (if any).
  • We are now going to add code in Source section

 

const https = require('https');

const key = {Enter Your Key Here}

const generateGeoCode = (address) => {

let data = '';

return new Promise((resolve,reject) => {

https.get(`https://maps.googleapis.com/maps/api/geocode/json?address=${address}&key=${key}`,

(response)=>{

response.on('data',(chunks)=>{

data+=chunks;

})

response.on('end',()=>

{

// console.log('check123',data);

// response = data;

// return data;

resolve(data);

})

response.on('error',(error)=>{

console.log(error)

reject(error);})

});

//  console.log("google",response);

})

}




module.exports = {

main: async function (event, context) {

console.log('event',event.extensions.request.query.address);

address = event.extensions.request.query.address;

const output = await generateGeoCode(address);

let {results} = JSON.parse(output);

console.log('add',results[0].geometry.location.lat);

let final = '';

final +=`${results[0].geometry.location.lat}, ${results[0].geometry.location.lng}`

return final;

//console.log('f',f);

}}


In the above Picture you can see that there is a variable key that has been purposely left blank. Here you need to get your credentials from Google Cloud Platform before entering them. you can get them from Here

 

  1. Here we can see that our code is dependent on HTTP module to run so we are going to put that in our dependency:
{

"name": "geocode-api",

"version": "1.0.0",

"description": "geocode api to get latitude and longitude",

"dependencies": {

"https": "^1.0.0"

},

"license": "ISC"

}

 

This Completes first part of our function. Now it’s time to create the URL to send request to API.

 

Creation of URL for Function to place request to API:

 

  • Click on Configuration tab next to Code tab and click on ‘Expose Function’.

 

  • Enter API Rule Name and Host Name and press Enter. This will generate the URL.

 

  • You can run the URL above in your browser and see the output.

Summary:

In this blog post we saw about

  • How to create serverless function in Kyma.
  • How to use Google APIs to find Geocodes using address send by user.
  • And How to deploy the serverless function and generate API URL which can be used to send get request to server.

Next Steps: 

We will be seeing how to Integrate the Kyma serverless function in any SAP S/4HANA applications

For any queries and doubts please comment in comment section I will be more then happy to help you, also you can follow my profile to get updated of upcoming blog posts.

Assigned tags

      1 Comment
      You must be Logged on to comment or reply to a post.
      Author's profile photo Daily stock picks
      Daily stock picks

      Do  you have any video about serverless function in Kyma?