Calling script library in agent and button - lotus-notes

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 )

Related

(Domino Notes)How to move the document back to the original NSF?

I already ask "How to move documents from the original NSF to another NSF?"
(Domino Notes)How to move documents from the original NSF to another NSF?
Now, I still want to ask "How to move the document back to the original NSF?"
I want to make the form that can be moved back to the original database, but there is an error in the command, and cannot be moved back.
The following instructions are written in the buttons of the NOTES form.
How can I modify them?
Sub Click(Source As Button)
Dim ws As New notesuiworkspace
Dim uipr As NotesUIDocument
Dim ask_me As Variant
Set uipr = ws.CurrentDocument
data(0) = "Back original NSF"
data(1) = "Non-person case"
ask_me = ws.Prompt(PROMPT_OKCANCELEDITCOMBO,"Reset Reason","Choose a reason...",data(0),data())
If ask_me = False Then Exit Sub
If uipr.editmode=False Then uipr.editmode=True
If ask_me = data(0) Then
Dim achiveDB As New NotesDatabase("fcpnotesM" , "EFA00B7.nsf")
Dim doc As NotesDocument
Set doc = uipr.Document
Call ChangeField
Msgbox "Change field OK"
Call doc.CopyToDatabase(achiveDB)
Msgbox "Copy success"
Call doc.Remove(True)
Msgbox "Move success"
End If
End Sub
uipr seems to be used before it's defined/asigned
If uipr.editmode=False Then uipr.editmode=True //used here
If ask_me = data(0) Then
Dim achiveDB As New NotesDatabase("fcpnotesM" , "EFA00B7.nsf")
Dim uipr As NotesUIDocument //defined here, but no asignment

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

No "Script Area" in Lotus Notes Agent editor

I need the functionality described here: Determining which folder contains a document in Lotus Notes without using the LotusScript FolderReferences property
It says: "And the following code in the Script Area". But there's no script area. There's a tree containing "Document Selection", "(Options)", "Declarations", "Initialize", and , "Terminate".
You can enter that code into the body of the Initialize method. Make sure to keep the code that automatically appears when you select Initialize and just enter the code in the middle
Sub Initialize
Dim session As New notessession
Dim db As notesdatabase
Dim doc As notesdocument
Dim doc2 As notesdocument
Dim view As notesview
Dim noteid1 As String
Dim noteid2 As String
Dim item As notesitem
Dim collection As notesdocumentcollection
Set db=session.CurrentDatabase
Set collection=db.UnprocessedDocuments
Set doc=collection.getfirstdocument
noteid1=doc.NoteID
Forall v In db.Views
If v.isfolder Then
Set doc2=v.GetFirstDocument
While Not doc2 Is Nothing
noteid2=doc2.NoteID
If noteid1=noteid2 Then
Messagebox v.name
End If
Set doc2=v.getnextdocument(doc2)
Wend
End If
End Forall
End Sub

Document command is not available whend using uidoc.copy

I got an error 'Document command is not available' when the program hits the uidoc.Copy line. I have researched this error message. But all I get is only if it has anything to do with Edit Mode and I am not using that at all here.
Sub Click(Source As Button)
' ===========================================================
' Get common username, mail server, and mailfile information
to be used on ComposeDocument method
Dim session As New NotesSession
Dim reg As New NotesRegistration
Dim user As String
reg.RegistrationServer = "Test"
user = session.CommonUserName
Call reg.GetUserInfo(user, _
mailserver$, _
mailfile$)
' ======================================================================
Dim workspace As New NotesUIWorkspace
Dim uidoc As NotesUIDocument
Set uidoc = workspace.CurrentDocument
Call uidoc.GotoField("QSContactEMail")
Call uidoc.SelectAll
Call uidoc.Copy
Set uidoc = workspace.ComposeDocument _
(mailserver$, mailfile$, "Memo")
Call uidoc.GotoField("Subject")
Call uidoc.Paste
End Sub
You don't need to use copy and paste to transfer values from one document to another. You can directly assign those values using document objects.
There are several methods to do this- here is one that is similar to yours. Replace everything below the second line with this:
Dim workspace As New NotesUIWorkspace
Dim uidoc As NotesUIDocument, newuidoc as NotesUIDocument
Set uidoc = workspace.CurrentDocument
Set newuidoc = workspace.ComposeDocument _
(mailserver$, mailfile$, "Memo")
Call newuidoc.FieldSetText("Subject", uidoc.FieldGetText("QSContactEMail"))
End Sub
Check to see if you have a field called $KeepPrivate set to "1". It will prevent copying.
The error can also happen if nothing is selected. Try STOP just before the line and make sure it is highlighted.
There are also other conditions listed here:
http://www-01.ibm.com/support/docview.wss?uid=swg21094450

Filter Process script Library

