Filter link field on child table


I have a problem with a Link field inside a child table.

I created a doctype called Case with a field “Case Type” and it has a child table called “Status Shipping”, this child table has two fields: Item (type link to Doctype Item) and Reason (type link to Doctype Cancellation Reason, that I created too).

I need to filter the field Reason based on the field Case Type and the Item that the user selects, this is my code:

frappe.ui.form.on('Status Shipping', {
	item(frm, cdt, cdn) {
		const case_type = frm.doc.case_type
		let item = frm.selected_doc.item

		const filters = {

		frm.fields_dict.status_shipping.grid.get_field('reason').get_query = function() {
			return {
				query: '',

This code is not working, because I need the filter to change when the Item changes, and this is not happening

However, if I close the modal and I re-open it, and I see the field, now the filters have been applied:

I have tried by refreshing the field, but it didn’t work, I think that a solution could be to close and re-open the modal automatically, but I don’t know how to do it either.

Is there any solution to this problem?

Thank you

Hi, you could try this.

function filterChildFields(frm, tableName, fieldTrigger, fieldName, fieldFiltered) {
    frm.fields_dict[tableName].grid.get_field(fieldFiltered).get_query = function(doc, cdt, cdn) {
        var child = locals[cdt][cdn];
        return {    
                [fieldName, '=', child[fieldTrigger]]

This function could solve your problem, as you can see it has some parameters.

tableName: is the name of the childtable, but the name inside the parent doctype
fieldTrigger: is the name of the field which will trigger the filter
fieldName: is the name of the attribute against the filter will be applied (in other words, the field against the value of fieldTrigger will be matched)
fieldFiltered: is the name of the field where the filtered results will be showed

Another thing you need to do is getting the context of which row you are modifying.

Finally the function should be called in ‘refresh’ controller.

1 Like

You can refer to this code to filter the child table accordingly.

frappe.ui.form.on('Purchase Receipt', {
    	refresh(frm) {
        	frm.set_query('batch_number_', 'items', function(doc, cdt, cdn) {
                var d = locals[cdt][cdn];
                return {
                    "filters": {
                    	"item": d.item_code