(A) DocType, and it has a Multi-select field named (allowed_hotels). Which is a Link to the Supplier Doctype, and Filters applied: Supplier Group = Hotels
(B) DocType, linked to (A) DocType, and has another Link field (hotel).
I want the last field (hotel) in DocType B to display only pre-selected hotels in the multi-select in DocType A.
I tried this script on DocTyp B
frappe.ui.form.on('B', {
refresh: function(frm) {
frm.fields_dict['hotel'].get_query = function(doc, cdt, cdn) {
let a = doc.a; // Get the linked A DocType
let allowed_hotels = []; // default to empty if no A is linked
if (a) {
// Fetch the allowed_hotels field data from the linked A
return ('A', a, 'allowed_hotels')
.then(response => {
var allowed_hotels = response.message.allowed_hotels;
return {
filters: [
['supplier_group', '=', 'Hotels'],
['name', 'in', allowed_hotels]
]
};
});
} else {
return {
filters: [
['supplier_group', '=', 'Hotels']
]
};
}
};
},
@frappe.whitelist()
def get_allowed_hotels(doctype, txt, searchfield, start, page_len, filters):
a = filters.get("a")
# Fetch the allowed hotels from the child table
allowed_hotels_list = frappe.db.sql("""
SELECT hotel
FROM `tabA_Allowed_Hotels`
WHERE parent = %s
""", a)
if not allowed_hotels_list:
return []
allowed_hotels_list = [item[0] for item in allowed_hotels_list]
return frappe.db.sql("""
SELECT name, supplier_name
FROM `tabSupplier`
WHERE `tabSupplier`.name IN (%s)
AND `tabSupplier`.name LIKE %s
LIMIT %s OFFSET %s
""",
(tuple(allowed_hotels_list), "%%%s%%" % txt, page_len, start))
And it worked perfectly. A_Allowed_Hotels is my child doctype for the multi-select field