Agent to change fields

Hi,

I have a rather large MS Office Library database, which have been created with a readers field. Now I have to update all the documents so they also have a MainReders field. I have tried to make an agent with the following code:


Sub Initialize

Dim Item As NotesItem

Set Item=Source.Document.ReplaceItemValue"MainReaders", “”)

If Source.Document.Readers(0)<>“” ThenForall values In Source.Document.Readers

Call Item.AppendToTextList(Values)

End Forall

Call Item.AppendToTextList(“[Administrator]”)

Else

Call Item.AppendToTextList(“*”)

End If

End Sub


But it dosen’t work - what am I doing wrong ?

Thanks in advance.

Rene

Subject: Agent to change fields

Rene,

You need to get the correct handle on each document (look in the Notes Help for NotesDocumentCollection)

Also, once you have amended the value, you will need to save the document

Your code would work from a document event where source is already defined as the UIDocument

HTH

Mike

Subject: RE: Agent to change fields

Hi Mike,

Thanks a lot for your respond. I sorry to ask again, but I’m very very green in LS. (First time created anything). I’ve changed the agent to:


Sub Initialize

Dim db As NotesDatabase

Dim collection As NotesDocumentCollection

Dim doc As NotesDocument

Set collection = db.AllDocuments

Set db = session.CurrentDatabase

Dim Item As NotesItem

Set Item=Source.Document.ReplaceItemValue(“MainReaders”, “”)

If Source.Document.Readers(0)<>“” Then Forall values In Source.Document.Readers

Call Item.AppendToTextList(Values)

End Forall

Call Item.AppendToTextList(“[Administrator]”) Else

Call Item.AppendToTextList(“*”)

End If

Call doc.Save( False, False )

End Sub


And now this failure appears:

‘Object variable not set’

How does this agent have to look to work ?

Thanks in advance.

Rene

Subject: RE: Agent to change fields

Rene,

Once you have your collection, you need to navigate through each document in the collection

To do this, after you have your collection set…

Set doc=collection.getfirstdocument

Do until doc is nothing

…code to change field value AND save document

Set doc=collection.getnextdocument(doc)

Loop

And don;t forget to get rid of all references to “source”. It isn’t set. It is useless in this scenario.

HTH

Mike

Subject: RE: Agent to change fields

Sub InitializeDIM SESSION AS NEW NOTESSESSION ’ You need to set the Session Object

Dim db As NotesDatabase

Dim collection As NotesDocumentCollection

Dim doc As NotesDocument

Set db = session.CurrentDatabase '//FIRST SET THE DB OBJECT B4 GETTING COLLECTION

Set collection = db.AllDocuments

Dim Item As NotesItem, NewReaders as Notesitem

set doc = collection.getfirstdocument

do until doc is nothing ’ START the LOOP

If Doc.Hasitem(“Readers”<>“” Then 'DOES THE READERS FIELD EXIST

Set item = doc.getfirstitem(“Readers”)

Set NewReaders = Item.CopyItemToDocument( doct, “MainReaders”) ’ Create a copy of the old Readers Field and rename to new name

item.Remove ’ Remove the old item not required anymore

else

'Readers field does not exist create a new ONE

Set NewReaders = New NotesItem( Doc, “MainReaders”, “”, READERS ) ’ This creates a new field with readers

end if

Call NewReaders.AppendToTextList(“[Administrator]”) Else

Call NewReaders.AppendToTextList(“*”) ’ NOW, if you are going to put * which means ALL users then the recommendation is to remove the Readers field from the document.

Call doc.Save( False, False )

loop

End Sub

Check the code and hope that helps