Pass custom field from sales order to sales invoice

Hi, have custom field called customer_id into customer DocType.
I have two custom naming series:
SO-.customer_id.-.YY.-.### -> sales order
SI-.customer_id.-.YY.-.### -> Invoice

I have two custom script (client-side) one for Invoice and one for sales order with the same content:
cur_frm.add_fetch('customer', 'customer_id', 'customer_id');

If I create a sales order no problem, the order is generated with the right name: SO-XX-20-123
When I click “make -> Invoice” the Invoice get wrong name: SI-customer_id-20-123
But if I create one invoice from scratch then it works SI-XX-20-123

I am sure 90% that is a matter of server-side script, It means I need to create something in hooks.py of my app and then write a function that generates the custom document name, right?

If yes, I got some examples around the forum:

hooks.py:

doc_events = {
	"Sales Invoice": {
		"autoname": "custom_app.hooked_methods.set_si_autoname"
	}
}

hooked_methods.py:

from frappe.model.naming import make_autoname

@frappe.whitelist()
def set_si_autoname(doc, method):
	doc.name = make_autoname('SI-' + doc.customer_id + '-.YY.-.###')

but customer_id is not into Sales Invoice, how can I get it from Customer DocType?
Could you help me with an example?

Developer Cheatsheet

from frappe.model.naming import make_autoname

@frappe.whitelist()
def set_si_autoname(doc, method):
        customer_id = frappe.get_value("Customer", fieldname=["custom_id"], filters={"name"=doc.customer}

You can fetch a single field using frappe.get_value

2 Likes