Help with setting company filter to logged in user's default company

Hi Rushabh,

As requested, I am posting the structure of the report here. Basically we want to set the company filter on this report to logged in user’s default company (company that is saved in logged in user’s employee record). Please help.

frappe.query_reports["Leave Balance"] = {
"filters": [
	{
		"fieldname":"fiscal_year",
		"label": "Fiscal Year",
		"fieldtype": "Link",
		"options": "Fiscal Year",
		"default": frappe.defaults.get_user_default("fiscal_year")
	},
	{
		"fieldname":"company",
		"label": "Company",
		"fieldtype": "Link",
		"options": "Company",
		"default": frappe.defaults.get_user_default("Company") || frappe.defaults.get_global_default("company")
	},
	    {
		"fieldname":"status",
		"label": "Status",
		"fieldtype": "Select",
        "options": ["","Active","Left"],
		"default": "Active"
	}
]

}

PS: thank you for confirming the approach for Account Head vs Account Code.
MP

1 Like

Mayur,

Is this code not working?

Hi Anand,

This code is working for all the users but not for 23 users. The reason for this is below.

In our ERPNext instance we have 7 companies.I am employed by company B
(in my employee record, the company is set as B). All my leave
applications are added against company B. I am assigned to two companies
(A and B) as part of User Permissions Manager so that I can create
purchase order for both Company A and B. Please note there are about 23
users who has this kind of configuration.

In our custom report, we have a company filter. Ideally, we want to
set company filter based on logged in user’s employee record. There is
an issue with using “frappe.defaults.get_user_default(“Company”) ||
frappe.defaults.get_global_default(“company”)”. It always return the
first record from user permissions manager for company doctype for the
logged in user. So when I logged it set the company filter as Company A (this is the first record in the permitted list of the companies).

I hope above make sense.

Can you switch the sequence in the User Permissions? Does that help?

If you want to use brute force, just create a dict for those 23 users and hard code their company names.

Yes, switching the sequence in the user permissions does help. So I will use that solution for now.

BTW, what do you mean by using brute force and creating a dictionary?

PS: You can close the topic if you want.