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.
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.
Here is what my LS agent looks like: (Note: This agent must have Run as Web User checked in the security tab.)
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;
Option PublicIt is that simple. The in-memory document is never saved so there is nothing to clean up afterward.
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
Excellent post! you have save me a lot of time.
ReplyDeleteCan 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
I just noticed I never responded to this. Thank you for the comment, this is a very good point.
DeleteHey, 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.
ReplyDeleteHi 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.
DeleteThe 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.
Hi, Gary. Why do you use the docPass? If I alredy have the document in database, may I use it?
ReplyDeleteMy 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);
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