Fetching a select list based on values of a select list

Greetings,
Suppose there’s doctype A with one field, provinces. In doctype B, containing town names of each province in doc A. Then, in doctype C, a field (place of residence) displays a list of province names from Doc A, when one selected, another field is created/displayed where it shows a select field of the town names for that province.

Any idea on how to best implement that? Any help is appreciated.

Hi @rads :

Option 1

Use new (and amazing) link field filter editor.

On options use …
[[“Town”,“province”,“=”,“eval: doc.province”]]

(your field names, doctypes, etc … .can vary)

Option 2

Classic method. You will need a little client script . frm.set_query is your friend here :slight_smile:

frappe.ui.form.on('Doctype C', {
 province(frm) {
     frm.set_query("town", (doc) => {
         return {
             filters: {
                 "province": doc.province
             }
         }
     });
 }
})

Check this sample, I think it’s exactly what you are looking for.

Hope this helps.

1 Like

Thank you @avc .
I tried both options, the first one returns this error on saving the doctype " Invalid Filter Format for field Location (town) of type Link . Try using filter icon on the field to set it correctly".
The second option does apply the filter in the town filed, but doesn’t return any value even though i have some province & town records.

Hi @rads:

Maybe related to field names, etc …
Please, share the field list for each doctype.

Thanks.

I double checked that in advance. It is as following:
Doctype Province, 1 data field by fieldname >>province
Doctype Town, 2 fields, Town data field (town) & Province link field (province).
Doctype Location, 2 fields, same names as above, linked fields with doctype names in Options.

Hi @rads:

Umm … it’s working properly here … with both options.

Carefully check fieldnames (not labels) and doctype names. Show the fields table, maybe would be helpful.

For option 1, try to filter manually with some text value instead eval expression. Working?

For option 2 … any error in console? Insert console.log("Hello") before frm.set_query and check if it’s working.

Which version of Frappe are you using? I think this is not related anyway. Dynamic link filter is working some releases ago.

Hi @avc
After deleeting all three doctypes and re-creating them again with the same names mentioned above, now the second option works flawlessly. However, the first option still returns the same error. I use Frappe Framework v15.34.0.
I’m gonna mark the first reply as the solution nevertheless because Option 2 works fine. Thank you very much for your help and replies.