Home > Ask the Domino Experts > LotusScript Questions & Answers > Creating an 'After new mail is delivered' agent
Ask The Domino Expert: Questions & Answers
EMAIL THIS

Creating an 'After new mail is delivered' agent

Cregg Hardwick EXPERT RESPONSE FROM: Cregg Hardwick

Pose a Question
Other Domino Categories
Meet all Domino Experts
Become an Expert for this site


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


>
QUESTION POSED ON: 06 February 2006
I have had several issues with Lotus Notes rules not working. I have checked the rules and they are fine. Is there a way to do this with an agent? For example, I want to search the Inbox for any e-mail that contains "Delivery failure" in the subject line, and move it to a certain folder. It seems simple, but I am not a programmer and I would like to know if this is possible.

>
EXPERT RESPONSE

Rules should work for what you are describing. However, rule processing has to be enabled on the server, and sometimes you need to disable and re-enable your rules.

The biggest problem with rules (from my perspective as a developer) is that, since they are executed by the mail router, we are dependent on the administrators to figure out why they don't work. That's OK, but it's always easier to debug something you can actually step through in the debugger. Indeed, that's one of several reasons that our group has always promoted using LotusScript instead of Formula Language in most situations.

At any rate, you certainly can create an "After new mail is delivered" agent to do the same thing and at least you can debug that agent yourself. I should point out, however, that such an agent will still run on the server, which means you need to keep a few things in mind. First, you may or may not have access to run agents on the server. Second, such an agent cannot access the user interface -- no references to NotesUi objects of any kind.

When you create an "After new mail is delivered" agent, all newly delivered mail will be presented to the agent in the NotesDatabases's "UnprocessedDocuments" collection. Therefore, the following code (pasted into the agent's "initialize" event) will do the job:

Dim session As New NotesSession
Dim db As notesdatabase
Set db =session.CurrentDatabase
Dim failures As NotesDocumentCollection
Set failures=db.UnprocessedSearch({Form="NonDelivery Report"},Nothing,0) 
Call failures.PutAllInFolder("Delivery failures")

Whenever the agent runs, it will search the collection of any mail received since the last time it ran. Any matching documents will be placed in the "Delivery failures" folder, which will be created from the default view unless you have created it ahead of time (You may also choose to call failures.RemoveAllFromFolder to remove these documents from the $Inbox folder where the mail router places all new mail).

The selection formula in the "UnprocessedSearch" call is a Formula Language search -- the same language used when composing a view. I have selected delivery failure messages by the form name that the Lotus Notes mail router gives them, but you can select any set of documents for which you can write a selection formula.

Indeed, I used this simple concept to write an entire enterprise mail-scan tool that enforced our corporate retention policy, deleted large attachments, removed slam and could be used to quickly retract e-mails sent by mistake.

There is one thing I'd like to point out about such an agent. Just as Lotus Notes offers multiple ways to code an action (LotusScript, Formula Language and "Simple Actions"), it also offers multiple ways to select documents. In the case of an agent, you can use one of the database search or FTsearch methods, or you can use the little built in selection wizard on the agent design page. When you use the latter, you are actually creating a full-text search, the results of which will be returned to the agent through the UnprocessedDocuments collection.

My advice is NEVER to use this feature. If you want to perform a full-text search, then code it explicitly by using the FT search syntax in conjunction with the NotesDatabase FTSearch or UnprocessedFTSearch methods.

If you use the wizard, it's hard for someone later reviewing your code to clearly see exactly what documents you are selecting, and they may not realize a FT search is being performed. Indeed, many developers and administrators are not aware of this, so let me reiterate -- if you use the document selection wizard of an agent, you are creating a full-text search.

If the Lotus Notes database does not, in fact, have a full-text index, then every single time the agent runs, a message will be written to the server log indicating that "Warning: Agent is performing full- text operations on database 'Mailscan.nsf' which is not full text indexed. This is extremely inefficient."

To understand how inefficient this is, consider that running a db.search is equivalent to the first time you open a view with a single column. Performing a FT search does the same amount of work as building the full text index for the database (and uses the same amount of disk space).

In both cases, the work is done each time the agent runs. So while you might well elect to perform a db.search in order to provide flexibility or avoid having another view for the indexer to maintain, in general, if you need to perform a FT search, you need to build a FT index for the database and allow the FT Indexer to keep it current for you.

That's my little digression on document selection. For details on writing this sort of agent, read Julie Kadashevich's classic "Managing the agent manager" and other related articles available under "technical articles" (formerly LDD Today) in IBM's developer Works site.


Do you have comments on this Ask the Expert Q&A? Let us know.


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
LotusScript
Can I use LotusScript to merge cells in a Microsoft Word table?
Modifying LotusScript code for date and time handling
Use DXL utilities for advanced replication settings in Notes
Use LotusScript to create encrypted replicas
Write a LotusScript agent to automate file attachments
Troubleshooting a scheduled agent not running on a Global Notes Architecture (GNA) server
Setting up local replication of a Lotus Notes database for offline employees
Creating a link on an HTML page to a Microsoft Word attachment in a Lotus Notes database
LotusScript to extract and move attachments to a Lotus Notes mailbox or file folder
Copying a rich-text field with attachments to a Lotus Notes document

LotusScript
Display a custom message box using a LotusScript-generated button
Can I use LotusScript to merge cells in a Microsoft Word table?
Debug Lotus Notes documents using extracted data
Extracting attachments from a Lotus Notes rich-text field
Programmatically replace the design of Lotus Notes databases
Reading a binary field in an Oracle database with LotusScript
LotusScript equivalent of @Picklist for Lotus Notes
Launch large attachments within an email from a Notes database
How to find files on a hard drive or mapped network with LotusScript
Update the ACL from the Roles view with LotusScript

Lotus Notes Domino Agents
Verify scheduled agent status with Domino Extensible Language (DXL)
Top 10 Lotus Notes Domino programming and development tips of 2007
How to export data from a Lotus Notes database to a CSV file
Must-know Lotus Notes Domino agents -- 10 tips in 10 minutes
Enable or disable scheduled agents without opening the Lotus Notes database design
Creating custom views in Lotus Notes databases
Editing fields in a Lotus Notes view with Ajax
Troubleshooting a scheduled agent not running on a Global Notes Architecture (GNA) server
Can I stop Lotus Domino Server from transforming embedded images into attachments?
How to automatically create a backup copy of your Domino Directory

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice

HomeNewsTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersDomino IT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 1999 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts