Autoname format CO-{YYYY}{MM}{DD}-{#####} — reset counter monthly, not daily

Hi everyone :waving_hand:

I’m trying to set up an autoname format in Frappe like this:

CO-{YYYY}{MM}{DD}-{#####}

My goal:

  • Keep the full date (YYYYMMDD) visible in the document ID
  • But only reset the 5-digit counter every month, not every day

Here’s the behavior I want:
CO-20251110-00001
CO-20251110-00002
CO-20251111-00003 ← next day, counter continues

CO-20251201-00001 ← new month, counter resets

By default, using {YYYY}{MM}{DD} makes Frappe reset the counter daily since the day ({DD}) is part of the prefix.
How can I prevent this and make it reset monthly instead? Any solutions?

Think about replacing the {DD} with a real field. Then replace the {DD} with the field name that you fill before naming the document, and maybe gets emptied later.

This might increase the database slightly, but you might consider this solution.

For custom app development you can go for autoname with something like this

from frappe.model.naming import getseries
from frappe.utils import today

class DocType(Document):
    def autoname(self):
        date_str = today().replace("-", "")
        month_str = date_str[:6]
        counter = getseries(f"CO-{month_str}-", 5)
        number = counter.split('-')[-1]
        self.name = f"CO-{date_str}-{number}"

For server scripts via UI I am not sure if that’s possible but I think there is frappe.rename_doc