Technical Articles
Unboxing: Cloud Functions in SAP Build Apps
This is the 2nd blog related to the new feature referred to as Visual Cloud Functions. In the first blog, I discussed how to create entities. In this blog we’ll discuss the other half, building functions that can called from your app.
Note there is really nice documentation for the feature.
Use Case
I was trying to think of use cases for the cloud functions.
- Maybe I have complicated calculations that may be needed in many applications (e.g., discount pricing), and want/need to be maintained separately.
- Maybe I have some calculations that I want to keep private, and not on the client, for example, related to permissions.
- Maybe I have calculations that combine a bunch of backend tables, and I don’t want to bring back all that data to the client.
Here’s my use case:
I used the product and orders entities from the TechEd workshop by Akseli Virtanen and Marc Huber (the GitHub tutorial is here).
- Products has a price for each product.
- Orders has the name of a customer, then a list of products.
So I wanted to be able to call a function and get a customer’s total open cost: finding all the orders for that customer, then getting the price for each product in the list, and adding all that up.
Here goes..
Creating Functions
Just as a reminder, you create functions in a SAP Build Apps backend project, where we already have our entities.
On the Functions tab, click Create New to create a new function.
And we get a canvas to work on. You’ll notice more than a passing similarity to the logic canvas, and more specifically to the JavaScript flow function canvas.
The first thing I did was define the inputs. Since I want to return the cost to a specific customer, I needed the customer’s name, so I created an input called name that was a string.
And finally, I need to specify what I want as the output.
For now, I get a formula editor, like in SAP Build Apps, with access to the inputs and the data entities I retrieved. And I specified the following formula, which:
- Gets all the orders for the customer whose name was passed as an input (SELECT).
- Gets only the lists of products ordered (PLUCK).
- Combines all the items in all the lists to a single list (FLATTEN).
- For each product name in the list, replace with the price associated with the product (MAP).
- Sum all the prices together (SUM).
SUM(MAP(FLATTEN(PLUCK(SELECT(outputs["Order / Records listed"].records,item.name == inputs.name),"product")), FIRST_ITEM(outputs["Product / Records listed"].records).price))
Deploying Functions
Deploying a function is done exactly as you would an entity in the Deployments tab (see my last blog).
And enabling functions is also done as you would an entity. When you are in the Data tab and open your backend, select the Functions tab to see your functions. Note that you cannot browse the “data”, or run or test the function (as you would in the traditional interface to set up a REST API).
Enabling Functions
You access the functions like you do the entities, and pretty much like any service. Go to the Data tab, cllck Add Integration > Visual Cloud Functions, and select your backend.
Once you’ve selected the backend you want, you can see all the entities, and if you click on Cloud functions, you can see the functions. I selected my MyOpenCost. You can then see the inputs and outputs for the function.
And then you can enable it.
Accessing Functions
The magic here is that there is a new flow function, for calling cloud functions.
I simply created a quick form, and then when the button is clicked, it runs the function and puts the result in the text field.
Notice that when configuring the Execute cloud function flow function, the Properties already knows there’s a required input.
Also, notice, that the binding editor knows that the output of the cloud function is cost.
Epilogue
It’s nice to be able to create my own backend, and to create functions on the server, to be shared among several apps and separate from the UI part of the app.
I’m sure they are working on all this, but it would be nice to have to decision-making in the function – e.g., retrieve a record and test the result and if something is true create a record in a different entity, and stuff like that. It would be nice to be able to specify conditions for the list of records, so for example I can check if there are any products with a certain barcode (barcode is not a key).
It would be nice to access other data from within the function, like calling REST APIs, and of course to be able to save files/images within the entities.
But it’s a nice additional to the SAP Build Apps capabilities.
See all my blogs and connect with me on Twitter / LinkedIn
thanks for sharing this blog Daniel. This seems straight forward and easy to follow.
are there any restrictions on how long a function needs to complete? The reason I ask is due to some of those limits exists for executing AWS Lambda for example. Also, the execution time is how the customer is billed.
any documentation on that area is helpful.
For the moment, the only actions are retrieving data from the same backend, and running a function. But as more capabilities are added, this might be an issue.
Kirill Leventcov Do you have any documentation to answer Sergio?
Sergio Guerrero There are currently no restrictions.
thank you for the response Kirill.. is there any public documentation available?
Here is documentation for the Cloud Functions: https://help.sap.com/docs/BUILD_APPS/431746e4c663458aa68d9754b237bfc6/c2f99da6ff24490bbd0e705eb036ce84.html
I am afraid there is not any describing the restrictions, as there are none at the moment. Except for the Free tier of course; more on that here: https://help.sap.com/docs/BUILD_APPS/431746e4c663458aa68d9754b237bfc6/1972e21be8954ae7ba06f2ff8e773184.html
I have come back a second and a third time to this blog to read about some other stuff I had questions regarding these visual cloud functions. Another question I have which I am unable to get an answer from the documentation... Can these functions be consumed from a different UI/project that is not low code? like a rest api? can the function be exposed as a different URL / masked to hide the backend URL and still expose the functionality as if it was a REST API?
I believe that is on the roadmap. Marc Huber can you confirm?
Hi Daniel Wroblewski ,
We have built the UI application using the SAP Build Apps by consuming the Visual Cloud functions to store some code list values however, while deploying the app to SAP BTP, the Visual Cloud functions are not working as expected and it raises 403 (Forbidden).
Please advise will the Visual Cloud functions work in deployed version of app in SAP BTP.
Regards,
Arul
Currently the build and deploy from SAP Build Apps does not support VCF, and if you want to deploy you have to deploy your own approuter. I’m sure they are working on supporting this soon but I can’t say when.
Hi Daniel Wroblewski
Thanks for your response! can you please suggest any workaround to access the VCF for the apps deployed to BTP and we do have own standalone approuter.
Regards,
Arul
Hi Daniel Wroblewski
Thanks for your response! any inputs to access the VCF for the apps deployed to BTP will be really helpful to us.
Regards,
Arul
I'm not an expert on approuter, and I have asked internally for some direction.
Beatrice Pasch Do you know how we could get more info on this?
Hi Daniel Wroblewski , Arulraj Athiseharan - I tried deploying the application using a standalone router where I am getting 401 error (Jwt issuer is not configured). Did you succssed in using standalone approuter?
Kr,
Jay
Unfortunately, I have not been able to do this yet. Marc Huber can you help here?