Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
Dan_Wroblewski
Developer Advocate
Developer Advocate
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 marchuber03 (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.


Then I needed the data, so I added both the products and orders table to the flow. To do this, you click the + in the middle, say you want a list of records, and then specify from which entity. (Here, there is a strong resemblance to SAP Build Process Automation process canvas, as well as the Data tab of SAP Build Apps.)



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:

  1. Gets all the orders for the customer whose name was passed as an input (SELECT).

  2. Gets only the lists of products ordered (PLUCK).

  3. Combines all the items in all the lists to a single list (FLATTEN).

  4. For each product name in the list, replace with the price associated with the product (MAP).

  5. 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 daniel.wroblewski#content:blogposts and connect with me on Twitter / LinkedIn

 
14 Comments