snv
March 29, 2017, 1:38pm
#1
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
):
this.calculate_outstanding_amount(false);
this.frm.refresh_fields();
}
write_off_amount() {
this.set_in_company_currency(this.frm.doc, ["write_off_amount"]);
this.write_off_outstanding_amount_automatically();
}
items_add(doc, cdt, cdn) {
var row = frappe.get_doc(cdt, cdn);
this.frm.script_manager.copy_from_first_row("items", row, ["income_account", "discount_account", "cost_center"]);
}
set_dynamic_labels() {
super.set_dynamic_labels();
this.frm.events.hide_fields(this.frm)
}
items_on_form_rendered() {
1 Like
snv
March 29, 2017, 3:10pm
#3
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
snv
March 29, 2017, 5:18pm
#5
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)
frappe.provide("erpnext.queries");
$.extend(erpnext.queries, {
user: function() {
return { query: "frappe.core.doctype.user.user.user_query" };
},
lead: function() {
return { query: "erpnext.controllers.queries.lead_query" };
},
customer: function() {
return { query: "erpnext.controllers.queries.customer_query" };
},
supplier: function() {
return { query: "erpnext.controllers.queries.supplier_query" };
},
item: function(filters) {
var args = { query: "erpnext.controllers.queries.item_query" };
if(filters) args["filters"] = filters;
customer query in queries.py does not send filters in mysql query:
(case when locate(%(_txt)s, name) > 0 then locate(%(_txt)s, name) else 99999 end),
(case when locate(%(_txt)s, lead_name) > 0 then locate(%(_txt)s, lead_name) else 99999 end),
(case when locate(%(_txt)s, company_name) > 0 then locate(%(_txt)s, company_name) else 99999 end),
idx desc,
name, lead_name
limit %(page_len)s offset %(start)s""".format(
**{"fields": ", ".join(fields), "key": searchfield, "mcond": get_match_cond(doctype)}
),
{"txt": "%%%s%%" % txt, "_txt": txt.replace("%", ""), "start": start, "page_len": page_len},
)
# searches for customer
@frappe.whitelist()
@frappe.validate_and_sanitize_search_inputs
def customer_query(doctype, txt, searchfield, start, page_len, filters, as_dict=False):
doctype = "Customer"
conditions = []
cust_master_name = frappe.defaults.get_user_default("cust_master_name")
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: Unable to modify get_query for customer link field · Issue #15876 · frappe/erpnext · GitHub