Customizing naming series

Hello, I’m trying custom my naming series to code of item.
It’s like, if the item code is 4545 then, the serie will be 4545-0001.
How can i do that?

write method for autoname

self.name = make_autoname(self.item_code + '-.#####')

Show me the complete code please.
I tried:

frappe.ui.form.on(“Item”, {
refresh: function(frm) {
// use the __islocal value of doc, to check if the doc is saved or not
self.item_code = make_autoname(self.item_code + ‘-.#####’);
}
});

And not works at all…

you should do it in server side in item.py not in client side

1 Like

As @Mohammed_Redha said you have to write it in python file (server side).
you can use hooks.py for it.

In hooks.py give an autoname function path for Item.

"Item": {
	   "autoname": "dotted.path.to.autoname.function"
}

Where your method will be like, (In Python file)

def item_autoname(doc, method):
	from frappe.model.naming import make_autoname
	doc.name = make_autoname(doc.item_code + '-.#####')

Hope it resolve your problem.

2 Likes

I have to create an app to script server side?

yes, If you want to not lose your changes after update

1 Like

I’ll try do that, thank’s for help!

I worked on this too, here was how I did it:

2 Likes

This is JS, what file i code this?

hooks.py python file can be found at the root of every app.
You can customize erpnext using this file.

This should help you : https://frappe.github.io/frappe/user/en/guides/basics/hooks

Also @kolate_sambhaji did a very nice explanation about exploiting the autoname function to add some context.

1 Like

@Leonardo_Augusto

Did you get this working? Great to skype with you on Friday, it seemed like you were really close. If you did get what you were looking for, post your solution for the community; there’s an excellent chance somebody else wants to do the same thing.

Not yet, i tried with this post what you share:
PO-.YY.MM.-.{vendor_id}.-.#####
Resulting in “PO-1603-WN-00001”
(where “Vendor ID” is custom field created under Document: Supplier and
fetched into Purchase Order.)

but {} it’s not permited.
So, i don’t know if i have to create an app to do that on server side, or if i can do that with a custom script… each person tell me each person tells me something different

Actually, it should be PO-.YY.MM.-.vendor_id.-.#####. Ignore the {} braces, just seperate the fields with dots (.). This is the real way.

item_code.-

Tried the above ?

not works at all…

The solution is @Sangram s post #5.
It was talked over gitter and fixed.
Custom app was not installed in his site.