relation.newRecord() isn't working

I’m puzzled over why I can’t get .newRecord() to work - I’m getting an error… TypeError: Cannot call method “newRecord”

I create record in table ‘A’
I’m then trying to create a record in table ‘B’ using the tableAtoBrelation.newRecord()

I checked the relation and its set to…
Allow creation of related records
Allow parent delete when having related records
Delete related records

I’m using Servoy 4.1.0 build 651

Any ideas ?

Are you calling databaseManager.saveData(record a) before creating your record b? I use Microsoft SQL with the pk set using dbidentity and I find that until I’ve pushed the newly created record into the database with saveData, relationships aren’t valid.
Steve in LA

Thx for your suggestion, Steve.
I tried issuing a databaseManager.saveData() right before issuing .newRecord() and I get the same error, unfortunately.

The error is “TypeError”, so maybe you have a typo here and Servoy see your relation as a String or another type of var?

If you use code completion (type “.” after the name of your relation and ctrl space if needed), does it propose you the newRecord() method?
Isn’t there a variable somewhere with the same name as your relation that could cause a conflict?

Just some suggestions…

Thanks Patrick, I’m actually using the code completion (which is very handy, btw) so I’m confident that the spelling is good. I double-checked just to be sure and all looks correct.

The only thing that I found works is to create table ‘A’ record then create table ‘B’ record without the relation, then issue a databaseManager.saveData() - then everything shows up correctly as I was wanting. It really isn’t optimal, however, because I’m forcing a save - something I want to leave up to the user to decide.

  //
  // create a SEIS_SET record
  //
  foundset.newRecord();
  foundset.seis_set_id = globals.bizlogic_get_next_sequence('k_seis_set_id_seq', forms.seis_line_view_detail_1.controller.getServerName());
  foundset.seis_set_type = 'SEIS_LINE';
  foundset.active_ind = 'Y'
  
  //
  // create a SEIS_LINE record to match the SEIS_SET record 
  //
  var lv_fs_line = databaseManager.getFoundSet(controller.getServerName(),'seis_line');
  lv_fs_line.newRecord();
  lv_fs_line.seis_line_id = foundset.seis_set_id;  
  lv_fs_line.seis_set_type = 'SEIS_LINE';
  
  //
  // create a SEIS_ACQTN_DESIGN record to match the SEIS_SET record
  //
  forms.seis_acquisition_design_view_detail_1.foundset.newRecord();
  forms.seis_acquisition_design_view_detail_1.foundset.acqtn_design_id = globals.bizlogic_get_next_sequence('k_seis_acqtn_design_id_seq',  forms.seis_line_view_detail_1.controller.getServerName());
  forms.seis_acquisition_design_view_detail_1.foundset.active_ind = 'Y';
  foundset.acqtn_design_id = forms.seis_acquisition_design_view_detail_1.foundset.acqtn_design_id;
  databaseManager.saveData();

what kind of relation is it?
fk → pk or pk → fk?

if (relation)
{
// does it come here
}
else
{
// or here
}

Your code is a little strange. Unless you are storing the new record as an object in a variable, you can’t be sure that you are setting values in the correct record or that your relationship is evaluating the correct record’s key value. When I need to create a new record and then immediately create a record related to that new record, I use code like this:

//Create the parent record
var vRecordA = forms.form_a.foundset.getRecord( forms.form_a.foundset.newRecord() );
databaseManager.saveRecord( vRecordA);
vRecordA.primary_key_id = <Method to get next PK value for Table A>;
vRecordA.value1 = <whatever>;
databaseManager.saveRecord( vRecordA);

//Create the related record
var vRecordB = vRecordA.relationshipTableAtoTableB.getRecord( vRecordA.relationshipTableAtoTableB.newRecord() );
databaseManager.saveRecord( vRecordB );
vRecordB.primary_key_id = <Method to get next PK value for Table B>;
databaseManager.saveRecord( vRecordB);

Like I said in my first post, I use dbidentity to assign primary key values to my records, but this should still work as long as all the values in Record A that are used in the relationship are set and set to the correct types. I tested this in 4.1.3 and it works, but I have used this method for many years.
Steve in LA

Thanks again for your replies.

The relation is PK → FK with the PK being form1 for table ‘A’ and the FK being form2 for table ‘B’.

I have it working but I was trying to avoid having to issue a ‘save’ until the user wanted to save i.e. create record for table ‘A’, then create related record for table ‘B’, show the data on the forms, have the user edit and save or abandon the creation of the new records altogether. If nothing is committed the abandon is very simple, but with records created I need to perform a delete.

It appears that I need to issue a ‘save’ in order for related table ‘B’ data to show up.

you shouldnt need to save the parent record first before you can make a child record
that should go fine for a those kind of relations (orders–>orderitems)

I found the problem - definitely pilot error on my part :oops:

One of the relationships was the other way around and totally explains why things failed.

  // create SEIS_SET
  foundset.newRecord();
  foundset.seis_set_id = globals.bizlogic_get_next_sequence('k_seis_set_id_seq', controller.getServerName());
  foundset.seis_set_type = 'SEIS_LINE';
  foundset.active_ind = 'Y'

  // create SEIS_LINE
  foundset.seis_set_to_seis_line.newRecord();
  foundset.seis_set_to_seis_line.seis_line_id = foundset.seis_set_id;
  foundset.seis_set_to_seis_line.seis_set_type = foundset.seis_set_type;

  // create SEIS_ACQTN_DESIGN
  foundset.acqtn_design_id = globals.bizlogic_get_next_sequence('k_seis_acqtn_design_id_seq', controller.getServerName()); 
  foundset.seis_set_to_seis_acqtn_design.newRecord();
  foundset.seis_set_to_seis_acqtn_design.acqtn_design_id = foundset.acqtn_design_id; 
  foundset.seis_set_to_seis_acqtn_design.active_ind = 'Y';

All three records get created and display correctly in my forms.
Thanks for the responses!