Sequenced Document Number

The user would like to have a ProjectNumber field that has 7 digits. The first 4 digits are the year/month from the Date_Entered field and the last 3 numbers are sequenced by year/month. For example, if today’s date and time is 01/15/2008 12:34:40 PM, the ProjectNumber would be 0801001 and it keeps adding until February. Then it goes back to 1 again (Ex: 0802001, 0802002, etc.)

I know that this topic is mentioned in this forum many times but I couldn’t find exactly what I’m looking for. Is there a way I could loop through the year and then month? If there is a document that explains how to do this, please let me know. Thanks for your help in advance!

Subject: RE: Sequenced Document Number

Why would you want to loop through the year and month? What are you doing at the time? Assigning the next number?

Just locate the latest project ID assigned, see whether the year and month are the same as today, and if so read the number off the end and add one.

Subject: RE: Sequenced Document Number

Thank you, Andre, for your assistance. The reason why I thought I would loop through the year and month was because if it’s the same year and different month, the number needs to start with 1 again. Then the numbers add by one until the next month. For example: 0712001, 0712002, 0712003, etc. and 0801001, 0801002, etc. I hope this makes sense.

Subject: RE: Sequenced Document Number

The way you’ve designed your ID, the most recently assigned number will also be the “last” one in alphabetical order. So what you need is a view that has the existing documents sorted by ID number – ascending or descending. Then the LotusScript code that assigns the number can use a NotesView object, GetFirstDocument method (or GetLastDocument depending whether the view is sorted ascending), read the key off of that, compare the first four digits with the first four digits we’re about to use, and if they’re the same, add one. Something like this:

Sub AssignNewNumber(docToAssign As notesdocument)

Static db As NotesDatabase

Static vu As NotesView ' in case we're creating multiple documents, keep the view handy to avoid overhead of looking for it each time.

Dim docLast As NotesDocument

Dim lastkey$, colkey, newkey$

If vu Is Nothing Then

	Set db = doc.ParentDatabase

	Set vu = db.GetView("NameOfViewSortedAscendingByKey")

Else

	vu.Refresh ' make sure we're up to date.

End If

Set docLast = vu.GetLastDocument()

If Not (docLast Is Nothing) Then

	colkey = docLast.ColumnValues(0)

	If Isarray(colkey) Then lastkey = colkey(0) Else lastkey = colkey

End If

newkey = Right$(Cstr(Year(Today)), 2) & Format$(Month(Today), "00")

If Left(lastkey, 4) = newkey Then

	' add one to the last number to get the new number

	newKey = newKey & Format(1+Cint(Mid$(lastkey, 5)), "000")

Else

	' starting a new month -- number 1

	newKey = newKey & "001"

End If

docToAssign.ReplaceItemValue "Key", newkey

End Sub