Question

I have an agent with the following code:

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

Previously it was ran within the onchange event of a field in a form.

I've now created an agent as above, and want to invoke it from the ui both in lotus script and @formula language.

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

I created the agent as trigger, on event - menu selection, target: none, options: shared. I do get the "Agent Ran" messagebox.

I've tried this however although checking the agent it says it last ran when the onchange event was fired i don't get any message boxes or print output.

The first question, is why isn't the messagebox working? the 2nd question is how can i get the current document?

Was it helpful?

Solution

It would help to know why you moved it from onChange to an agent, but I think there are ways to do what you want to do.

You mentioned invoking the agent from formula language- I was able to display a Messagebox calling the agent this way:

@Command([RunAgent];"CheckSupplierSpec")

Another option would be doing your agent as a Java agent. This gives you access to Java UI classes that will display even if called by NotesAgent.Run. Example here.

If you don't want to rework the entire agent in Java, you can use LS2J to access the Java UI classes. For example, you could create a Java script library called "Java Messagebox":

import javax.swing.JOptionPane;

public class JavaMessagebox {

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

}

and then call it from a LotusScript agent like this:

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

For a more sophisticated example using a Java AWT component that uses the native look and feel of your operating system, I recommend studying Julian Robichaux's LS2J Examples Database. His StatusBox example is non-modal but you can find the parameter to make it modal here if needed.

OTHER TIPS

The problem is that you lose context when you call an agent using the Run method. As the designer help states:

The user cannot interact directly with a called agent. User output goes to the Domino log.

You could try to pass the document's ID as a parameter to the run method instead:

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

That parameter is available to the agent in the ParameterDocID property:

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top