Examples: Agents
- This LotusScript code writes a value to the Category field based on the value of the TotalSales field of each document in the database. Compare with Examples 2 and 3, which use Java and formula solutions. The script example requires more lines of code than the formula solution but includes the algorithm for finding the documents being processed.
Sub Initialize Dim session As New NotesSession Dim db As NotesDatabase Dim dc As NotesDocumentCollection Dim doc As NotesDocument Set db = session.CurrentDatabase Set dc = db.AllDocuments Set doc = dc.GetFirstDocument While Not(doc Is Nothing) category = doc.Category totalSales = doc.TotalSales Select Case totalSales(0) Case Is >= 200000 : category(0) = "Above Quota" Case Is >= 100000 : category(0) = "OK" Case Else : category(0) = "Below Quota" End Select doc.Category = category Call doc.Save(True, False) Set doc = dc.GetNextDocument(doc) Wend End Sub
- This Java agent writes a value to the Category field based on the value of the TotalSales field of each document in the database. Compare with Examples 1 and 3, which use LotusScript and formula solutions. As with LotusScript, the Java code includes the algorithm for finding the documents being processed.
import lotus.domino.*; public class JavaAgent extends AgentBase { public void NotesMain() { try { Session session = getSession(); AgentContext agentContext = session.getAgentContext(); // (Your code goes here) Database db = agentContext.getCurrentDatabase(); DocumentCollection dc = db.getAllDocuments(); Document doc = dc.getFirstDocument(); while (doc != null) { double totalSales = doc.getItemValueDouble("TotalSales"); if (totalSales >= 200000) doc.replaceItemValue("Category", "Above quota"); else if (totalSales >= 100000) doc.replaceItemValue("Category", "OK"); else doc.replaceItemValue("Category", "Below quota"); doc.save(true, false); doc = dc.getNextDocument(); } } catch(Exception e) { e.printStackTrace(); } } }
- This formula writes a value to the Category field based on the value of the TotalSales field of each document in the database, assuming that all documents are selected for processing. Compare with Examples 1 and 2, which use LotusScript and Java solutions. The formula executes once on each document selected by combining outside criteria with the SELECT statement. Data declarations are implicit and formula syntax is cryptic making for compact source code.
FIELD Category := @If(TotalSales >= 200000; "Above Quota"; TotalSales >= 100000; "OK"; "Below Quota"); SELECT @All
- This formula writes a value to the Category of selected documents based on the value of the TotalSales field. The SELECT statement, if it is not SELECT @All, must precede the statements in the formula that it applies to.
SELECT TotalSales >= 200000; FIELD Category := "Above Quota"
- This LotusScript code finds the sum of all OrderTotal fields in a database for one day, and writes a new record to the database containing the daily total. Each record in the database has OrderNumber, Date, and OrderTotal fields. The script finds all the documents in the database, then uses a loop and a comparison of dates to limit processing to today's documents. For each document, the script adds the OrderTotal to a dailyTotal variable. The script places the words "DAILY TOTAL" in the OrderNumber field for the document that it writes, and places the dailyTotal value in the OrderTotal field.
Sub Initialize Dim session As New NotesSession Dim db As NotesDatabase Dim dc As NotesDocumentCollection Dim doc As NotesDocument Dim dateDate As New NotesDateTime("") Dim dateToday As New NotesDateTime("Today") Set db = session.CurrentDatabase Set dc = db.AllDocuments dailyTotal = 0 Set doc = dc.GetFirstDocument While Not(doc Is Nothing) odate = doc.Date dn = Datenumber(Year(odate(0)), _ Month(odate(0)), Day(odate(0))) orderNumber = doc.OrderNumber If dn = Today _ And orderNumber(0) <> "DAILY TOTAL" Then orderTotal = doc.OrderTotal dailyTotal = dailyTotal + orderTotal(0) End If Set doc = dc.GetNextDocument(doc) Wend Dim docNew As New NotesDocument(db) Set itm = _ docNew.AppendItemValue("OrderNumber", "DAILY TOTAL") Set itm = _ docNew.AppendItemValue("OrderTotal", dailyTotal) Set itm = _ docNew.AppendItemValue("Date", Date$) Call docNew.Save(True, False) End Sub