Create a document in another database and set the field values - lotus-notes

My goal is to create a document from one database in another database and fill the fields with values from the source database. I was able to connect to the other database and compose a document but editing is not possible.
Sub Click(Source As Button)
Dim mydb As NotesDatabase
Dim workspace As NotesUIWorkspace
Dim uidoc As NotesUIDocument
Dim doc As NotesDocument
Dim Server As String
Dim DBPath As String
Dim DBForm As String
'-----------------------------------------------------------------
' Set target database information
'-----------------------------------------------------------------
Server = "Server"
DBPath = "Path"
DBForm = "Form"
'-----------------------------------------------------------------
' Attempt connection to target server
'-----------------------------------------------------------------
Print "Connecting to target database"
Set mydb = New NotesDatabase("", "")
Call mydb.Open(Server, DBPath)
If (mydb.IsOpen) Then
'-----------------------------------------------------------------
' Create new document
'-----------------------------------------------------------------
Print "Connection established to: " + mydb.FileName
Set workspace = New NotesUIWorkspace
Print "Composing change management record"
Set uidoc = workspace.ComposeDocument (Server, DBPath, DBForm)
Call uidoc.FieldSetText("FIELD", "12345")
Else
Msgbox "Warning: unable to open target database."
End If
End Sub
When I'm calling
Call uidoc.FieldSetText("FIELD", "12345")
I'm getting an error message saying that I have to open the document in edit mode. When I'm trying to change the edit mode with
uidoc.EditMode = True
I'm getting the error message "Document command is not available". Can someone please help me here?

it is possible, take the backend not the frontend classes
Dim workspace As New Notesuiworkspace
Dim session As New NotesSession
Dim myDb As NotesDatabase
Dim doc As notesdocument
Set myDb= session.Getdatabase("Server", "Path", false)
If (mydb.IsOpen) Then
Set doc = myDb.Createdocument()
doc.field="12345"
Call workspace.Editdocument(true, doc)
Else
Msgbox "Warning: unable to open target database."
End If

Related

How can I delete a mail database in HCL Domino over Lotus Script?

I would like to delete a mail database over a notes agent. Sometimes the mail databases have a replica.
Whats the best way to delete the mail file itself and also all other replicas (if they exist)?
My code below looks like this, but it doesn't delete the mail file in a replica.
Dim mailfile As String
mailfile = "mail\doe.nsf"
Dim db As New NotesDatabase("", mailfile)
If db.IsOpen Then
'Mark to delete it later
Call db.MarkForDelete()
Else
'Delete now
Call db.Remove
End If
You could use the built in function to do this by using NotesAdministrationProcess:
Sub Initialize
Dim session As New NotesSession
Dim adminp As NotesAdministrationProcess
Set adminp = _
session.CreateAdministrationProcess("Software_Server")
noteid$ = adminp.DeleteReplicas("Software_Server", "Guys1")
'- in case you want to open the generated adminp request
If noteid$ <> "" Then
Dim db As New NotesDatabase("Software_Server", "admin4")
Dim ws As New NotesUIWorkspace
Call ws.EditDocument(False, db.GetDocumentByID(noteid$))
End If
End Sub
If you do not want to wait for this (as it needs time: replicate admin4.nsf to all servers, execute admin process there, replicate back...), you can do this by yourself if you know the servers where the replicas are in advance:
Dim mailfile As String
mailfile = "mail\doe.nsf"
Dim otherServers(2) as String
Dim replicaID as String
Dim db as NotesDatabase
otherServers(0) = "FirstServerName/Certifier"
otherServers(1) = "SecondServerName/Certifier"
otherServers(2) = "ThirdServerName/Certifier"
Set db = New NotesDatabase("PrimaryServer/Certifier", mailfile)
If db.IsOpen Then
replicaID = db.ReplicaID
On Error Goto ErrorRemove
'Delete now
Call db.Remove
On Error Goto ErrorHandler
Forall serverName in otherServers
Set db = New NotesDatabase("", "")
Call db.OpenByReplicaID( serverName, replicaID )
If db.IsOpen Then
On Error Goto ErrorRemove
'Delete now
Call db.Remove
On Error Goto ErrorHandler
End If
End Forall
End If
EndOfRoutine:
Exit Sub
ErrorRemove:
Call db.MarkForDelete()
Resume Next
ErrorHandler:
'- do usual error handling here
REMARK: Your check for "db.IsOpen" does not help at all. As "IsOpen" does NOT return if a database is currently open somewhere. It returns, if YOUR SCRIPT was able to open the database at exaxtly that moment... I added an error handler to take this into account.

Copy document is saved but original not changes status using lotusscript

