Auto Barcode Generation

I have modified item.py by adding following lines to validate_barcode function:

	if self.barcode == "auto":
			existing = frappe.db.sql("""select barcode from tabItem""", as_list=True)
			self.barcode = random.choice([item for item in range(1000,10000) if item not in existing]) 

However, barcode remains as ‘auto’ instead of getting updated. What am I doing wrong?

Here’s the modified file for reference: https://gist.github.com/anonymous/6e69ba4a1ab855ca6a57df73f639c659

Try using frappe.db.set_value() method.

Here’s the code that got it working:

import random
..
..
..
	def validate_barcode(self):
		if self.barcode:
			if self.barcode == "auto":
				existing = frappe.db.sql_list("select barcode from tabItem")
				self.barcode = random.choice([item for item in map(str, range(1000,10000)) if item not in existing])
			..
..
..

Thanks @KanchanChauhan

4 Likes