Try ERPNext Buy Support Partners Foundation

Autoincrement field

Hello I want to autoincrement one field for employee number like EMP-NNN(EMP-001,EMP-002…) SO on …I know doc events but not getting idea on how to achieve is as autoincrement is supported in MySQL but how to achieve in doctype
Any help will be more helpful

maybe you can do something like

frappe.db.sql(""“SELECT Count(*) FROM tabEmployee“””)

@johnskywalker what it will return means number of rows in db means an integer number or any other?

Count(*) will return number of rows

@johnskywalker can i fire this query in javascript ? i dont know about python or controller structure of frappe and how it will work.

yes you can but you will need this

	return  frappe.call({
		method: "path.to.your.python_script",
		args: {
				"some_var": some_field -> is fieldname in current Window
		},		
           callback: function(r, rt) {
			if(r.message) {
                    }}
       })

@johnskywalker thank you so much. i will try and let you know .

1 Like

@Hardik_Mehta, there is one issue with this approach. There’s no way that you can execute SQL only using JavaScript, so, you’ll have to implement a Python Script on the server side in order for you to be able to call that script from JavaScript. I guess you already figured that out. But problem comes when you set a value with that aproach, let’s say 45 just for saying a number. In that moment it will mean that you have 45 employees or records in the selected table. So, if you delete an employee from the table then you will have numbers repeated. A better aproach will be using naming_series and the frappe method autoname.

@yefritavarez ohh i have not think about delete scenrio …thank you so much for reminding
second thing i want to increment field like it is read only field in doctype every time when new employee get crated it will be incremented by one.it is prefilled when doctype is loaded. and frappe provide naming series for doctype name only not for field am i right?

Thanks,
Hardik

that’s why I provided this

return  frappe.call({
	method: "path.to.your.python_script",
	args: {
			"some_var": some_field -> is fieldname in current Window
	},		
       callback: function(r, rt) {
		if(r.message) {
                }}
   })

@johnskywalker @yefritavarez i am just begginer in frappe and i dont know how to interact with database from frappe controller using python.

First you need to import the function:

  1. from frappe.model.naming import make_autoname
  2. self.yourcustomfield = make_autoname(‘ABC-.####’)

This is Python code. You can place the second line in the before_insert method.

With this approach you wont have to worry about database queries.

Your file will look something like this:
from frappe.model.naming import make_autoname

class Employee(Document):
def before_insert(self):
self.yourcustomfield = make_autoname(“ABC-####”)

You’ll need to create the naming serie first from the UI. Just type Naming series in the search bar and then add a new one.

If you don’t want your field to have letters and be just numbers you can use a naming serie without letters, but I guess the ERPNext Application is already using it. So, my recomendation will be to use a Regular Expression to extract the numbers only. So, your code will look something like this:

from frappe.model.naming import make_autoname
import re

class Employee(Document):
def before_insert(self):

match = re.search(’[0-9]+’,make_autoname(‘ABC-####’))
self.yourcustomfield = match.group(0)

@yefritavarez thank you so much for you quick response. can u plz provide some tutorial link on how this python interaction with frappe happens.bcz this script satifies one purpose but for randome scenrios throughout understand requires,that’s why i ask.

Yes, of course. Here you have the link for the manual: http://frappe.github.io/erpnext/user/manual/en/customize-erpnext/

And also, for any reference on how any functionality is done in the framework you can always take a look the source code of the app or even to the framework itself.

@yefritavarez @johnskywalker thank you so much for your help. i will implement it and let you know.

@yefritavarez can we set up this naming series for our app? also i want such field like any other field like employee name, and employee no.

hello @johnskywalker i tried below code

frappe.ui.form.on(“Employee”, {
onload: function(frm) {
frappe.msgprint(“script called”);
frappe.call({
“method”: “employee.employee.generate_code”,
callback:function®{
frappe.msgprint(“method Called”);
console.log(“value is”+j);
cur_frm.set_value(“emp_no”,j);}
});

}
});

and below is employee.py

from future import unicode_literals
import frappe
from frappe.model.document import Document

class Employee(Document):
pass

@frappe.whitelist
def generate_document():
j=frappe.db.sql(""“SELECT Count(*) FROM tabEmployee”"")
frappe.msgprint(“Number of employees are “+j+” in db”)
return j;

but the method not get called.can u please help.

It is completely possible. Rembember that as soon as you create field on a Doctype, the framework automatically creates a column in the database and an attribute in the doctype class with the same name. So, let’s supose that you name your field employee_number, then you’ll have a column in the database table with the same name and in order for you to access or set that value from a Python Script within the class you can just use type: self.employee_number = 1234 and then just save the document.

The only thing that you should be aware of is the value will only be visible or set when you save or submit the document, depending on your implementation.

Hi does it have traceback errors?