Examples: Accessing agents using Java

AgentGetParameterDocID

  1. This agent sends a memo to the managers of the current database containing the name and comment of each agent in the database.
import lotus.domino.*;
import java.util.Vector;
public class JavaAgent extends AgentBase {
  public void NotesMain() {
    try {
      Session session = getSession();
      AgentContext agentContext = 
          session.getAgentContext();
      // (Your code goes here) 
      Database db = agentContext.getCurrentDatabase();
      Document doc = db.createDocument();
      RichTextItem rti = 
           doc.createRichTextItem("Body");
      Vector agents = db.getAgents();
      for (int i=0; i<agents.size(); i++)
      {
        Agent agent = (Agent)agents.elementAt(i);
        if (agent.isPublic() & agent.isEnabled())
        {
          rti.appendText(agent.getName());
          rti.addTab(2);
          rti.appendText(agent.getComment());
          rti.addNewLine(1);
        }
      }
      doc.appendItemValue("Subject",
      "Shared agent summary for " + db.getTitle());
      doc.send(false, db.getManagers());
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
}
  1. This example shows an agent that gets the NoteID of a document using the runOnServer method via another agent that executes the getParameterDocID method.
import lotus.domino.*;
import java.util.*;
public class JavaAgent extends AgentBase {
    public void NotesMain() {
        try {
        	Session session = getSession();
        	AgentContext agentContext = session.getAgentContext();
        	// (Your code goes here) 
         Database db = agentContext.getCurrentDatabase();
        	Agent ag1 = agentContext.getCurrentAgent();
        	Agent ag2 = db.getAgent("AgentGetParameterDocID");
        	Document doc = db.createDocument();
        	doc.replaceItemValue("AgentList", ag1.getName() + 
            	" performing agent.run(NoteID)");
        	doc.save(true,true);
        	String paramid = doc.getNoteID();
        	ag2.runOnServer(paramid);
        	doc.recycle();
        	Document doc2 = db.getDocumentByID(paramid);
        	Vector v = doc2.getItemValue("AgentList");
        	String sAgList = (String)v.elementAt(0);
        	System.out.println("Agent calling sequence: " + 
                	sAgList );
        	doc2.remove(true);
         } catch(Exception e) {
        	e.printStackTrace();
    	}
    }
}
 

AgentGetParameterDocID

import lotus.domino.*;
import java.util.*;
public class JavaAgent extends AgentBase {
    public void NotesMain() {
    	try {
        	Session session = getSession();
        	AgentContext agentContext = session.getAgentContext();
        	Database db = agentContext.getCurrentDatabase();
        	Agent ag1 = agentContext.getCurrentAgent();
        	String paramid =  ag1.getParameterDocID();
        	Document doc = db.getDocumentByID(paramid);
        	Vector v = doc.getItemValue("AgentList");
        	String sAgList = (String)v.elementAt(0);
        	doc.replaceItemValue("AgentList", sAgList +
            	" > " + ag1.getName());
        	doc.save(true,true);
        	} catch(Exception e) {
        	e.printStackTrace();
    	}
    }
}