Support Topics Documentation Slack YouTube Blog

Custom Server API call creating object instead of updating existing

I have built a custom Java API with a call to update an existing record. It does a find for the record, finds it, updates the field, then does a save. Unfortunately the save generates a new record instead of updating the existing. I’ve made sure the class has a full set of getters/setters and the default constructor.

Here’s my log output.

searching for? CA87360A-06CF-466D-FFCE-306C3BDE5400
records found 3
existing record? Optional[DeviceUsage{Id=‘3D839E06-C647-76EC-FF35-F6939C33CC00’, DeviceId=‘0D4A9057-B9D8-95AA-FF36-8229B0339000’, MobileDeviceId=‘CA87360A-06CF-466D-FFCE-306C3BDE5400’, UsageCount=4}]
Trying to update existing Device Usage DeviceUsage{Id=‘3D839E06-C647-76EC-FF35-F6939C33CC00’, DeviceId=‘0D4A9057-B9D8-95AA-FF36-8229B0339000’, MobileDeviceId=‘CA87360A-06CF-466D-FFCE-306C3BDE5400’, UsageCount=4}
Trying to update existing Device Usage DeviceUsage{Id=‘3D839E06-C647-76EC-FF35-F6939C33CC00’, DeviceId=‘0D4A9057-B9D8-95AA-FF36-8229B0339000’, MobileDeviceId=‘CA87360A-06CF-466D-FFCE-306C3BDE5400’, UsageCount=5}
Trying to save Device Usage DeviceUsage{Id=‘3D839E06-C647-76EC-FF35-F6939C33CC00’, DeviceId=‘0D4A9057-B9D8-95AA-FF36-8229B0339000’, MobileDeviceId=‘CA87360A-06CF-466D-FFCE-306C3BDE5400’, UsageCount=5}
Device Saved:DeviceUsage{Id=‘3D839E06-C647-76EC-FF35-F6939C33CC00’, DeviceId=‘0D4A9057-B9D8-95AA-FF36-8229B0339000’, MobileDeviceId=‘CA87360A-06CF-466D-FFCE-306C3BDE5400’, UsageCount=5}

It does a search by criteria, finds 3 that match, then filters for the single record that matches exactly. After that the log shows the record, with a “UsageCount = 4”, increments the count to 5, then attempts to save it back to the database, and even says it saved successfully. However, instead of updating, it creates a new one (see the DB screenshot. 2nd highlighted row should’ve update instead of 1st highlighted row being generated)

Thanks, Mark

Hi @Mark_Moline,

Make sure the class you use to represent the retrieved objects declares either the
public String objectId field
or
public String getObjectId() and
public void setObjectId( String value ) getter/setter methods.

Please let me know if that solves the problem.

The reason the code saves a new object is the save operation checks whether objectId is present or not. If it is not present, Backendless will save a new object, otherwise, it will update the existing one.

Regards,
Mark

Mark, thanks. I have a mapping from objectId to Id. See below. To confirm, I need to keep the objectId mapping exact and cannot rename this field in any way?

FYI. i did the change and this appears to be correct. Thanks!

Correct. The objectId, created and updated properties are somewhat special and should not be renamed through mapping. I’ll make sure we update the docs to say that.

Cheers,
Mark