LotusScript Agent to Modify All Documents Containing a Specific Field Value

Like simple actions, LotusScript can be used to manage and perform actions in a database. However, LotusScript offers greater flexibility to create more robust agents. This section illustrates how to modify all documents in a database where a field contains a specific value.

A.16.1

How It Works

For this example, LotusScript is used to iterate through database documents in a specific view and compare field values. When a match is found, the new value is assigned to the field, and the document is saved. The looping then continues with the next document in the list.

This example could be used to modify field values for select documents in a database view. Lets say, for example, that a team member has recently transferred to a new department. Using this sample, you could reassign all documents from one person to another person.

Implementation

To implement this solution, complete the following steps.

Step 1.

Select the Create > Design > Agent menu options to create the agent. When the properties dialog displays, give the agent a name such as Reassign Documents and set the Runtime Target to None (see Figure 16.2). After these values are set, close the properties dialog.

Figure 16.2. Setting the runtime property for an agent

Step 2.

Change the Language Selector from Simple action(s) to LotusScript.

Step 3.

Insert the following code in the Programmers pane. Be sure to replace VIEW with the name of the view to iterate through, FIELD with the field name to check, VALUE1 with the comparison string, and VALUE2 with the replacement string. If the searchandreplace objects are numbers, remember to remove the quotes to indicate that the value is a number (and not a string).

Sub Initialize Dim s As New NotesSession Dim db As NotesDatabase Dim doc As NotesDocument Dim view As NotesView Set db = s.CurrentDatabase Set view=db.GetView("VIEW") Set doc = view.GetFirstDocument While Not (doc Is Nothing) If doc.FIELD(0) = "VALUE1" Then doc.FIELD = "VALUE2" End If Call doc.Save(True, False) Set doc=view.GetNextDocument(doc) Wend Print "Complete" End Sub

Step 4.

Save and close the agent to complete the configuration. To run the agent, open the database in the Lotus Notes client and select Reassign Documents from the Actions menu.

Note

Performance for this agent will be affected by a variety of factorstotal documents in the database, total fields in the document, runtime location (server versus local), and so on. If performance becomes an issue, consider replacing If doc.FIELD(0) = "Value1" with the statement If doc.GetItemValue ("FIELD")(0) = "Value1". The GetItemValue method is more efficient and will improve overall agent performance for large databases.

Категории

© amp.flylib.com,