Skip to main content

Pass data between XPages and existing LS scripts

I'm working on modernizing a fairly hefty application with a lot of existing script libraries which we want to leverage within the XPages environment. Here is a technique that works very well. First, create an in-memory document in SSJS. We can set any input values needed for the back end. Then we pass that document to a LS Agent which can work it's magic using the values set in SSJS and use the same document to return values back to the XPage.

Here is how it works in detail:

Here is a button I created. This same thing could be done on the querySave event, which would allow the save to abort based on errors returned from the LS Agent.

var db = session.getDatabase(@Subset(@DbName(), 1), @Subset(@DbName(), -1));
var thisDoc = currentDocument;
var docPass = db.createDocument();

//Pass input parameters in here or pass the entire document
thisDoc.getDocument().copyAllItems(docPass, true);

docPass.replaceItemValue("Processed", "0");
var agent = db.getAgent("XDataExchange");
agent.runWithDocumentContext(docPass);
if (docPass.getItemValueString("Processed") != "1") {
print("Processing not done");
} else {

//Copy all items from the back-end document to the UI document.
var items:java.util.Vector = docPass.getItems();
var iterator = items.iterator();
while (iterator.hasNext()) {
var item:NotesItem = iterator.next();
thisDoc.replaceItemValue(item.getName(), item.getValues());
}

// Or do it one item at a time
// thisDoc.replaceItemValue("mtgSubject", docPass.getItemValueString("mtgSubject"));
print("Processing complete");
}
//Clean up
delete docPass;
return;
Here is what my LS agent looks like: (Note: This agent must have Run as Web User checked in the security tab.)
Option Public
Option Declare

Sub Initialize
REM This agents reads the passed in doc for parameters,
REM and writes the result back to the document.

Dim s As New NotesSession
Dim db As NotesDatabase
Dim agent As NotesAgent
Dim doc As NotesDocument

Set db = s.CurrentDatabase
Set agent = s.CurrentAgent
Set doc = s.Documentcontext
If doc Is Nothing Then
Print "Oops, doc is nothing"
Else
Call processDoc(db, doc)
End If
End Sub

Sub processDoc(db As NotesDatabase, doc As NotesDocument)
Call doc.Replaceitemvalue("Processed", "1")
Call doc.Replaceitemvalue("mtgSubject", "Subject")
End Sub
It is that simple. The in-memory document is never saved so there is nothing to clean up afterward.

Comments

  1. Excellent post! you have save me a lot of time.

    Can I do a small Addition?

    if you change the line:
    from :
    thisDoc.getDocument().copyAllItems(docPass, true);
    to:
    thisDoc.getDocument(true).copyAllItems(docPass, true);

    The agent will have access to values the user has written in the in memory document. usefull if you need do some process in the agent based on the user input. :-)

    @sir_alx

    ReplyDelete
    Replies
    1. I just noticed I never responded to this. Thank you for the comment, this is a very good point.

      Delete
  2. Hey, Mr Forbis, where is the parameter handling in the LS agent? All I see is two fields writing hard-coded strings. This is a great project but looks like a piece is missing in the sample code.

    ReplyDelete
    Replies
    1. Hi Charles. The example is deliberately simplified. You could pass additional parameters with docPass.replaceItemValue() and then handle those values in the processDoc() function, and then you can remove them before returning back to the SSJS if desired.

      The idea I'm demonstrating here is that we can pass values to the LS agent on an in-memory document rather than having to save and later delete the document, potentially leading to a bunch of temporary documents down the road if removing them doesn't happen, for example if the LS agent fails.

      Delete
  3. Hi, Gary. Why do you use the docPass? If I alredy have the document in database, may I use it?
    My code is something like this:

    var db=sessionAsSigner.getDatabase(@Subset(@DbName(), 1), @Subset(@DbName(), -1));
    var thisDoc=currentDocument.getDocument(true);
    var doc=db.getDocumentByUNID(thisDoc.getUniversalID());

    //Pass input parameters in here or pass the entire document
    doc.copyAllItems(thisDoc, true);
    doc.replaceItemValue("user", context.getUser().getFullName());

    var agent=sessionAsSigner.getCurrentDatabase().getAgent("m_assinar")
    agent.runWithDocumentContext(doc);

    //Copy all items from the back-end document to the UI document.
    doc.copyAllItems(thisDoc, true);

    ReplyDelete
  4. I use docPass because I believe it is the cleanest and least error prone method. With my method, the current document is never populated with temporary values that don't need to be stored. Those can, of course, be cleaned up and your solution can work if that is what you prefer.

    ReplyDelete

Post a Comment

Popular posts from this blog

Rows per page selection: Part 1

I was asked to create a control that would allow users to select the number of rows per page in a view/repeat control (the application uses both). It seemed simple at first, but I ran into a few issues that I thought I'd share the solutions to. First, lets start at the beginning. I went through the relevant design elements and set row="#{viewScope.tableRows}" , and I created an xp:comboBox with value="#{viewScope.tableRows}" and added items for 20, 30, 50, and 100, and I assigned it an onChange event handler that did a partial execution and partial refresh of a div containing the combo box, pager and the table. Then I started fixing all the problems. Problem 1: The combobox value was a string, but the rows parameter requires an integer. This was causing IllegalArgumentException / java.lang.String incompatible with java.lang.Integer. I added a NumberConverter, but this only slightly changed the exception message to java.lang.Long incompatible with java.lang....

Quick tip: Convert a number to String in EL

I just had a need to do this and a Google search didn't immediately turn up a solution. So I thought for a couple of minutes and came up with this: value="0#{numberVar}" This takes advantage of the way Java auto-converts objects to strings when doing a concatenation. So if your number is 13, Java EL turns this into new String("0"+13), which becomes "013". You can then strip off the leading zero or just parse the string back into a number.