Update Data Object by objectId

Hi!
I have two activities. In the first one i create an object named “User” and save it as a new row in a new table with a phone number.
Here:

final HashMap user = new HashMap();
user.put("phoneNumber", phoneNumber);
// save object asynchronously
Backendless.Persistence.of("User").save(user, new AsyncCallback<Map>() {
public void handleResponse(Map response) {
Intent intent = new Intent(SMSActivity.this, MainActivity.class);
intent.putExtra(Constants.OBJECT_ID, response.get("objectId").toString());
startActivity(intent);
finish();
}
public void handleFault(BackendlessFault fault) {
Log.e(TAG, "fault " + fault.toString());
finish();
}
});

Then i pass an objectId to the second activity, there i want to update the row with new data.

HashMap user = new HashMap();
user.put( "objectId", objectID );
Backendless.Persistence.save( user, new AsyncCallback<Map>() {
public void handleResponse( Map savedContact )
{
// New contact object has been saved, now it can be updated
savedContact.put( "email", email );
savedContact.put( "name", name );
Backendless.Persistence.save( savedContact, new AsyncCallback<Map>() {
@Override
public void handleResponse( Map response )
{
// Contact objecthas been updated
Log.e(TAG, response.toString());
}
@Override
public void handleFault( BackendlessFault fault )
{
// an error has occurred, the error code can be retrieved with fault.getCode()
Log.e(TAG, fault.toString());
}
} );
}
@Override
public void handleFault( BackendlessFault fault )
{
// an error has occurred, the error code can be retrieved with fault.getCode()
Log.e(TAG, fault.toString());
}
});

This code doesn`t work, it creates a new table called “HashMap”. BTW, is it a good practice to update data by objectId?
Thnx!,

You need to use Backendless.Persistence.of(“User”).save in case you’re working with Maps.

I`m not working with maps, just a regular User.class with name and email etc.

But you’re trying to call save on a HashMap.

i took it from your docs, here: https://backendless.com/documentation/data/android/data_updating_data_objects.htm

Thanks, we’ll update the docs in the last example. But all the other examples on this page state the following syntax:

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

Sorry, it`s the map again, can you send me the link to correct docs for updating an existing row?

This doc is correct, you just need to add .of(“YOUR-TABLE-NAME”) part to the save call.

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.Persistence.of( "Contact" ).save( contact, new AsyncCallback<Map>() {
 public void handleResponse( Map savedContact )
 {
 // New contact object has been saved, now it can be updated 
 savedContact.put( "title", "Most favorite" );
 savedContact.put( "phone", "666-666-666" );
 
 Backendless.Persistence.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()
 }
 });
 }

thnx! It works!

Hoa to update an exists object in a row by objectId?

You should first retrieve it, then change the fields you need, then save it again.
Or else you could create a dummy object, set the proper object ID to it and do the same.