0

I want to run a Java agent from node.js via the proton interface. Sadly I can't get the Agent Context to work

Node.js Code

async function callEvalAgent(query) {
  const agent = await db.useAgent({ name: "search" });
  console.log("got the agent");

  const requestDocUNID = await db.createDocument({
      document: {
        Form: "searchRequest",
        query
      }
  });
  console.log("queryDoc created");
  console.log(requestDocUNID);

  await agent.run({
    selection: { search: { query: "Form = 'document'" } },
    context: { unid: requestDocUNID }
  });

  ...
}

Output:

got the agent
queryDoc created
B72CA8819EDA0691C1258592003BFBE5

...

Agent Code

public class JavaAgent extends AgentBase {

    public void NotesMain() {

      try {
          Session session = getSession();
          AgentContext agentContext = session.getAgentContext();
          Document requestDoc = agentContext.getDocumentContext();
          String query = requestDoc.getItemValueString("query");
          
         ...

      } catch(Exception e) {
          System.out.println("bla");
          e.printStackTrace();
      }
   }
}

When running the code I get a NullPointerException at

String query = requestDoc.getItemValueString("query");

on the Domino Server due to the agentContext being null. If I check on it afterwards by hand the document with the given UNID is present in my view.

What did I do wrong? Used the same approach as in the HCL Example here

Marvin Rabe
  • 4,141
  • 3
  • 25
  • 43
J Mers
  • 67
  • 6
  • I don't see where query is defined in your Node.js code. Can you include that as well? – ddumont Jun 25 '20 at 12:25
  • the node.js code is working fine, problem occures in the Java code. query is given as a param to the function, so can be anything. updated the Code above – J Mers Jun 25 '20 at 12:33
  • It looks like the query var isn't defined, and if you're not adding any value to the document, there won't be anything to read in the agent. I just wanted to make sure that it was defined. – ddumont Jun 25 '20 at 16:00
  • Assuming that's all working fine, I'm going to go check on the java apis for context doc and check our tests. – ddumont Jun 25 '20 at 16:02
  • Thanks! doublechecked a document in the database just to make sure. Creation was successfull, item "query" with valid query as content is present – J Mers Jun 25 '20 at 17:09

1 Answers1

1

The documentation should be more clear.

The documentation should say that the optional document that is provided to the Agent is available to the agent using the ParameterDocID.

In a Java agent do something like this:

    final Session session = getSession();
    final Database db = session.getCurrentDatabase();
    final AgentContext agentContext = session.getAgentContext();
    final Agent agent = agentContext.getCurrentAgent();
    final String noteid = agent.getParameterDocID();
    final Document context = db.getDocumentByID(noteid);

In a LotusScript agent do something like this:

    Dim session As New NotesSession
    Dim agent As NotesAgent
    Dim db As NotesDatabase
    Dim context As NotesDocument

    Set agent = session.CurrentAgent
    Set db = session.CurrentDatabase

    Set context = db.GetDocumentByID(agent.ParameterDocID)