Skip to content

Filtering Link Fields

Use Case

In India, there are states and each state has several districts. Suppose, we want to implement a Location form for India which asks the user to select a State and then a District. But we have to make sure the District field only shows districts belonging to the selected state:

Screenshot showing filter getting applied

The district doctype has a field which stores which State it belongs to:

District DocType Form View

The Script

frappe.ui.form.on('Location', {
// whenever "state" field is changed
state(frm) {
frm.set_query("district", (doc) => {
return {
filters: {
"state": doc.state // whatever state is selected

The JS API that let’s you apply filters to link fields is highlighted above. As you can see above, doc (in this case, the Location document) is provided for you. Here, doc is equivalent to frm.doc.

Learn More

You can read more about set_query API here.