Selected values in link field

I have a doctype with Link field with option as “User”. But I don’t want ti show all users in link field. i.e. want to filter this link field, say having email id in [“aaa@gmail.com,"bbb@gmail.com", …].

how can I achieve this?

I have tried following code:

frm.set_query(“user”, function() {
return {
filters: [
[“User”,“full_name”, “in”, [“bbb@gmail.com”, “aaa@gmail.com”, “ccc@gmail.com”]]
]
}
});

But getting following error:

Traceback (most recent call last):
File "/home/frappe/frappe-bench/apps/frappe/frappe/app.py", line 62, in application
 response = frappe.handler.handle()
File "/home/frappe/frappe-bench/apps/frappe/frappe/handler.py", line 22, in handle
 data = execute_cmd(cmd)
File "/home/frappe/frappe-bench/apps/frappe/frappe/handler.py", line 53, in execute_cmd
 return frappe.call(method, **frappe.form_dict)
File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 941, in call
 return fn(*args, **newargs)
File "/home/frappe/frappe-bench/apps/frappe/frappe/desk/search.py", line 14, in search_link
 search_widget(doctype, txt, query, searchfield=searchfield, page_length=page_length, filters=filters)
File "/home/frappe/frappe-bench/apps/frappe/frappe/desk/search.py", line 39, in search_widget
 searchfield, start, page_length, filters)
File "/home/frappe/frappe-bench/apps/frappe/frappe/desk/search.py", line 35, in search_widget
 searchfield, start, page_length, filters, as_dict=as_dict)
File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 941, in call
 return fn(*args, **newargs)
File "/home/frappe/frappe-bench/apps/frappe/frappe/core/doctype/user/user.py", line 820, in user_query
 if filters and filters.get('ignore_user_type'):
AttributeError: 'list' object has no attribute 'get'

How can I bring selected link field values on the form?

Hi @grh

Try this:

refresh: function(frm) {
    // filter for user based on full_name field
    cur_frm.fields_dict['user'].get_query = function(doc) {
         return {
             filters: [
                 ["full_name", "in", [“bbb@gmail.com”, "aaa@gmail.com", "ccc@gmail.com"]]
             ]
         }
    }
}
1 Like

Getting same error as posted in question

use this

frm.set_query(“user”, function() {
    return {
        filters: {
            “linked_field_name”: [“in”, [“bbb@gmail.com”, "aaa@gmail.com", "ccc@gmail.com"]]
    }
});

As you said, tried this…

frm.set_query(‘user’, function() {
return {
filters: {
“user”: [“in”, [“bbb@gmail.com”, “aaa@gmail.com”, “ccc@gmail.com”]]
}
}
});

But it is not getting filtered.

Try this one,

@frappe.whitelist()
def get_data(doctype, txt, searchfield, start, page_len, filters):
	return frappe.db.sql("""select name, first_name, last_name from `tabUser`
		where email IN ('test@gmail.com','abc@gmail.com')""")

.

frappe.ui.form.on('Test', {
	refresh: function(frm) {
		frm.set_query("user", function() {
		return {
			query: "erpnext.hr.doctype.test.test.get_data",
			filters: {
				user: frm.doc.user
			}
		};
	});

	}
});

Hi all

Solved by the post : https://discuss.frappe.io/t/can-not-filter-in-link-field/44910/2?u=grh

i.e. by making own api and calling it

1 Like