Can you programatically use autocreate relations?

Hi there. Servoy newbie here, so forgive me if this is blindingly obvious or has been covered before (I searched the forum, honest).

One of my favorite tricks in FMP is to set up a relationship with multiple critera, set to allow automatic creation of records. I can use this relation to easily create (or update) a lot of child records for a found set, using a simple looping set-field script. Set a field via the relation, and, boom!, the child’s created and automatically populated. The nice thing is if the child already exists, the set field command simply updates the appropriate field in the child.

So, in getting my feet wet with Servoy, I’ve tried to do something similar, and, not surprisingly, I haven’t gotten very far.

If I put the related fields on a layout linked to the parent table, I can manually click in a field, enter some data, and when I click out of the field, the related record gets created, with fields populated much as in FMP.

My attempts to write a method to do this haven’t worked. I loop through a found set, and use form.related_table.field = newValue.

And nothing happens.

So either I’m doing something wrong which should be blindingly obvious, or it just doesn’t work this way.

Can someone help me?

Eric Taub

Hi Eric,

In Servoy, the record is not created just by setting the value - you need to create the record.

You can create the record through the relation (if you have checked “Allow creation of related records”):

yourRelationName.newRecord([addToTop],[changeSelection])

So your method would look something like:

yourRelationName.newRecord(true,true)
yourRelationName.field = value

Hope this helps - and welcome to Servoy!

bcusick:

yourRelationName.newRecord(true,true)

yourRelationName.field = value




Hope this helps - and welcome to Servoy!

And if you add an if statement you can achieve the goal of updating if the child record exists or otherwise create the child.
Pseudocode follows (look for the right syntax):

if( !hasRecords(yourRelationName) ) // Create the record if related foundset is empty
{
     yourRelationName.newRecord(true,true)
}

yourRelationName.field = value // update the value on the child record