Unable to make custom script work - query filter not sent to db?

frappe.ui.form.on("Sales Invoice", "onload", function(frm) {
    cur_frm.set_query("customer", function() {
        return {
            "filters": {
                "customer_group": "Loans and Advances",
            }
        };
    });
});

Ideally, above script should result in Sales Invoice showing customers of Loans and Advances group only. But the same does not work. I tried calling it on refresh too, but that does not work either.

try it like this (outside frappe.ui.form.on):

1 Like

Thanks for the reply.

cur_frm.set_query("customer", function() {
    return {
        filters: {
            'customer_group': 'Loans and Advances',
        }
    }
});

I made the following changes:

  • removed quotes from ‘filters’ key
  • removed frappe.ui.form.on

It still shows the default query only.

Hi, I think you should try something like this
cur_frm.fields_dict['customer'].get_query = function(doc, cdt, cdn) { return{ query: "your_app.queries.get_customer_name", filters: {'customer_group': 'Loans ans Advances'} } }
and write python script get_customer_name

1 Like

I spent a lot of time trying to figure out what’s wrong. Here’s what worked:

frappe.ui.form.on("Sales Invoice", "refresh", function(frm) {

cur_frm.set_query("customer", function() {
    return {
        query: "myappname.queries.customer.customer_query",
    	filters: [
                ["Customer", "customer_group", "!=", "Loan to Staff" ],
		["Customer", "customer_group", "!=", "Loans and Advances" ],
            ],
    }
});

});

Now I get the list of customers not in the groups pertaining to loan as they are not for invoice purposes.

What was wrong:

  • customer query in queries.js does not accept any filters (unlike item)
  • customer query in queries.py does not send filters in mysql query:

Hi can you guide me i am not able to do the same please

Hi all,

this seems to still be an issue. I am trying to filter a customer link field using

refresh: function(frm) {
    cur_frm.fields_dict['customer'].get_query = function() {
        return {
            filters: { 'disabled': 0 }
        }
    }
}

All other filters work nicely but this has no effect as described in this thread. Has this ever been addressed?

Created a GitHub issue: https://github.com/frappe/erpnext/issues/15876