Today I had the need to process documents in a certain view in a random order.
I have read previously about different algorithms that would iterate a collection (or array) randomly. Luckily, in Notes this can be achieved very easily using a View.
- Create a new View. Let’s name it “RandomDocs”.
- Set the selection formula to contain your documents of interest.
- Set the index to get built manually and expire immediately. This will force Notes/Domino to rebuild the View’s index every time it is accessed by your script, but not in the nightly index update.
- Add a column, sorted ascendingly or descendingly, and use the following formula:
@Random
- Save and close the View.
- Use a script like this to iterate over the documents randomly:
Sub Initialize Dim s As New NotesSession Dim db As NotesDatabase Dim v As NotesView Dim doc As NotesDocument Set db = s.CurrentDatabase Set v = db.GetView("RandomDocs") v.AutoUpdate = False Call v.Refresh() Set doc = v.GetFirstDocument Do Until doc Is Nothing 'We've got a random document in hand. Print doc.GetItemValue("Subject")(0) Set doc = v.GetNextDocument(doc) Loop End Sub
That’s it!
Remember that every time the script runs the View’s index is getting rebuilt. This is not a very friendly operation for Domino/Notes, especially when the database contains thousands of documents, even if only a few are being filtered into the view, so you might want to wisely consider when and how often this code runs.