Filter Child items dependent on parent form

Sir,

I want to filter items on Material Request form. basically I want group wise material request (In one material request items should be same groups of items)

I have add a custom field called item group in material request filed and add a custom script to filter the items.but my script is not working. Kindly help.

My script:

cur_frm.fields_dict['items'].grid.get_field('item_code').get_query = function(doc, cdt, cdn) {
	var d = locals[cdt][cdn]
	return {
		filters: [
			['Item', 'item_group', '=', doc.item_group]
		]
	}
}

@Prashanta_Mahato

There is already query written for item_group on material_request.js
to override that try this,

frappe.ui.form.on("Material Request",{
	onload: function(frm) {
		frm.set_query("item_code", "items", function() {
			return {
				filters: {"item_group": frm.doc.item_group}
			}
		});
	}
})

Thank you @Sangram angram,

its not work for me,still calling the “erpnext.controllers.queries.item_query” function.

Do you have added any item group field in Material Request? As I know there is no field exist in default form.

Add console under onload: function(frm) {

console.log("Item Group", frm.doc.item_group)

and check your browser’s console

OR

for test purpose give hard coded value to item_group filter

i.e.

filters: {"item_group": "Products"}

and check it works or not

Yes sir, I have added a item group link filed in my material request form.
Still not works for me,even console log also not showing. I have also tried with refresh.
Here is my code:

frappe.ui.form.on(“Material Request”,{
reload: function(frm) {
console.log(“Item Group”, frm.doc.item_group);
frm.set_query(“item_code”, “items”, function() {
return {
filters: {“item_group”: “Tea”}
}
});
}
});

Thank you.

What is the result of console log?

Have you checked your field name of item group? Also select value in it before adding item code

I have hard code filter with filters: {“item_group”: “Tea”}. Result of console.log is Tea. But items are not filtered as per the item group.

Regrads,

frappe.ui.form.on(“Material Request”,{
refresh: function(frm) {
cur_frm.set_query(“item_code”, “items”, function() {
return {
“filters”: [“Item”,“item_group”,"=", cur_frm.doc.item_group]
}
});
}
});

Does anybody have any idea about how to filter the items as per item group selected in parent form.

Regards,

@Sangram
The below code is worked after bench restart, But the code is working until I refresh the browser.
After refresh the browser again the code is not working. Kindly share if you get any solution.
cur_frm.cscript.item_group = function(doc, cdt, cdn) {
cur_frm.set_query(“item_code”, “items”, function(){
return{
query: “erpnext.controllers.queries.item_query”,
filters:{ ‘item_group’: cur_frm.doc.item_group }
}

});

};

Thank you.

@Prashanta_Mahato

There are query written in material_request.js as well as in buying controller purchase_common.js.
So you have to either modified in material request js or override it by extending class.

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

Above script is working but I have to override the onload event. Try this with another method

4 Likes

Thank you @Sangram, your code works for me.

1 Like

This worked for me too. Thank you so much

1 Like

Cscript is still working in v13. Note: it needs to be put outside of frappe.ui.form.on(‘Doctype’, {})