I have two document which is original and copy document. When I save copy, original will changes status too.
Follow up by my other question Save copy document and change status field for copy document and original document using lotusscript button. Below are my code.
Sub Click(Source As Button)
Dim session As New NotesSession
Dim db As NotesDatabase
Dim workspace As New NotesUIWorkspace
Dim uidoc As NotesUIDocument
Dim doc As NotesDocument
Dim activeDoc As NotesDocument
Dim view As NotesView
Dim keys(1) As String
'// Set database and doc
Set db = session.CurrentDatabase
Set uidoc = workspace.CurrentDocument
Set doc = uidoc.Document
keys(0) = doc.PTagNo(0)
keys(1) = "Lock"
Set view = db.GetView("Computer")
vpswd = Inputbox$("Pls input code to save :")
If vpswd = "o" Then
Set activeDoc= view.GetDocumentByKey(keys, True)
If Not activeDoc Is Nothing Then
If activeDoc.PStatus(0) = "Lock" Then
activeDoc.DocumetId = doc.UniversalID
Call activeDoc.ReplaceItemValue("PStatus", "Inactive")
Call activeDoc.Save(True, False)
End If
End If
Call uidoc.FieldSetText("PStatus" , "Active")
Call uidoc.FieldSetText("SaveOptions" , "1")
Call uidoc.Save
Call uidoc.Close
Else
Msgbox "Wrong Code"
Exit Sub
End If
End Sub
I already set variable and successfull saved. When I saved, Copy document is saved and change status to Active, but for Current document which currently with "Lock" status, did not change to "Inactive" status. Can anyone help me? Is there any error that I have made inside coding? Your help really appreciate. Thanks!
I've solved this question. For my problem, I do not set TagNo as my first column. So I need to created new view with two column which is TagNo and Status. Set both column as ascending.

NotesAgent.Run method always returns 0 no matter what

All,
I have the following problem. I want to launch an agent and want it to communicate back to the calling script if things went OK or if something went less OK.
I tried to use a solution that seems obvious, the return value of the NotesAgent.Run
My agent looks like this (Terminate sub routine is empty)
Sub Initialize
Set ws = New NotesUIWorkspace
Set uidoc = ws.Currentdocument
Set doc = uidoc.Document
Set pass = doc.Getfirstitem("Passcode")
Error 1144
' log information here
End Sub
I am calling(or launching if you may) the agent like this
Sub Click(Source As Button)
Dim session As New NotesSession
Dim db As NotesDatabase
Dim agent As NotesAgent
Set db = session.CurrentDatabase
Set agent = db.GetAgent("MyAgent")
returnVal% = agent.Run
Messagebox "It returned " & returnVal%
End Sub
If I take out the Error statement the logs get updated, and that doesn't happen if I leave there the Error statement, so it is definitely causing an error. But the Message box always prints stubbornly "It returned 0". I also tried to put the Error statement on Terminate. Result was the same, unfortunately ..
Could you please kindly point me where I'm going sideways on this ? I was expecting this to be simple.
Thank you
Kind Regards,
Carlos
This method returns a value that indicates that the agent is started or did not started. It does not return the result of the agent itself. If the agent is started, but during its work the agent made an error, then this method returns 0, because the agent is started.
0. To recieve a status from agent you can use the in-memory document as described here.
Your agent:
Sub Initialize
Set ws = New NotesUIWorkspace
Set uidoc = ws.Currentdocument
Set doc = uidoc.Document
Set pass = doc.Getfirstitem("Passcode")
Dim ses As New NotesSession
Dim docContext = ses.DocumentContext
Call docContext.ReplaceItemValue("ReturnVal", 1444)
' log information here
End Sub
Call the agent like this:
Sub Click(Source As Button)
Dim session As New NotesSession
Dim db As NotesDatabase
Dim agent As NotesAgent
Dim doc As NotesDocument
Set db = session.CurrentDatabase
Set doc = db.CreateDocument
Set agent = db.GetAgent("MyAgent")
Call agent.RunWithDocumentContext(doc)
returnVal% = doc.ReturnVal(0)
Messagebox "It returned " & returnVal%
End Sub
1. If you cannot use in-memory document then you need to save document and reopen it.
Your agent:
Sub Initialize
Set ws = New NotesUIWorkspace
Set uidoc = ws.Currentdocument
Set doc = uidoc.Document
Set pass = doc.Getfirstitem("Passcode")
Dim ses As New NotesSession
Dim agent As NotesAgent
Dim db As NotesDatabase
Dim docContext As NotesDocument
Set agent = ses.CurrentAgent
Set db = ses.CurrentDatabase
Set docContext = db.GetDocumentByID(agent.ParameterDocID)
Call docContext.ReplaceItemValue("ReturnVal", 1444)
Call docContext.Save(False, False)
' log information here
End Sub
Call the agent like this:
Sub Click(Source As Button)
Dim session As New NotesSession
Dim db As NotesDatabase
Dim agent As NotesAgent
Dim doc As NotesDocument
Set db = session.CurrentDatabase
Set doc = db.CreateDocument
Call doc.Save(False, False)
noteID$ = doc.NoteID
Set agent = db.GetAgent("MyAgent")
Call agent.Run(noteID$)
Delete doc
Set doc = db.GetDocumentByID(noteID$)
returnVal% = doc.ReturnVal(0)
Messagebox "It returned " & returnVal%
Call doc.Remove(True)
End Sub
I believe you need to use the RunOnServer() method, which according to the documentation is synchronous:
http://www.ibm.com/developerworks/lotus/library/ls-Troubleshooting_agents_ND5_6/
Sub Click(Source As Button)
Dim session As New NotesSession
Dim db As NotesDatabase
Dim agent As NotesAgent
Set db = session.CurrentDatabase
Set agent = db.GetAgent("MyAgent")
returnVal% = agent.RunOnServer()
Messagebox "It returned " & returnVal%
End Sub

