Create new Student and set default password via server script

Hello, I’m trying to set a default password for a Student account when it’s created.

Because for now, I’ve noticed that every new Student I create, I gotta go manually in Users and set a new password for the student.

Is it possible to make a server script that sets a default password for every Student created, so He/She can make the first login and change password?

Thanks in advance

Hii @mtssantos18

Please refer this:

Hello Manav, I have seen this post, but that didn’t help me.

I was looking for something that happens automatically, with no need of me going through the Database, or doing it manually.

Like a server script, where it sets a password after insert, something like this. Would you know if it is possible?

Hi @mtssantos18

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."),
        )
3 Likes

Oh great, thanks for your help @madhusona,

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.

1 Like

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:

apps/XYZ/XYZ/XYZ/doctype/job/job.py

To control the number of users, we can use Setting Limits for your Site

For Better understanding refer the directory structure of apps.

1 Like

Perfect guys, thank you so much.

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?

Can i do the same with Student doctype like when a student applicant is created?

Hi, can you help me with it please?

Yes.

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.