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

XPages Application Framework (Part 1?)

Note: I changed projects and priorities after my last POI article, resulting in a long hiatus. I anticipate using that framework on my current project and will likely refine and complete my related article series. For the past several months, I've been fortunate enough to lead a project overhaul from traditional Domino Webapp to XPages. I had a few goals in mind, but the top three were integrating Bootstrap, embracing MVC principles, and eliminating all SSJS . I imagine the motivation for Bootstrap integration is self-explanatory. There is an OpenNTF project out there called Bootstrap4XPages. I didn't use that for a number of reasons - the main being a policy in the current environment. But you may ask yourself (if you didn't follow that link), why so much hatred for SSJS? I have a litany of reasons. It impacts readability of the XPage source; it hurts maintainability when logic is scattered across dozens of XPages, custom controls, and script libraries; it mixes log...

Killing SSJS - Passing a parameter using Expression Language - Part 2

In part 1, I showed how you can expand the DataObject implementation to allow you to pass a parameter to a method and return the result. It's not a perfectly elegant solution, and it has some limitations, but it's a very solid technique and not at all difficult to implement or maintain. But I kept running into roadblocks. What if I have to compute part of the expression? What if I have to get a value based on another parameterized getter? You can't compute part of an EL expression in your EL. So after some research and experimentation, I created the GetMap. This isn't a new concept - it was developed for JSF 1.1 (which is what XPages is built on) but the rest of the world has moved on to JSF 2.0 and so some of these older techniques can be a little tricky to unearth since this hasn't been an issue in the JSF world for years.

Rows per page selection: Part 2

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. Problem 2: When the page refreshed, the combobox value always reverted back to the default It turns out that the Integer value stored in the viewScope doesn't get run through the converter back to a string before being compared to options. I needed a way to calculate the option values so that they would be Integers. This is also not the first time I've run into issues where I need a value to be of a different type, and I see questions like this on StackOverflow from time to time. I attempted a few minor things before I realized I needed to break out my old stand-by, the GetMap . The GetMap is just a fake Map implementation that takes the key and transforms it or uses it to look up some other value. In this case, we are doing the f...