[Help] Adding secondary_reports_to in Employee – Same Access as reports_to

How reports_to is currently working

In the Employee DocType, the reports_to field defines the direct reporting manager.
In Frappe HR / ERPNext, this field is used to build the employee hierarchy and automatically give the User linked to the reports_to employee permission to view their subordinates’ HR documents (e.g., Expense Claims, Leave Applications, Appraisals).

The current mechanism works through:

  • NestedSet structure (nsm_parent_field = "reports_to") for efficient hierarchy queries.
  • User Permissions and “Apply User Permissions” on HR DocTypes to scope visibility to the manager’s subtree.
  • Occasionally, permission_query_conditions or has_permission methods for certain HR DocTypes to further control access.

Why I need secondary_reports_to

In our setup, some employees have an alternate or dotted-line manager who also needs to review or approve their documents.
Right now, this isn’t possible without:

  • Making the alternate manager the primary manager (which breaks the actual reporting structure), or
  • Manually creating individual User Permissions for every secondary relationship.

So the goal is simple:
Let the secondary manager see the exact same HR docs for that employee as the primary manager does.


What I tried implementing

Goal achieved

  • Added a new secondary_reports_to Link field to Employee.
  • Wrote logic to give the User linked to the secondary_reports_to employee the same access as the reports_to manager.

Implementation approach

1. Field creation

  • Added secondary_reports_to (Link → Employee) in the Employee DocType.

2. Dynamic User Permission creation

  • On save/update of an Employee:
    • Use get_descendants_of("Employee", doc.name) (from frappe.utils.nestedset) to get the employee and their primary subtree.
    • For each employee in that set, create User Permission records for the secondary manager’s user:
      allow="Employee", for_value=<employee>, apply_to_all_doctypes=1.

3. Role Permission Manager

  • Verified that all relevant HR DocTypes (Expense Claim, Leave Application, Appraisal, etc.) have “Apply User Permissions” enabled for Employee.

Code used (simplified)

from frappe.utils.nestedset import get_descendants_of

def _user_of_employee(emp):
    return frappe.db.get_value("Employee", emp, "user_id")

def _employees_in_primary_subtree(root_emp):
    desc = get_descendants_of("Employee", root_emp) or []
    return list({root_emp, *desc})

def _upsert_user_perm(user, emp):
    if not frappe.db.exists("User Permission",
        {"user": user, "allow": "Employee", "for_value": emp}):
        doc = frappe.get_doc({
            "doctype": "User Permission",
            "user": user,
            "allow": "Employee",
            "for_value": emp,
            "apply_to_all_doctypes": 1
        })
        doc.insert(ignore_permissions=True)

def sync_secondary_user_perms(doc, method=None):
    sec_emp = getattr(doc, "secondary_reports_to", None)
    if not sec_emp:
        return
    sec_user = _user_of_employee(sec_emp)
    if not sec_user:
        return
    targets = _employees_in_primary_subtree(doc.name)
    for emp in targets:
        _upsert_user_perm(sec_user, emp)

Results

  • The secondary manager’s User Permission rows were created successfully.
  • But:
    • They often can’t see the subordinate’s HR docs in list or report views.
    • Opening a doc directly by URL sometimes throws a permission error.
  • Strangely, if I create the same User Permission manually in the UI, it usually works.

Request for Guidance

If anyone has:

  • A working solution for giving a secondary manager the same access as reports_to, or
  • Has tried a similar approach and found a fix for the inconsistencies,

Please guide me on:

  • Whether there’s any internal HR DocType code that specifically checks reports_to and bypasses general User Permissions.
  • Any extra steps needed so programmatically created User Permission rows are treated exactly like those created via the UI.
  • Best practice for integrating secondary_reports_to into existing permission flows.