From dc4509796d8284803d661125cc1a96bb10300a0b Mon Sep 17 00:00:00 2001 From: Samuel Danieli <23150094+scdanieli@users.noreply.github.com> Date: Tue, 28 Mar 2023 08:17:56 +0200 Subject: [PATCH] fix: suggested email ids in New Email dialog (#20319) * chore: enhance UX of New Email dialog * do not show contacts without an email * use name as value, y? if several contacts use the same email address, the entry will be displayed several times, but always with the same description, which leads to confusion - using name as value makes the entries distinguishable * chore: ignore semgrep Rewriting the query is not in the scope of this PR. * chore: keep semgrep failing on raw query [skip ci] * fix: use email_id as value * Revert "fix: use email_id as value" This reverts commit e4c44e2094ddb9b525bc34d400642dcee5656096. * chore: comment confusing code --------- Co-authored-by: Raffael Meyer <14891507+barredterra@users.noreply.github.com> Co-authored-by: Ankush Menat --- frappe/email/__init__.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/frappe/email/__init__.py b/frappe/email/__init__.py index 1f6af4a3e7..486db2a784 100644 --- a/frappe/email/__init__.py +++ b/frappe/email/__init__.py @@ -11,7 +11,7 @@ def sendmail_to_system_managers(subject, content): @frappe.whitelist() def get_contact_list(txt, page_length=20) -> list[dict]: - """Returns contacts (from autosuggest)""" + """Return email ids for a multiselect field.""" if cached_contacts := get_cached_contacts(txt): return cached_contacts[:page_length] @@ -19,11 +19,14 @@ def get_contact_list(txt, page_length=20) -> list[dict]: reportview_conditions = build_match_conditions("Contact") match_conditions = f"and {reportview_conditions}" if reportview_conditions else "" + # The multiselect field will store the `label` as the selected value. + # The `value` is just used as a unique key to distinguish between the options. + # https://github.com/frappe/frappe/blob/6c6a89bcdd9454060a1333e23b855d0505c9ebc2/frappe/public/js/frappe/form/controls/autocomplete.js#L29-L35 out = frappe.db.sql( - f"""select email_id as value, + f"""select name as value, email_id as label, concat(first_name, ifnull(concat(' ',last_name), '' )) as description from tabContact - where name like %(txt)s or email_id like %(txt)s + where (name like %(txt)s or email_id like %(txt)s) and email_id != '' {match_conditions} limit %(page_length)s""", {"txt": f"%{txt}%", "page_length": page_length},