Search code examples
parametersxpagesagentxpages-ssjs

calling a LS agent from SSJS


I'm having trouble passing a param from a SSJS action to a LS backend agent. In the SSJS i have this code:

var doc:NotesDocument = database.createDocument();
doc.appendItemValue("DeleteDocs",arr);
doc.appendItemValue("Form","frmWFSDeleteDocs");
var UNID = doc.getUniversalID();
dBar.info(UNID, "Doc ID");
doc.appendItemValue("ParentID",UNID);
doc.save();
var agent:NotesAgent = database.getAgent("WFSDeleteDocs");
dBar.info(doc.getItemValueString("ParentID"));
agent.runOnServer(doc.getItemValueString("ParentID"))

The UNID that is displayed in the dBar info is the correct document UNID. In my LS agent I have this code:

Set agent = s.CurrentAgent    
NoteId = agent.ParameterDocID 
Print "Notes Param " + NoteID
Set thisDoc = db.Getdocumentbyunid(NoteId)

In the log the NoteID is not the UNID but HTTP Server: Agent printing: Notes Param 5A3439

and of course the getDocumentbyunid fails. My understand looking at the help is that this should be the same value as I insert as the param in the runOnserver. Am I missing something


Solution

  • You need to run the agent using agent.run(<note id>). This will transfer the note id to the agent and you can then read the note id using agent.getParameterDocID().

    So, in your case do this in SSJS:

    var agent:NotesAgent = database.getAgent("WFSDeleteDocs");
    agent.run(doc.getItemValueString("ParentID"))
    

    Update: If you want to run the agent as signer, use sessionAsSigner:

    var backendDb:NotesDatabase = sessionAsSigner.getDatabase(session.getServerName(), database.getFilePath());
    var agent:NotesAgent = backendDb.getAgent("WFSDeleteDocs");
    agent.run(doc.getItemValueString("ParentID"))