Number Documents Sequentially using an Agent

Author: Tripp W Black

Created: 02/03/2000 at 01:06 PM

 

Category:
Notes Developer Tips
Agents, Forms/Subforms, LotusScript

You're in luck - I administer the design of a database where everyone in the department assigns work items to themselves and other people. In the "Main Topic" document (highly modified), I added a field called 'number' which is an editable text field. Its default value is:

@If(number = ""; "TBA";number)

We all work on local replicas and when we replicate, our new documents, with 'TBA' in the 'number' field are sent to the server. When they get there, there is an agent that runs on them, which has the following code (good for up to 9,999 documents with this field in it) in the Initialize event:

Sub Initialize

Dim Session As New NotesSession
Dim db As NotesDatabase
Dim view As NotesView

Dim ProfileDoc As NotesDocument
Dim doc As NotesDocument
Dim item As NotesItem
Dim item2 As String
Dim Nitem As NotesItem
Dim Nitem2 As String
Dim num As Integer
Dim NumStr As String

Dim docnum As Integer
Dim TBAnum As Integer
Dim initialnum As Integer

Set db = Session.CurrentDatabase


Set view = db.GetView("(CR by Number)")


Set ProfileDoc = db.GetProfileDocument("ChangeProfile")
Dim maildoc As NotesDocument
Set maildoc = New NotesDocument(db)
Dim mailitem As NotesItem
'Use UpdateFTIndex to update the Full Text Index
Call db.UpdateFTIndex(True)

'Find count of CRs there are, and how many have TBA in number field
docnum = view.FTSearch("*",0)
TBAnum = view.FTSearch("TBA",0)

'Set nextid in Profile Doc for use in updating number field in CRs
NumStr = ProfileDoc.nextid(0)
If NumStr = "" Then
initialnum = docnum - TBAnum
num = initialnum
Else
num = Cint(NumStr)
End If

ProfileDoc.nextid = Cstr(num)
Call ProfileDoc.save(False,False)

'The view is sorted by "number" in descending order, so TBA fields come first.
'Go through the documents containing TBA, and change to next consecutive number
'appending leading 0s

If TBAnum > 0 Then
For i = 1 To TBAnum
Set doc = view.GetNthDocument(i)
Set item = doc.GetFirstItem("number")
item2 = item.text

If item2 = "TBA" Then
num = num + 1
If num < 10 Then
newnum = "0000" & Cstr(num)
Elseif num < 100 Then
newnum = "000" & Cstr(num)
Elseif num < 1000 Then
newnum = "00" & Cstr(num)
Elseif num < 10000 Then
newnum = "0" & Cstr(num)
Else
newnum = newnum
End If

doc.number = newnum
Call doc.save(False,False)

ProfileDoc.nextid =Cstr(num)
Call ProfileDoc.save(False,False)
End If

Next
End If
Call MailApprovers
End Sub

The agent is set to run "If documents have been created or modified" and "On newly modified documents".

That should do it - good luck!

previous page