diff --git a/erpnext/manufacturing/doctype/work_order/work_order.py b/erpnext/manufacturing/doctype/work_order/work_order.py index a0357751635..428c274d5cd 100644 --- a/erpnext/manufacturing/doctype/work_order/work_order.py +++ b/erpnext/manufacturing/doctype/work_order/work_order.py @@ -516,7 +516,7 @@ class WorkOrder(Document): PackedItem = frappe.qb.DocType("Packed Item") ProductBundleItem = frappe.qb.DocType("Product Bundle Item") - so = ( + so_query = ( frappe.qb.from_(SalesOrder) .inner_join(SalesOrderItem) .on(SalesOrderItem.parent == SalesOrder.name) @@ -533,9 +533,13 @@ class WorkOrder(Document): | (ProductBundleItem.item_code == production_item) ) ) - .run(as_dict=1) ) + if self.sales_order_item: + so_query = so_query.where(SalesOrderItem.name == self.sales_order_item) + + so = so_query.run(as_dict=1) + if not so: so = ( frappe.qb.from_(SalesOrder)