Solved

Querying received e-mails from an agent

Posted on 2014-12-04
7
207 Views
Last Modified: 2014-12-04
Hi,

I'm new to Lotus Notes development and I'm trying to write an agent that when triggered searches a mail database of any received e-mails with a specified time range.

I figured out how to query by date range but I don't know how to limit the search to received e-mails only (e.g. no drafts, not sent items, etc.).

Any help would be greatly appreciated.

Thank you in advance.

Alex
0
Comment
Question by:fhios
  • 4
  • 3
7 Comments
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 40480837
How do you do the search, which method do you use: NotesDatabase.Search, NotesDatabase.FTSearch, NotesView.FTSearch, or do you use a view that your code traverses from beginning to end, maybe with a GetElementsByKey with a date?

In all cases: check the Sent view in the mail database, more precisely the Select statement. That's what you need to copy to your Search. If you use FTSearch, the syntax is different but the same can be accomplished (usually).

Could you show us some leg, er... some code of the agent? Thanks.
0
 

Author Comment

by:fhios
ID: 40480942
Here is a snippet of the code in the agent.

The logic within the callback marks the document as processed by calling
context.updateProcessedDoc(document);

Open in new window

.

	public <T> int withEachDocumentFromBatch(AgentContext context, AgentLog log, DocumentCallback<T> callback) throws Exception {
		int processedCount = 0;
		DocumentCollection unprocessedDocuments = context.unprocessedSearch([NEED TO FIGURE OUT THE QUERY HERE]);
		log.info("Documents found in 'batch' mode");

		int count = (unprocessedDocuments != null) ? unprocessedDocuments.getCount() : 0;
		log.info("Found " + count + " documents");

		for (int i = 1; i <= count; i++) {
			try {
				Document document = unprocessedDocuments.getNthDocument(i);
				try {
					callback.call(context, document);
				} finally {
					recycle(log, document);
				}
				// printDebugInfo(document);
				processedCount++;
			} catch (Throwable e) {
				log.error("Unable to process document number [" + i + "]", e);
			}
		}
		return processedCount;
	}

Open in new window


Thanks
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 40481298
I have some difficulty in understanding your code (Java isn't one of my stronger points, I'm afraid, but I'm working on it). Assuming you are developing a Java Agent using the Domino Designer, there are some elements I don't know: AgentLog log (your own instead of the Log class?), the unprocessedSearch method (your invention?), recycle() which probably should be document.recycle(), but that's not the real issue here.

Here's a simple agent in Java that searches a database using a linear search method:
http://www-01.ibm.com/support/knowledgecenter/SSVRGU_9.0.1/com.ibm.designer.domino.main.doc/H_EXAMPLES_SEARCH_METHOD_JAVA.html

To access every document in a collection, you'd better use the getFirstDocument and getNextDocument methods. getNthDocument is slower, usually, but it should work all-right. An example as well:
http://www-01.ibm.com/support/knowledgecenter/SSVRGU_9.0.1/com.ibm.designer.domino.main.doc/H_EXAMPLES_GETNEXTDOCUMENT_METHOD_COLLECTION_JAVA.html

The Select statement in the Sent view is
SELECT DeliveredDate = "" & PostedDate != "" & !(@IsMember("S"; ExcludeFromView))
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

Author Comment

by:fhios
ID: 40481775
Hi,

AgentLog is my own class that uses org.apache.log4j.net.SocketAppender to send the logs to a centralised server.

recycle() is a defensive version of document.recycle() that handles exceptions more gracefully.

The getFirstDocument and getNextDocument methods, unfortunately they don't seem to be working from JAVA.

unprocessedSearch is a method in the Lotus Notes JAVA API that takes as a first parameter a "A Lotus Domino formula that defines the selection criteria."

Looking at the formula from the sent view, I would need to invert the criteria because I need to search for incoming e-mails rather than outgoing. Does the following make sense?

SELECT DeliveredDate != "" & PostedDate = "" & (@IsMember("S"; ExcludeFromView))

Open in new window


What does (@IsMember("S"; ExcludeFromView)) do?

Thanks
0
 
LVL 46

Accepted Solution

by:
Sjef Bosman earned 500 total points
ID: 40481824
Now why on earth did I take the Sent view... Goodness, what was I thinking? Never mind, humble apologies.

There are 2 ways to get received mails, but the approach is different, and so might be the number of mails found. If you want really all mails received, there is this query (source: Richard Schwarz):

SELECT ( @IsNotMember("A"; ExcludeFromView) & IsMailStationery != 1 & Form != "Group" & Form != "Person" ) & !( DeliveredDate = "" & PostedDate != "" & !(@IsMember("S"; ExcludeFromView)) )

If you can do with the messages that are in the Inbox at the moment of the search (the Inbox is a folder, mails can be moved out of this folder to other folders), you don't need to do a search: you can just open the Inbox view and walk through it (using getFirst and getNext calls).

The IsMember "S" tests if the mail is in the Sent folder.

Strange that getFirstDocument and getNextDocument don't work, it's the first time I hear of this.

And a warning. Do not rely too much on "unprocessed documents": once processed they can revert to unprocessed easily, e.g. when you change the agent code and save it. Or when someone switches the agent off and back on (when scheduled). It might be better to use a profile document somewhere, where you save the current cutoff date and time. You need another agent (can be in Formula language) if you want to change that cutoff date manually.
0
 

Author Comment

by:fhios
ID: 40481999
Thank you, I'll play around following your suggestions.
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 40482069
I'd love to hear from you when you cracked it. Thanks!
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article covers general Notes 8.5 troubleshooting information including recreating the Notes\Data folder.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question