Before creating code for looking up a document, you need a view that presents documents in a certain way.
It is up to you to determine how the view should present the documents.
You can then write the lookup code, depending on how the view was set up.
Here, we have main documents and response documents. The two are based on two different forms.
A response doc is defined by the fact that the record created always contains a field named $Response. This field contains the UniversalID of the main document that it responds to.
So what you can do is create a view listing all main and response docs, without respecting hierarchy (it is a property of the view). This detail is generally important for purposes of sorting.
The first column of this view should contain a formula like this :
Code:
@if(form=<main form name>;@text(@documentuniqueID);$Response)
In the next column, you want the field containing the remaining amount.
Both columns must be sorted. The first can be sorted either ascending or descending. The second column must be sorted descending.
Once you have this view working, searching for the last doc is a trivial exercise in LScript. The code to do it is as follows :
Code:
dim session as new notessession
dim uiws as new notesuiworkspace
dim uidoc as notesuidocument
dim db as notesdatabase
dim view as notesview
dim coll as notesdocumentcollection
dim doc as notesdocument
dim dockey as string
set uidoc = uiws.currentdocument
set db = session.currentdatabase
dockey = uidoc.fieldgettext("$Response")
if not (dockey = "") then
set view = db.getview(<view name>)
set coll = view.getalldocumentsbykey(dockey)
if coll.count > 0 then
set doc = coll.getlastdocument
call uidoc.fieldsettext("AmountBalance",doc.AmountBalance(0))
end if
end if
Now, a few remarks.
* First, this code supposes that all response docs are created from the main doc only. If that is not the case, you will have to amend the code a bit to take into account creating from the main doc and creating from a response doc.
* Second, it requires that the field $Response be created on the response form. Make it a hidden field, type text and editable. Put no formulas in the field definitions.
* Third, it supposes that the search code is put in the PostOpen event of the Response form. The reason for that is that the UIdoc is only available after the document has actually been opened. You could design something similar for the QueryOpen event, but you would have to keep the info stored somewhere until the PostOpen was available anyway.
There, that should be enough to get you started.
Good luck !