Support Topics Documentation Slack YouTube Blog

Update is creating new records

***Updated code for missing field. I am now getting the following error:

Caused by: BackendlessException{ code: ‘1110’, message: ‘Column ‘nextmeterread’ specified twice’, extendedData: ‘{}’, detail: ‘Column ‘nextmeterread’ specified twice’ }
at com.backendless.Invoker$SyncResponder.errorHandler(Invoker.java:122)
at com.backendless.core.responder.AdaptingResponder.responseHandler(AdaptingResponder.java:79)

This makes it appear that java.Date objects don’t translate to Backendless DateTime objects? Is there a way to map this correctly?
Here’s the java.Date I’m attempting (Sun Jan 24 12:57:56 CST 2021)

Expected Behavior

  1. Save action is supposed to find existing record, by objectId, and update

Actual Behavior

  1. Device is queried from database, a field is altered, and save is called
  2. Save completes with no errors, but there is a new record in the table instead of updating existing
    Note:
    When debugging and stopping just before the save, I can confirm the object has the “ObjectId” field filled in with the correct value from the database, so I know it is pulling the information.

Object class:

public class Device {
//region fields
@MapToProperty(property = “objectId”)
private String ObjectId;

@MapToProperty(property = "meterreadingperiod")
private String MeterReadingPeriod;

@MapToProperty(property = “nextmeterread”)
private Date NextMeterRead;

}

Code to retrieve object, update field and save:

         try {
            String whereClause = "objectId = '" + deviceIds[0] + "'";
            DataQueryBuilder queryBuilder = DataQueryBuilder.create();
            queryBuilder.setWhereClause(whereClause);
            device = Backendless.Data.of(Device.class).find(queryBuilder).get(0);
        } catch (Exception e) {
            Log.e("MeterReading", "error: can't load device: " + e.toString());
        }
        Log.i("MeterReading", "after device load");
        if (device != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
            Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            //Default to Annual (1 year from now)
            c.add(Calendar.DATE, 365);
            if (device.getMeterReadingPeriod().equalsIgnoreCase("QUARTERLY"))
                c.add(Calendar.DATE, 90);

            
            nextMeterReadingDate = c.getTime();
            device.setNextMeterRead(nextMeterReadingDate);
            Backendless.Persistence.save(device);
            Log.i("MeterReading", "after device save for meter reading update");
            messageText.append("Meter Reading submitted, next reading reminder set: ")
                    .append(sdf.format(c.getTime()));

        } else {
            Log.e("MeterReading", "error: cant find Device");
            messageText.delete(0, messageText.length());
            messageText.append("Error: Device not found!");
        }

Hi @Mark_Moline,

Please make sure the Device class declares the objectId field (public), or it is private, make sure to add the getter and setter methods.

Regards,
Mark

Device.txt (3.7 KB)
This is the class. It has the fields and getters/setters public

The issue seems to be more with the date mapping now, than generating new records. At least according to the error shown

Do not use MapToProperty for objectId. I believe that’s the reason it is not initialized.

That works! I had to reference this topic and look at the TestColumnName.java class to see that the mapping of non-objectId columns should be on the getter.

Thanks!