newRecord in a relation

Hello All,
Sorry this seems very daft but everything I have read implies that if I select a relation from a form I should just be able to do a newRecord on it.

var id = forms.frmBooks_MainDetails.books_to_author.newRecord(true);

However this just gives me the error:

TypeError: Cannot call method “newRecord” of null (C:_Data\Servoy\Trains\globals.js#191)

I don’t actually use the relationship on the form so is that my problem?
Any thoughts would be appreciated.
Caroline

Hi Caroline,

Assuming you didn’t made any typos in your code there can be 2 things going on here:

  • forms.frmBooks_MainDetails has no records and therefor any relation on it won’t work. I.e. there is nothing to relate on.
  • forms.frmBooks_MainDetails is not based on the same datasource (i.e. table) as the ‘books_to_author’ relationship.
    It really doesn’t matter if you use the relationship on the form or not as long as the relationship and the form are based on the same datasource you can use it in your code.

Also you seem to expect to get the record PK back from the newRecord() function. This however only returns a true/false to denote the successful creation of the record.

Hope this helps.

ROCLASI:
Hi Caroline,

Assuming you didn’t made any typos in your code there can be 2 things going on here:

  • forms.frmBooks_MainDetails has no records and therefor any relation on it won’t work. I.e. there is nothing to relate on.
  • forms.frmBooks_MainDetails is not based on the same datasource (i.e. table) as the ‘books_to_author’ relationship.
    It really doesn’t matter if you use the relationship on the form or not as long as the relationship and the form are based on the same datasource you can use it in your code.

Also you seem to expect to get the record PK back from the newRecord() function. This however only returns a true/false to denote the successful creation of the record.

Hope this helps.

In my experience, odds are the first hypoteses is the one. I’ve struggled so many times with null related foundsets I can hardly remember their number :P
As for the second part, newRecord actually returns the index of the newly created record, so you could access its primary key with something like

var _authorsFs = forms.frmBooks_MainDetails.books_to_author;
var id = _authorFs.getRecord(_authorFs.newRecord(true)).getPKs()[0];

provided that you have a normalized pk and the books foundset isn’t empty ;)

Robert thanks for your reply, but neither of those points seem to be my problem!

forms.frmBooks_MainDetails definitely has records as that is my main entry screen and it is based on books, I even tried showing the related field on the form and that shows correctly - so the relationship must be working!

But as soon as the child is empty you can no longer do anything!!!
Can I not use the relationship to create a child in a Many-to-One relationship??

Hi,

studiomiazzo:
As for the second part, newRecord actually returns the index of the newly created record…

You are absolutely correct! :oops:
When used over a relationship you do get the index of the record.
However when used directly on the controller you get a boolean. I mixed that up.
Thanks for correcting me. :)

studiomiazzo:
In my experience, odds are the first hypoteses is the one. I’ve struggled so many times with null related foundsets I can hardly remember their number :P

studiomiazzo,
I suspect you are correct and the “null related foundset” is the issue, if so how do you overcome it?
Many thanks

Hi Caroline,

caroline.j:
But as soon as the child is empty you can no longer do anything!!!
Can I not use the relationship to create a child in a Many-to-One relationship??

Ah!
So you have a FK on books and you want to create a new author on that book. That won’t work.
See it this way ;) How can a non-existing author write a book first and then get ‘born’ :)

In computer terms. Your FK is (most likely) NULL and you can’t use that in a relationship (i.e. join).
So how to solve this?
Create a new author record directly on the Author table and then put the newly created PK value in the FK column.
I.e. don’t use the relationship for creating new authors from books.

Hope this helps.

Ok when you explain it like that it makes sense. I’ll stop trying to use the relationship here!
Many thanks for all the help
Caroline

caroline.j:

studiomiazzo:
In my experience, odds are the first hypoteses is the one. I’ve struggled so many times with null related foundsets I can hardly remember their number :P

studiomiazzo,
I suspect you are correct and the “null related foundset” is the issue, if so how do you overcome it?
Many thanks

Ahahah, sorry bur “hearing” someone else calling my username (it’s the name of the job consultancy firm we work for) made me laugh, eheheh :mrgreen:
Back to your issue, I don’t think you really have any problem. You simply need to check whether the related foundset exists before accessing if, i.e.

var _authorsFs = forms.frmBooks_MainDetails.books_to_author;
if(_authorsFs)
{
    // put the rest of your logic here
}
else
{
    // put your error-dealing logic here
}

EDIT: in light of the new post from ROCLASI I think he has already answered your doubts: Servoy (and logic as well) only allows you to create child records on the ‘n-side’ of a 1 to N relation. I’ve experienced the same while dealing with persons and their role within a company. I had two entities: Person and PersonRole. Basically I was trying to create the person from the role s/he covered. But if you think a little about that, the entity “ruling” the relation is Person, not PersonRole. PersonRole is the foreign entity referencing a Person’s primary key, not the other way around. So the following code won’t work

var _person = personRole_to_person.getRecord(personRole_to_person.newRecord());

since Servoy doesn’t know yet the id of the Person you’re asking it to create!
So what’s the solution? Just first create your Person (Author in your case) and then assign it to the book you’re interested in, i.e.

var _authorsFs = databaseManager.getFoundSet(yourServer, yourAuthorsTable);
var _newAuthorId = _authorsFs.getRecord(_authorFs.newRecord()).getPKs()[0];
forms.frmBooks_MainDetails.foundset.authorId = _newAuthorId;

That should do the trick ;)

@ROCLASI: we’re all here to help each other. For example, I didn’t know the controller version of the method returned a boolean! One never stops learning eheh :D

Hi Caroline,

Just to be clear, you can still use the relationship to show related data.
It’s just that creating author records will not work over this relationship.

Brilliant many thanks to both of you.
(Do I owe you double time as it’s a Saturday?)
Enjoy the rest of your weekend.
Caroline