beforeCreate gives exception "Property 'objectId' must be set by the server. Make sure the property"

Hi!
As the title says, when I’m creating an beforeCreate handler in Java I get the error “Property ‘objectId’ must be set by the server. Make sure the property is not initialized when saving new object”. But I need to set the objectId in the beforeCreate because the object I’m about to add to the database already exists (but my script don’t know that until the beforeCreate looks that up)! How can I set the objectId for the “new” object to instead be this already existing object?
Thanks,
Tobias
EDIT: Forgot my code :stuck_out_tongue:

@Asset( "Standings" )
public class StandingsTableEventHandler extends com.backendless.servercode.extension.PersistenceExtender<Standings>
{

@Override
public void beforeCreate( RunnerContext context, Standings standings) throws Exception
{
String whereClause = "season = " + standings.getSeason() + " AND team.objectId = '" + standings.getTeam().getObjectId() + "'";
BackendlessDataQuery dataQuery = new BackendlessDataQuery();
dataQuery.setWhereClause( whereClause );
BackendlessCollection<Standings> result = Backendless.Persistence.of(Standings.class).find(dataQuery);
if (result.getData().size() > 0) {
if (result.getData().size() == 1) {
Standings existingObj = result.getData().get(0);
standings.setObjectId(existingObj.getObjectId());
} else {
throw new Exception("Multiple matching objects already found -> Something is wrong in the database");
}
}
}

}

Hi, Tobias!

There is no way to set objectId on client. Other ways should be used instead.
As far as I see you’re trying to prevent creation of duplicates in some table. Maybe you can use unique constraint for this purpose?
Another way is to change hanlder in you existing logic, for example, if single existing object is found - you can remove it and save the new one.
Can you please describe the logic you’re trying to implement so we can help you?
best regards,
Alex

Hi Alex!

Thanks for your response.

Let’s see. The thing I’m trying to implement is a “On duplicate key update” feature one could say. I have an API that I fetch information from. This information will contain old and new data so I always do a “save” of this data to my tables on Backendless. The thing is that there is fields that sometimes gets updated so I can’t just save an object and let it sit there for ever, instead I need to update it if it’s already available (instead of creating duplicates) but when the script runs it has no idea of what’s new and what’s old.

I wrote this thread some time ago: http://support.backendless.com/t/on-duplicate-entry-update

and in that thread I got an response that I could do this using a “custom service” but I was hoping it was possible with already existing services as create… I can in my code always do the check before saving but since that would make my client make a lot of requests it’s not the perfect fit for me (but if necessary it’s possible).

Thanks,
Tobias

Ok, I think I’ve got you idea.
You can throw exception and then code execution would be stopped. Using this, I think that the easiest way to solve your case is to change your code in the following way:

......
if (result.getData().size() == 1) {
Standings existingObj = result.getData().get(0);
existingObj.setPropertyOne( standings.getPropertyOne() );
existingObj.setPropertyTwo( standings.getPropertyTwo() );
// ...and so on....
Backendless.Data.save( existingObject );
throw new Exception( "Object has been updated" );
}
.....

All other code you can leave as it is.

Thanks!

It’s really, really close now! This works great to update an already existing object with new data. The only problem now is that my script saves multiple objects after each other and the Exception stops all execution (as expected though with an exception…) Is there some way to stop the code to run the create (as with exception) but without stopping the script that called beforeCreate?

Thanks,
Tobias

Unfourtanely, not. But you can surround this call with try-catch block to suppress these exceptions and avoid stopping.
As I’ve told before, this is one of the easiest ways to solve case. Another way is to incapsulate this logic inside custom service. After deploying the service to your app, you’ll be able to generate sdk for it, attach it to your client project, and call it only in one row of code. This approach would make your client code shorter, and will avoid making extra requests to the server.
If you need help with the second way - we will be glad to help you.
Best regards,
Alex

Ohh okay!

I’m starting to understand what these beforeSaves and so on are for now. I’m coming from Parse and on Parse beforeSave was a function you could call to check (like above) and I was a bit confused until now that Backendless had the same structure! :slight_smile:

But I’ll be moving my checks in to each and everyone of my Custom Services (API) instead for one beforeCreate handler then.

Thanks for your answers Alex!

// Tobias