Filtered dropdown in Link Field Type

@priya_s still won’t work. Btw I have 2 field which is a link type and a select type. I copied the name on the employee module list and inserted it on the select type field. Is it correct?

Can you please share screen-shots?


@kelscey90, This is not correct way to listout employees. For Requested By field set field type is Link and in Option set Employee as like department. Correct this and try again.

Another question is that why are you write frm.add_fetch code on onload of form.? add_fetch method use when you select any value from dropdown then related to that form value fetched and set automatically in current forms field.

1 Like

We need those fetch codes for our other fields to insert automatically when item code is inserted. Is it not possible to write a very long and different script at on module?

I tried it and this error appeared when I tried the department field.

Traceback (innermost last):
File “/home/frappe/press/benches/1605021224/apps/frappe/frappe/app.py”, line 57, in application
response = frappe.handler.handle()
File “/home/frappe/press/benches/1605021224/apps/frappe/frappe/handler.py”, line 19, in handle
execute_cmd(cmd)
File “/home/frappe/press/benches/1605021224/apps/frappe/frappe/handler.py”, line 36, in execute_cmd
ret = frappe.call(method, **frappe.form_dict)
File “/home/frappe/press/benches/1605021224/apps/frappe/frappe/init.py”, line 806, in call
return fn(*args, **newargs)
File “/home/frappe/press/benches/1605021224/apps/frappe/frappe/desk/search.py”, line 13, in search_link
search_widget(doctype, txt, query, searchfield=searchfield, page_len=page_len, filters=filters)
File “/home/frappe/press/benches/1605021224/apps/frappe/frappe/desk/search.py”, line 92, in search_widget
as_list=True)
File “/home/frappe/press/benches/1605021224/apps/frappe/frappe/desk/reportview.py”, line 17, in execute
return DatabaseQuery(doctype).execute(*args, **kwargs)
File “/home/frappe/press/benches/1605021224/apps/frappe/frappe/model/db_query.py”, line 66, in execute
result = self.build_and_run()
File “/home/frappe/press/benches/1605021224/apps/frappe/frappe/model/db_query.py”, line 83, in build_and_run
return frappe.db.sql(query, as_dict=not self.as_list, debug=self.debug)
File “/home/frappe/press/benches/1605021224/apps/frappe/frappe/database.py”, line 147, in sql
self._cursor.execute(query)
File “/home/frappe/press/benches/1605021224/env/lib/python2.7/site-packages/MySQLdb/cursors.py”, line 205, in execute
self.errorhandler(self, exc, value)
File “/home/frappe/press/benches/1605021224/env/lib/python2.7/site-packages/MySQLdb/connections.py”, line 36, in defaulterrorhandler
raise errorclass, errorvalue
OperationalError: (1054, “Unknown column ‘tabEmployee.department_name’ in ‘where clause’”)

Oh it got fixed now! Thank you @priya_s. It just my mistake with the scripts! Sorry!

Btw does our choices have to be emp/0001 then under it is the name of the employee. Is there anyway that we can see only the name? and can we hide the field and appears only when department is chosen?

@kelscey90, Ohhkk…Try with your old code in filter, repace department instead of department_name
i.e. “department”:doc.department

1 Like

Employee get saved by EMP/001 so you are not see only name. But you can also search employee by name. For that go to customize_form,select form Employee and set employee_name in Search Fields. Then you can see employee list like this -

1 Like

if one field means it’s working if i retrieve multiple field in same doctype means

Hi @priya_s I had wrote set query for this link field to display employee_name alone in drop down and i am getting the result but problem after selecting the employee_name from drop down it is not setting in that field it gets disappeared is there any way to set the value which i am selecting from drop down?

Hello @Nivedha . I have done what you requested. I think link formatters would do it too but I simply used the frm.set_value(field_name, value) method in the field event that gets triggered when the field is edited. You have to retrieve the value from a hidden column which fetches the necassary column. Here is a demonstration:

frappe.ui.form.on('YourDoctype', {
    linkfieldname(frm){
        frm.set_value('linkfieldname', frm.doc.fieldnamewithfetch)
    }
});

But I’m struggling to do the set query. If you can show what you have done, I can use that part in my code as well. I’m doubtful whether my part will work since you say the value disappears. But if you show how you could achieve it, I’ll see how it works. Please help me.

Try this:
Doctype = The doctype you want to apply the script to
linkfield = the field which links to user
gender = gender fieldname in user doctype

I’ve added three options for gender, you can only have a single one or multiple ones separated by a coma

frappe.ui.form.on("Doctype", {
	linkfield: function(frm) {
		frm.set_query("linkfield", function() {
			return {
				filters: [
					["User","gender", "in", ["male", "female", "etc"]]
				]
			}
		});
	}
});
1 Like

Add this to the link field fetch from field in edit doctype:

linkfield.full_name

linkfield is the name of the field linked to User Doctype

i need to filter a link field to item table with an ‘OR’ option.
i.e. link field should show items that either have serial number option enabled or that is in a specific item group.
is it possible?
i searched a lot but couldnt find what i was looking for.

https://frappeframework.com/docs/user/en/guides/app-development/overriding-link-query-by-custom-script

You can also use a server side method to generate the result set.

I also believe that the OR operand can be applied to filters but can’t find it in my notes.

have you gotten solution?

f Leave Type == “Earned Leaved” then in clubbed Leave Type should I show only Sick Leave and paternity Leave ?

Can u pls help me

How do I filter dropdown in Link Field Type based on selection let’s say region. So if an applicant selects same region it filters and shows only supervisors from that region for the applicant to select from.