There are two ways of running an Agent from XPages and I will elaborate both in a step by step process.

Method 1 – Run agent with Document Context

Step 1: Create an xpage and put an edit box field. This field is used to display the response from Agent as shown below.

Step 2: Create a button and call an agent using a “in-memory document” as shown below.

Code:

var newDoc:NotesDocument = database.createDocument();

newDoc.replaceItemValue(“TestField”,””);

var agent:NotesAgent = database.getAgent(“agDocContext”);

if(agent!=null){

agent.runWithDocumentContext(newDoc);

getComponent(“Response”).setValue(newDoc.getItemValueString(“TestField”));

}

Step 3: Create an agent which gets the “in-memory document” and performs some action. Ensure the agent properties are shown below.

Code:

Sub Initialize

Dim ses AsNew NotesSession

Dim doc As NotesDocument

Set doc = ses.Documentcontext

Call doc.Replaceitemvalue(“TestField”,“Test Field Value”)

EndSub

STEP 4: Ensure the agent security properties are shown below.

Step 5: Run the XPAGE and click on “Run Agent With Document Context” button.

  • On clicking of this button, a “in-memory” document is created.
  • An agent has been called by passing this “in-memory” document.
  • Agent gets this “in-memory” document and performs required actions to be done.
  • After successful execution of this agent, xpage gets the “in-memory” document handle and does the required action.

Method 2 – Using a Buffered Reader

Step 1: One more button called “Trigger Agent” as shown below.

Step 2: Call an agent using this button as shown below.

Code:

importPackage(java.net);

importPackage(java.io);

//create a url object by passing the Lotusscript agent’s url and ensure that you have some print statements in your agent

var agent:URL = new URL(“https://localhost/AgentsInXPAGES.nsf/agResponse?OpenAgent”);

//create a connection with the agent’s url provided

var agentConnection:URLConnection = agent.openConnection();

//open a buffered reader that gets the stream of response from the agent connection

var inputReader:BufferedReader = new BufferedReader(new InputStreamReader(agentConnection.getInputStream()));

//read the output line by line and store them in a string

var res:String=””;

while ((inputLine = inputReader.readLine()) != null) {

res+=inputLine;

}

//update the result to a output field on the XPage to view the same visually

getComponent(“Response”).setValue(res);

 

These are the two ways on how you can run an agent from XPages. Buzz me at karthikeyanmp@maargasystems.com if you have any clarifications, suggestions or discussions. Waiting to hear from you 🙂