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.
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:
- 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.
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.
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 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).
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.
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.
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.