I know I've seen similar questions raised before, but I didn't see an
answer that quite met my needs.
I've put together a database to track work orders, and I want to be
able to auto-generate a serial number. The numbering system we use
follows a YYYY-MM-XXX pattern, in which XXX is a three-digit serial
that starts over at the beginning of each month. For example, the third
work order received in the month of November would be 2004-11-003,
while the first order of December would be 2004-12-001.
I'm stumped as to how to script this. And, unfortunately, changing the
numbering system isn't an option. Any ideas?
Hi,
There may be other and better ways to do this, but my first idea would be to
set up a self-join relationship based on the left part (yyyy-mm) of the
serial_number, and then set new ones to max (selfjoin: number part of the
serial number) + 1
First, create two new calculted fields:
- Left_part: to store the left part of the serial number, something like
Left(serial_number,7). Text result.
- Right_part: to store the number part, something like
TextToNum(Right(serial_number,3). Number result.
To define the relationship, you will also need a global field (text) for the
left side and of course the Left_part calculated field is used on the right
side.
Finally the script would look something like thatL
- set field (the global field, year (status(current date)) & "-" & month
(status(current date)))
- set field (serial_number, global field & "-" & Right("00" &
Max(selfjoin::Right_part) + 1,3)
I didn't test this, so I'm not sure whether you need a case statement for
the first order of each month (when the relationship is empty).
Let me know if this is not working or not clear enough, that is if somebody
doesn't come up with a better way in the meantime...
Marc-André Paiement
OUCH! That looks painful. ;o)
If the database is a simple-use one, it's probably easier to simply
have three fields and a short opening script rather than muck about
with Relationship links. (Files that are hosted on FileMaker Server or
web-hosted would require something more complicated.)
The first field is a normal Number field (SerialNum) with an auto-enter
option of a serial number.
The second field is a Calculation with a Text result to give the
InvoiceNumber, which you've probably already got:
Invoice Number = Year(Status(CurrentDate))
& "-"
& Right("0" & Month(Status(CurrentDate)), 2)
& "-"
& Right("00" & SerialNumber, 3)
The last field is a Global Date field that stores the date of the last
time the serial number was reset (g_SerialDate).
Then the script only needs to be something like:
If [Month(g_SerialDate) = Month(Status(CurrentDate))]
Exit Script
Else
Set Field [g_SerialDate, Status(CurrentDate)]
Set Next Serial Value [SerialNum, 1]
End If
Using the Preferences: Document option you can have this script run
every time the database is opened. If "this month" is not the same as
the month of the last reset, then the serial number is set back to 1
... as long as you computer's date is working properly of course.
Making the script test just the month rather than the 1st of the month
means you don't have to worry about months where the 1st of a month
being a weekend or public holiday where the file isn't opened. The
serial number will simply reset the next day that the database is
opened instead.
Notes:
1. If you're using FileMaker 7 then you'll need to replace all the
"Status(CurrentDate)" functions with the new "Get(CurrentDate)"
function.
2. The two "Status(CurrentDate)" functions in the Invoice Number
field will need to be replaced with an "InvoiceDate" field if
you're manually entering invoice dates instead of using
"today's" date when the invoice record is created.
Helpful Harry
Hopefully helping harassed humans happily handle handiwork hardships ;o)
You're right Harry, this is mnch simpler. I must admit I'm still working
mostly with Filemaker 5 and Set Next Serail Value is not available.
I haven't got version 5 (I think I skipped over that one), so I'll
believe you on that. It is a function in FileMaker 5.5, so maybe it's
time for a half-step upgrade ... although FileMaker don't sell 5.5 any
more. :o\
Without that command you do have to resort to some gymnastics with
relationship links to reset a "serial" number ... either that or always
have a fresh clone file with serial number of "1" and script the import
process to transfer existing records.