Calling script library in agent and button

I want to use the following script library in button and also in agent.
My script library code:Validate
Option Public
Option Declare
Dim Sess As NotesSession
Dim currentDb As NotesDatabase
Dim dataDb As NotesDatabase
Dim doc As NotesDocument
Dim workspace As NotesUIWorkspace
Dim uidoc As NotesUIDocument
Dim rtitem As NotesRichTextItem
Sub Initialize
Set Sess = New NotesSession
Set currentDb = Sess.CurrentDataBase
Set workspace = New NotesUIWorkspace
Set uidoc = workspace.CurrentDocument
End Sub
Function ValidateForm ( Source As NotesUIDocument) As Boolean
On Error GoTo e
Set doc=source.Document
Dim Txt As String
Dim trimmed As string
txt = doc.Name(0)
trimmed = Trim(Txt)
If ( trimmed = "") Then
MsgBox "Please enter some text."
source.GotoField("Name")
ValidateForm= false
Else
ValidateForm= True
End If
Exit Function
e:
MsgBox "error at"& Erl() & " and error is "& Error()
End Function
In Button:
In button when i call the script library since in the validateform function it has source as notesuidocument and in button click it has souce as button it i giving me error.
Sub Click(Source As Button)
End Sub
I have tried using in agent in options using below:
Use "Validate"
and tried calling it in button using formula
#Command([ToolsRunMacro]; "Val")
But no use I am not getting the desired output.
I am new to lotus notes.Please help me in doing above tasks.
You don't need to take a parameter at all. In the initialize- Sub of your Script- Library you already set the global variable "uidoc" to the currently opened document:
Set workspace = New NotesUIWorkspace
Set uidoc = workspace.CurrentDocument
In your Function "validateForm" you simply omit the parameter and then replace "source" with "uidoc"
Set doc=source.Document
The other possibility (if you want to give the current document as a parameter):
Sub Click( Source as Button)
Dim ws as New NotesUIWorkspace
Dim uidoc as NotesUIDocument
set uidoc = ws.CurrentDocument
Call ValidateForm( uidoc )
End If
Or if you keep the initialize code in your Library:
Sub Click( Source as Button)
Call ValidateForm( uidoc )
End If
This works, as "uidoc" is a global variable, that is already initialized by the Sub initialize of your Script- Library.
HTH
Make it an agent, not a script library. If it's named Validate, use that formula you had in the button without trying to include a script library.
#Command([ToolsRunMacro]; "Validate")
Script libraries are typically used for subroutines and functions that you will call from multiple agents or other scripts, not for entire agents. You can call an agent from a button or allow users to click on it in the Action menu or any number of other ways of calling it. You don't have to put it in a script library.
You could reduce the code in the agent to be as follows:
Option Public
Option Declare
Sub Initialize
Dim workspace As New NotesUIWorkspace
Dim uidoc As NotesUIDocument
Dim doc As NotesDocument
Dim Txt As String
Dim trimmed As string
Set uidoc = workspace.CurrentDocument
On Error GoTo e
Set doc=uidoc.Document
txt = doc.Name(0)
trimmed = Trim(Txt)
If ( trimmed = "") Then
MsgBox "Please enter some text."
uidoc.GotoField("Name")
End If
exit sub
e:
MsgBox "error at"& Erl() & " and error is "& Error()
End Sub
Or, if all you want to do is verify that a field is not empty and shift focus to that field, just add the following to any field's Input Validation formula:
#If ( #ThisValue = ""; #Failure ( "You must enter a value for " + #ThisName ); #Success )

