Loading Specific Relation Record?

Find out how to get things done with Servoy. Post how YOU get things done with Servoy

Loading Specific Relation Record?

Postby john1598360627 » Fri Nov 22, 2024 12:32 am

PROBLEM
I created two tables and connected them via a Servoy Relation with a key value. However, this is a One to Many relation. As in, there's multiple records connected to one specific record.

The issue I'm facing is trying to figure out how to load a specific related record to coincide with the main record. Is there a way of doing that?

CODE
Code: Select all
   var record_second = foundset.getSelectedRecord()
   
   /** @type {UUID} */
   var uuid_second = record_second.uuid
   

   forms.form_view.load_2ndUUID( uuid_second )
   
   forms.form_view.load_mainUUID( scopes.MainView.get_main_uuid() )
   
   scopes.NAV.change_forwardForm( 'form_view' )


HELP
Right now I'm getting this secondary record in the 'Many' relation, load it into the foundset & then load the main record in the 'One' relation.... and instead of loading the specified secondary record, it loads another record in the 'Many' relation. Again, I'm not sure how to specify to the foundset which related record I want to load. Is there anything else besides ' foundset.loadRecord( UUID ) ' that I can do that?
john1598360627
 
Posts: 191
Joined: Tue Aug 25, 2020 3:03 pm

Re: Loading Specific Relation Record?

Postby sean » Tue Dec 10, 2024 5:55 am

Hi John,

To be honest it's not exactly clear what you are trying to do. Perhaps you should include the content of the other methods you have referenced, also which forms are based on which data sources.

It's important to understand that all forms having the same data source will share the same foundset instance by default. Therefore, you might see something loaded in one form affect another. It's possible to change which foundset a form uses by calling its controller. i.e. controller.loadRecords(myRelation). This will switch the foundset that is controlled by the form. Another approach is to use named/separate foundsets. This guarantees that a form will use its own, separate foundset instance.

Some docs on this: https://docs.servoy.com/reference/servo ... edfoundset

Hope this helps.

Best,
Sean
Software Engineer
Servoy USA
sean
 
Posts: 383
Joined: Mon May 21, 2007 6:26 pm
Location: USA

Re: Loading Specific Relation Record?

Postby john1598360627 » Tue Dec 10, 2024 10:18 pm

sean wrote:Hi John,

To be honest it's not exactly clear what you are trying to do. Perhaps you should include the content of the other methods you have referenced, also which forms are based on which data sources.

It's important to understand that all forms having the same data source will share the same foundset instance by default. Therefore, you might see something loaded in one form affect another. It's possible to change which foundset a form uses by calling its controller. i.e. controller.loadRecords(myRelation). This will switch the foundset that is controlled by the form. Another approach is to use named/separate foundsets. This guarantees that a form will use its own, separate foundset instance.

Some docs on this: https://docs.servoy.com/reference/servo ... edfoundset

Hope this helps.

Best,
Sean


When I do ' controller.loadRecords(myRelation) ' for whatever reason it'll load the Relation... but not the record the relation is connected to.

Essentially have a 'Main' Record and a 'Related' Record. These are connected by a shared 'Key Value'.

When I load the Related Record by itself, for whatever reason it doesn't load the Main Record. However, when I load the Related Record & then the Main Record one after the other it'll work... if it's the most recently created Related Record.

Not sure how to get around this. Do I need to set something up so that the Relation automatically loads the Main Record via the Key Value? Or do I have to load both of them somehow.
john1598360627
 
Posts: 191
Joined: Tue Aug 25, 2020 3:03 pm

Re: Loading Specific Relation Record?

Postby sean » Tue Dec 10, 2024 10:42 pm

Sorry, still not clear to me. The two records would not be expected to load in the same foundset. ...unless doo the records on either side of the relation belong to the same data source? (this is uncommon)

Please share your relation definitions, your code and a description of what you expect to happen and what actually happens.
Software Engineer
Servoy USA
sean
 
Posts: 383
Joined: Mon May 21, 2007 6:26 pm
Location: USA

Re: Loading Specific Relation Record?

Postby john1598360627 » Wed Dec 11, 2024 1:57 am

sean wrote:Sorry, still not clear to me. The two records would not be expected to load in the same foundset. ...unless doo the records on either side of the relation belong to the same data source? (this is uncommon)

Please share your relation definitions, your code and a description of what you expect to happen and what actually happens.


DATASOURCES
I have

* Table1

and

* Table2

These are separate datasource tables.

RELATION
I have a Relation

* Table1_to_Table2

Which connects the tables with a Servoy Relation via

Source: Table1
From: table1_keyvalue

Operator: Equals

Destination: Table2
To: table2_keyvalue

Join Type: Left Outer Join

Encapsulation: Public

FORM
I have a Form page that displays Table1 values, as the datasource is Table1. However, thanks to the Relation I also am showcasing Table2 values.

These values are showcases by various Form Elements.

However, I'm trying to load a specific record of Table2. When I load the record into the foundset

* foundset.loadRecord( record_Table2_uuid )

