Filtered dropdown in Link Field Type

Hi,

I have Link field in my doctype which points to user doctye. When I click it I get a dropdown listing all users.

I need a list of male users. How can I filter that dropdown to have only male or female users.

1 Like

It is possible by using get_query method and you have to write custom script for that. Refer following example -

cur_frm.fields_dict['user'].get_query = function(doc) {
	return {
		filters: {
			"gender": 'Male'
		}
	}
}
8 Likes

Thanks @priya_s :smiley:

How can I do the same in a child form?
(ie, if user field is in the child form)

1 Like
6 Likes

could you please explain @revant_one ?

@ninjas005 You can also use this -

cur_frm.fields_dict["child_table_fieldname"].grid.get_field("user").get_query = function(doc){
       return {
               filters:{
                       "gender": "Male"
               }
       }
}
6 Likes

Check out Mode of Payment in ERPNext.

There is a docfield “accounts” in Mode of Payment with type “Table”.
“account” field is a child table “Mode of Payment Account”.

default_account is the field in child table which is to be filtered.

3 Likes

Hi @revant_one
Hi @priya_s

I’ve tried both methods… Both Works :smile:

Thank you

1 Like

Hi @revant_one @priya_s

Need help with two things.

  1. How can I change a value in the child form, when I change a value in the same form?
  2. Also change the value in the parent form, when i change the value in the child form?

Something like how “source warehouse” in the row in items of Stock Entry is selected?
Checkout stock_entry.js

Something like how BOM calculates raw material cost from rows in items? Checkout bom.js

I want to filter item_code in child table based on custom field brand_name

My parent doc name: Delivery Note
child table : Delivery Note Item ( field_name : items)

i had added following script in “Delivery Note” custom script but not working. Please help

cur_frm.fields_dict[‘items’].grid.get_field(‘item_code’).get_query = function(doc) {
return{
filters:[
[‘brand’, ‘=’, brand_name]
]
}
}

@jyotinb , Please try with following code :

frappe.ui.form.on("Delivery Note", "onload", function(frm, cdt, cdn){
	frm.set_query("item_code", "items", function(cdt, cdn) {
		var c_doc = locals[cdt][cdn];
		return {
		       "filters": ['brand', '=', c_doc.brand_name]
		};
	});
});
1 Like

Thanks for fast reply

Not working

copy script in custom script of “Delivery Note” custom script is ok?

My query again with clear documentation.

I want to filter item_code in Delivery note based on field brand_name
(both item_code & brand_name are in child table Delivery Note Item (field name items))

I had created extra field ‘i1’ type link field and options Item (same as item_code)

  1.   My following code works pefectly for 'i1' , but not for 'item_code'. 
    

cur_frm.fields_dict[‘items’].grid.get_field(‘i1’).get_query = function(doc) {
return {

		filters: {
			 "brand": "brand1"
			
		}
	}
}
  1. and how to refer dynamic filter field
    ‘brand_name’ instead of fixed value “brand1”.

hi @jyotinb,

if you need a dynamic filter, then you need to

  1. get the value with which you need to filter (use get_value) in onload
  2. then you apply the filter with that value.
    3 then you refresh listview

refer Filter list view using callback data

@priya_s Hi, I’ve tried the code but it won’t categorize our list??


Here’s the code:

cur_frm.fields_dict[‘requested_by’].get_query = function(doc) {
return {
filters: {
“Operations”: doc.department,
“Admin”: doc.department,
“Sales”: doc.department,
“Engineering”: doc.department,
“Legal”: doc.department,
“Purchase”: doc.department,
“Marketing”: doc.department,
“Human Resources”: doc.department,
}
}
}

@kelscey90, something is incorrect in code.
By seeing screenshots,as per my understanding you have to show only those employee in drop down which having department “Engineering”.
so in filter you have write only department value,in filter try with this-
filters: {
“department”: doc.department
}

@priya_s Yes I only need to show those people who are engineers only when engineering is chosen. But it kept showing everyone. I’ve tried your custom script but is still wont categorize it?

This is my overall script:

frappe.ui.form.on(“Stock Request”, “onload”, function(frm, cdt, cdn, doc, dt, dn){
frm.add_fetch(“item_code”, “description”, “description”);
frm.add_fetch(“item_code”, “item_name”, “item_name”);
frm.add_fetch(“item_code”, “stock_uom”, “uom”);
frm.add_fetch(“item_code”, “item_group”, “item_group”);
frm.add_fetch(“item_code”, “brand”, “brand”);
frm.add_fetch(“requested_by”, “Non-Alcon Personnel”, “non_alcon_personnel”);
frm.add_fetch(“tc_name”, “terms”, “terms”);
frm.set_value(“date_requested”, frappe.datetime.add_days(frappe.datetime.nowdate()));

})

cur_frm.fields_dict[‘requested_by’].get_query = function(doc) {
return {
filters: {
“Department”: doc.department,
}
}
}

If you are using default Department form try with this in filter-
“department_name”: doc.department

Thanks, Priya