How to set filter company in report view and set this field as readonly for users

how to set filter company in report view and set this field as readonly for users

Try this,
“fieldname”: “company”,
“label”: __(“company”),
“fieldtype”: “Read Only”,
“default”: frappe.datetime.get_today()

If you want to do this on a particular condition, try below:

$(‘body’).find(“[data-fieldname=company]”).val(frappe.boot.company).prop(“disabled”, true)
report.trigger_refresh();

i want to set readonly for roles other than System manager

Use this condition to set read-only permissions for users other than the system manager.
frappe.user.has_role(“System Manager”)

thank you

Also i want to set the session default of the logged in user company,the session default clear every time logged in again.The company field has no value during user login,it is only populated when the user selects a company.

To set the session default of the logged-in user’s company

  • in your hooks.py, include an app include js like
app_include_js = ["/assets/supercool/js/persistent_defaults.js"]

create the above file in public/js and add this code:

$(document).on('app_ready', function() {
    if (document.referrer.endsWith("/login")) {
        // get the company of the logged-in user
        frappe.call({
            'method': "frappe.client.get_value",
            'args': {
                'doctype': "User",
                'filters': {'name': frappe.session.user},
                'fieldname': "company"
            },
            'callback': function(response) {
                if (response.message) {
                    var company = response.message.company;
                    // set the company as a session default
                    frappe.defaults.set_user_default_local("company", company);
                }
            }
        });
    }
});

You can change the site name and your app name according to your project.

1 Like