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

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.

Project in Review - Part 3: What didn't work

Of course, not everything was an unmitigated success. I tried many things that didn't work out. Much of which I've removed and forgotten about, but a few things remain - either scarred into my psyche or woven too deeply to fix. What didn't work Storing my entire configuration in application.properties Using properties files is great. It let me get configuration out of a profile document and into something much easier to edit - particularly configuration that users will never see or maintain (and thus there is no need for an interface for). But I took it too far. The paths to the other databases are there, and that's good. But view aliases are also there, and that was a mistake. I already have a ViewDefinition enum that describes each view and all the information I need to know about it. I could have set view names there, but instead I'm reading them from the application config. I can change where a view is pointing without having to go into my code. Except of co