Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
Showing results for 
Search instead for 
Did you mean: 
Active Contributor


The trigger for this blog is very a good SAP Cloud Platform API management course going on at openSAP.  While going through one of the unit where it was shown how to add different elements in API flow via policy designer for example quota, Verify API key etc. That is when it occurred to me can we have our custom logic added during the request cycle. That is where we found this Extension Policies option where we can add our own Javascript/Python code.  So i decided to dig more.

I decided to use our current ES5 SalesOrderSet call from the course and started to experiment.The moment you start challenges are the first thing which we always face so read on.

How do we find out the available variable names?

The first challenge for me was how will i read the request or response in the javascript file. On searching further and reading the great blogs by svenhuberti found the way to read or write the data from or to the request or variables.

Sample code to read data from request
/* Variable name shall be available*/
context.getVariable(<Variable name here>);

Sample code to write data to request
/* Variable name if does not exist it will be created*/
context.setVariable(<Variable name>, Variable Value)

So finally we found out how to use the JavaScript the next step was to try it .So in our simple POC we are going to add a new variable value for incoming request and modify the response (delivery status) received from Gateway system. This POC had its own set of learning so read on.

Discovering Debugging API calls.

First we added the code for incoming request where i was setting the value of new variable. This variable can be used further.

The moment i added my code to JS for incoming request  the first thing i was faced with was 500 error. Although the error was pretty basic, request variable is not in JSON format so change it, the error was fixed. But i did not know whether my script was setting the  variable or not that is where i discovered debugging and found out my script was working.

Now we were able to add variable next step was to modify the delivery status text to a custom one in response. This was verified in the output.
var response = JSON.parse(context.getVariable("response.content"));
switch(response.d.DeliveryStatus ) {
case 'D':
response.d.DeliveryStatus = 'Delivered the goods';
case 'C':
response.d.DeliveryStatus = 'Cancelled the goods';
response.d.DeliveryStatus = 'Goods Not available';

What is next

So after exploring Javascript Extension Policies option i will be trying out Service callout option to call any external API’s. I believe there are many hidden features which will be discovered in coming weeks of the Open SAP course. Will share the experience, feel free to provide your feedback open to all ears.

#Mustread API blog series by divya.mary just found out, great stuff going on.
Labels in this area