I have the following client script:
frappe.ui.form.on('Sales Order', {
onload(frm) {
console.log("[DEBUG] onload fired");
frm.set_query('item_code', 'items', function(doc, cdt, cdn) {
console.log("[DEBUG] query registered");
return {
query: 'custom_app.api.get_available_items',
reference_doctype: 'Sales Order Item',
}
});
}
});
Although the onload event is triggered, the set_query never gets executed. The child table’s name and field name is correct.
I’m not sure what the issue is. There are no errors in the console.
snv
June 11, 2023, 4:48am
#2
You’re trying to check whether the query got registered in the callback function, but the callback function isn’t called until after the you change any item code.
If you want to check whether or not it got registered, considering add a console.log
somewhere inside frm.set_query
function:
field.hidden = show ? 0 : 1;
});
}
get_files() {
return this.attachments
? frappe.utils.sort(this.attachments.get_attachments(), "file_name", "string")
: [];
}
set_query(fieldname, opt1, opt2) {
if (opt2) {
// on child table
// set_query(fieldname, parent fieldname, query)
this.fields_dict[opt1].grid.get_field(fieldname).get_query = opt2;
} else {
// on parent table
// set_query(fieldname, query)
if (this.fields_dict[fieldname]) {
this.fields_dict[fieldname].get_query = opt1;
}
More importantly, to stop your code from getting overwritten, consider using the refresh
or onload_post_render
event. Or consider patching this controller in your code to run your code after it’s setup_queries
call:
cur_frm.email_field = "contact_email";
frappe.provide("erpnext.selling");
erpnext.selling.SellingController = class SellingController extends erpnext.TransactionController {
setup() {
super.setup();
}
onload() {
super.onload();
this.setup_queries();
this.frm.set_query('shipping_rule', function() {
return {
filters: {
"shipping_rule_type": "Selling"
}
};
});
}
setup_queries() {
Wow, thanks for the insights and detailed explanation. I had not even considered the possibility that it might get overwritten.
I changed the onload
to onload_post_render
and now it’s working!
If there are no side effects, then I think I’ll just use this event in the future for all my set_queries.
1 Like
snv
June 11, 2023, 4:56am
#4
This should be fine for set_query
since it’s called when the user clicks the link field which is generally after onload_post_render
has triggered.
1 Like