Setting query filter to a child table

I have a child table called Subject Child and the parent table called Subject Parent. I want to filter the score field in the child table Subject Child.

This is now my initial code but I error occurred “Cannot set property ‘get_query’ of undefined”. Any idea how to filter the field in the child table?

@max_morais_dmm :smile:

frappe.ui.form.on("Subject Parent", "subject_child_on_form_rendered", function (doc, grid_row) {
    cur_frm.set_query("score", function () {
        return {
            "filters": {
                "kind": "Child"
            }
        };
    });
})

Hi @ccfiel

Please check journal_entry.js for set_query example.
The syntax for set query is :

me.frm.set_query("field_name", "childtable_name", function(doc, cdt, cdn) {
		return {
			filters: {
				company: me.frm.doc.company,
				is_group: 0
			}
		};
	});
5 Likes

@makarand_b, @ccfiel just to complement

you can add filters in this notation too

filters: [
  ["DocType", "docfield", "op", "value"]
]

where the valid op are:

=
>
<
>=
<=
<>
like
4 Likes

@max_morais_dmm @makarand_b

I made a sample code

frappe.ui.form.on("Combine Subject Grade", {
    setup_queries: function () {
        console.log("im setup queries!");
        var me = this;
        me.frm.set_query("subject", "combine_subject_score", function (doc, cdt, cdn) {
            return {
                filters: {
                    kind: "Child"
                }
            };
        });
    },
    onload: function () {
        console.log("im loading!")
    }
});

The onload function triggers but setup_queries is NOT :frowning: Any idea why? Did I miss something?

1 Like

@ccfiel

this.setup_queries();

@max_morais_dmm Sorry for a dumb question :blush: Where do I put that code?

@ccfiel try this

frappe.ui.form.on("Sales Invoice", "onload", function(frm) {
    frm.fields_dict.items.grid.get_field('child_field').get_query =
			function() {
				return {
					filters: {
						"customer": "",
                                                "delivery_document_no": ""
					}
				}
			}

});
4 Likes

@jof2jc It works :slight_smile: Thanks!

1 Like

@max_morais_dmm I tried to put it onload but I got this error Uncaught TypeError: this.setup_queries is not a function @jof2jc solution works. I was wondering why its not working what is in ERPNext code? :smile:

frappe.ui.form.on("Combine Subject Grade", {
    onload: function () {
        this.setup_queries();
    },
    setup_queries: function () {
        console.log("im setup queries!");
        var me = this;
        me.frm.set_query("subject", "combine_subject_score", function (doc, cdt, cdn) {
            return {
                filters: {
                    kind: "Child"
                }
            };
        });
    }
});

@ccfiel, sorry, my bad!

frappe.ui.form.trigger("Combine Subject Grade", 'setup_queries');

if this dont work, use the old-style to create the function

cur_frm.cscript.setup_queries = function(){}

and use

frm.cscript.setup_queries()

to call it

2 Likes

@max_morais_dmm No worries. Thanks a lot!