fix(lead): don't crash deriving lead name when only ignore_mandatory is set

set_lead_name fell through to email_id.split('@') when a lead had no name,
company or email but ignore_mandatory was set (e.g. data import), raising
AttributeError on a None email. Only derive from email when one exists; the
lead name is then left blank, as intended for that path.
This commit is contained in:
Nabin Hait
2026-06-22 12:47:44 +05:30
parent 3f832d4ee0
commit 8f69697212
2 changed files with 24 additions and 1 deletions

View File

@@ -140,7 +140,7 @@ class Lead(SellingController, CRMNote):
frappe.throw(_("A Lead requires either a person's name or an organization's name"))
elif self.company_name:
self.lead_name = self.company_name
else:
elif self.email_id:
self.lead_name = self.email_id.split("@")[0]
def set_title(self):

View File

@@ -193,6 +193,29 @@ class TestLead(ERPNextTestSuite):
lead.delete()
self.assertFalse(frappe.db.exists("Prospect", prospect_name))
def test_set_lead_name_fallbacks(self):
# organization name is used when there is no person name
lead = frappe.new_doc("Lead")
lead.company_name = "_Test Org Lead"
lead.set_lead_name()
self.assertEqual(lead.lead_name, "_Test Org Lead")
# the email local-part is used when only an email is present
lead = frappe.new_doc("Lead")
lead.email_id = "jane.doe@example.com"
lead.set_lead_name()
self.assertEqual(lead.lead_name, "jane.doe")
# data import (ignore_mandatory) with no name/company/email must not crash
lead = frappe.new_doc("Lead")
lead.flags.ignore_mandatory = True
lead.set_lead_name()
self.assertFalse(lead.lead_name)
# otherwise a lead with no name source is rejected
lead = frappe.new_doc("Lead")
self.assertRaises(frappe.ValidationError, lead.set_lead_name)
def create_event(subject, starts_on, reference_type, reference_name):
event = frappe.new_doc("Event")