Filter Listview with Custom Script

I’m trying to filter doctype Item list view to show only non template and not disabled items for non System Manager users. My custom script for Item doctype below is not working.

frappe.ui.form.on("Item", "refresh", function(frm) {
    cur_frm.set_query("item_code", function() {
        if (frappe.user_roles.indexOf("System Manager")==-1){
    return {
            "filters":     
        [
        ["Item": "has_variants", "=", "No"],
        ["Item": "disabled", "=", "No"]
        ]
    }
        };
    });
});

What am I missing here? Any help please.

Custom Script will not filter your item list view. Above code will get executed when New / Existing Item form is opened. You have to try by server side logic.

You can try permission_query_conditions hook in your custom app.
Check how User listview is filtered.

Try removing “” from “filters”:

My Code:

cur_frm.set_query("ifsc_code", function(doc, cdt, cdn) {
    var d = locals[cdt][cdn];
    if(d.micr){
    return {
        filters: [
            ["micr", "=", d.micr]
        ]
    }
    }
});

Thanks. Will check these out.

first add this in your hook file:

permission_query_conditions = {
	"Credit Control Process": "property_handover.property_handover.doctype.credit_control_process.credit_control_process.get_permission_query_conditions",
	"Customer Service Processing": "property_handover.property_handover.doctype.customer_service_processing.customer_service_processing.get_permission_query_conditions",
}

has_permission = {
	"Credit Control Process": "property_handover.property_handover.doctype.credit_control_process.credit_control_process.has_permission","Customer Service Processing": "property_handover.property_handover.doctype.customer_service_processing.customer_service_processing.has_permission",
	
}

after that add this in your py file :slight_smile:

def get_permission_query_conditions(user):
	if not user: user = frappe.session.user
	if "Customer Service User" in frappe.get_roles(user):
		u = frappe.db.sql("""select * from `tabProperty Handover Officers` where user_id=%s""",(frappe.session.user),as_dict=True)
		return """(`tabCredit Control Process`.officer_name = '{current_sales_admin}' or `tabCredit Control Process`.officer_name = '{officer_name}')""".format(current_sales_admin=u.name, officer_name="")
	
def has_permission(doc):
	u = frappe.db.sql("""select * from `tabProperty Handover Officers` where user_id=%s""",(frappe.session.user),as_dict=True)
	if "Customer Service User" in frappe.get_roles(frappe.session.user) and (u.full_name==doc.officer_name or doc.officer_name==""):
		return True
	else:
		return False