• Purge Old Documents Agent

    By John Smart 1 decade ago

    I wrote an agent to purge old documents. Slightly cheesey since it uses the "Events\By Date" view. I should at least have added an alias to the view, but I wanted to change as few elements as possible to preserve my ability to upgrade to later releases.



    Scheduled to run on the first of the month. Uses a DbProfile profile document. If one doesn't exist, it creates one. I didn't bother making the functionality to actually edit the profile document yet.





    Sub Initialize

    Dim db As NotesDatabase<br/>
    Dim doc As NotesDocument ' from view<br/>
    Dim docNext As NotesDocument ' view.GetNextDocument(doc)<br/>
    Dim docProfile As NotesDocument ' db.GetProfileDocument(&quot;DbProfile&quot;)<br/>
    Dim dtPurge As Variant ' @Adjust(today, 0; -iPurgeMonths; 0; 0; 0; 0)<br/>
    Dim iPurgeMonths As Integer ' number of months to leave in db<br/>
    Dim iRemoved As Long ' number of documents removed by this agent<br/>
    Dim itemPurgeMonths As NotesItem ' the PurgeMonths field on docProfile<br/>
    Dim ndt As NotesDateTime ' today - iPurgeMonths<br/>
    Dim sngKey As Single ' YYYY.MM number correlating to dtPurge, used to match against similar values within view<br/>
    Dim view As NotesView ' Events\By Date view<br/>
    Dim session As New NotesSession<br/>
    <br/>
    On Error Goto ErrorSub<br/>
    <br/>
    Set db = session.CurrentDatabase<br/>
    Set docProfile = db.GetProfileDocument(&quot;DbProfile&quot;)<br/>
    Set itemPurgeMonths = docProfile.GetFirstItem(&quot;PurgeMonths&quot;)<br/>
    If itemPurgeMonths Is Nothing Then<br/>
        iPurgeMonths = 3<br/>
        With docProfile<br/>
            .PurgeMonths = 3<br/>
            .Form = &quot;DbProfile&quot;<br/>
            .Save True, True<br/>
        End With<br/>
    Else<br/>
        iPurgeMonths = itemPurgeMonths.Values(0)<br/>
    End If<br/>
    <br/>
    Set ndt = New NotesDateTime(&quot;Today&quot;)<br/>
    Call ndt.AdjustMonth(0 - iPurgeMonths)<br/>
    <br/>
    dtPurge = ndt.LocalTime<br/>
    sngKey = Year(dtPurge) + (Month(dtPurge) - iPurgeMonths) * 0.01<br/>
    Set view = db.GetView(&quot;Events\by Date&quot;)<br/>
    Set doc = view.GetFirstDocument<br/>
    Do Until doc Is Nothing<br/>
        If doc.ColumnValues(0) &gt; sngKey Then Exit Do<br/>
        Set docNext = view.GetNextDocument(doc)<br/>
        Call doc.Remove(False)<br/>
    Loop<br/>
    Set ndt = New NotesDateTime(&quot;Today&quot;)<br/>
    Call ndt.AdjustMonth(0 - iPurgeMonths)<br/>
    <br/>
    dtPurge = ndt.LocalTime<br/>
    sngKey = Year(dtPurge) + (Month(dtPurge) - iPurgeMonths) * 0.01<br/>
    Set view = db.GetView(&quot;Events\by Date&quot;)<br/>
    Set doc = view.GetFirstDocument<br/>
    Do Until doc Is Nothing<br/>
        If doc.ColumnValues(0) &gt; sngKey Then Exit Do<br/>
        Set docNext = view.GetNextDocument(doc)<br/>
        Call doc.Remove(False)<br/>
        iRemoved = iRemoved + 1<br/>
        Set doc = docNext<br/>
    Loop<br/>
    

    ExitSub:

    If iRemoved Then Call LogEvent(&quot;Removed &quot; &amp; iRemoved &amp; &quot; old documents.&quot;, SEVERITY_LOW, Nothing)<br/>
    Exit Sub<br/>
    

    ErrorSub:

    Call LogError()<br/>
    On Error Resume Next<br/>
    Resume ExitSub<br/>
    

    End Sub

    • Purge Old Documents Agent

      By Thomas H Povlsen 1 decade ago
    • Added in Release 1.0

      By Julian Robichaux 1 decade ago

      I added this agent to release 1.0 as an example. Thanks John!