Tuesday, January 11, 2011

Triggering lotus script agents and obtaining their response on the XPage using Server side javascript - An XMLHTTPRequest equivalent

Triggering agents from server side javascript in XPages is easy. How ever obtaining the response from the agents once the lotus script agent completes execution and displaying the same on the xpage was always a challenge to me. The following is a way helped me achieve it,

/***************************************************/
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("Agent URL");
//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("output").setValue(res);

/***************************************************/


This attempt was successful and I was able to recieve responses on the XPage but is a Synchronous way. Hence you may have to wait until the agent gets executed and the response is obtained.

Am yet to find the same to do in a Asynchronous way.

Hope this helps :)

7 comments:

  1. I guess your agent is printing out the content using "print"

    ReplyDelete
  2. Yep thats right... is there an other way to do it!!!!!

    just curious!!!

    ReplyDelete
  3. why do you need to get data from and agent?

    ReplyDelete
  4. When XPage enabling existing applications, it is faster to reuse the existing lotusscript functions right...!!! So I call them into XPages by converting them into agents and obtain the specific funtion's return value as response.
    The end user's use both 7 and 8.5.2... so i am supposed to minimize duplications to reduce complications in maintainence..

    Hope I make sense :)

    ReplyDelete
  5. what if I want to write multiple values back to the xpage?

    eg
    doc.FirstName = apCardScanData(4)
    doc.LastName = apCardScanData(4)

    doc is in memory document on XPage

    ReplyDelete
  6. I had this question 6 months back. The following stuff helped me resolve it.

    http://www-10.lotus.com/ldd/ddwiki.nsf/dx/XPages_and_Calling_Agents_Using_an_In-Memory_Document

    Try it out and let me know if you dont get it.... Possibly I will do a simple post on this soon

    Hope that helps :)

    ReplyDelete
  7. @quintessens : Please check the following post that I have added to answer your query

    Working with in memory documents

    ReplyDelete