# Sorted records on a related form

**URL:** <https://forum.servoy.com/t/sorted-records-on-a-related-form/9845>\
**Category:** Classic Servoy\
**Created:** [November 6, 2008, 2:51pm UTC](https://forum.servoy.com/t/sorted-records-on-a-related-form/9845 "2008-11-06T14:51:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![martinh](https://avatars.discourse-cdn.com/v4/letter/m/848f3c/32.png) [@martinh](https://forum.servoy.com/u/martinh)\
**Post date:** [November 6, 2008, 2:51pm UTC](https://forum.servoy.com/t/sorted-records-on-a-related-form/9845/1 "2008-11-06T14:51:34Z")

</div>

I have the following situation.

I have a form A using a parent table  
I have a form B using a child table (1:n relation)

Form B is on a tabpanel and is dynamically put on the tabpanel using:

```auto
forms[_tab_form].elements.tabs.addTab(forms[_form], <other parameters>)

```

I want the records on Form B to be sorted and this sort is determined on runtime. So the initialSort in the form properties can not be used.  
I’ve put in my onShow method a sort command:

```auto
forms[_form].controller.sort('someField asc');

```

This works, but only for the first record shown.  
When I go to the next record in my foundset on form A, then the related data is shown, but not in the way I want it to be sorted.

Why doesn’t this work and how can I solve this (other then on change record on form A, resort all related forms on tabpanel)

I’ve seen that in 4.0 there is a initialSort field in the relation properties, but I have the problem in 3.5.7 and I don’t want this sort for all forms that use this relation.  
So even the initialSort in relation properties is not a solution for me.

Why doesn’t the controller.sort() remain active?

---

<div class="post-metadata">

**Author:** ![ngervasi](https://avatars.discourse-cdn.com/v4/letter/n/f9ae1b/32.png) [@ngervasi](https://forum.servoy.com/u/ngervasi)\
**Post date:** [November 6, 2008, 2:59pm UTC](https://forum.servoy.com/t/sorted-records-on-a-related-form/9845/2 "2008-11-06T14:59:30Z")

</div>

It’s because the onShow event is triggered only when the form B is shown, if you then change form A current record that event is not triggered again.  
You have two option:

1. Use the onRecordSelection event on form A to trigger the related sort
2. Use the onLoad event on form B to trigger the related sort

I’d go for option 1.

---

<div class="post-metadata">

**Author:** ![Hans\_Nieuwenhuis](https://avatars.discourse-cdn.com/v4/letter/h/87869e/32.png) [@Hans\_Nieuwenhuis](https://forum.servoy.com/u/Hans_Nieuwenhuis)\
**Post date:** [November 6, 2008, 3:12pm UTC](https://forum.servoy.com/t/sorted-records-on-a-related-form/9845/3 "2008-11-06T15:12:17Z")

</div>

I agree, i am using option 1 and it works fine.

Regards,

---

<div class="post-metadata">

**Author:** ![martinh](https://avatars.discourse-cdn.com/v4/letter/m/848f3c/32.png) [@martinh](https://forum.servoy.com/u/martinh)\
**Post date:** [November 6, 2008, 3:17pm UTC](https://forum.servoy.com/t/sorted-records-on-a-related-form/9845/4 "2008-11-06T15:17:19Z")

</div>

I understand that the onShow is triggered once.

What I don’t understand is why I have to resort each time my form when my parent record changes.

Since I put controller.sort() on the child table, I expected that on change record A, records B are changed (because of the relation) and automatically put in the sort that I entered.  
I expect that the controller.sort lives as long as the form lives. I expected that the controller.sort() would be similar as the initialSort of the form properties

---

<div class="post-metadata">

**Author:** ![martinh](https://avatars.discourse-cdn.com/v4/letter/m/848f3c/32.png) [@martinh](https://forum.servoy.com/u/martinh)\
**Post date:** [November 6, 2008, 3:20pm UTC](https://forum.servoy.com/t/sorted-records-on-a-related-form/9845/5 "2008-11-06T15:20:32Z")

</div>

> ngervasi:
> 
> 1. Use the onRecordSelection event on form A to trigger the related sort
> 
> I’d go for option 1.

Are you sure this works? I tried it and I see that the sort() is not being done, because the related form is shown after the onRecordSelection trigger is finished ![:-(]( "Sad")

---

<div class="post-metadata">

**Author:** ![ngervasi](https://avatars.discourse-cdn.com/v4/letter/n/f9ae1b/32.png) [@ngervasi](https://forum.servoy.com/u/ngervasi)\
**Post date:** [November 6, 2008, 3:41pm UTC](https://forum.servoy.com/t/sorted-records-on-a-related-form/9845/6 "2008-11-06T15:41:19Z")

</div>

It should… otherwise there’s always option 2 ![;)]( "Wink")

> Hannibal Smith, A-Team:  
> I love it when a plan comes together!

![:D]( "Very Happy")

---

<div class="post-metadata">

**Author:** ![martinh](https://avatars.discourse-cdn.com/v4/letter/m/848f3c/32.png) [@martinh](https://forum.servoy.com/u/martinh)\
**Post date:** [November 6, 2008, 3:51pm UTC](https://forum.servoy.com/t/sorted-records-on-a-related-form/9845/7 "2008-11-06T15:51:45Z")

</div>

I succeeded, but not like I expected.  
I did a sort on the child form and finally I found that I had to do a sort on the **parentform** relation:

```auto
		for (var i = 1; i <= forms[_tab_form].elements.tabs.getMaxTabIndex(); i++)
		{
			_relation_name = forms[_tab_form].elements.tabs.getTabRelationNameAt(i)
			_child_form = forms[_tab_form].elements.tabs.getTabFormNameAt(i)

			forms[_tab_form][_relation_name].sort('someField asc')
		}

```

---

<div class="post-metadata">

**Author:** ![jcompagner](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/jcompagner/32/4889_2.png) [@jcompagner](https://forum.servoy.com/u/jcompagner)\
**Post date:** [November 11, 2008, 4:39pm UTC](https://forum.servoy.com/t/sorted-records-on-a-related-form/9845/8 "2008-11-11T16:39:23Z")

</div>

in 4.0 you have an initial sort property on a relation so you can define what it should sort up front.
