Set filter in child table with value from same table

Get Query Method does not work when its applied to child form and the child row is open
Hi,

Here is my code

frappe.ui.form.on("Staff Payment Receipt Expenses", "reference_invoice", function(frm, cdt, cdn)
{		
	var ref_inv = locals[cdt][cdn]['reference_invoice'];
	cur_frm.fields_dict['expenses'].grid.get_field("reference_payment").get_query = function(infrm)
		{
		   return {
				   filters:{
						   "parent": ref_inv
				   }
		   }
		};
});

Staff Payment Receipt Expenses is a child form (field name - expenses)
reference_invoice,reference_payment are both link fields in the Staff Payment Receipt Expenses form
reference_invoice is link field referring to an invoice table
reference_payment is a link field referring to invoice items, a child table of invoice table

what i tried to do is when i select an invoice (reference_invoice field), i need reference_payment to show entries having parent reference_invoice.(simply i dont want to show an invoice item from another invoice)

I believe code is correct but doesnt work when i select reference_invoice.

But its filtered when i select the row after i insert it.

Why its not working at the same time ? What can i do to make it work properly?

1 Like

@ninjas005, let me be a bit ninja!

frappe.ui.form.on("Staff Payment Receipt Expenses", "reference_invoice", function(frm, cdt, cdn){
  var child = locals[cdt][cdt], 
       grid_row = cur_frm.fields_dict['expenses'].grid.grid_rows_by_docname[child.name],
       field = frappe.utils.filter_dict(grid_row.docfields, {fieldname: "reference_payment")[0];

  field.get_query =  function(){
    return {
       filters: { parent: child.reference_invoice
    }
  }
  grid_row.refresh_field("reference_invoice");
});
6 Likes

hey, how can i refresh the field after setting the get_query ? I am changing the get_query based on a Checkbox.
New fields gets the latest get_query function, but how to update the old ones ?

I tried this :

cur_frm.fields_dict["items"].grid.get_field("item_code").get_query =  customGetQuery;
$.each(cur_frm.fields_dict["items"].grid.grid_rows, function(i, row) {
    var field = frappe.utils.filter_dict(row.docfields, {fieldname: "item_code"})[0];
    field.get_query = customGetQuery;
    row.refresh_field("item_code");	
});

Can you please tell what I am doing wrong ? :slight_smile:
Thanks :slight_smile:

`

@fahimalizain, you are trying to refresh all grid_rows, you just need to refresh the active!

Use form_render event, in the table doctype, to get it working!

well I am :slight_smile:
My requirement is to show only those items having balance stock in current warehouse in the dropdown list of the link field, based on a check. So sometimes, many items will be added and later on the checkbox will be checked. I want to update the old field’s link queries…

By the way, when is form_render event fired ? Where can i get some docs to read on client side programming ? :slight_smile:

Thank you

@fahimalizain,

I got what are you trying to do, but by experience, in the way that are you trying to do, frappe don’t support. You only can refresh a field that is being diplayed!

Form_render is fired when the user display a table form!

To it work properly you need disable editable grid.

The best place, is in frappe and erpnext code.

well what is the best way to do then ? :slight_smile:

Disable editing in grid, and edit it in the expanded-row form ? That is hard for the users :frowning2:

cant we reset the link-query in the grid after it is initialized and used ?

@fahimalizain, you can use a dynamic link query:

eg:

    var d = locals[cdt][cdn];
    if (something){
        return {
           filters: other_things;
        }
    } else if (anything) {
       return {
           filters: usually_things
       }
    } else {
       return {
           filters: stranger_things;
       }
    }
} 
1 Like

@max_morais_dmm

Thank you very much sir !! :grinning:
Got everything working…

Test Code, in case anyone in future has such requirement

frappe.ui.form.on("Sales Invoice", {
refresh : function(frm, cdt, cdn) {
	// item_code is a field in grid whose fieldname is items.. refer Sales Invoice
	frm.set_query("item_code", "items", function(doc) {
		if (frm.doc.custom_checkfield == 1) {
			return { "filters" : {"valuation_rate" : "10"}}
		} else {
			return { "filters" : {}}
		}
	});
	
	console.dir(cur_frm.fields_dict.items.grid.get_field("item_code"));
    }
);

Reading the chrome js console was helpful too :slight_smile:

Thank you.

1 Like

@max_morais_dmm @fahimalizain

Thank you very much .

This code has just helped me too at the close of 2019!