Fetch Delivery Date from Sales Order to Production Plan Sales Order

In the Production Plan Sales Order doc which is a Child Table of the Production Plan doctype, we have created a new field called “Delivery Date”.

We would like to fetch data on “Delivery Date” field from the Sales Order doctype.

What would be the script to achieve this?

Hi @dfranco,

Please check it and apply it.

If manually select Sales Order then will show quickly but when Click the Get Sales Order Button then does not show but when you save the Production Plan then will be set automatically.


If you want to apply via code then apply it.

Here’s an example server script that fetches the delivery date from the Sales Order and sets it in the corresponding Production Plan Sales Order row:

from frappe.utils import flt

def set_delivery_date(doc, method):
    for row in doc.sales_orders:
        sales_order = frappe.get_doc("Sales Order", row.sales_order)

        # fetch the delivery date from the Sales Order
        delivery_date = sales_order.delivery_date

        # set the delivery date in the Production Plan Sales Order row
        for item in row.items:
            doc_item = doc.get_child_doc("sales_orders", {"sales_order": row.sales_order, "item_code": item.item_code})
            doc_item.delivery_date = delivery_date

        # save the changes to the Production Plan
        doc.save(ignore_permissions=True)

below script should be added to a Python file (e.g., my_module.py) in your custom app’s hooks.py file as follows:

doc_events = {
    "Production Plan": {
        "before_save": "<app_name>.<module_name>.my_module.set_delivery_date"
    }
}

It will run the set_delivery_date function before saving a Production Plan document, which will fetch the delivery date from the linked Sales Order and set it in the corresponding Production Plan Sales Order row.

Thank You!

Thanks NCP-

The first solution should be adequate for our application as we are only using browser for ERPNext.

Now, we are trying to get the delivery dates for each Sales Order Item (child table of Sales Order doctype transferred into the Production Plan Item (child table of Production Plan doctype)

I have tried the same procedure:

image

but it was fetching us the Delivery Date of the Sales Order and not the specific Sales Order Items (Which are sometimes different for every item).

I have also tried to code a script based on:

Fetch values from a child table to another child table###

I ended up with the following script:

frappe.ui.form.on("Production Plan Item", {
	sales_order_item: function(frm, cdt, cdn) {
		var row = locals[cdt][cdn];
		if (row.sales_order_item) {
			frappe.model.with_doc("Sales Order", row.sales_order_item, function() {
				var doc = frappe.model.get_doc("Sales Order", row.delivery_date);
				$.each(doc.sales_order_item || [], function(i, r) {
					if(r.sales_order_item == frm.doc.sales_order_item) {
						var df = frappe.meta.get_docfield("Production Plan Item","delivery_date", frm.doc.name);
						df.options += ["\n" + r.delivery_date];
					}
				})
			});
			frm.refresh_field("Production Plan Item")
		}
	}
});

It is not fetching anything and I am not sure what I am doing wrong.

Can you please help verify if my code is correct and what should I do to fix it?

Thank you!