LotusScript: Invalid Key Value Type

Can someone offer some help on this? I'm getting an 'invalid key value type' error when trying to search a view using the userName variable as a key.
Is this because it's the NotesName type? Is there any way to get around this? Any help appreciated. Thanks!
%REM
Sub aiv_checkPersonIsValid
Description:
Checks that the person(s) that have been entered into the location database are valid
members of the CCL Address Book
Function calls: N/A
Sub calls: N/A
Called in event: onChange (to populate fields)
Called in action: Save and Close, Save, Save And Add Another
Called by: co_countValuesAndPopulate()
%END REM
Public Function aiv_checkPersonIsValid (userName As NotesName) As Boolean
Dim s As New NotesSession
Dim w As New NotesUIWorkspace
Dim peopleDb As NotesDatabase
Dim peopleView As NotesView
Dim peopleDoc As NotesDocument
Dim thisDoc As NotesDocument
Dim uidoc As NotesUIDocument
Dim key(0) As Variant
Dim noMatchFound As String
Let noMatchFound = "No match found for this name. Please choose a name from the menu provided."
Dim PersonNameField As NotesItem
'Dim userName As NotesDocument
Set uidoc = w.CurrentDocument
Set thisDoc = uidoc.Document
'get handle to People database and check we've found the database
Set peopleDb=s.GetDatabase("****", "ccl\development\LocSysPeople.nsf")
If Not peopleDb Is Nothing Then
'get handle to view in People database and check we've found the view
Set peopleView = peopleDb.GetView("All\LocSysLookup")
If Not peopleView Is Nothing Then
'Make the PersonNameField variable equal to the value of the first item in the Person Name Field
Set PersonNameField = thisDoc.GetFirstItem("PersonName")
ForAll pName In PersonNameField.Values
'Start to loop through the PersonNameField and check that the names chosen exist in the people database
'lookup record in People database which matches the name in the PersonName field on this document
Set userName = New NotesName(pName)
Set key(0) = userName
'Set peopleDoc = peopleView.GetDocumentByKey(uidoc.Document.GetItemValue("PersonName")(0), True)
Set peopleDoc = peopleView.GetDocumentByKey(key, True)
'If there is no match found to the document in the peopleDb, show a MsgBox
If peopleDoc Is Nothing Then
MsgBox "No match found in the CCL People Database for '" + pName.Common + "'. Please choose a valid name.", 0, "Error: No match found"
aiv_checkPersonIsValid=False
Exit Function
End If
'End Loop
End ForAll
End If
End If
End Function
You seem to be using the userName variable multiple times for different purposes, once in the function definition and then changing the type later in the code. The code looks like it was ripped from a different function and adapted because the value being passed in it not the one your comparing in the code later, there is also nothing to set the return value of the function which is another error
Here's how I would write that function
Public Function aiv_checkPersonIsValid As Boolean
aiv_checkPersonIsValid=False
Dim s As New NotesSession
Dim w As New NotesUIWorkspace
Dim uidoc As NotesUIDocument
Set uidoc = w.CurrentDocument
Dim thisDoc As NotesDocument
Set thisDoc = uidoc.Document
Dim userName As NotesName
Dim peopleDoc As NotesDocument
Dim PersonNameField As NotesItem
'get handle to People database and check we've found the database
Dim peopleDb As NotesDatabase
Set peopleDb=s.GetDatabase("****", "ccl\development\LocSysPeople.nsf")
If Not peopleDb Is Nothing Then
'get handle to view in People database and check we've found the view
Dim peopleView As NotesView
Set peopleView = peopleDb.GetView("All\LocSysLookup")
If Not peopleView Is Nothing Then
'Make the PersonNameField variable equal to the value of the first item in the Person Name Field
Set PersonNameField = thisDoc.GetFirstItem("PersonName")
ForAll pName In PersonNameField.Values
'Start to loop through the PersonNameField and check that the names chosen exist in the people database
'lookup record in People database which matches the name in the PersonName field on this document
Set userName = New NotesName(pName)
Set peopleDoc = peopleView.GetDocumentByKey(userName.Abbreviated, True)
'If there is no match found to the document in the peopleDb, show a MsgBox
If peopleDoc Is Nothing Then
MsgBox "No match found in the CCL People Database for '" + pName.Common + "'. Please choose a valid name.", 0, "Error: No match found"
Exit Function
End If
End ForAll
aiv_checkPersonIsValid = true
End If
End If
End Function

Resources