0
votes

I have two tables that are related as follows:

  • PMLprojects ONE - MANY Inovice_stat

I have a script to create a record in the Invoice_stat table. It goes as follows:

var myProjectList = app.datasources.PMLprojects;
var myProjectListID = myProjectList.Id;
var myDatasource = app.datasources.Invoice_stat;
var myCreateDatasource = myDatasource.modes.create;
now = new Date();
var draft = myDatasource.modes.create.item;

draft.EmailStatus = "Yes";
draft.PaidStatus = "No";
draft.DateCreate = now;

myCreateDatasource.createItem(function(newRecord) {
    var key = newRecord._key;
});

myDatasource.saveChanges();

All the fields are properly populates except the relation to PMLprojects. How can I related the record from Invoice_stat to PMLprojects? I'm getting the following message:

Error log :
com.google.apps.appmaker.client.datasource.AbstractModelDataSource WARNING: Could not select element with key RecordKey{key=private$6, model key=1Y8Ijd68IZyWFllY3d_C9fhAOFtVgKCtH|Gu5LnmmFmZHfEbrL5Ug1fybNaVLSEPn6}. No records bound.

2
both of your comment is correct. I can entry data with those script. But it's not fully complete as I want. On Invoice_stat table, there is field PMLproject_fk that it should linked to table PMLproject. But it always show NULL field. One record on PMLprojects table may have many relation record on Invoice_stat table. And I want to able to list all record on Invoice_stat that is related with one record on PMLproject table. how to do it ? @Markus-melessa - MSFajari
So are you just wanting to display a table on the client that shows all related invoices based on the currently selected project? - Markus Malessa
Yes. I take app maker sample (relation) as reference. In that sample every time I create a employee record, and it belong to a department, it also create a foreign key on employee table. Because this is one to many relation, One PMLProject, may be have more than one Invoice number. I want to create query to group all record belong to a PMLproject . So I need a relation between PMLproject and Invoice_stat, and I can group record based on the same PMLproject. ? @MarkusMalessa - MSFajari
So just create a table that is a direct relation to the project, in order to do that prefetch invoices in your project datasource then in your page have a panel set to project datasource and drag in a table scroll down in your datasource choices to find PMLproject: Invoice_stat (relation) - Markus Malessa
Thanks @MarkusMalessa, I solve this problem, with add one more field on Invoice_stat. - MSFajari

2 Answers

0
votes

Here is some proposed edited code for you to try. However, do remember that if your PMLprojects datasource is not loaded on the client, then this will still fail. I also highly recommend that you check out the official documentation here https://developers.google.com/appmaker/models/relations#modify_associations.

var myProjectList = app.datasources.PMLprojects.item; //change this line to point to an item in the datasource
//var myProjectListID = myProjectList.Id; This line is not necessary 
var myDatasource = app.datasources.Invoice_stat;
var myCreateDatasource = myDatasource.modes.create;
now = new Date();
var draft = myCreateDatasource.item; //you already declared the create mode

draft.EmailStatus = "Yes";
draft.PaidStatus = "No";
draft.DateCreate = now;
draft.YourRelationToPMLprojects = myProjectList; //here is where you create your relation, replace YourRelationToPMLprojects with your actual relation name should show up in code autocomplete

myCreateDatasource.createItem(function(newRecord) {
  var key = newRecord._key;
});

myDatasource.saveChanges();
0
votes

Since you are probably using both tables with the Manual Save mode... then @MarkusMalessa's approach might return you an error. If that is so, you have to make sure that you create the relation after you create the item but before you save changes. For that, take into consideration the following example:

var project = app.datasources.PMLprojects.item; //project item
var ds = app.datasources.Invoice_stat;
var createDs = ds.modes.create;
var draft = createDs.item;
draft.EmailStatus = "Yes";
draft.PaidStatus = "No";
draft.DateCreate = new Date();

createDs.createItem(function(){
  ds.item.PMLproject = project; //here is where you create your relation
  ds.saveChanges();
});

Just remember, this will only work as long as the PMLprojects datasource has already been loaded, otherwise you will probably get an error.