Examples: SavedData property (AgentContext - Java™)
This agent uses the save data record to find the maximum weekly sales.The data records have Weeky_Sales and Name fields. The saved data record has MaxSales and MaxSales_Salesperson fields. The data record fields replace the agent record fields whenever Weekly_Sales is greater than MaxSales.
import lotus.domino.*;
import java.util.Vector;
import java.lang.Float;
public class JavaAgent extends AgentBase {
public void NotesMain() {
try {
Session session = getSession();
AgentContext agentContext =
session.getAgentContext();
// (Your code goes here)
Document agentDoc = agentContext.getSavedData();
Database db = agentContext.getCurrentDatabase();
View view = db.getView("Weekly Sales");
Document doc = view.getFirstDocument();
if (!agentDoc.hasItem("MaxSales"))
{
agentDoc.replaceItemValue("MaxSales", "0");
agentDoc.save(true, true);
agentDoc = agentContext.getSavedData();
}
while (doc != null)
{
Vector mvalue = agentDoc.getItemValue("MaxSales");
Vector wvalue = doc.getItemValue("Weekly_Sales");
Float maxSales =
new Float((String)mvalue.firstElement());
Float weeklySales = new
Float((String)wvalue.firstElement());
if (weeklySales.floatValue() >
maxSales.floatValue())
{
agentDoc.replaceItemValue
("MaxSales", doc.getItemValue("Weekly_Sales"));
agentDoc.replaceItemValue
("MaxSales_Salesperson", doc.getItemValue("Name"));
}
agentDoc.save(true, true);
System.out.println("Best salesperson to date is " +
agentDoc.getItemValue("MaxSales_Salesperson") +
" who sold $" + agentDoc.getItemValue("MaxSales"));
}
} catch(Exception e) {
e.printStackTrace();
}
}
}