Apply filters in link field globally

Hello Team,

I want to apply a filter to the Project link field globally across all relevant DocTypes. This means I need to set default filters for every DocType’s form where the Project is linked, ensuring it only shows the filtered data of projects as example I want to show only Active Projects. Please help me with how to implement this globally.

Thank you!

1 Like

Please check this
https://docs.erpnext.com/docs/user/manual/en/creating-custom-link-field#adding-filters-to-link-field

This solution works only on particular form. I need a global solution which works on each and every form.

As far as I know, there is no such feature in Frappe to set globally for all the fields.

If you want to do that, you will have to modify or override this function of Frappe, which is not recommended.

One approach you can apply is to use frappe set_query, but you will have to do this in all the doctype JS files where you are using the Project link field.

https://frappeframework.com/docs/user/en/api/form#frmset_query

One way to do it to write standard query hook.

standard_queries = {"User": "rtcamp.api.user.user_query"}

and define the method like below:

from frappe import whitelist, validate_and_sanitize_search_inputs, session, get_list


@whitelist()
@validate_and_sanitize_search_inputs
def user_query(doctype, txt, searchfield, start, page_len, filters):
    doctype = "User"

    list_filters = {
        "enabled": 1,
        "docstatus": ["<", 2],
    }

    # Check if we have a search term, and decide the filters depending on the search term
    or_filters = [[searchfield, "like", f"%{txt}%"]]
    if "name" in searchfield:
        or_filters += [[field, "like", f"%{txt}%"] for field in ("first_name", "middle_name", "last_name")]

    if filters:
        if not (filters.get("ignore_user_type") and session.data.user_type == "System User"):
            list_filters["user_type"] = ["!=", "Website User"]

        filters.pop("ignore_user_type", None)
        list_filters.update(filters)
    else:
        if session.data.user_type == "System User":
            list_filters["user_type"] = ["!=", "Website User"]

    return get_list(
        doctype,
        filters=list_filters,
        fields=["name", "full_name"],
        limit_start=start,
        limit_page_length=page_len,
        order_by="name asc",
        or_filters=or_filters,
        as_list=True,
    )
# Based on frappe's user query

It’s also possible to add a disabled custom field