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(
{method: "get",
onComplete: processValue

function processValue(xmldoc){

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.


Unknown said...

Hi! I'm tried your example but can't seem to catch what my agent returns. alert("Response: " + xmldoc.ResponseText) says undefined. I also tried var info = xmldoc.get('response') but info turns out null. Can you help me?

Unknown said...

Yes, I realize this is a very old post but I can't find much documentation on the subject.

William Beh said...

You can try to open the URL directly and see what it return. That should verify if your agent returns correct info.

Unknown said...

Hey! I wasn't expecting an answer. I'm pleasantly surprised :)
Opening the URL directly was the first thing I tried but I ended up stuck in a page that said "Agent run", tried error handling and it was returning err code 0 in line 0 and in the server log I could see that it was getting the info.
I ended up hitting the net again and finally found this(*) resource and made it work with the second example.
Anyway, I just wanted to say thank you for your time and you have a nice blog going on. Take care!