I have a form called Approver in "Approver" db.
The form has two editable text fields: Office and Group. It also has a dialog list field superior1.
The superior1 dialog list field should show the staff details filtered based on office & group:
if office = TSP & group = HR from the approver form, then shud filter the staffs based on these fields group" & "office" with the "Staff info" view of another database "TSP_Staff" and show in superior1.
But it is not getting filtered for me. :(
I am new to this tech, so I am confused and have no one to help me in this. This is the script I used:
for the superior1 field:
Sub Entering(Source As Field)
Dim s As New NotesSession
Dim db As NotesDatabase
Dim view As NotesView
Dim uidoc As NotesUIDocument
Dim doc As Notesdocument
Dim work As New NotesUIWorkspace
Dim workspace As New NotesUIWorkspace
Dim sname As String
Dim consr As String
Dim cview As notesview
Set db = s.CurrentDatabase
Set uidoc = work.CurrentDocument
Set uidocs = workspace.currentdocument
Set cview = db.getview("(Application)")
'etype = uidoc.FieldGetText("Office")
'ftype = uidoc.FieldGetText("Group")
etype = "TSP"
ftype = "TSP1-G"
If(etype <> "" And ftype <> "") Then
Call filter
End If
Set view = db.getview("(x_search_staff)")
Set doc = view.GetDocumentByKey (uidoc.fieldgettext("Superior1"),True)
If doc Is Nothing Then
Msgbox "There is no previous transaction please select new trasaction.", 16, "Information"
Call uidoc.FieldClear("Superior1")
Call uidoc.gotofield ("Group")
Call uidoc.gotofield ("Office")
continue = False
Exit Sub
End If
Call uidoc.Refresh
End Sub
from the script library ...
Sub filter
Dim s As New notessession
Dim w As New notesuiworkspace
Dim uidoc As notesuidocument
Dim doc As notesdocument, newdoc As notesdocument, d As notesdocument, dd As notesdocument
Dim doc1 As NotesDocument, newdoc1 As NotesDocument
Dim dc As notesdocumentcollection
Dim bc As notesdocumentcollection
Dim view As notesview, v As notesview
Dim db As notesdatabase
Dim nextdoc As NotesDocument
Dim cview As notesview
Dim cnview As NotesView
Dim get_db As New notesdatabase(gsserver2, gspath2 & "Master\TSP_Staff.nsf")
Set db = s.currentdatabase
Set view = get_db.getview("(Staff Info)")
Set cview = db.getview("(x_search_staff)")
Set cnview = db.getview("(x_superior)")
Set uidoc=w.CurrentDocument
'To delet searched previous datas from form2 ----------------------------------------
Print "Please wait ..."
key = "Approver2"
Set v = db.getview("(x_delete_2)")
Set dc = v.GetAlldocumentsByKey(key,True)
'Set bc = v.GetAlldocumentsByKey(key,True)
'Call bc.RemoveAll(True)
Call dc.RemoveAll(True)
Call cview.Refresh
Call view.Refresh
Call cnview.Refresh
Call v.Refresh
'To start searching process based on Superior1 --------------------------------------
'f1= uidoc.FieldGetText("Office")
f1= uidoc.FieldGetText("Group")
'f1 = "TSP1-G"
Set dc = view.getalldocumentsbykey(f1, True)
'Set bc = view.getalldocumentsbykey(f2, True)
For b =1 To dc.count
Set doc = dc.getnthdocument(b)
Set newdoc = doc.copytodatabase(db)
'For c =1 To bc.count
'Set doc1 = bc.getnthdocument(b)
'Set newdoc1 = doc.copytodatabase(db)
If doc.form(0) = "Approver" Then
'If doc1.form(0) = "Approver" Then
newdoc.form = "Approver2"
'newdoc1.form = "Approver2"
'End If
End If
newdoc.save True, True
' Next
'newdoc.save True, True
'Next
Call w.viewrefresh
Call cview.Refresh
Call v.Refresh
Call cnview.Refresh
Call view.Refresh
Print "Process Completed....."
End Sub
if u got another way for this requirement temme in stepwise wat to do... or else... chk out ma script for errors... hope u help me :( today due date for this task...
I'm not sure how smart it is to filter the documents shown in a view by deleting documents from a database :)
My suggestion is to first post the code properly. This is simply unreadable.
How to display only subset of documents in your dialog list?
Create a hidden field on your form (you'll fill it with values you want displayed in the list using your code).
And then, on your dialog list field properties, second tab, set choices option to be "Use formula for choices" and set it to be the hidden field name.
Ask if you need more help...
Your code is very hard to follow, but if I understand your intention and parts of the filter function (does it even compile?) you could replace all of the code with this #dblookup-based formula in "use formula for choices" section of superior1 properties:
#dblookup("":"ReCache";"ServerName":"foo\Master\TSP_Staff.nsf";"(Staff Info)";Group;NameOfInterestingField);
You might want to add a #sort and/or #unique around it if the view contain duplicate values, and you might want to add the keyword [FAILSILENT] if some groups should result in an empty list.
An even simpler method could be to configure superior1 to use view dialog for choices.

Resources