Lotus notes 从UI通过lotusscript调用的代理访问当前文档并显示messagebox

Lotus notes 从UI通过lotusscript调用的代理访问当前文档并显示messagebox,lotus-notes,lotusscript,agents,Lotus Notes,Lotusscript,Agents,我有一个具有以下代码的代理: Sub Initialize MessageBox "AgentStart" Print "AgentStart" Dim ws As New NotesUIWorkspace Dim s As New NotesSession Dim db As NotesDatabase Dim vItemsBySupplierSpec As NotesView Dim Doc As NotesDocument

我有一个具有以下代码的代理:

Sub Initialize
    MessageBox "AgentStart"
    Print "AgentStart"

    Dim ws As New NotesUIWorkspace
    Dim s As New NotesSession
    Dim db As NotesDatabase
    Dim vItemsBySupplierSpec As NotesView
    Dim Doc As NotesDocument
    Dim DocsWithSameSupplierSpec As NotesDocumentCollection
    Dim MatchingDoc As NotesDocument
    Set Doc = ws.CurrentDocument.Document

    If Len(Doc.ItemSupplierSpecification(0)) > 0 Then
        ' Check that this supplier specification isn't use anywhere else.'
        Set db = s.CurrentDatabase
        Set vItemsBySupplierSpec = db.GetView("vItemsBySupplierSpec")

        Set DocsWithSameSupplierSpec = vItemsBySupplierSpec.GetAllDocumentsByKey(Doc.ItemSupplierSpecification(0), True)
        Set MatchingDoc = DocsWithSameSupplierSpec.GetFirstDocument

        Dim ItemsString As String

        ItemsString = "The following items already use this supplier specification." + Chr(10) + Chr(10) + _
        "You should check whether you really want to raise another, or use the existing one." + Chr(10)


        While Not MatchingDoc Is Nothing
            ItemsString = ItemsString + Chr(10) + MatchingDoc.ItemNumber(0) + " - " + MatchingDoc.ItemDescription(0)
            Set MatchingDoc = DocsWithSameSupplierSpec.GetNextDocument(MatchingDoc)
        Wend

        If DocsWithSameSupplierSpec.Count > 0 Then
            Print ItemsString
            MsgBox ItemsString
        End If
    End If
End Sub
以前,它是在表单中字段的onchange事件中运行的

我现在已经创建了一个如上所述的代理,并希望在LotusScript和@formula语言中从ui调用它

Dim s As New NotesSession
Dim db As NotesDatabase

Set db = s.CurrentDatabase

Dim CheckSupplierSpec As NotesAgent
Set CheckSupplierSpec = db.GetAgent("CheckSupplierSpec")

If CheckSupplierSpec.Run = 0 Then
    MessageBox "Agent Ran"
End If
我将代理创建为触发器,在事件菜单选择中,target:none,options:shared。我确实收到了“代理运行”消息框

不过,我尝试过这个方法,尽管我检查了代理,它说它上次运行时触发了
onchange
事件,但我没有收到任何消息框或打印输出


第一个问题是,为什么messagebox不工作?第二个问题是如何获取当前文档?

问题是,使用Run方法调用代理时会丢失上下文。作为:

用户不能直接与被调用的代理交互。用户输出进入Domino日志

您可以尝试将文档ID作为参数传递给run方法:

Dim ws as New NotesUIWorkspace
Dim s As New NotesSession
Dim db As NotesDatabase

Set db = s.CurrentDatabase

Dim CheckSupplierSpec As NotesAgent
Set CheckSupplierSpec = db.GetAgent("CheckSupplierSpec")

If CheckSupplierSpec.Run(ws.CurrentDocument.Document.NoteID) = 0 Then
    MessageBox "Agent Ran"
End If
代理可以在ParameterDocID属性中使用该参数:


了解您为什么将它从onChange移动到代理会有所帮助,但我认为有一些方法可以实现您想要做的事情

您提到从公式语言调用代理-我能够显示一个消息框,以这种方式调用代理:

@Command([RunAgent];"CheckSupplierSpec")
另一个选择是将您的代理作为Java代理。这使您可以访问Java UI类,这些类即使被NotesAgent.Run调用也会显示。例如

如果不想在Java中重写整个代理,可以使用LS2J访问Java UI类。例如,您可以创建一个名为“Java Messagebox”的Java脚本库:

然后从LotusScript代理调用它,如下所示:

Use "Java Messagebox"
Uselsx "*javacon"
Sub Initialize
    Dim mySession  As JavaSession
    Dim myClass As JavaClass
    Dim myObject As JavaObject
    Set mySession = New JavaSession()
    Set myClass = mySession.GetClass("JavaMessagebox")
    Set myObject = myClass.CreateObject()
    myObject.Messagebox(|This is my Java messagebox!|)
End Sub

对于一个使用Java AWT组件的更复杂的示例,该组件使用您操作系统的本机外观,我建议学习。他的StatusBox示例是非模态的,但是如果需要,您可以找到使其成为模态的参数

谢谢这个例子,看起来这就是获取当前文档的解决方案。但我的第一个问题是,为什么没有显示消息框。代理的第一行应该显示一个,这有点符合这样一个事实,即当代理以这种方式运行时,不允许使用UI方法。显示messagebox或打印语句将是一个UI功能。但是,我认为您应该在服务器日志中看到消息。要么就是这样,要么在开始时扫描整个代理的UI调用,如果它包含任何UI调用,甚至都不会启动。我模模糊糊地记得几年前我处理过的一个bug就是这样,但不要引用我的话:)嗯,好的。这很烦人,但有道理。再次感谢。
Use "Java Messagebox"
Uselsx "*javacon"
Sub Initialize
    Dim mySession  As JavaSession
    Dim myClass As JavaClass
    Dim myObject As JavaObject
    Set mySession = New JavaSession()
    Set myClass = mySession.GetClass("JavaMessagebox")
    Set myObject = myClass.CreateObject()
    myObject.Messagebox(|This is my Java messagebox!|)
End Sub