Subject: Run Agent from Javascript
Here is the method that I use to accomplish this. It’s not difficult but you need to pay attention to the details. In essence you are taking a single process and breaking it up amongst several systems, each one working independently. The messaging between systems is the key to controlling what’s happening and maintaining the unity of the process.
This process uses an old concept called reentrant programming. This concept is used in mainframe programming. It was the method use to get a mainframe of 30 years ago to handle 1000 users. Basically, you run your program and present some interface to a user (usually a green terminal screen.) Then your program ends…completely. It may even get tossed from memory. When the user presses Enter the terminal starts your program again. At that point you figure out where you left off and continue processing. Since users spend more time thinking and typing than the computer spends processing, you can have a thousand people working while the mainframe is only processing for a few users at any given moment.
We’re going to use this concept as a method of switching between languages.
First you create a field on the form that you’ll use to transfer instructions from one language to another. I usually call it NextStep. I usually also have an ErrorMessage field to go with that. Create other fields as needed to transferring data between languages. These can be computed fields equal to themselves.
Next you create a button at the bottom of your form with the formula for running the agent. DO NOT HIDE THE BUTTON. Rather, set the HTML tag Style to display=none. Also set the HTML Name. You’ll reference this later in JavaScript. In this example the HTML Name is WebProcesses.
Enter a formula such as:
@Command( [RunAgent] ; “web_processes” )
Next you create some onload code in your form to process the response from your agent. So in the onload of the form you’ll have something like:
var NextStep = thisform.NextStep.value
if (NextStep != “” ) {
thisform.NextStep.value = "" //reset NextStep as quickly as possible. Don't let it linger.
switch (NextStep) {
//Close process
case "UpdateNotes_CloseForm": //Closes the dialog and returns data.
window.opener.close()
window.close()
break
//Messages
case "Error":
alert( thisform.ErrorMessage.value )
break
//Refresh
case "Refresh":
thisform.ViewRefreshFields.click()
break
}
}
Finally, write the JavaScript for your hotspot.
thisform.NextStep.value = “UpdateNotes_SaveNote”
thisform.WebProcesses.click()
Okay…now you’re ready to process this. Here’s how it works. First you click your hotspot to execute the JavaScript. The JavaScript sets the NextStep field as an instruction to the agent. Then it clicks the WebProcesses button. That’s it. The JavaScript is over.
Next the formula in the button takes over and executes the agent. The formula hangs and waits for the agent to finish.
In your LotusScript agent use NotesSession.DocumentContext to get the document and check the NextStep field. Have a Select statement to choose which sub gets executed based on the NetStep value.
NextStep = docCurrent.NextStep(0)
docCurrent.NextStep = "" 'Reset immediately to prevent this process from running again if there's a failure.
Select Case NextStep
Case "WebQueryOpen"
Call WebQueryOpen
Case "UpdateStatus_QueryClose"
Call UpdateStatus_QueryClose
Case "UpdateNotes_SaveNote"
Call UpdateNotes_SaveNote
End Select
Execute your code. When the code need to break for a message, you set your NestStep field to the appropriate value to be processed by the form onload. For example, you might have a validation failure and set it to “ValidationError” and you might set the ErrorMessage field to “Please enter a recipient.” Then you simply end your agent.
When your agent ends, your form will reload. This happens automatically whenever a button on the form is clicked and ends processing. When it reloads the onload event will run. In the onload you simply check the NextStep field and do what’s necessary based on its contents. Refer to the JavaScript Switch statement above.
Now that you’re back in JavaScript you can click buttons and start the process all over again. In this way you can loop between JavaScript, formula, and LotusScript to accomplish anything you need to.