How can I rename a document when saving it?

Hello Team, All,

I’m setting the name of a document as a combination of two of its fields. I would like to rename the document whenever the fields change. I’m trying the code below but it doesn’t quite help. Any assistance is welcome.

class Object(Document):
    def autoname(self):
        #Concatenate the academic year and the term name to form a unique name for combination of academic year/term
        self.name = self.a + "/" + self.b
        
    def validate(self):
        #Add here the possibility to check if self.name = self.academic_year + "/" + self.term_name
        #and rename if they are not the same
        new_name = self.a + "/" + self.b
        if self.name != new_name:
            frappe.rename_doc(self.doctype,self.name,new_name)

Regards,
cksgb

1 Like

try using
self.name=make_autoname(self.a+"/"+self.b)
for that you have to import
from frappe.model.naming import make_autoname
Regards,
Ravindra Lakal

Hi @ravindra_l tried this but gives error “Doctype name not found”
also tried set_new_name(self) still gives the same error, I guess won’t work if you are in same window?

@johnskywalker

on first Validate it will give you self.name = 'New Document Name 1' for New Document. So, you have to check on validate Document is New one or Existing one.

Try this,

def validate(self):
	new_name = self.a + "/" + self.b
	if self.name != new_name and not self.is_new():
		frappe.rename_doc(self.doctype,self.name,new_name)
#import the function
import frappe.model.rename_doc as rd
rd.rename_doc(Doctype, old_name, new, force=False, merge=False, ignore_permissions=False, ignore_if_exists=False)

This works for me

1 Like

Where should I place this code?

1 Like

It will depend when and where you want to rename your document.

@hizbul
Right now I had a DocType in which the name is a combination of two fields, and I want it to keep in sync. So I do the following.

    def autoname(self):
        """If field is new sets the name, if fields that set
        the name have changed, renames"""
        # Get the name the record should have
        name = self.get_name()
        # If it doesn't exis, e.a Is a new record
        # Name it and end it
        if not frappe.db.exists('Package', self.name):
            self.name = name
            return
        else:
            # This will be called on_update, meaning if the name
            # Is not what It should be, rename
            if self.name != self.get_name():
                frappe.rename_doc("Package",
                                  self.name, name, ignore_permissions=True)

    def get_name(self):
        '''Method that returns the record name'''
        customer = self.customer
        if len(customer) > 3:
            customer = customer[0:3].upper()

        return f"{customer}-{self.guide}"

    def on_update(self):
        self.autoname()

It works this way:

When Document is new

  1. Fiels is new, calls the autoname controller.
  2. The controller check if the document exists already. If it doesn’t exist, is new, so sets the name.

When Document Exists

  1. call on_update, which call autoname.
  2. Since field already exists (the fact that on_update is called tells you the field exists). It checks if name is what It should be (given by get_name()) if not, rename.
1 Like

After frappe.rename_doc use frappe.db.commit

frappe.rename_doc(“Your Doctype”, “old_name”, “new_name”)
frappe.db.commit()