Client Script on Delivery Note Item is not executing onload

I’m trying to run custom script on the child table of a Delivery Note. This is my sample code and the script status is Enabled and DocType is set to “Delivery Note” but it’s not executing.

frappe.ui.form.on("Delivery Note Item", "onload", function(frm) {    
    msgprint("Delivery Note Item script loaded.");
});

@thelahera - Onload event is triggered when the form is loaded and is about to render. This will be triggered when the DocType is loaded and not child Doctype. If you are using onload you can call it on the DocType directly and then traverse the child DocType using a loop.

If you could elaborate your use case for onload event on the child table, maybe I could suggest another event or a way to trigger it.

To the best of my understanding the script inside the onload event of a child doctype will never be triggered.

@atulagrawal In the one of the fields (Link Query) on the child table form, I’m looking at setting up a custom filtering query like this example: Overriding Link Query By Custom Script

So I need to access the form variable for the child table.

You can run the script on the onload event on the Delivery Note, and then set the query against the field in the child table.

frappe.ui.form.on('Delivery Note', {
    onload: function(frm, cdt, cdn) {
        frm.set_query('field_name_here', 'items', function(item_frm, item_dt, item_dn) {
            return {
                filters: [
                   // Your custom filter here.
                ]
            };
        });
    }
});

@thelahera

frappe.ui.form.on('parent-doctype-name', {
    setup: function(frm) {
        set_filters_for_link_field_in_child_table(frm);
    },
}

function set_filters_for_link_field_in_child_table(frm) {
    frm.set_query('child-doctype-field', 'child-doctype-field-name-in-parent-doctype', function() {
        return {
            filters: { ...your filters here... }
      };
    });
}
2 Likes