2
votes

I have two related tables. This is a relationship where parent can have zero or many notes. I am trying to save a note that's related to original record. Code itself is

  app.datasources.RadiosManualSave.load(function() {
    ...
    var existingRecord = app.datasources.RadiosManualSave.item;
    ... // update some fields of the existingRecord & save later
    try {
      app.datasources.RadiosManualSave.saveChanges(function() {

        var notesCreate = app.datasources.RadioNotes.modes.create ;  
        var newNote = notesCreate.item ;
        newNote.Notes = tempRecord.Notes ;
        //newNote.Radios_fk = existingRecord.Id ;
        newNote.Radios = existingRecord ;

        notesCreate.createItem(function() {
          app.showPage(app.pages.Radios);  
        });
      });  
   } catch(e) {
        showSnackbar('Error saving record');
        app.datasources.RadiosManualSave.clearChanges();
   }
   ...
}

The code fails when I associate the note with the existing record.

The error message is:

Can't associate draft record with record in draft datasource.

However existing record is not in draft datasource and is already in the table. Assigning to the foreign key works without problems.

1
Could you maybe clarify where existingRecord comes from or where the variable is declared? Also in client side code that has a success or failure dependency I would suggest using .saveChanges({success: function() {}, failure: function() {}); instead of try/catch. - Markus Malessa
Agreed, I can change to success or failure and will give it a try but I have a suspicion I will still get the same error. It would be nice if App Maker documented possible errors and reasons for it, rather than having people trying to guess it. Finally I am not a fan of the relations api since it looses some of the flexibility of using foreign keys, especially if the back end is relational. - work monitored

1 Answers

0
votes

I had a similar issue where I had two related tables i.e. Employee and Attendance. Employee could have many Attendance. So when creating an Attendance I had to specify the Employee. On selecting the Employee from drop-down to associate it with Attendance, there would be the same error saying:

Can't associate draft record with record in draft datasource.

Nothing really actually worked. Going through the documentation I found some information about draft records Draft Record

So setting my parent datasource(Employee) to automatic save mode actually did the work.