In on_submit python controller, I initiated the user creation process. Based on your need change the controller. You can also set user permissions if you want. Here i generated a random password for everyone.
import frappe
from frappe import _
from frappe.utils import random_string, validate_email_address
from frappe.model.document import Document
def on_submit(self):
# use any logic to get user_email and full_name
user_email = #######
full_name = #######
user_id, password = self.create_user_account(user_email, full_name) # Adjusted call
frappe.publish_progress(50, description="User account created")
# Proceed with setting permissions and sending the welcome email
#self.set_user_permissions(#####name#####, user_id)
frappe.publish_progress(75, description="User permissions set")
self.send_welcome_email_custom(user_email, password)
frappe.publish_progress(100, description="Application processed successfully")
@frappe.whitelist()
def create_user_account(self,email, full_name, role='ROLE'):
"""Create a new user account."""
validate_email_address(email) # Assuming you have a validation function
password = frappe.generate_hash(length=8) # Securely generate a random password
user_doc = frappe.get_doc({
"doctype": "User",
"email": email,
"send_welcome_email": 0,
"first_name": full_name,
"new_password": password,
"roles": [{"role": role}]
})
user_doc.insert(ignore_permissions=True)
print("user created")
return user_doc.name, password
def send_welcome_email_custom(self, user_email, password):
print("Send an email to the user with the generated password");
frappe.sendmail(
recipients=user_email,
subject=_("Welcome to ABCD Organization"),
message=_("Your account has been created!\n\n"
f"Username: {user_email}\n"
f"Password: {password}\n\n"
"Please login to your account."),
)
Yesterday I did something like this for the Guardians account, so it generates a standard password, but I’ve noticed you did it as a python file, instead I did it through Server Script.
Just to confirm, you think it’s best to do it via code in python file, instead of Server Script? if so, where Should I code it exactly?
Inside Users doctype? sorry for my question, It’s just cause I’m learning to understand ERPNext, and I always struggle with it haha
@mtssantos18 it’s in the backend files. If you go to frappe bench directory and apps directory, you will find erpnext. And other apps installed. In each there are doctype directory. In each doctype you will find, js,py files. There you can find which file you need to add the code.
This is also a server script. In my case, I have developed a new app called XYZ, which is a job portal. The DocType for job applications is named “Job” Whenever a job application is created, a user account is automatically generated in the background. To implement this functionality, the code is written in the following file:
and just to make sure, about the function def create_user_account(), Don’t I need to call the function, the Doctype will already know the function? and about the parameters, is it given by the field values in my form or what?
Note: The account creation process should be manual; otherwise, bulk importing accounts using Data Import. If bots submit applications, accounts will automatically be created. To prevent this, you need to implement other mechanisms to detect and stop bots.