Getting Started with Cloud Code for Data

Getting Started with Cloud Code for Data

In this article you will learn how to write cloud code around content types through an example that implements simple server-side data validation.

For simplicity, the example utilizes the built-in content type Users. Users is just like any custom data type, but features predefined fields and is readily available in every app.

Users have a DisplayName predefined field (among other) that stores the full name of the user. It is not a required field, so you can register users without specifying a value for it. In the steps below you will add server-side cloud code validation that prevents creation (registration) of new users accounts with DisplayName not filled in. The cloud code cancels the register operation and fails with a friendly message.

This type of validation is very common and you should be able to easily translate it in a real application.

To test your server-side validation you will be using the Telerik Platform portal where you can easily register new users through the user interface (UI).

Users is only used for simplicity as this content type is readily available in every Telerik Platform app. The same validation approach can be applied to your custom data (content types).

Steps at a glance:

  1. Create a New User Account
  2. Add Validation
  3. Create a New User Account

Prerequisites

Create a New User Account

Start by creating a new user account through the Telerik Platform portal:

  1. Log in to the Telerik Platform portal.
  2. Click your app.
  3. Navigate to Users > Users Browser.
  4. Click the Add a user button and fill in the form.

    You can enter any values but it is important to leave "Display name" blank for this example as we want to make certain empty values are initially allowed.

  5. Click the Save button to register the user.

    The operation will succeed and you will have one new user account in your app, with empty DisplayName.

Add Validation

Now that you have tested the default behavior, you are ready to change it to disallow user accounts with empty DisplayName.

  1. Click Business Logic > Explorer to open the Business Logic Explorer.

    On the right, under the Cloud Code for Data tree node, you will see a list of all data types that you have, suffixed with .js. Each content type has its own cloud code.

  2. Click the built-in Users content type to edit the cloud code associated with it.

  3. Replace the default cloud code with the following:

    Everlive.Events.beforeCreate(function(request, context, done) {
        var user = request.data;
        if (!user.DisplayName) {
            var errorMessage = 'The "Display name" field must not be empty!';
            var errorCode = 10001;
            var httpStatusCode = 400;
    
            Everlive.Response.setErrorResult(errorMessage, errorCode, httpStatusCode);
        }
        done();
    });
    
  4. Click the Save button to store the new code.

The replacement code checks for a value in the DisplayName field and returns an error if none is present. You defined it in the beforeCreate event handler which means that the code will run just before writing the new data record. The code uses the Everlive.Response.setErrorResult function to send an error to the client in the standard format for Telerik Platform.

Create a New User Account

You are almost done. Now everything is configured properly so that users with no DisplayName are not allowed. The only thing left is to make certain your Cloud Code works as expected.

  1. Go to Users > Users Browser.
  2. Create a new user, leaving the Display name field empty, just like you did before.
  3. Click the Save button.

    You should get the error message from the cloud code you defined.

View the Execution Log

In addition to the error message that you received in the Telerik Platform portal, you can view detailed information about the Cloud Code execution. It includes request information, the contents of the request, response, context, and data objects, as well as date and time information.

  1. Click Business Logic > Explorer to open the Business Logic Explorer.
  2. In the tree on the right-hand side, click Cloud Code for Data > Users.js.
  3. In the middle pane, under the code view, switch between the Console Output and Errors tabs to view JavaScript messages that you code printed to the console or error messages respectively (if any).
  4. On the Console Output tab, click the book icon to view the execution log.

    This sends you to the Business Logic > Logs tab where you can view logs for all server-side logic that you have.

  5. Find the entry marked as BEFORE CREATE on Users and click it.

    The details pane on the right shows execution information.

Summary

With Cloud Code for Data you can easily implement custom data validation on the server side. The example above showcases a very simple scenario, and it is just a glimpse at the power of Cloud Code.

Aside from validation you can also do:

  • external HTTP calls to integrate with other services
  • query other data you have
  • send push notifications
  • and more

See Also

Start a free trial Request a demo
Contact us: +1-888-365-2779
sales@telerik.com
Copyright © 2016-2017, Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved.