It is not uncommon over time for an application to accumulate a lot of fields in documents that are no longer being used. This will usually occur as a a result of fields being removed from a form. In the early stages of development this is especaially common. The task of cleaning up the database so these fields no longer exist on documents is often a task that is either ignored or considered to be too much of an effort/pain to perform.
The latest release of the .Domino Framework provides a RemoveNonFormFields method for the DominoDocumentCollection class. Running against selecetd documents it build a list of the fields used in the default form assigned to each document and removes document fields if they are not found on the form. Field names starting with "$" are retained.. The All Document view provides an action button to invoke this method as one of the list of operations that can be perfromed against selecetd documents.
Note: This presently only work in situations in which subforms are NOT used and additional fields are not created via events, agents, actions etc.
The code is as follows:-
Sub RemoveNonFormFields
Dim Form As NotesForm ' Default form used to display document
Dim FormName As String ' Name of form
Dim FormFields As Variant ' List fo fields on form
Dim Doc As NotesDocument ' Document in collection being processed
Dim HasChanged As Boolean ' One or more fields have been removed => document must be saved
Try:
On Error Goto Catch
Set Doc = iDocumentCollection.GetFirstDocument
While Not Doc Is Nothing
' Identify form used for document
If Doc.HasItem("Form") Then FormName$ = Doc.GetItemValue("Form")(0) Else FormName$ = ""
If FormName$ <> "" Then
If Form Is Nothing Then
Set Form = DB.GetForm(FormName$)
Elseif FormName$ <> Form.Aliases(0) Then
Set Form = DB.GetForm(FormName$)
End If
If Not Form Is Nothing Then FormFields = Form.Fields
' Remove all fields not found on form
If Not Form Is Nothing Then
HasChanged = False
Forall DocField In Doc.Items
If Left(DocField.Name$,1) <> "$" Then
If Isnull(Arraygetindex(FormFields,DocField.Name$,5)) Then
Call Doc.RemoveItem(D