Frm.set_query() does not even get registered on Sales Order

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.

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:

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:

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

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