isplay fields are set on the Employee doctype, so they apply everywhere. You can’t set them per link field in the UI. The fix is a custom search query for each doctype.
Server side (myapp/api.py)
python
import frappe
@frappe.whitelist()
@frappe.validate_and_sanitize_search_inputs
def employee_query_a(doctype, txt, searchfield, start, page_len, filters):
return frappe.db.sql("""
SELECT name, employee_number
FROM `tabEmployee`
WHERE name LIKE %(txt)s OR employee_number LIKE %(txt)s
ORDER BY name
LIMIT %(start)s, %(page_len)s
""", {"txt": f"%{txt}%", "start": start, "page_len": page_len})
@frappe.whitelist()
@frappe.validate_and_sanitize_search_inputs
def employee_query_b(doctype, txt, searchfield, start, page_len, filters):
return frappe.db.sql("""
SELECT name, passport_number
FROM `tabEmployee`
WHERE name LIKE %(txt)s OR passport_number LIKE %(txt)s
ORDER BY name
LIMIT %(start)s, %(page_len)s
""", {"txt": f"%{txt}%", "start": start, "page_len": page_len})
Client script on Document A
js
frappe.ui.form.on("Document A", {
setup(frm) {
frm.set_query("employee", () => ({
query: "myapp.api.employee_query_a"
}));
}
});
Do the same on Document B with employee_query_b.
The first column is the value stored in the field. The other columns show as the grey description in the dropdown. So you get HR-EMP-00031 with the employee number under it in A, and the passport number in B.
If you want the search to also match those fields, the OR in the query already handles that.
Use setup, not refresh, so the query is set once.