Thursday, October 18, 2007

A Useful Little Routine to Change Data in Fields

Level: Beginner Development

Today I thought I'd just share with you a little routine that I use when I want to make field level changes to a lot of documents without having to edit them. It's beginner stuff but I use it a lot.

What you need
A document, or group of documents with a field called DocNumber
A view where you can select those documents.
Notes Designer.

Creating the Agent
  1. Open Designer on the database (or template)
  2. Click on Shared Code then Agents
  3. Click on the button marked New Agent
  4. In the Properties box, give your agent a Name eg: Change Document Number
  5. It should be a Shared Agent
  6. In the Target Section, it should read All Selected Documents
  7. You don't need to change anything else in the properties box, so just close it.
  8. Now, in the intialize event of your Agent (just click on the word initialize and type in the box that appears).
  9. Type your code (or paste it from this page).
  10. When you've finished, Close the agent windows and choose YES to save your changes.

The code

Sub Initialize
   Dim NewSection As String
   Dim Counter As Long

   Dim ses As New notessession
   Dim doc As notesdocument
   Dim db As notesdatabase
   Dim col As notesdocumentcollection

   Counter = 0
   NewSection = Trim$(Inputbox$("What is the name of the New Category?"))

   If NewSection = "" Then
      Print "You did not specify a category to move documents to. No documents moved."
      Exit Sub
   End If

   Set db = ses.currentdatabase
   Set col = db.unprocesseddocuments

   Set doc=col.getfirstdocument

   While Not doc Is Nothing
      doc.DocNumber = NewSection
      Counter = Counter +1
      Call doc.Save(True, True)

      Set doc=col.getnextdocument(doc)
   Print Trim$(Str$(Counter)) + " document(s) moved to category: " + NewSection
End Sub

How to Use the Routine
Simply tag a whole heap of documents then click on Actions, then Change Document Number (from the Lotus Notes Menu). You'll be prompted for a new number and then it will change all the selected documents.

I use this routine a lot when I want to move documents from one section/category to another.

No comments: