Based on Role Profile, fetch User

In custom doctype has a “User” field (link type),
I want to filter this field based on Role Profile = “System Manager”

using client script

Assuming you have a field called: “role_profile”, and a field called: “user_id”… in your custom DocType…

Execute the following script…

frappe.ui.form.on('YourDoctypeName', {
	role_profile: function(frm) {
        frm.set_query('user_id', () => {
            return {
                filters: {
                    role_profile_name: "System Manager"
                }
            }
        })
	}
})

I hope that helps

1 Like

Thank you for reply @MohamedAbdulsalam
but this code is not working as I want,
like below image the Role Profile, is check but it’s fetched from another doctype

I want filtering with all users has “System Manager” Role

can you help me in this case @NCP

some custom logic require.

check the syntax for that.

Thank You!

1 Like

Hi @Omar_Mohammed
Sorry for misunderstanding you,
In your previous topic, you talked about your desire to filter the “User” field based on the value of the “Role Profile” field = “System Manager”

However, it seems that is not exactly what you want.

What you want is to filter the “User” field based on the list of users who have the “System Manager” role, regardless of what the “Role Profile” is.
Correct me if I’m wrong…

If this is true,
I think the appropriate way in this case is to provide custom filter method on the server side.
Also, there may be one of the functions built into Frappe that performs the function of fetching all users who have a specific role.

I’m not sure,
I will try to do that, and I will send you the script, if it works…

1 Like

Don’t worry brother,

I want the following, I have a field (User Link) and I want to filter it to fetch just (Team Leader) Users

and I will try custom server script filter
@NCP thank you I will try it

Good news, ^ ^
I made a nice attempt… and It worked with me…

I have used set_query() on the client side, and override the filter method by passing a custom method on the server side…(get_system_manager_users)

Please, try this code…

frappe.ui.form.on('YourDoctype', {
    refresh: function (frm){
        cur_frm.set_query("user_fieldlink_name", function() {
            return {
                query: "path_for_file_that_you_set_custom_method_inside.get_system_manager_users"
            };
        });
    },
})

> python method in your module path…

@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 + "%"))

.

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 ^ ^

3 Likes

Thank you so much bro @MohamedAbdulsalam