Overriding link query by custom script not working for material_request.js

erpnext.buying.MaterialRequestController = erpnext.buying.BuyingController.extend({
** onload: function(doc) {**
** this._super();**
** this.frm.set_query(“item_code”, “items”, function() {**
** return {**
** query: “erpnext.controllers.queries.item_query”**
** }**
** });**
}

I am trying to override the above script query by a custom script, But its not works for me.
I want to filter the items in material request item by item group. I have created a custom field item group in material request doctype.

I have tried with the following query.

frappe.ui.form.on(“Material Request”, “onload”, function(frm) {
cur_frm.set_query(“item_code”,“items”, function() {
return {
“filters”: {
“item_group”: frm.doc.item_group
}
};
});
});
When I have committed the material_request.js code then only its works.

Kindly help how to override the standard query.

Regards,

By default there is a query on the link field you are trying to apply filter.

Try applying a query as a python function. Check item_query

e.g.

frappe.ui.form.on("Material Request", "onload", function(frm) {
	frm.set_query("item_code","items", function() {
		return {
			"query" : "custom_app.controllers.queries.custom_item_query",
			"filters": {
				"item_group": frm.doc.item_group
			}
		};
	});
});
2 Likes

Thank you @revant_one
I have solved the problem with the below code.

cur_frm.cscript.onload = function(frm) {
	cur_frm.set_query("item_code", "items", function() {
		return {
			filters: {"item_group": frm.doc.item_group}
		}
	});
}
4 Likes