How to Filter Fields with Type Link (Using Custom Script)

Guide

Use the following code in the Custom Script section for whatever DocType.

The logic is that it will run the code when a search is done from the field, or when the field is selected.

frappe.ui.form.on("DocTypeYouAreWorkingOn", {

setup: function(frm) {

frm.set_query("FieldSelectedToApplyFilter", function() {

return {

filters: [

["TheDocTypeFieldLinksTo","FieldYouWantToFilter", "in", ["Value1", "Value2"]]

]

}

});

}

});

Here's one specific example for the Vehicle doctype if you are trying to filter the supplier to only those that supply vehicles or transportation services:

frappe.ui.form.on("Vehicle", {

setup: function(frm) {

frm.set_query("hauler_company", function() {

return {

filters: [

["Supplier","supplier_group", "in", ["Hauler"]]

]

}

});

}

});