Different display citeria for different docs

Hi

I have an “Employee” on my system. On employee there are two fields…

employee_number

passport_number

Employee is used on tow custom doctypes … Document A and Document B

Document A … Employee ( Link to "employee ) ; When employee link field is selected, drop down must show … employee_number and Employe doc ID no i.e.HR-EMP-00031

Document B … Employee ( Link to "employee ) ; When employee link field is selected, drop down must show … passport_number and Employe doc ID no i.e.HR-EMP-00031

So in doc A, I can search per employee_number and in the case of Doc B, I can search per passport_number

Does anyone have a suggestion , please ?

Hi @trainingacademy

You can use set_query to customize your search dropdowns.
Check this

Hope this helps.

Thank you @avc let me look at that. Appreciate your time

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.