Skip to Content

Create a Social Media Backend

Use the Visual Cloud Functions, a part of SAP Build Apps, to build a backend to store social media information for each product, and create a function to do calculations on the server.
You will learn
  • How to create entities in Visual Cloud Functions
  • How to create a function in Visual Cloud Functions
  • How to deploy your backend project
thecodesterDaniel WroblewskiMarch 5, 2023
Created by
thecodester
January 25, 2023
Contributors
thecodester

The application you are building will get business data from the Northwind OData service, but you will also want to create and manage your own data and mash it up in the front end with your business data.

Specifically, you will want to keep track of product ratings, as well as comments about the products.

Backend entities
  • Step 1

    Go to the SAP Build lobby, and click Create

    Create

    Select Build an Application.

    Build an application

    Select Application Backend.

    Create project

    For the project name, enter Social Media Backend, then click Create.

    Log in to complete tutorial
  • Step 2

    Every time a user rates a single product, we want to store that as a single record in an entity.

    1. In the Entities tab, click Create new.

      Rating entity

      Set the entity title to Rating, and click Add.

    2. Add the following fields to the entity:

      Field Type
      Rating Number
      RatedBy Text
      ProductID Text
      DateCreated Date/time
      Rating fields
    3. Click Save App.

    Log in to complete tutorial
  • Step 3

    Every time a user adds a comment about a product, we want to store that as a single record in an entity.

    1. In the Entities tab, click Create new.

      Set the entity title to Comment, and click Add.

    2. Add the following fields to the entity:

      Field Type
      Comment Text
      ProductID Text
      CommentedBy Text
      DateCreated Date/time
    3. Click Save App.

    Log in to complete tutorial
  • Step 4

    We want to create a function that will aggregate the ratings of the products and return a single average rating for each product. If there are no ratings for a product, no record is returned for that product.

    Functions are created using the standard SAP Build Apps formula, based on JavaScript syntax.

    1. In the Functions tab, click Create new.

      Set the name to AverageRating, and click Create.

      New function
    2. Click the + sign in the middle,then List records.

      Add entity to function

      Under Configuration, set the entity to Rating.

      Configuration
    3. Click on Success (on the right).

      Alt text

      Click Output Value and name the output Ratings.

      Click Set Formula, and set the formula to the following:

      JavaScript
      Copy
      GROUP(outputs["Rating / Records listed"].records, item.productId, {productID: key, avg: AVERAGE(PLUCK(items,"rating")), count: COUNT(items) } )
      

      Click Save, and then click Add.

      Output formula
    4. Click Save App.

    What the formula does

    The GROUP function takes our list of rating records, groups them by the product ID, and creates a new list with one element for each product ID.

    The object for each product contains the product ID, the average rating for all the ratings for that product, and the number of ratings for that count.

    Inputs

    On the left, you can define inputs to the function, so the user could send a specific product ID and the calculations could be done only for that product..

    How do you specify the return value of a function (2 correct answers)?

    Log in to complete tutorial
  • Step 5

    In the Deployments tab, click Review and Deploy.

    Deploy

    After reviewing the changes, click Deploy to Development.

    It should take about a minute or two (going from Not deployed to Missing to Progressing to Healthy status). Once you see Healthy, you can add data to the entities and make calls from your front-end app. We will do this in the upcoming tutorials.

    What are the 3 tabs of the Visual Cloud Functions (backend)?

    Log in to complete tutorial
Back to top