Merge pull request #32061 from resilient-tech/fix-appointment-creation

fix(Appointment): create lead notes as child table
diff --git a/erpnext/crm/doctype/appointment/appointment.py b/erpnext/crm/doctype/appointment/appointment.py
index 5f5923d..6e7ba1f 100644
--- a/erpnext/crm/doctype/appointment/appointment.py
+++ b/erpnext/crm/doctype/appointment/appointment.py
@@ -7,7 +7,7 @@
 import frappe
 from frappe import _
 from frappe.model.document import Document
-from frappe.utils import get_url, getdate
+from frappe.utils import get_url, getdate, now
 from frappe.utils.verified_command import get_signed_params
 
 
@@ -104,16 +104,28 @@
 		# Return if already linked
 		if self.party:
 			return
+
 		lead = frappe.get_doc(
 			{
 				"doctype": "Lead",
 				"lead_name": self.customer_name,
 				"email_id": self.customer_email,
-				"notes": self.customer_details,
 				"phone": self.customer_phone_number,
 			}
 		)
+
+		if self.customer_details:
+			lead.append(
+				"notes",
+				{
+					"note": self.customer_details,
+					"added_by": frappe.session.user,
+					"added_on": now(),
+				},
+			)
+
 		lead.insert(ignore_permissions=True)
+
 		# Link lead
 		self.party = lead.name
 
diff --git a/erpnext/crm/doctype/appointment/test_appointment.py b/erpnext/crm/doctype/appointment/test_appointment.py
index 776e604..178b9d2 100644
--- a/erpnext/crm/doctype/appointment/test_appointment.py
+++ b/erpnext/crm/doctype/appointment/test_appointment.py
@@ -6,29 +6,20 @@
 
 import frappe
 
-
-def create_test_lead():
-	test_lead = frappe.db.get_value("Lead", {"email_id": "test@example.com"})
-	if test_lead:
-		return frappe.get_doc("Lead", test_lead)
-	test_lead = frappe.get_doc(
-		{"doctype": "Lead", "lead_name": "Test Lead", "email_id": "test@example.com"}
-	)
-	test_lead.insert(ignore_permissions=True)
-	return test_lead
+LEAD_EMAIL = "test_appointment_lead@example.com"
 
 
-def create_test_appointments():
+def create_test_appointment():
 	test_appointment = frappe.get_doc(
 		{
 			"doctype": "Appointment",
-			"email": "test@example.com",
 			"status": "Open",
 			"customer_name": "Test Lead",
 			"customer_phone_number": "666",
 			"customer_skype": "test",
-			"customer_email": "test@example.com",
+			"customer_email": LEAD_EMAIL,
 			"scheduled_time": datetime.datetime.now(),
+			"customer_details": "Hello, Friend!",
 		}
 	)
 	test_appointment.insert()
@@ -36,16 +27,16 @@
 
 
 class TestAppointment(unittest.TestCase):
-	test_appointment = test_lead = None
+	def setUpClass():
+		frappe.db.delete("Lead", {"email_id": LEAD_EMAIL})
 
 	def setUp(self):
-		self.test_lead = create_test_lead()
-		self.test_appointment = create_test_appointments()
+		self.test_appointment = create_test_appointment()
+		self.test_appointment.set_verified(self.test_appointment.customer_email)
 
 	def test_calendar_event_created(self):
 		cal_event = frappe.get_doc("Event", self.test_appointment.calendar_event)
 		self.assertEqual(cal_event.starts_on, self.test_appointment.scheduled_time)
 
 	def test_lead_linked(self):
-		lead = frappe.get_doc("Lead", self.test_lead.name)
-		self.assertIsNotNone(lead)
+		self.assertTrue(self.test_appointment.party)