How to avoid customer duplication

I have to restrict creation of customer against opportunity as one customer for one opportunity. By default multiple customers can be created against an opportunity/lead, want to avoid it.

if not frappe.db.exists(“Customer”, {“customer_name”: doc.customer_name}):
customer = frappe.new_doc(“Customer”)
customer.customer_name = doc.customer_name
customer.territory = doc.territory
customer.lead_name = doc.party_name
customer.opportunity_name = doc.opportunity
customer.customer_primary_address = doc.customer_address
customer.insert()
frappe.msgprint(“Customer Created!”)

else:
pass

frappe.msgprint(f"Customer {doc.customer_name} with Opportunity "{doc.opportunity}" Already Exists")

i have used this code, but didnt get any solution.

I guess this could help
To Change

To

if not frappe.db.exists("Customer", {"customer_name": doc.customer_name, "opportunity_name": doc.opportunity}):

this search for one with the same customer_name and opportunity

OR
If you want to create only if there is no other one with same customer_name and there is no other one with the same opportunity
You can use

if not frappe.db.exists("Customer", {"customer_name": doc.customer_name) and not frappe.db.exists("Customer", {"opportunity_name": doc.opportunity}):
1 Like