How to create user when click on create user button on employee same do in custom doctype


in the employee doctype if i click on create user button automatically user created and permission automatically apply to that user like wise i want to do this same in to one custom doctype i have create same button and create one field link to user but when i click on create user button not working

@Rahul7218 Please write this code in your custom doctype

write frappe.call in your custom doctype button and call this py file code

def create_cu_user(email,customer):
      cu = frappe.get_doc("Customer", customer)
      customer_name = cu.customer_name.split(" ")
      middle_name = last_name = ""
      if len(customer_name) >= 3:
            last_name = " ".join(customer_name[2:])   
            middle_name = customer_name[1]
      elif len(customer_name) == 2:
            last_name = customer_name[1]

      first_name = customer_name[0]
      user = frappe.new_doc("User")
      user.update(
            {
                  "name": cu.customer_name,
                  "email": email,
                  "enabled": 1,
                  "first_name": first_name,
                  "middle_name": middle_name,
                  "last_name": last_name,
                  "module_profile": "Unity Customer",
                  "new_password": "Unity@2024",
            }
      )
      user.insert()
      user.add_roles("Customer User")
      cu.custom_email_id = user.name
      cu.custom_user_id = user.name
      cu.save()
1 Like