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

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);
}

Use the following whereClause query:

objectId = ‘YOUR-OBJECTID-VALUE’

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

Ok, thank you!