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,


//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) {

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


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 :)


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


    1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training from India . Nowadays Java has tons of job opportunities on various vertical industry.

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

    just curious!!!

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

  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 :)

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

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

    doc is in memory document on XPage

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


    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 :)

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

    Working with in memory documents