How to filter one list from another to display a shorter list on form.

I have two imported tables/lists from an external DB, 

One for companies and another for contacts. Companies can have multiple contact persons, they are related by company ID.  

In an instantiation form, I would like to be able to select a company from a list (this is working already), and then only allow the user to choose from the contacts (in a separate list field) that relate to the company. 

I feel like this should be as simple as a js asset but can't seem to implement one correctly (I'm beginner). Any help and advice appreciated.

Hi, you could do it using the UID variables.

First, you need to have your companies data have a certain form. You need an array that has multiple objects. Something like this :

[{displayKey: "company1", returnKey: "company1"}, {displayKey: "company2", returnKey: "company2"}]

Then, use this data in the companies dropdown, Display key is displayKey and Return key is returnKey.
Have the value of your companies dropdown be assigned to a UID variable (ex: companiesValue).
Then, create a UID variable (let's say it's called controller) that will have a function that will filter the contacts data depending on the selected companiesValue. The value of the controller variable will be something like this:

return {
    filterDataDependingOnCompanySelected: function(companySelected) {
        $data.contacts.filter(function (contact) {
            contact.company = companySelected;
        });
    }
};

Or create a list that can be used in a dropdown (through displayKey, returnKey). Now, all you need to do is call "controller.filterDataDependingOnCompanySelected(companiesValue)"and you will get a list of contacts that are part of the company. You can always change how you filter the contacts if your data differs.

 

Hope this helps,
Dumitru

Hi!

Watch this video: https://www.youtube.com/watch?v=hk_SHNKPPd8

At minute 28:49 you can see how you can do what you need.

I hope that helps!

Solved thanks, didn't even think about that route

What do you mean here by contact.company? 

I assume in $data.contacts you mean the list of contacts. Then contact.company refers to what? 
contactsList.companyID? 

Also I call this 'controller.filterDataDependingOnCompanySelected(companiesValue)' in available values, yes? 

Solved by comment of walo2014.