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!
cancel
Showing results for 
Search instead for 
Did you mean: 
nabheetscn
Active Contributor
0 Kudos

What we are trying to make?


In our previous blog we have created a basic SAP Cloud Platform Workflow with forms after all the trouble shooting. Moving on in this part we will be creating our Function which will be hosted on SAP Cloud Platform. The purpose of this function is to read information about all the workflow instances triggered and create a summary of running/completed/error etc. work items.In this part we will test it via API trigger and in next blog we will have a look at a different trigger type.

Activating FaaS service and Hello World Function


We have good number of blogs highlighting how to activate this service in beta features. Following this blog by hemchander we got our service activated and created a basic function named workflowmonitor. This function is a basic hello world one. We added a HTTP trigger to test the function call and got the desired results. Sound so simple but our experiences are never that straight forward, read on.

Hello World Sample Code



module.exports = {
handler: function (event, context) {
console.log('event data ' + JSON.stringify(event.data));
console.log('event type ' + event['event-type']);
console.log('event time ' + event['event-time']);
return 'hello world from a function!';
}
}

Hello World Output



Time to add our Actual code


Since we wanted to read all the workflow instances and summarize based on status, we made use of the request package and added it as a dependency also.

Actual code



const request = require('request');
module.exports = {
handler: function (event, context) {
var opts = {
uri: 'https://bpmworkflowruntimewfs-<url>trial.hanatrial.ondemand.com/workflow-service/rest/v1/workflow-instances',
method: 'GET',
json: true,
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic <token>'
}
};
request(opts, (err, res, body) => {
if (err) { console.log(err); }
const runningWorkflow = body.filter(res1 => {
return res1.status === "RUNNING";
});
return runningWorkflow.length;
});
}
}

 

Dependencies



{
"dependencies":{
"request":"latest"
}
}

 

Testing our function


We created the HTTP trigger and on testing we were expecting the list of workflow instances to be returned as returned in API call via Postman But sadly the output was nothing and no error was shown also.

No output 



Postman Call




We checked by adding some hardcoded return statements in the code they were all working fine only when placed before the GET request call. The hardcoded statements in request response were not getting displayed. This is where we need to dig deep to troubleshoot it Read on!


Troubleshooting our function


One of the thing which we knew was it was an asynchronous call. The serverless function gets executed without waiting for the response to be updated on browser(like it happens when we have html etc.)and hence we don’t see anything on the browser being returned. What to do, that is when we had a look at hello world program and saw some console.log statements. We thought of using the same. We were expecting them to show up in browser developer console but nothing was shown.



Then on searching further found this blog by Murali where he talks about logs of serverless functions. Bingo so all console.log statements are actually writing the logs to this place not to the browser console.


Finally it worked


So after replacing return with console.log in the response received from the request, we can see the summary of the running instances in the log.


const request = require('request');
module.exports = {
handler: function (event, context) {
var opts = {
uri: 'https://bpmworkflowruntimewfs-<url>.hanatrial.ondemand.com/workflow-service/rest/v1/workflow-instances',
method: 'GET',
json: true,
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic <token>'
}
};
request(opts, (err, res, body) => {
if (err) { console.log(err); }
const runningWorkflow = body.filter(res1 => {
return res1.status === "RUNNING";
});
console.log('Running Workflows '+runningWorkflow.length);
});
}
}




What is next?


So now we have our serverless function ready, it is able to read the instances of workflows in the system. As of now it is being called based on the API as and when we want. In the next blog we will explore the scheduling type of trigger for serverless functions, so that it can automatically fetch the daily workflow related data automatically. Feel free to provide your feedback.

 
Labels in this area