Backendless Support

Custom server-side code to handle Data Service API events

The custom business logic feature in Backendless allows to add your own server-side code to handle client API requests. This topic reviews how to add the Data Service API handlers

With that type of handler you can intercept and add additional logic for the APIs which store data in Backendless, run queries, update or delete data objects. 

The easiest way to start building server-side Backendless code is the developer console:

  1. Login to your account, select an app and click the Business Logic icon. It is assumed that there are some data tables in the backend worked with. If not, make sure to create a few. 
  2. Since we are going to add an event handler which works with persistent data, click Data Tables from the list of available handler categories. 
  3. Click Add Event Handler, the screen should look as shown below: 

Items in the Event drop-down list correspond to the APIs available in Data Service: 

The Context list shows a list of available data tables. A selection in that list determines for which table the event handler will be processing the API events. 

                                             NOTICE: there is a special value of *(All Context)

4.    When selecting that option the event handler will process API events for all data tables: 

5.   There has been added an event handler for the Order table and selected the Create event. Backendless generated the minimally necessary code for the event handler: 

Take a closer look at the generated code:

import com.backendless.BackendlessCollection;
import com.backendless.geo.GeoPoint;
import com.backendless.persistence.BackendlessDataQuery;
import com.backendless.servercode.ExecutionResult;
import com.backendless.servercode.RunnerContext;
import com.backendless.servercode.annotation.Asset;
import com.backendless.servercode.annotation.Async;
import com.backendless.servercodedemo.models.Order;
import java.util.HashMap;
import java.util.Map;
* OrderTableEventHandler handles events for all entities. This is accomplished
* with the @Asset( "Order" ) annotation. 
* The methods in the class correspond to the events selected in Backendless
* Console.
@Asset( "Order" )
public class OrderTableEventHandler extends com.backendless.servercode.extension.PersistenceExtender<Order>
 public void beforeCreate( RunnerContext context, Order order) throws Exception
 // add your code here
Specifically, notice that one of the arguments for the beforeCreate method is the Order class.
This is a very powerful Backendless feature.

Indeed, the client side of the API may be a REST client, or an iOS or a JavaScript app. That's right - the data describing an order is morphed into an instance of the Java Order class by Backendless and provided to your custom code. The source code for the Order class is also created by Backendless (see the screenshot above).

This is what the Order class looks like:
package com.backendless.servercodedemo.models;
import com.backendless.Backendless;
public class Order
 private java.util.Date updated;
 private String name;
 private java.util.Date created;
 private String ownerId;
 private String objectId;
 public java.util.Date getUpdated()
 return this.updated;
 public String getName()
 public java.util.Date getCreated()
 return this.created;
 public String getOwnerId()
 return this.ownerId;
 public String getObjectId()
 return this.objectId;
 public void setUpdated( java.util.Date updated )
 this.updated = updated;
 public void setName( String name )
 { = name;
 public void setCreated( java.util.Date created )
 this.created = created;
 public void setOwnerId( String ownerId )
 this.ownerId = ownerId;
 public void setObjectId( String objectId )
 this.objectId = objectId;
 public Order save()
 return Backendless.Data.of( Order.class ).save( this );
 public Long remove()
 return Backendless.Data.of( Order.class ).remove( this );
 public static Order findById( String id )
 return Backendless.Data.of( Order.class ).findById( id );
 public static Order findFirst()
 return Backendless.Data.of( Order.class ).findFirst();
 public static Order findLast()
 return Backendless.Data.of( Order.class ).findLast();

The class can be used to check or modify the values received from the client app.

The rest of the process for working with the custom event handler is the same as has been described in the the topics of "Server Code" category, like those described in the related topics below.

Review related topics:

Is article helpful?