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.
#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
#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:
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.
"""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
# This will be called on_update, meaning if the name
# Is not what It should be, rename
if self.name != self.get_name():
self.name, name, ignore_permissions=True)
'''Method that returns the record name'''
customer = self.customer
if len(customer) > 3:
customer = customer[0:3].upper()
It works this way:
When Document is new
Fiels is new, calls the autoname controller.
The controller check if the document exists already. If it doesn’t exist, is new, so sets the name.
When Document Exists
call on_update, which call autoname.
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.