Skip to content

Instantly share code, notes, and snippets.

@nabinhait
Last active February 18, 2020 13:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nabinhait/5294657 to your computer and use it in GitHub Desktop.
Save nabinhait/5294657 to your computer and use it in GitHub Desktop.
set item code based on incremental alphanumeric number
def custom_validate(self):
# serial_no is a custom field in item group which can be any alphanumeric number
serial_no = webnotes.conn.get_value("Item Group", self.doc.item_group, "serial_no")
#increment serial_no value and assign to item_code
next_serial_no = self.next_string(serial_no)
self.doc.item_code = next_serial_no
# update incremented serial_no in item group
webnotes.conn.set_value("Item Group", self.doc.item_group, "serial_no", next_serial_no)
def next_string(self, s):
strip_zs = s.rstrip('z')
if strip_zs:
return strip_zs[:-1] + chr(ord(strip_zs[-1]) + 1) + 'a' * (len(s) - len(strip_zs))
else:
return 'a' * (len(s) + 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment