Skip to content

Updating Single Object

Blocking API

// the "enntity" object must contain the "objectId" key with a valid
// server-generated value
public Map Backendless.Persistence.of( "TABLE-NAME" ).save( Map entity ) throws BackendlessException
// the "E" class must declare either "public String objectId" field
// or have the getter/setter methods for the "objectId" property:
// public String getObjectId();
// public void setObjectId( String value );
public <E> E Backendless.Persistence.of( E ).save( E entity ) throws BackendlessException

Non-Blocking API

public void Backendless.Persistence.of( "TABLE-NAME" ).save( Map entity, AsyncCallback<E> responder )
public <E> void Backendless.Persistence.of( E ).save( E entity, AsyncCallback<E> responder )

where:

Argument                Description
TABLE-NAME Name of the table where the object represented by java.util.Map will be updated. There must be objectId property in the map. The value of the property identifies the object which will be updated.
E Java class of the data object to update.
entity Java object to persist, must be of type E or java.util.Map (depending on the method used).
responder a responder object which will receive a callback when the method successfully updates the object or if an error occurs. Applies to the asynchronous method only.

Return Value

The synchronous method returns the updated object. The asynchronous call receives the return value through a callback executed on the AsyncCallback object.

Example

Blocking API

``` java
public void updateContact()
{
    // create new contact object first. Then we will update it.
    HashMap contact = new HashMap();
    contact.put( "name", "Jack Daniels" );
    contact.put( "age", 147 );
    contact.put( "phone", "777-777-777" );
    contact.put( "title", "Favorites" );

    Map savedContact = Backendless.Data.of( "Contact" ).save( contact );

    // now update the saved object - the savedContact map now has a valid "objectId" value.
    savedContact.put( "title", "Most favorite" );
    savedContact.put( "phone", "666-666-666" );
    Backendless.Data.of( "Contact" ).save( savedContact );
 }
```

Non-Blocking API

``` java
public void updateContact()
{
    HashMap contact = new HashMap();
    contact.put( "name", "Jack Daniels" );
    contact.put( "age", 147 );
    contact.put( "phone", "777-777-777" );
    contact.put( "title", "Favorites" );
    Backendless.Data.of( "Contact" ).save( contact, new AsyncCallback<Map>() {
      public void handleResponse( Map savedContact )
      {
        // New contact object has been saved, now it can be updated.     
        // The savedContact map now has a valid "objectId" value.  
        savedContact.put( "title", "Most favorite" );
        savedContact.put( "phone", "666-666-666" );

        Backendless.Data.of( "Contact" ).save( savedContact, new AsyncCallback<Map>() {
          @Override
          public void handleResponse( Map response )
          {
            // Contact objecthas been updated
          }
          @Override
          public void handleFault( BackendlessFault fault )
          {
            // an error has occurred, the error code can be retrieved with fault.getCode()
          }
        } );
      }
      @Override
      public void handleFault( BackendlessFault fault )
      {
        // an error has occurred, the error code can be retrieved with fault.getCode()
      }
    });
 }
```

Consider the following class:

package com.sample;

public class Contact
{
  private String objectId;
  private String name;
  private int age;
  private String phone;
  private String title;

  public String getObjectId() {
    return objectId;
  }

  public void setObjectId( String objectId ) {
    this.objectId = objectId;
  }

  public String getName() {
    return name;
  }

  public void setName( String name ) {
    this.name = name;
  }

  public int getAge() {
    return age;
  }

  public void setAge( int age ) {
    this.age = age;
  }

  public String getPhone() {
    return phone;
  }

  public void setPhone( String phone ) {
    this.phone = phone;
  }

  public String getTitle() {
    return title;
  }

  public void setTitle( String title ) {
    this.title = title;
  }
}
The following code saves a new instance of the Contact class and subsequently updates it:

Blocking API

``` java
public void updateContact()
{
    // Create a contact object first. This way (for the sake of the example)
    // there will be a saved object which will be updated after it is created.
    Contact contact = new Contact();
    contact.setName( "Jack Daniels" );
    contact.setAge( 147 );
    contact.setPhone( "777-777-777" );
    contact.setTitle( "Favorites" );

    Contact savedContact = Backendless.Data.of( Contact.class ).save( contact );

    // now update the saved object
    savedContact.setTitle( "Most favorite" );
    savedContact.setPhone( "666-666-666" );
    Backendless.Data.of( Contact.class ).save( savedContact );
 }
```

Non-Blocking API

``` java
public void updateContact()
{
    // Create a contact object first. This way (for the sake of the example)
    // there will be a saved object which will be updated after it is created.
    Contact contact = new Contact();
    contact.setName( "Jack Daniels" );
    contact.setAge( 147 );
    contact.setPhone( "777-777-777" );
    contact.setTitle( "Favorites" );

    Backendless.Data.of( Contact.class ).save( contact, new AsyncCallback<Contact>() {
      public void handleResponse( Contact savedContact )
      {
        savedContact.setTitle( "Most favorite" );
        savedContact.setPhone( "666-666-666" );

        Backendless.Data.of( Contact.class ).save( savedContact, new AsyncCallback<Contact>() {
          @Override
          public void handleResponse( Contact response )
          {
            // Contact instance has been updated
          }
          @Override
          public void handleFault( BackendlessFault fault )
          {
            // an error has occurred, the error code can be retrieved with fault.getCode()
          }
        } );
      }
      @Override
      public void handleFault( BackendlessFault fault )
      {
        // an error has occurred, the error code can be retrieved with fault.getCode()
      }
    });
 }
```

Codeless Reference

data_service_saving_object

where:

Argument                Description
table name Name of the data table where a record must be updated.
object An object with properties that match the names of the table columns, these properties must contain new values for update operation. The objectId property and the corresponding value must be also included in the object.
return result Optional parameter. When this box is checked, the operation returns the updated object.

Returns the updated object.

Consider the following record stored in the employees data table:
data_service_example_update_2

The example below uses the objectId: "18AE9147-8016-412C-8E55-83B3188E153F" to find the record in the data table and update the value in the contactType column from "Personal" to "Work". To update more values in one query, specify the required number properties/column in the object and the new values.

data_service_example_update_1

The result of this operation will look as shown below after the Codeless logic runs:

data_service_example_update_3

Moreover, the operation described above has returned the updated object:

data_service_example_update_4