Gidday,
I need to set the value of a field within a document when I save a different document.
eg. I create a new doc using a form, I fill it in as normal. When I hit save I want the ‘parent’ document to have a date field set to Now. The ‘parent’ is not really a parent, it just shares a UniqID field. In formula I would do a @dblookup on a View and use UniqID to return the document.
In lotus script how do I locate this ‘parent’ document and set a fields value?
Dim doc As NotesDocument
Set doc = ??
Thanks
Brendon
Subject: db.getdocumentbyUNID(id)
Subject: Thanks here is the answer in full
That worked great thanks, here is my full code:
'Set value of LastClientNoteDate on ClientDetails form
If Source.IsNewDoc Then
Dim session As New NotesSession
Dim db As NotesDatabase
Dim view As NotesView
Dim doc As NotesDocument
Dim Key As String
Set db = session.CurrentDatabase
Set view = db.GetView (“AllClientDocuments”)
Key = source.FieldGetText(“ClientUniqID”)
Set doc = view.GetDocumentByKey(Key,True)
If Not (doc Is Nothing) Then
doc.LastClientNoteDate = Now()
Call doc.Save( True, True )
End If
End If
Subject: you can do that but…
It’s slower than the suggestion I gave you - getdocumentbyUNID is the fastest way to get a doc if you know the UNID, no need to build a view and use getdocumentbykey if the key is the document UNID
Subject: I think Brendon may be using a non-Notes ID…
-
If so there’s nothing else he can really do but instantiate a view and fetch from it.
-
If ClientUniqID is actually the Notes UniversalID then Dan is of course correct, and his suggestion would be the preferred route. It’s also less lines:
Dim ns as New NotesSession
Dim doc as NotesDocument
Set doc=ns.currentDatabase.getDocumentByUNID(Source.fieldGetText(“ClientUniqID”, true))
’ The rest
- Also, my experience is that setting a date field to “Now” doesn’t always yield a datetime data type on the back-end, and in particular Formula may or may not use that field value correctly. Or worse, it will work perfectly for a year or so then suddenly fail. You will look at that setting to Now and never think to change it. If you don’t use ClientLastNoteDate for any computation then it won’t matter. If you do I would suggest setting it thusly:
Dim dt as New NotesDatetime(“”)
dt.setNow()
call doc.replaceItemValue(“ClientLastNoteDate”, dt)
This will ensure the back-end always has an actual datetime data type, and any computations done with that data will always work as expected.
Hope this helps…
Subject: i have found a problem with this…
this all seemed to be working fine but I started to get complaints from some staff saying that all of a sudden they are getting save conflicts.
Effectively what I think is happening is Staff1 opens up ClientA main record and starts to edit it. Then Staff2 opens ClientA and creates a new Note document. Staff2 then saves the Note document which runs the little script which updates the ClientNotesLastDone field on ClientA’s main record. Staff1 who is still editing ClientA’s main record decided to save it and gets an error a long the lines of, this document has allready been saved, do you want to create a conflict or cancel… etc and no matter what they do they lose all their work and have to start again.
This really sux.
I’ll repeat my requirement again…
I have a view that displays all current Clients. On this view I want to display a traffic light icon that changes colour (changes icon) depending on the date that the client last had a client note created.
Green = Today
Orange = Last week
Red = Older than last week
How would you go about achieving this?
many thanks for your help, and I will look at optimising my code with the suggestions made.
Brendon
Subject: Are you using document locking?
- That should prevent the scenario you describe; while Staff1 has ClientA open, Staff2 will be able to read it all day long, but not update it. Staff2 must wait for Staff1 to save and exit. No system really effectively allows two users to edit the same data at the same time. That’s a serious can of worms that’s generally addressed by denying edit to all but one at a time … ie … document locking.
Hope this helps…
Subject: Way around the issue
I have had an idea that I will test out.
As you know I have a Client form and a Note form. When a new Note is created for a Client I want a field to update on the Client. This needs to be a real time update.
What if I save the required data to a new Global form instead of to the Client form when the Client form is allready in edit mode. Then when the Client form is saved or closed it can check the Global form for any changes.
If this is possible it will fix the issue of my scripts editing forms while users are editing them and creating conflicts and loss of data.
-oh and the original reason as to why I need this data on the client form… I need to display this info in a view of clients and I have been told that @dblookup and @dbcolumn does not work to populate columns in a view. if they did then I would not have this issue. and this is going to become a great issue as our database expands and clients need even more forms associated with them, we need snapshot views of clients and what dates or status their associated forms are at.
Subject: This will allow two people to edit…
- But if a third tries, then the issue will simply be moved to the Global Form instead of the Client Form. Unless you can guarantee that nor more than two will ever edit, you haven’t gotten far, it seems…
Subject: GetDocumentByKey
NotesView.GetDocumentByKey is similar to @DbLookup