Backendless Support
 
Answered

Check the presence of an object by id

Hi, I'm having a problem trying to verify from the server code that an object exists in a table. I have the potential id and I want to find out if there is already a chat (with this id) in the table (and in case modify it) or the isn't (and in case create it). I tried to use the code below, but still, when it should create the object, it gets an error and doesn't start the create function.

Can anyone help me figure out how to adjust my code or tell me another way for doing this?

Thank you very much

Gabriele

  1. try {

    HashMap chat = (HashMap) Backendless.Persistence.of( "Chats" ).findById( id );

    if (!chat.isEmpty()){

    // modify the object

    Backendless.Persistence.of( "Chats" ).save( chat );

    } else {

    createChat(id, sendername, text);

    }

    } catch (Exception e) {

    createChat(id, sendername, text);

    }

Leave a Comment

Comments (2)

photo
1

Use the following whereClause query:

objectId = 'YOUR-OBJECTID-VALUE'

If the result is an empty collection, the object does not exist.

photo
1

Ok, thank you!