Skip to content

Inline Update (BackendlessExpression)

Consider a scenario when a numeric property of an object should be updated with an arithmetic operation. For example, the database table below is called Employee and contains employee objects with properties name and hourlyRate.

employees-with-hourlyrate

Suppose you need to modify Sponge Bob's hourlyRate and give him a nice pay bump. The most straight-forward approach would look like this:

  1. Retrieve the object
  2. Modify the hourlyRate property value
  3. Save the object back in the database.

This approach results in two API requests - one to retrieve the object and the other to save it back. This inefficiency can be solved with the "Inline Update" feature discussed in this chapter.

The Inline Update feature is possible with a special class called BackendlessExpression. The example below demonstrates its usage by giving Sponge Bob from the table shown above a raise:

var dict = new Dictionary<String, Object>();
var exp = new BackendlessExpression("hourlyRate + 10");
dict.Add("objectId", "4F38C3BA-7477-45DA-AA32-617F9966C9BE");
dict.Add("hourlyRate", exp);

var result = Backendless.Data.Of("Employee").Save(dict);
// class declaration
public class Emplyee
{
  public String objectId;
  public dynamic hourlyRate;
}

// code to save person instance
Employee entity = new Employee();
entity.objectId = "4F38C3BA-7477-45DA-AA32-617F9966C9BE";
entity.hourlyRate = new BackendlessExpression("hourlyRate + 10");

var result = Backendless.Data.Of<Employee>().Save(entity);

As you can see, the hourlyRate property in the request, while being a column of a numeric data type, has the following value:

new BackendlessExpression("hourlyRate + 10");

Notice the BackendlessExpression value contains an arithmetic expression ("hourlyRate + 10.00"). The database evaluates and executes the expression. The final value in the column will be the result of the expression - 10.00 is added to the value of the hourlyRate column at the time of the API call.

The BackendlessExpression value is always a string. It can reference multiple columns from the same table, but at the present moment is limited only to the basic four arithmetic operations - addition, subtraction, division and multiplication.

The Inline Update (BackendlessExpression) can be used in different APIs:

  • Update single object
  • Update multiple objects
  • Single or Bulk update in transactions

Codeless Reference

The example below demonstrates the BackendlessExpression usage in Codeless logic. The logic updates the Sponge Bob hourly rate by increasing it by 10.00:

backendless-expression-codeless

The Create Expression block is located in the Data API section of the BACKENDLESS group of the Codeless toolbox:

create-expression-codeless