Domino Code Fragment

Code Name*
Creating a Search Agent - Challenge*
Date*
04/28/2024
Source (or email address if you prefer)*
Rlatulippe@romac.com
IP address:.18.119.133.96
Description*
Creating a Search Agent - Challenge*
Type*
LotusScript
Categories*
(Misc)
Implementation:
Required Client:
Server:
Limitations:
Comments:
Files/Graphics attachments (if applicable): Code:
Exercise Title : Media Search ( Selected ) - Challenge

Where : Agent

Solution :

Sub Initialize
'-- For each Query document selected by the Agent :
'-- This script will perform searches of multiple Notes databases based on the criteria in the
'-- queryText field in the current document and return the results as a one newsletter response document
'-- to the original query document

'-- Get unprocessed document collection from Current Database
Dim Session As New NotesSession
Dim currentdb As NotesDatabase
Set currentdb = Session.CurrentDatabase

Dim unprocessed As NotesDocumentCollection
Set unprocessed = currentdb.unprocesseddocuments

'-- This array hold the names of the Notes DBs from which we search
Dim dbpath(2) As String
dbpath(0) = "PGN2\Newstand\news1.nsf"
dbpath(1) = "PGN2\Newstand\news2.nsf"
dbpath(2) = "PGN2\Newstand\news3.nsf"

Dim doc As NotesDocument

'-- Iterate through collection
Set doc = unprocessed.getfirstdocument

Do While Not (doc Is Nothing)

'-- Process Media Documents Only
If doc.form(0) = "Media Search" Then

'-- Get the search string from the document
Dim FTQuery As String
FTQuery = doc.QueryText(0)

'-- Create a new document to contain all newsletter document information from all searches
Dim ResultsDoc As NotesDocument
Set ResultsDoc = New Notesdocument(currentdb)

'-- Working Newsletter document reference for each database
Dim workdoc As NotesDocument

'-- Set Reset Total Matches
totalMatches = 0

'-- Search all databases (defined) by array
For i = 0 To 2

'-- Open the database and update the index
Dim db As New NotesDatabase("",dbpath(i))

Call db.UpdateFTIndex(True)

'-- Perform the Full Text Search, returning the results

Dim collection As NotesDocumentCollection
Set collection = db.FTSearch(FTQuery,0)

'-- If matches are found, create a newsletter object using the matching document collection.
If collection.count > 0 Then

'-- Keep count of total matches
totalMatches = totalMatches + collection.count

Set newsletter = New NotesNewsletter(collection)

'-- SubjectItemName is the field from the news db which provides the newsletter subject values
newsletter.SubjectItemName = "Head"
newsletter.DoScore = False
newsletter.DoSubject = True

'-- Create Newsletter document
Set workdoc = newsletter.FormatMsgWithDoclinks(currentdb)

'-- Copy the work document into the final results document (which will hold
'-- the information from all newsletters for each database searched).
Call workDoc.CopyAllItems(ResultsDoc)

End If

Next i

'-- Make the Newsletter document a response to the query document
Call ResultsDoc.MakeResponse(doc)

'-- Set Form name and Title
ResultsDoc.Form = "Media Search Results"
ResultsDoc.Title = "Total Search Results : " + Str$(totalMatches) + " matches found."

'-- Save the newsletter document
Call ResultsDoc.save(True,True)

End If

'-- Get next document
Set doc = unprocessed.getnextdocument(doc)

Loop

End Sub