Delete All Unused Fields From Db And Clear Up Design Field List
This is an agent that deletes all document fields which don't show up in any
forms or subforms. This eliminates the need to write an agent which lists every
single unwanted field, like
FIELD goAway := @DeleteField;.
If you compact the database after running this agent, the unwanted fields also
disappear from the design list in views etc.
Sub Initialize
Dim session As New NotesSession
Dim db As NotesDatabase
Dim collection As NotesDocumentCollection
Dim doc As NotesDocument
Dim usedFields() As String
Dim count As Integer
Dim index As Integer
Dim i As Integer
Dim found As Integer
Dim items As Variant
Set db = session.CurrentDatabase
'Get all the fields in the forms of this db
Redim usedFields(0)
usedFields(0) ="Form" 'don't delete the form field
Forall form In db.Forms
count = Ubound(form.Fields)
index = Ubound(usedFields)+1
Redim Preserve usedFields(index+count)
For i = 0 To count
usedFields(index+i) = form.Fields(i)
Next
End Forall
Set collection = db.AllDocuments
'Run thru all docs and delete unused Fields
For i = 1 To collection.Count
Set doc = collection.GetNthDocument( i )
items = doc.Items
Forall item In items
found = False
Forall field In usedFields
If item.name = field Then
found = True
Exit Forall
End If
End Forall
If Not(found) Then
Call item.Remove
End If
End Forall
Call doc.Save( False, True )
Next
End Sub