It loads the Record from Table2, however it doesn't load any record for Table1.

HELP
I want to load a Record from Table2 into a Form that has the Data Source of Table1 due to how Table1 has a relation connecting it to Table2. But it's not working properly. It only works to load both Table1 & Table2 records when I create a new Table2 record for the foundset to load. Otherwise, it'll only load the Table2 record.

I'm not sure what exactly the approach is for using the Servoy Relation to fix this. Perhaps I have the Relation setup wrong?
john1598360627
 
Posts: 191
Joined: Tue Aug 25, 2020 3:03 pm

Re: Loading Specific Relation Record?

Postby mboegem » Wed Dec 11, 2024 9:24 am

Hi John,

If I understand correctly, you are loading a specific table 2 record and then expect the relation to load the related table 1 record.
This could work when the form was based on table 2, and the relation is table2_to_table1.
Basically everything works from left to right, not the opposite way.

It's still unclear how you display table 2 records, is that on a tabpanel, or through fields that show the table2 dataprovider over table1_to_table?
When it's the latter, then what you are trying to do is impossible.
The only thing to resolve this is to do the opposite as explained above, but then you can't load a table1 record and see the related table2 record (if that's also needed)

If you really need both in the existing situation, I guess this should work.
1) load record of table 1 > it will automatically show values of the first record (based on sort order) of table 2
2) now call table1_to_table2.selectRecord(table2_pk)

Personally I would build the form differently:
1) form A with table 1 datasource and fields displaying table 1 values
2) form B with table 2 datasource and fields displaying table 2 values
3) add form B to form A using a tabpanel (no relation)
4) when loading records, load correct record on form B, work out related record of table 1 and load correct record on form A

It's difficult to do all the thinking if we can't see what your form looks like. A picture tells more than a thousand words.
Although your explanation does contain some rough information, the correct answer can only be given when knowing details and probably also the use case and reasons why you build the current form as it is.

Nevertheless, I hope the above makes sense.
Marc Boegem
Solutiative / JBS Group, Partner
Servoy Specialist
• Servoy Certified Developer
• Servoy Valued Professional
• Freelance Developer

Image
User avatar
mboegem
 
Posts: 1819
Joined: Sun Oct 14, 2007 1:34 pm
Location: Amsterdam

Re: Loading Specific Relation Record?

Postby john1598360627 » Thu Dec 12, 2024 3:06 am

mboegem wrote:Hi John,

If I understand correctly, you are loading a specific table 2 record and then expect the relation to load the related table 1 record.
This could work when the form was based on table 2, and the relation is table2_to_table1.
Basically everything works from left to right, not the opposite way.

It's still unclear how you display table 2 records, is that on a tabpanel, or through fields that show the table2 dataprovider over table1_to_table?
When it's the latter, then what you are trying to do is impossible.
The only thing to resolve this is to do the opposite as explained above, but then you can't load a table1 record and see the related table2 record (if that's also needed)

If you really need both in the existing situation, I guess this should work.
1) load record of table 1 > it will automatically show values of the first record (based on sort order) of table 2
2) now call table1_to_table2.selectRecord(table2_pk)

Personally I would build the form differently:
1) form A with table 1 datasource and fields displaying table 1 values
2) form B with table 2 datasource and fields displaying table 2 values
3) add form B to form A using a tabpanel (no relation)
4) when loading records, load correct record on form B, work out related record of table 1 and load correct record on form A

It's difficult to do all the thinking if we can't see what your form looks like. A picture tells more than a thousand words.
Although your explanation does contain some rough information, the correct answer can only be given when knowing details and probably also the use case and reasons why you build the current form as it is.

Nevertheless, I hope the above makes sense.


Aha, I didn't realize you could select the related record through the relation. That might fix it!


My current setup is that the Form has Form Elements like textfields for Table 1 dataproviders, since that's the DataSource. But then I have a singular HTML Area that has a Table2 dataprovider.

Since it's only one Form Element I thought I would just use the relation I created and then just manually load the appropriate Table2 record... but I wasn't sure how to do that till now.

Typically I have been making Forms for each table, and then I use a Form Container to embed and manually load as you mentioned. But I thought it was overkill just to get one form element working.

But if the relation record selection doesn't work, I suppose I might have to do the 2 Form setup with the Form Container instead.

Thank you for clearing it up!
john1598360627
 
Posts: 191
Joined: Tue Aug 25, 2020 3:03 pm

Re: Loading Specific Relation Record?

Postby mboegem » Thu Dec 12, 2024 9:05 am

Hi John,

if loading the table2 record is what you are doing all the time, it seems to me that table2 is the datasource and you should be looking into displaying table 1 dataproviders over a relation.
Marc Boegem
Solutiative / JBS Group, Partner
Servoy Specialist
• Servoy Certified Developer
• Servoy Valued Professional
• Freelance Developer

Image
User avatar
mboegem
 
Posts: 1819
Joined: Sun Oct 14, 2007 1:34 pm
Location: Amsterdam


Return to How To

Who is online

Users browsing this forum: No registered users and 5 guests