Client-side script not working?

Below code I have written for creating a custom filter for the item_code to be selected in a Delivery Note. It works fine on other fields but it specially doesn’t work on item_code field. Any ideas?

frappe.ui.form.on('Delivery Note', {
    onload: function(frm, cdt, cdn) {
        frm.set_query('item_code', 'items', function(item_frm, item_dt, item_dn) {
            return {
                filters: [
                    ['item_name', '=', 'test'],
                ]
            };
        });
    }
});

@thelahera Try this…

frappe.ui.form.on('Delivery Note', {
    onload: function(frm, cdt, cdn) {
        frm.set_query('item_code', 'items', function(item_frm, item_dt, item_dn) {
            return {
                filters: {
                    item_name: 'test',
                }
            };
        });
    }
});
1 Like

@kid1194
I’ve tried it and still have the same issue. I have another field where I’m apply a filter using similar code, but item_code specifically is not getting filtered.

Is there anything in ERPNext itself that is preventing filters from being applied to item_code specifically?

@thelahera It looks like the item_code is a child table field soake sure that the doctype for the client script is the parent doctype not the child doctype and the script should be something like…

frappe.ui.form.on('Parent Doctype', {
    onload: function(frm) {
        frm.set_query('item_code', 'child_table_fieldname', function(item_frm, item_dt, item_dn) {
            return {
                filters: {
                    item_name: 'test',
                }
            };
        });
    }
});

@kid1194
I’ve set the child table field name correctly. It is “items”. As mentioned already, I am able to apply the filter on other fields in the child table, except on item_code.

@thelahera I have never faced nor heard of a link field that can’t be filtered so this is something new…
Or there might be another script that is overriding your filter…

I’ve run into this issue before and found that in such situations, you can use get_query instead as a workaround.

frappe.ui.form.on("Delivery Note", "refresh", function(frm) {
    cur_frm.fields_dict['items'].grid.get_field('item_code').get_query = function(doc, cdt, cdn) {
    	return {
    		filters:[
    			['Item', 'item_name', '=', 'test 1'],
    		]
    	}
    }
});
1 Like

Hey, you need to rewrite the query.js in your public/js file.

@Jitendra_Rathod

Won’t that be lost during backup and restore? Why is editing files necessary when it can be done with Client scripts?