Subject: Possible solution
Hi
I had the same issue where I was trying to use legacy LS code to perform the bulk of the work.
It turned out that I needed to recycle the in memory notesDocument and the retrieve it back from the database, after the LS has worked on it.
This is quiet normal and you DO NOT need to create a temporary document to achieve this. For me I had the following constraints:
With Domino 8.5.2 onwards there is a function agent.runWithDocumentContext(doc) that will perform this, but the LS agent must have ‘Run as web user’ enabled.
In my case, the web user didn’t have/ will never have the sufficient authority access the relevant databases.
Domino will not allow the programmer to recycle a notesDocument based on the current database source. So I had to create a temporary notesdocument on the database, copy across all the fields, save it, and then pass that document to the agent top process. After the agent has processed the temporary document, my SSJS code retrieves the document back from the database, copy all the fields (now modified by the LS agent) back to the in memory document; then remove the temporary document.
var doc:NotesDocument = null;
var docToProcess:NotesDocument = null;
var ag:NotesAgent = null;
try{
doc = document1.getDocument(true);
//Where document1 is the XSPDocument (currentDocument).
doc.save();
//This (below) is only valid if you are attemting to process the in Memory Notes XSp document, as Domino will not let you recycle the backend NotesDocument.
//So we must go through the process of creating a temorary document then process that document.
docToProcess = database.createDocument();
doc.copyAllItems(docToProcess, true);
docToProcess.save();
var docUNID:String = docToProcess.getUniversalID();
var docNoteID:String = docToProcess.getNoteID();
ag = database.getAgent("(agTest)");
if(ag)
{
ag.runOnServer(docNoteID);
docToProcess.recycle();
docToProcess = database.getDocumentByUNID(docUNID);
if(docToProcess)
{
docToProcess.copyAllItems(doc, true);
doc.save();
print(doc.getItemValueString("name"));
// here it will show the new value
docToProcess.remove(true);
docToProcess.recycle();
// the finally in the try/catch will removed the temp document, or you can do it here.
return true;
}
}else
{
//Do some error handling here.
return false;
//finally in the try/catch will recycle all the domino objects used.
}
/* agTest is in lotuscript and change the value of field "name" of a document */
}catch(e)
{
}finally
{
if(docToProcess)
{
docToProcess.remove(true);
docToProcess.recycle();
}
if(ag) ag.recycle();
if(doc) doc.recycle();
}