Creating New Documents Based on the DocumentWrapper Class
So far, we've seen how to "wrap" an existing document inside a custom class, which is based on the DocumentWrapper subclass. But the document we wrap doesn't have to exist. We can use the DocumentWrapper classes to handle creation of new documents.
Imagine that, anywhere in your LotusScript, you could write this:
Set invoice = New Invoice(web.database.CreateDocument) invoice.Number = factory.GetNextInvoiceNumber() invoice.Value = 12345.98 invoice.Save()
Wouldn't that be cool!?
It's quite simple to do. To help take some of the pain out creating new documents of a certain type you can use the "New()" method of the derived class to check if it's a new document being created. If it is new then we can "initialise" it by adding some of the key fields and values. Such as the "Form" field.
So, for the Invoice class the New() method would look like this:
Class Invoice As DocumentWrapper Sub New(doc As NotesDocument), DocumentWrapper(doc) If doc.IsNewNote Then SetFieldValue "Form", "Invoice" AllowedEditors = web.user.Canonical AllowedReaders = Split("*/ACME;[Administrators]", ";") 'Any other fields this form *needs* to have!? End If End Sub Property Set Value As Variant SetFieldValue "Value", CCur(Value) End Property Property Set Number As String SetFieldValue "Number", Number End Property End Class
So far the derived classes I've shown only had "getter" properties, which returned field values. But the above two snippets of code demonstrate the user of "setter" properties also.
Notice the AllowedEditors and AllowedReaders properties. These are a new addition to the base DocumentWrapper class, which we can use to add Names-type fields. In turn they rely on a new AddNamesField method, like so:
Property Set AllowedEditors As Variant AddNamesField "DocAuthors", AllowedEditors, AUTHORS End Property Property Set AllowedReaders As Variant AddNamesField "DocReaders", AllowedReaders, READERS End Property
Sub AddNamesField(FieldName As String, UserNames As Variant, FieldType As Integer) Dim item As New NotesItem(document_, FieldName, UserNames, FieldType) End Sub
All document creation logic for each business class/model can now be encapsulated in one place. Then, no matter where in your code you find yourself creating new instances from, you don't need to worry about what the form name is or what fields are needed.
As I keep saying: the more I use and extend these three classes the more I wonder how I got by without them...
This is nice. Just be careful with GetNextInvoiceNumber() and replication. You may end up with numbers that are not unique.
Reply