Relational save not adding the related objectId

Hi, there!

I’m trying to save a new record that have relationship with two existing records, is a new Device that have a pointer to the DeviceModel and to the User that it belongs (not the Owner).

When I save the record, all data is saved correctly except for the relationships. I’m getting the values from a html element and saving in a variable, then trying to save with this:

var objDevice = new Device( {
  name: deviceNome,
  serial: deviceSerial,
  patrimony: devicePatrimonio,
  model: deviceModel,
  user: userId
    });


Backendless.Persistence.of( Device ).save( objDevice );

Both deviceModel and userId variables values are their existing objectId.

What is the right way to save this data?

Regards,

Tiago Schmidt

Tiago,

deviceModel must be an object with the “objectId” property. So something like this should work:

var objDevice = new Device( {
name: deviceNome,
serial: deviceSerial,
patrimony: devicePatrimonio,
model: { objectId: "deviceModel-objectId-value", ___class:"DeviceModelTableName"  },
user: { objectId:userId, ___class:"Users" }
});

On line 5, make sure to change “deviceModel-objectId-value” to the specific objectId value. And put the right table name instead of “DeviceModelTableName”.

The ___class property is required for all related elements so Backendless would know which table it should be working with.

Cheers,
Mark

Worked perfectly! Thanks!

Best regards,

Tiago