Can anyone tell me why this code will not work on the Web?
It works in Notes client (with UI Clases). I changed the code to use DocumentContext.
The code assigns a number to a new document, if the category exists it assigns the next available number, if it is a new Category it assigns number 1. Uses a lookup to a Categorised view.
The same code reorders the number within a category, when document 2 is changed to document 1, it reoders the other documents so there is only on Number 1 doc.
The code is in an agent, on event, Agent List, target none.
Problem: When a doc is saved from the web the agent appears to run (agent log says “ran LotusScript code”) but document number is not set, it is saved with the default value 99.
I tried using an error handler, nothing reported.
I tried using remote debugger, changing to schedule, never, the document closes immediately without being saved so I can’t get the debugger to catch it.
DocOrder and OldOrder are hidden if new doc, tried unhiding, no difference.
Is there something else in the code that doesn’t work on the Web?
Many thanks in advance,
Nicola
******************************* CODE********************
’ PURPOSE: Validate document and update the docOrder of all peers if necessary
'Print "WebSave Agent is about to start running*************************************"
'Sleep(30)
On Error Goto errorHandler
Dim session As New NotesSession
Set db = session.CurrentDatabase
Dim doc As NotesDocument
Set doc = session.DocumentContext
'Goto SkipModifications ' this is just a jump around the code for debugging purposes
Dim CrLf As String
' required field helpers
Dim RequiredInput As String
Dim message As String
Dim answer As Integer
Dim verb As String
Dim iverb As Integer
Dim iLoop As Integer
Dim docPeer As NotesDocument
Dim success As Variant
Dim incrementer As Integer
CrLf = Chr$(13)+Chr$(10) ' carriage return and linefeed
'--------------------------------------------------------------------------------------
' (1) Validation
Dim itemType As Integer
’ this may not be needed because the FIELD validation should catch a blank or text value but …
Dim item As NotesItem
Set item = doc.GetFirstItem( "docOrder" )
itemType = item.Type
If Not itemType = 768 Then ' docOrder field does NOT have a numeric value in it
RequiredInput = RequiredInput + "Order not number" + CrLf
iVerb = iVerb + 1
End If
If doc.Categories(0) = "" Then
RequiredInput = RequiredInput + "Document Category" + CrLf
iVerb = iVerb + 1
End If
If doc.Subject(0) = "" Then
RequiredInput = RequiredInput + "Document Subject" + CrLf
iVerb = iVerb + 1
End If
'
If RequiredInput = "" Then ' Nothing was missing
' left here for messages if necessary
Else
If iVerb > 1 Then
verb = " fields need "
Else
verb = " field needs "
End If
message = "The following" + verb +"your attention: " + CrLf + CrLf + RequiredInput
answer = Messagebox(message, 16, "Ooops")
Continue = False
Goto SkipProcessing
End If
End If
' SECTION ONE - - - Reorder for the master list (one viewable in Lotus Notes)
' (1) Was the FIELD docOrder changed?
If Not doc.docOrder(0) = doc.oldOrder(0) Then ' order was changed OR it's a new document
' create a collection of documents in the same category (peers)
Dim view As NotesView
Dim vc As NotesViewEntryCollection
Dim entry As NotesViewEntry
Set view = db.GetView(“vwCategories”)
Set vc = view.GetAllEntriesByKey(doc.Categories(0))
’ (2) Are there any peer documents?
If vc.count = 0 Then ’ there are NO peers … this is the first document
doc.docOrder = 1
Else
' (3) Is it a NEW document?
If doc.IsNewDoc Then ' this is a new document
' (1) Check to see if the number is too high
If doc.docOrder(0) > vc.count Then ' put it at the bottom
doc.docOrder = vc.count + 1
Else
' (4) Lets reorder some document then
If doc.docOrder(0) = 0 Then doc.docOrder = 1
’ Did they make the order 0? That won’t work so lets make it 1
' Point at the document currently in the UI document's new place
Set entry = vc.GetNthEntry( doc.docOrder(0) )
iLoop = doc.docOrder(0)
’ Set the counter
Do While iLoop < vc.Count + 1
Set docPeer = entry.Document
docPeer.docOrder = iLoop + 1
docPeer.oldOrder = docPeer.docOrder(0)
’ match the oldDoc to it
Call docPeer.save(True, True)
iLoop = iLoop + 1
Loop
End If
' Is NOT NEW so change next documents -----------------------
' the document is IN the collection so don't touch it UNLESS you want a replication/save conflict
Else
If doc.docOrder(0) < doc.oldOrder(0) Then ' We're going UP
If doc.docOrder(0) < 1 Then ' picked too low a number so make it first
doc.docOrder = 1
End If
If doc.docOrder(0) > vc.count Then ' put it at the bottom
doc.docOrder = vc.count
Else
Set entry = vc.GetNthEntry(doc.docOrder(0))
’ point at the entry that corresponds to the place that the UI doc is going to go
iLoop = doc.docOrder(0) ' make that the loop/entry count
incrementer = 1 ' add 1 to the Order
Do While iLoop < vc.Count + 1 ' make sure you work all of the documents
Set docPeer = entry.Document ' get a handle to the document from the view entry
docPeer.docOrder = iLoop + incrementer ' stuff the loop value + 1 into the order field
docPeer.oldOrder = docPeer.docOrder(0) ' match the oldDoc to it
success = docPeer.ComputeWithForm( False, False )
Call docPeer.save(True, True) ' save it
iLoop = iLoop + 1 ' increment the loop counter
If Not iLoop = doc.oldOrder(0) Then ' make sure you aren't grabbing the current UI document
Set entry = vc.GetNextEntry(entry)
Else
’ you were about to grab the current document so change the incrementer & loop counter and move to that entry
'incrementer = 0 ’ don’t over increment the remaining document order
iLoop = iLoop + 1 ' increment the loop counter
Set entry = vc.GetNthEntry(iLoop)
’ get the document AFTER the current UI document
If entry Is Nothing Then Exit Do
End If
Loop
End If
Else ' We're going DOWN (or we've got a document being archived)
If doc.docOrder(0) > vc.count Then ' picked too high a number so make it last
End If
' See if there's anything after the document
Set entry = vc.GetNthEntry(doc.oldOrder(0)+1) ' point at the entry AFTER the UI doc
If entry Is Nothing Then Goto SkipModifications
Set docPeer = entry.Document ' get a handle to the document from the view entry
iLoop = doc.oldOrder(0) + 1 ' make that the loop/entry count
incrementer = -1 ' subtract 1 from the Order
Do While iLoop < vc.Count + 1
’ make sure you work all of the documents
’ Msgbox "About to change " + docPeer.subject(0) + " to order → " + Cstr(iLoop + incrementer)
docPeer.docOrder = iLoop + incrementer
’ stuff the loop value + (something -1 or 0) into the order field
docPeer.oldOrder = docPeer.docOrder(0) ' match the oldDoc to it
success = docPeer.ComputeWithForm( False, False )
Call docPeer.save(True, True) ' save it
iLoop = iLoop + 1 ' increment the loop counter
Set entry = vc.GetNextEntry(entry)
If entry Is Nothing Then ' you're at the bottom
Exit Do ' loop breaker
Else
Set docPeer = entry.Document ' get a handle to the document from the view entry
If docPeer.docOrder(0) > doc.docOrder(0) Then ' is this Order .GT. the UI document's Order?
incrementer = 0 ' don't over increment the remaining document order
End If
iLoop = iLoop + 1 ' increment the loop counter
End If
Loop
End If
End If
End If
End If
' (3) Save the document
SkipModifications:
doc.refresh
doc.dateModified = Now()
doc.oldOrder = doc.docOrder(0)
Call doc.Save(True,True)
'workspace.ViewRefresh
SkipProcessing:
Exit Sub
errorHandler:
Print "Error " & Cstr(Error) & " @ line " & Cstr(Erl) & " : " Err
Exit Sub
End Sub