How to map employees to respective user ids from user list?

I have imported employees into HR module of ERPNext. Also, I have created Users under User List as System User, as I want to provide employee logins for employee self-service. Now how can I map these users to employees in bulk? Under employee overview we have user details section, it should reflect there.

Screenshot 2025-01-21 112012

Apologies for tagging @rmehta @michelle

This can also be imported using the Data Import tool.
Fill in the user_id column with the corresponding User’s Email ID
and Import it

You can use this script to map users with the respective employee.
Search System Console doctype, select type as Python, add this code in Console, and execute.

def map_users_to_employees_by_email():
    employees = frappe.get_all(
        "Employee",
        filters=[
            ["user_id", "is", "not set"],
            ["user_id", "=", ""]
        ],
        fields=["name", "user_id", "personal_email"]
    )
    if not employees:
        frappe.msgprint("No employees found to update.")
        return

    for emp in employees:
        try:
            personal_email = emp.get("personal_email")
            if personal_email:
                user_exists = frappe.db.exists("User", {"email": personal_email})
                if user_exists:
                    frappe.db.set_value("Employee", emp.name, "user_id", personal_email)
                  
        except Exception as e:
            frappe.log_error(f"Error processing employee {emp.name}: {str(e)}")

    frappe.db.commit()
    
    frappe.msgprint(f"Process completed")

map_users_to_employees_by_email()

@anilpoda

1 Like

Thanks, it worked. In place of personal_email I have used company_email as I created user list using company email ids.