Hi
Busy with some server side scripting. 95% complete on this one.
Goal: When “sales_order” field is selected, filters for “item_code” field must be set
so that user can only select items that are on that sales order.
JS file in …/doctype/bdh/bdh.js
frappe.ui.form.on('BDH', {
// refresh: function(frm) {
// }
//..............
//..............
// other code
//.............
},
sales_order: function(frm) {
if (frm.doc.sales_order) {
return frm.call({
method: "get_customer",
doc: frm.doc,
callback: function() {
refresh_field('customer');
}
});
frm.set_query('item_code', function() {
return {
filters: [
['Item', 'name', 'in', itemCodeOptions]
]
};
});
}
}
});
PY file in …/doctype/bdh/bdh.py
import frappe
from frappe.model.document import Document
def get_template_details(template):
//......
// other code
//.........
class BDH(Document):
def validate(self):
frappe.msgprint("Validate")
//...........
// other code
//.............
@frappe.whitelist()
def get_customer(self):
if self.sales_order:
sales_order_doc = frappe.get_doc("Sales Order", self.sales_order)
self.customer = sales_order_doc.customer
self.set_item_code_filters()
@frappe.whitelist()
def set_item_code_filters(self):
if self.sales_order:
# Filter item codes based on the selected sales_order
item_codes = frappe.get_all("Sales Order Item",
filters={"parent": self.sales_order},
fields=["item_code"]
)
item_code_list = [item.item_code for item in item_codes]
# Get the item_code_options and initialize it as an empty list if it's None
item_code_options = self.get("item_code")
if item_code_options is None:
item_code_options = []
item_code_options[:] = [] # Clear existing options
for item_code in item_code_list:
item_code_options.append({"item_code": item_code})
I have used bench --site console and ran the commands in my code through console and it seems that
“item_code_options” contains the correct dict / array of items codes that must be available for selection
in “item_code” by the user. So I added the “set_query” in my JS code,but I am not getting the result
I want.
Can someone offer some guidance as to the correctness of my set_query command ? Please ?
Thank you