I have created a custom DocType named “Item Category”, Fields inside that DocType are;
- item_group
- main_category
- main_category_code
- sub_category
- sub_category_code
And in the “Item” DocType, There are some “Check” type fields which are “Maintain Stock”, “Is Fixed Asset” and “Include Item In Manufacturing”. And some custom fields which it fetches data from “Item Category”(Custom DocType), which are;
- Item Category
- Main Category
- Sub Category
When adding Items, if “Is Fixed Asset” field is checked, “Item Category” should list Item Categories where “Item Group” is “Asset”. If “Maintain Stock” is Checked and “Include Item In Manufacturing” is unchecked, then it should list Item Category where it’s Item Group is “Consumable”. If both “Maintain Stock” and “Include Item In Manufacturing” are unchecked, then it should display Item Categories where Item Category’s Item Group is “Service”.
Wrote a script, but it doesn’t work, if anyone could specify the errors in the below code, it would much more beneficial
frappe.ui.form.on('Item', {
refresh: function(frm) {
frm.trigger("set_item_category_filter");
},
"Maintain Stock": function(frm) {
frm.trigger("set_item_category_filter");
},
"Is Fixed Asset": function(frm) {
frm.trigger("set_item_category_filter");
},
"Include Item In Manufacturing": function(frm) {
frm.trigger("set_item_category_filter");
},
// Trigger on change for each checkbox field
validate: function(frm) {
frm.trigger("set_item_category_filter");
},
set_item_category_filter: function(frm) {
let filters = {};
// Set filters based on the conditions of checkbox fields
if (frm.doc.is_fixed_asset) {
filters = { 'item_group': 'Asset' };
} else if (frm.doc.maintain_stock && !frm.doc.include_item_in_manufacturing) {
filters = { 'item_group': 'Consumable' };
} else if (!frm.doc.maintain_stock && !frm.doc.include_item_in_manufacturing) {
filters = { 'item_group': 'Service' };
}
// Apply filters to "Item Category" field in "Item" DocType
frm.set_query("item_category", function() {
return {
query: "frappe.desk.search.search_link",
filters: filters,
doctype: "Item Category"
};
});
}
});