I know there are already a lot of posts on this topic but I can’t find a solution for my problem in it.I have an agent in LS behind a button (“approve”)
.
.
.
.
.
AllowedEditors = uidoc.FieldGetText(“AllowedEditors”)
Call uidoc.FieldSetText("Status","4. Routing complete & Awaiting delivery")
Set agent=db.GetAgent ("AddPOnbr2")
agent.Run
Call uidoc.Save
… etc
which at the end invokes an other agent “AddPOnbr2” containing this script:
Sub Initialize
Dim session As New Notessession
Dim database As Notesdatabase
Dim Document As Notesdocument
Dim Last_Document As Notesdocument
Dim ID_View As Notesview
Dim getref As Variant
Dim newref As Integer
Set Document = session.DocumentContext
'If Document.IsNewNote Then
Set database = session.Currentdatabase
Set ID_View = database.GetView("(PONumber)")
Set Last_Document = ID_View.GetLastdocument
getref = Last_Document.GetItemValue("PONumber")
newref=Cint(getref(0)) + 1
Document.POnumber=Cvar(newref)
Call Document.Save(True, True)
'End If
End Sub
However the field POnumber remains on 0 in my document. (the most current POnumber in view (PONumber) is manually set to 18).
Anyone got a clue?
Subject: Sequential numbering in existing agent
Your basic approach is right but after the agent runs and updates this document, the control returns to the QS event and the UIDOC instance then OVERWRITES all the values set by the agent as it is NOT AWARE of any updates done by an external code.
An alternative way would be too put the agent code in a function that returns the next number in Sequence and that value is use to update the PONumber
Some thing like this below
function getNextPONumber (db as Notesdatabase)
dim id_view …
dim Las_Document …
dim getref as variant
Set ID_View = db.GetView(“(PONumber)”)
Set Last_Document = ID_View.GetLastdocument
if Last_Document is Nothing then
getNextPONumber = 1
else
getref = Last_Document.GetItemValue(“PONumber”)(0)
getNextPONumber=Cint(getref(0)) + 1
end if
End function
in the QS event call the function
uidoc.fieldsettext “PONumber”, getNextPONumber([DB Object])
Hope that helps
Subject: RE: Sequential numbering in existing agent
You’re mixing back end and front end code. Your agent updates the document on disk, but you have a different copy of the document in memory for editing. You have to assign the number in the UI doc or in a NotesDocument that you got from the uidoc, so that the Notes client knows that they are linked.
I also note that if two users save documents at the same time, they can easily get duplicate IDs.
See blog entry Sequential numbering by outside database for another way to do this.
I suggest you push back on the requirement for sequential numbering. You are locking yourself into a design that can never be expanded to multiple servers or local replicas. There are ways to generate identifiers which are guaranteed to produce unique values, but which are not a sequential number. The sequential number is a burden which will block you from realizing the advantages of the Notes architecture.
Subject: Sequential numbering in existing agent
Have you inserted the 2nd agent into the first to see if it works that way?
Subject: RE: Sequential numbering in existing agent
the second agent is in the first one. But also when I only execute the 2nd one, I get no result in the document
Subject: RE: Sequential numbering in existing agent
I meant actually inserting the lines into the 1st one, but if the 2nd one isn’t working on its own, then there is a problem.
Have you stepped through with debugger to see if last_document, getref & newref are actually getting set to the values you expect?
Subject: RE: Sequential numbering in existing agent
In spite of the value of the most recent field PONumber in the view “(PONumber)” is 18, the value of getref is 0, when debugging.
Subject: RE: Sequential numbering in existing agent
what is the value of last_document? Is it getting a value? Can you expand items, and scroll to PONumber, and see its value? If the doc is not right, the field is not right.
Subject: Sequential numbering in existing agent
Here are a few thoughts?
First how have you set up the field POnumber of the form?
You could have code in that field that is overriding what the following code does:
How is the view (PONumber) sorted? Are there other type of documents in this view?
Is there possibly any code in your QuerySave event that could be setting POnumber?
I would suggest you step through with your debugger as it will tell you if ID_View is set, getref is set.
Also check the value of POnumber after each save - this will tell you if there is code elsewhere changing the value.
To debug things you might want to replace the two lines in red with you code in the agent… Just to see. (Notice how you’re saving things twice)
AllowedEditors = uidoc.FieldGetText(“AllowedEditors”)
Call uidoc.FieldSetText("Status","4. Routing complete & Awaiting delivery")
Set agent=db.GetAgent ("AddPOnbr2")
agent.Run
Call uidoc.Save ' Front end save could overwrite backend changes
… etc
which at the end invokes an other agent “AddPOnbr2” containing this script:
Sub Initialize
Dim session As New Notessession
Dim database As Notesdatabase
Dim Document As Notesdocument
Dim Last_Document As Notesdocument
Dim ID_View As Notesview
Dim getref As Variant
Dim newref As Integer
Set Document = session.DocumentContext
'If Document.IsNewNote Then
Set database = session.Currentdatabase
Set ID_View = database.GetView("(PONumber)")
if Not ID_View is Nothing then
Set Last_Document = ID_View.GetLastdocument
if Not Last_Document Is Nothing then
if Last_Document.HasItem("PONumber") then
getref = Last_Document.GetItemValue("PONumber")
newref=Cint(getref(0)) + 1
Document.POnumber=Cvar(newref)
Call Document.Save(True, True) ' Back-End Save
Else
Print "Value PONumber not set on Last Document"
End if
Else
Print "Last document not found."
End if
Else
Print "ID_Vew Not Found"
End if
'End If
End Sub
Subject: Sequential numbering in existing agent
I solved this by creating a web service to get the next numberfrom a document
meaning everytime the web server is called it gets a number from a doc gives it to the application calling it then adds 1 to the number in the doc that stores the number
does that help