从UI中通过lotusscript调用代理访问当前文档并显示消息框

时间:2011-12-21 12:13:11

标签: 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
    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事件中运行的。

我现在已经创建了一个代理程序,并希望在Lotus脚本和@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

我在事件菜单选择中创建了代理作为触发器,目标:无,选项:共享。我确实收到了“Agent Ran”消息框。

我已经尝试了这个但是虽然检查代理它说它上次运行onchange事件时我没有得到任何消息框或打印输出。

第一个问题,为什么消息框不起作用?第二个问题是我如何获得当前文件?

2 个答案:

答案 0 :(得分:5)

问题是当您使用Run方法调用代理时会丢失上下文。正如designer help states

  

用户无法直接与被叫代理进行交互。用户输出转到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属性中的代理使用:

http://www-12.lotus.com/ldd/doc/domino_notes/rnext/help6_designer.nsf/Main?OpenFrameSet

答案 1 :(得分:2)

知道你为什么把它从onChange转移到代理商会有所帮助,但我认为有办法做你想做的事。

您提到从公式语言调用代理 - 我能够以这种方式显示以此方式调用代理的Messagebox:

@Command([RunAgent];"CheckSupplierSpec")

另一种选择是将您的代理作为Java代理。这使您可以访问即使由NotesAgent.Run调用也将显示的Java UI类。示例here

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

import javax.swing.JOptionPane;

public class JavaMessagebox {

    public void Messagebox (String message) {
        JOptionPane.showMessageDialog(null, message);
    }

}

然后从这样的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组件的更复杂的示例,该组件使用操作系统的本机外观,我建议学习Julian Robichaux's LS2J Examples Database。他的StatusBox示例是非模态的,但如果需要,您可以找到使其成为模态here的参数。