Based on Role Profile, fetch User

In my case,

  • I have a DocType named: “Master”…
  • Link-type field, named: “User”
  • Assuming I want to apply filters to the link field “User” to show only users with the “System Manager” role…

This is the code that I used…>>>

frappe.ui.form.on('Master', {
    refresh: function (frm){
        cur_frm.set_query("user", function() {
			return {
				query: "training_app.training_app.doctype.master.master.get_system_manager_users"
			};
		});
    },
})

> python method

@frappe.whitelist()
def get_system_manager_users(doctype, txt, searchfield, start, page_len, filters):
	return frappe.db.sql("""
		select u.name, concat(u.first_name, ' ', u.last_name)
		from tabUser u, `tabHas Role` r
		where u.name = r.parent and r.role = 'System Manager' 
		and u.enabled = 1 and u.name like %s
	""", ("%" + txt + "%"))

I hope that helps ^ ^