Filter a linked field based on a linked field

Hello community
I’m working on a custom app so in my form I have a linked field gets its list from a doctyp has one data field, and another field gets it’s list from other doctype has two fields a data field and a linked field to the first doc I mentioned I want in the third form to link poth docs and filter the second one based on the value chosen by the first one.
Let’s assume that a doctype called Case has values case1, case2 …
Another doctype called Test has values test1, test2 within value of case1 and test3, test4 within value case2 in the third form I want to link both doctypes so when I chose case1 so will filter the linked field with test1 and test2

Any idea how to achieve it

Did you try? Overriding Link Query By Custom Script

In a custom method approach, you can include your SQL query, giving you the flexibility to retrieve any data you need.

Hi @Gharieb:

Maybe it will be easier to understand with “real use case” …

Let’s say you have myCustomer (with customer data), myState (with states of a country) and myCity (with cities linked to states)

When you add a new customer, you want to select a city based on the state .
So, if you select Louisiana as State, City will be filtered with state and only cities that belongs to Louisiana will be shown… right?

From v15 you can do it from UI, without coding … just try this:

Create myState doctype … just one field, type Data

Create myCity doctype … two fields, City as Data and State as Link to MyState

Finally, create MyCustomer
Three fields, Customer as Data, State as link to MyState and City, link to MyCity and set filters
[["MyCity","state","=","eval: doc.state"]]

Hope this helps!

4 Likes

thanks that helps but it doesn’t reset the filtred field on update I fixed with
a line of code

{fieldName} (frm) {frm.set_value('fieldName', '')}

Hi @Gharieb:

It’s working well on my side … :thinking:

linkfilters

try to set the city then update the state!! this what i meant :slightly_smiling_face:

:man_facepalming:t4:
Got it :slight_smile:

Just to elaborate on @avc ‘s answer to make it crystal clear:

  • It is an array of filters so that you can filter on multiple fields of the target DocType
  • Entry 1: is the DocType Name that you are filtering (should match the ‘Options’ of the link field you are applying the filter to, for example Customer)
  • Entry 2: is the fieldname of the column you are filtering on that DocType
  • Entry 3: is the operator, one of =, <, >, <=, >=, “is”, “is not”, “like”, “in”, “not in” should work
  • Entry 4: is some javascript to run to obtain the value to filter against. doc is the current document record data to use in the filter
1 Like