Monday, April 17, 2006

Using AJAX to run LN agent from web

Sometimes we need to run an agent to process certain field in our web form. For example, when user select a name, you want to retrieve their company information. In Lotus Notes, we can use on "Refresh fields on keyword change". This works but it will do a document refresh. If you are working on a large form, it may take sometime to reload the form.
We can avoid the page reload by using an AJAX call to the agent. Prototype.js made calling an AJAX function easier.

var myAjax = new Ajax.Request(
"http://www.server.com/db.nsf/getDetail?OpenAgent",
{method: "get",
parameters:"name=myname",
onComplete: processValue
});

function processValue(xmldoc){
alert(xmldoc.responseText);
//processing
}


This would call the Agent "getDetail" in db.nsf using "GET" method. You can pass parameters from the current website. When the AJAX call completes, it will call the function processValue(). You can process the return values (in XML or other format).

On the "getDetail" agent, you can retrieve the parameter and do your processing.

Sub Initialize
Dim session As New NotesSession
Dim doc As NotesDocument
Dim param as String

Set doc = session.DocumentContext
'get the parameters
param = doc.Request_Content(0)

Print |Content-type:text/xml|
Print |<?xml version="1.0" encoding="UTF-8"?><data>|
'Do your processing and return the value..
'eg print |<company>company name</company>|
Print |</data>|

End Sub


Your agent can return just plaing text. Or from the example above, it returns a xml document. DOM can be used to retrieve the data in the XML document. You can specify in the agent what type of content-type to return, in this case, "text/xml".w
Print |Content-type:text/xml|

There's a lot of article comparing the use JSON (JavaScript Object Notation) vs. XML. I will go into more detail use of JSON instead of XML.

No comments: