oosterbaan
asked on
Copying documents using LotusScript agent
Hi experts,
How can I copy documents using a LotusScript agent ?
The agent should work as follows:
- Prompts the user for a selectionbox with 4 values in it
- Looks for the chosen value in a field on the documents which are present in a view **
- copy's the documents that matches the criteria to the same database and put on those new documents a new field with a value (doesn't matter what the value is)
** The source documents have a multivalue field. The value that the user entered (using the prompt) can be found in this field
Greetings,
Bob
How can I copy documents using a LotusScript agent ?
The agent should work as follows:
- Prompts the user for a selectionbox with 4 values in it
- Looks for the chosen value in a field on the documents which are present in a view **
- copy's the documents that matches the criteria to the same database and put on those new documents a new field with a value (doesn't matter what the value is)
** The source documents have a multivalue field. The value that the user entered (using the prompt) can be found in this field
Greetings,
Bob
ASKER
Hi HemanthaKumar,
I have a few questions about your code:
How is the value you are looking for assigned ?
Is this agent running only from the selected view ?
When I run your agent I get a error message: "Object variable not set"
Greetings,
Bob
I have a few questions about your code:
How is the value you are looking for assigned ?
Is this agent running only from the selected view ?
When I run your agent I get a error message: "Object variable not set"
Greetings,
Bob
I assume this script is run from the currently opened document, which has multvals field with available options, when you select few of them and hit this button script, the search begins with taking the field value from the document and copying takes place.
So to test this script, put it in a button and create a field called multvals in the form and assign few dummy options and save few docs and then finally use the same form to search for the saved options.
So to test this script, put it in a button and create a field called multvals in the form and assign few dummy options and save few docs and then finally use the same form to search for the saved options.
ASKER
Hi Hemanthakumar,
OK, I understand, but what I really want is a button in a view that will call "this" agent. This agent then searches for documents within this view which matches the criteria (in you're case the FIELD multvals on the documents) and copies those documents to the database (and add the new field).
If this is to complex I'm also helped out if this agent only copies all the documents from this view and adds a new field to the copied documents.
Greetings,
Bob
OK, I understand, but what I really want is a button in a view that will call "this" agent. This agent then searches for documents within this view which matches the criteria (in you're case the FIELD multvals on the documents) and copies those documents to the database (and add the new field).
If this is to complex I'm also helped out if this agent only copies all the documents from this view and adds a new field to the copied documents.
Greetings,
Bob
ASKER
Since I'm am not a complete idiot (-: I have figured out some code to copy the documents from a view that works fine for me. The only thing that would be great is to implement the search in this code that will not copy all the docs but a selection of the docs in this view. The user will get a InputBox with 4 values and must select 1 of them. Then this code will look for the documents that have this value in the multivalue field...
This is the code sofar:
Sub Initialize
Dim session As New NotesSession
Dim db As NotesDatabase
Dim view As NotesView
Dim DocTitle As Variant
Dim doc As NotesDocument
Set db = session.CurrentDatabase
Set view = db.GetView("Alle Protocollen")
Set doc = view.GetFirstDocument
While Not (doc Is Nothing)
DocTitle = doc.ProtocolNaam
Set newdoc = doc.CopyToDatabase(db)
newDoc.CopyStatus = "1"
Call newDoc.Save(True,False)
Set doc = view.GetNextDocument(doc)
Wend
Msgbox("Documents are copied")
End Sub
This is the code sofar:
Sub Initialize
Dim session As New NotesSession
Dim db As NotesDatabase
Dim view As NotesView
Dim DocTitle As Variant
Dim doc As NotesDocument
Set db = session.CurrentDatabase
Set view = db.GetView("Alle Protocollen")
Set doc = view.GetFirstDocument
While Not (doc Is Nothing)
DocTitle = doc.ProtocolNaam
Set newdoc = doc.CopyToDatabase(db)
newDoc.CopyStatus = "1"
Call newDoc.Save(True,False)
Set doc = view.GetNextDocument(doc)
Wend
Msgbox("Documents are copied")
End Sub
Create an agent to run on selected documents with this script in it. I have commented the lines and read it carefully
Sub Initialize
Dim s As New NotesSession
Dim db As NotesDatabase
Dim doc, note As NotesDocument
Dim col As NotesDocumentCollection
Dim ws As New NotesUIWorkspace
Dim choices(3) As String
Set db = s.CurrentDatabase
' Replace this choices with original one
choices(0) = "a"
choices(1) = "b"
choices(2) = "c"
choices(3) = "g"
choice = ws.Prompt( Prompt_OKCANCELLISTMULT, "TITLE", "PROMPT", choices(0), choices )
If Not Isempty(choice) Then
Set col = db.UnprocessedDocuments
Set note = col.GetFirstDocument
While Not note Is Nothing
matchcount = -1 ' match array index to 0
If Ubound(note.MultVals) = Ubound(choice) Then ' Does doc and choice match
Forall v In note.MultVals
Forall c In choice
If c = v Then
matchcount = matchcount + 1
End If
End Forall
End Forall
If matchcount = Ubound(choice) Then
Gosub CopyDoc
End If
End If
Set note = col.GetNextDocument(note)
Wend
Print matchcount
End If
Exit Sub
CopyDoc:
Set newdoc = note.CopyToDatabase(db)
newDoc.CopyStatus = "1"
Call newDoc.Save(True,False)
Return
End Sub
Sub Initialize
Dim s As New NotesSession
Dim db As NotesDatabase
Dim doc, note As NotesDocument
Dim col As NotesDocumentCollection
Dim ws As New NotesUIWorkspace
Dim choices(3) As String
Set db = s.CurrentDatabase
' Replace this choices with original one
choices(0) = "a"
choices(1) = "b"
choices(2) = "c"
choices(3) = "g"
choice = ws.Prompt( Prompt_OKCANCELLISTMULT, "TITLE", "PROMPT", choices(0), choices )
If Not Isempty(choice) Then
Set col = db.UnprocessedDocuments
Set note = col.GetFirstDocument
While Not note Is Nothing
matchcount = -1 ' match array index to 0
If Ubound(note.MultVals) = Ubound(choice) Then ' Does doc and choice match
Forall v In note.MultVals
Forall c In choice
If c = v Then
matchcount = matchcount + 1
End If
End Forall
End Forall
If matchcount = Ubound(choice) Then
Gosub CopyDoc
End If
End If
Set note = col.GetNextDocument(note)
Wend
Print matchcount
End If
Exit Sub
CopyDoc:
Set newdoc = note.CopyToDatabase(db)
newDoc.CopyStatus = "1"
Call newDoc.Save(True,False)
Return
End Sub
ASKER
I'm almost there HemanthaKumar,
The code is working fine when the value you're looking for is the first in the list of the MultVal field. For example: I give the user these values as choice: MAJOR, HGP, BOROS and COMB. When the MultVal field contains the value MAJOR,HGP and the user selects HGP, the document is not selected to copy. When the user would have selected MAJOR, the document will be copied...
Another small comment and maybe easy to solve:
During the selection, the user is able to select multiple values in the InputBox. Is it possible to force the InputBox the accept only one value ?
Greetings,
Bob
The code is working fine when the value you're looking for is the first in the list of the MultVal field. For example: I give the user these values as choice: MAJOR, HGP, BOROS and COMB. When the MultVal field contains the value MAJOR,HGP and the user selects HGP, the document is not selected to copy. When the user would have selected MAJOR, the document will be copied...
Another small comment and maybe easy to solve:
During the selection, the user is able to select multiple values in the InputBox. Is it possible to force the InputBox the accept only one value ?
Greetings,
Bob
Use Prompt_OKCANCELLIST instead of Prompt_OKCANCELLISTMULT in the above code for single selection.
Give me your completed code and will test
ASKER
OK....
The second answer is easy... I will replace the OKCANCELLISTMULT by OKCANCELLIST
Maybe I can send you the complete .NSF ???
Greetings,
Bob
The second answer is easy... I will replace the OKCANCELLISTMULT by OKCANCELLIST
Maybe I can send you the complete .NSF ???
Greetings,
Bob
Hemanth1@bigfoot.com is my id
Bob,
Recieved your mail and it works for me.
Let me explain what the agent will do, it will not do partial matches.
It will look if the Multval field matches exactly to the selected list. In your case you said atleast one value should match to copy, Is that what you were looking for ???
Recieved your mail and it works for me.
Let me explain what the agent will do, it will not do partial matches.
It will look if the Multval field matches exactly to the selected list. In your case you said atleast one value should match to copy, Is that what you were looking for ???
ASKER
Hi HemanthaKumar,
My experience with the code is, that it works when the "InspectieSoort" field contains more then one value, for example "MAJOR,COMB" and I'm looking for COMB (the second value of the list), the document is NOT selected for copying. When in the same case, I would have selected MAJOR (the first value on the list), the document will be selected for copying...
The document should be selected when the value selected using the InputBox function in the example is MAJOR or COMB...so I thougt using a @Contains function that will check the "InspectieSoort" field that will search for the value in the "InspectieSoort" list.
Greetings,
Bob
My experience with the code is, that it works when the "InspectieSoort" field contains more then one value, for example "MAJOR,COMB" and I'm looking for COMB (the second value of the list), the document is NOT selected for copying. When in the same case, I would have selected MAJOR (the first value on the list), the document will be selected for copying...
The document should be selected when the value selected using the InputBox function in the example is MAJOR or COMB...so I thougt using a @Contains function that will check the "InspectieSoort" field that will search for the value in the "InspectieSoort" list.
Greetings,
Bob
Ok then replace this statement
If matchcount = Ubound(choice) Then
with
If matchcount >= 0 Then
If matchcount = Ubound(choice) Then
with
If matchcount >= 0 Then
ASKER
Hi HemanthaKumar,
Great....the selection works fine now...
Only one little detail and I'm ready to go (-:
You have said to change the prompt_OKCANCELLISTMULT to prompt_OKCANCELLIST so that the user can select only one value. When I do this, and the agent is started, I get a Type Mismatch Error.
When I start the LotusScript debugger, I can see that the error message is generated in the line Forall C in Choice (line 21)..Can you explain that ?
Thanks for sofar...
Greetings,
Bob
Great....the selection works fine now...
Only one little detail and I'm ready to go (-:
You have said to change the prompt_OKCANCELLISTMULT to prompt_OKCANCELLIST so that the user can select only one value. When I do this, and the agent is started, I get a Type Mismatch Error.
When I start the LotusScript debugger, I can see that the error message is generated in the line Forall C in Choice (line 21)..Can you explain that ?
Thanks for sofar...
Greetings,
Bob
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks HemanthaKumar for your patience and expertise (-:
Greetings,
Bob
Greetings,
Bob
You are welcome.
Dim s As New NotesSession
Dim db As NotesDatabase
Dim doc, note As NotesDocument
Dim col As NotesDocumentCollection
Dim ws As New NotesUIWorkspace
Dim uidoc As NotesUIDocument
Set uidoc = ws.CurrentDocument
Set note = uidoc.DOcument
Set db = s.CurrentDatabase
Forall v In note.multvals
searchPara = searchPara & {@IsMember( "} & v & {"; MultVals) & }
End Forall
searchStr = Left( searchPara, Len(searchPara) - 2)
Set col = db.Search( searchStr, Nothing, 0)
Print col.Count
Set doc = col.GetFirstDocument
While Not doc Is Nothing
If Ubound(note.multvals) = Ubound(doc.MultVals) Then
Set newdoc = doc.CopyToDatabase(db)
newDoc.newValue = "Blah!Blah!"
Call newDoc.Save(True,False)
End If
Set doc = col.GetNextDocument(doc)
Wend
~Hemanth