Autofilled value for Custom field in User (DocType)

Hi , I have added a custom field _id for User (DocType). But I want to fill that with a random value something like uuid.uuid4() rather than asking for a user input.

Goal : Is to add a random but unique id for each user (System User) that is created

Please point me out where should I refer for this. Since I am unable to locate where actually user registration process takes place inside frappe and there is very little documentation on this subject.


@srajelli have look at:

Thanks for taking interest , But I am actually looking to insert a random value instead a series.

@srajelli …i see …i think u can use hash …as name field …have a look at docs

@JoEz can you please post the link . I can’t find any docs on hash

What’s the purpose for this? For User, the name attribute is the primarykey and is also unique (ends up the same as email). If you want to get only users that are System Users, you can filter by user_type (either System User or Website User).

If you want to go ahead with that custom field, be sure to tick “Unique” in the field information. You can hook into the “on_insert” method of User, get a list of the current _id values, and make a new value not in that list. If you make them sequential rather than random, you can get the highest value and just increment by one, which will be faster as the amount of records grows.

Good luck!

@alec_ruizramon1 I am integrating erpnext with an external application which tracks user behaviours , In case I just provide the name attribute and in future the user decides to change his/her information , There will be no way to recognise this change on the external application.

And also the external application is integrated with multiple erpnext instances so using a series will not be possible.


However, regardless of using a series or a random number you will have to check not only if they are unique within their own instance, but also unique against all of the other instances you are using.

How many instances are there? You could have the field use a naming series that is random/related to the company and then it becomes easier!

@alec_ruizramon1 Right now there are 17 instances , each with around 5 users , We are looking for fire and forget integration , manually assigning series would cost time. We are however storing the all the generated random id’s to cross check before issuing new one. There is only one catch how to automatically fill the custom field

Try this, and if it doesn’t work, change “on_insert” to “after_insert”:

In your custom app’s hooks,

   doc_events: {
        "User": {
            "on_insert": ""

In that method:

method_name(doc, method):
    #logic to check _id field values
    #logic to generate _id value that isn't in that list
    doc._id = your_value

@alec_ruizramon1 Thanks a ton , It worked ! :grin:

1 Like