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

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

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...


  1. This is nice. Just be careful with GetNextInvoiceNumber() and replication. You may end up with numbers that are not unique.

Your Comments


About This Page

Written by Jake Howlett on Mon 11 Mar 2013

Share This Page

# ( ) '


The most recent comments added:

Skip to the comments or add your own.

You can subscribe to an individual RSS feed of comments on this entry.

Let's Get Social

About This Website

CodeStore is all about web development. Concentrating on Lotus Domino, ASP.NET, Flex, SharePoint and all things internet.

Your host is Jake Howlett who runs his own web development company called Rockall Design and is always on the lookout for new and interesting work to do.

You can find me on Twitter and on Linked In.

Read more about this site »

More Content