Pre-filling information on document from previous document

I am in the Project document which has information like Customer, Project Name, Address, etc.

From within Project, I create a Delivery Note (linked). When it takes me to New Delivery Note document screen, I’d like to have the values for Customer, Project, etc. pre-filled in by pulling them in from the previous document (the Project). Is this doable?

I found this - Push Script for updating the value in another document - which is close but seems the reverse of what we want.

Do you mean you added a custom field “Delivery Note” in Project Doctype? And that when you create new delivery note from it is when you want the values to be copied?

Yes, exactly that. I have Custom Field in Project that is Delivery Note (Link) and I want to prepoulate the DN with values from the Project.

This article should show you how to do this:

https://kb.frappe.io/kb/customization/fetch-custom-field-value-from-master-to-all-related-transactions

I am not sure if the above article would help, I am not good with custom scripts. I would add a custom button via project.js and a server function in project.py to handle the above, I could elaborate if that would be a viable solution for you @ryarrow

@cpurbaugh - I’ve done that in other custom scripts for other purposes, but I don’t know the proper events and methods to detect if I came from one document into another and retrieve values backwards from it.

@vivek - I use the ERPnext hosted version, so i can’t do that.

I am no programmer. But you could try a version of this. I used it for when i clicked through to a New Doc from the Customer Doc. Any forum member please correct this if its wrong.

cur_frm.cscript.onload = function(doc, cdt, cdn) {
	cur_frm.add_fetch('customer', 'customer_name', 'customer_name');

	cur_frm.fields_dict.customer.get_query = erpnext.queries.customer;

	if(doc.__islocal) {
		var last_route = frappe.route_history.slice(-2, -1)[0];
		if(last_route && last_route[0]==="Form") {
			var doctype = last_route[1],
				docname = last_route.slice(2).join("/");
				var refdoc = frappe.get_doc(doctype, docname);
			}

	if(cur_frm.doc.doctype==="My New Document") {
					cur_frm.set_value("customer_name", refdoc.customer_name);
				}
			}
		}
1 Like

This looks like a good starting point for me. I will see what I can figure out.