Is there a way to set a query during runtime though?
In @imbra’s question, the filter is a constant (doc.name) and so can be set during creation using get_query. Suppose I want to set the filter based on another field in the dialog:
let d = new frappe.ui.Dialog({
fields: [
{ fieldname: "item_code", fieldtype: "Link", options: "Item", reqd: 1 },
{ fieldname: "bom", fieldtype: "Link", options: "Item", reqd: 1 },
]
});
//how do I do the part below?
d.item_code.onchange = function (event) {
d.set_query('bom', ()=>{
return {
'filters': { item: d.item_code }
}
})
}
Hello, it’s not a constant! Indeed it’s evaluated for every search, that’s why get_query is a function, so that the Link field can update the query on every keystroke.
Hi @corentin, thanks! I was able to figure it out.
I am not super familiar with JS, so at the time, I didn’t know you could refer to the dialog from within it’s own fields dict at the time of creation. Hence my confusion.
const dialog = new frappe.ui.Dialog({
fields: [
{ fieldname: "item_code", fieldtype: "Link", options: "Item", reqd: 1 },
{
fieldname: "bom",
fieldtype: "Link",
options: "Item",
reqd: 1,
get_query() {
return {
filters: {
// I didn't know you could refer to "dialog" here
item: dialog.get_value("item_code"),
},
};
}
},
],
});