I am creating a part number generator that looks at a column for module code, then a column for type, then a it grabs the last two digits of the year created finally 4 digits that increment.
I used the AI formula generator and I got mostly there.
The issue I have and maybe I need additional columns, is I do not want duplicates.
For example I have a Module code of BA a type code pf P and a date of 2023
A second one a Module code of BC a type code pf A and a date of 2024
the first part is easy and comes out as BAP23
the second one BCA24
What I am struggling with is when I add the next 4 digits and I want them to increment, I need them to check the codes and increment from there.
So if I have BAP23005 the next time BAP23 comes up it makes the next one BAP23006 when the year changes to say 2024 I would like it to start back at 0000 so I would get BAP240000
I think I am creating a lookup loop but not sure how to fix it.
Thanks for any help.
Here is my AI chat and the code:
I need the first two letters from the module code the first letter from the type the last two digits of the year from date created and then four digits after that the formula should check the previous rows and if it finds duplicate number increment the last four digits by 1 so there are no duplicates
def A(rec, table):
module_code = rec.MODULE_CODE[:2]
type_letter = rec.TYPE[0]
year = rec.CREATED_DATE[-2:]
digits = int(rec.CREATED_DATE[-4:-2])
incremented_digits = str(digits).zfill(4)
# Check for duplicate numbers in all previous rows
all_rows = Sheet1.lookupRecords()
for row in all_rows:
if row != rec and row.MODULE_CODE[:2] == module_code and row.TYPE[0] == type_letter and row.CREATED_DATE[-2:] == year and row.CREATED_DATE[-4:-2] == incremented_digits:
digits += 1
incremented_digits = str(digits).zfill(4)
result = module_code + type_letter + year + incremented_digits
return result