Create a mail rule in script

Hi,

I want to create a mail rule using script when the database is open.

The script bellow verify if the Rules already exist and create if not.

  1. In the Database Script, I verify if the rule exist and if not, I create the document

  2. I pass the new Doc and I run the same code as the OK button in the New rules form. Call ButtonOKClient()

The rules get created but the problem is that it doesn’t work.

Thank You

Alexandre

Database SCRIPT (Initialize):

Call SCISetJunkRule()

Sub SCISetJunkRule:

Sub SCISetJunkRule()

'// Verify if rules exist

Dim m_Session As New notessession	

Dim m_Db As notesdatabase		

Dim ViewEntry As Notesviewentry

Dim m_RulesView As notesview	

Dim m_ViewCollection As NotesViewEntryCollection

Dim  m_RuleDoc As notesdocument	

Dim doc As NotesDocument



Set m_db = m_Session.currentdatabase

Set m_RulesView = m_Db.getview("Rules")



If m_RulesView Is Nothing Then Exit Sub

Set m_ViewCollection = m_RulesView.Allentries

If m_ViewCollection.count > 0 Then

	Set ViewEntry = m_ViewCollection.Getfirstentry

	While Not ViewEntry Is Nothing

		Set doc = ViewEntry.Document			

		If Instr(doc.ConditionList(0),"[SPAM]") <> 0 Then

			Exit Sub

		End If

		Set ViewEntry = m_ViewCollection.Getnextentry(  ViewEntry  )

	Wend 

End If	



'//Find Highest Rules

Dim Highest As Integer



If m_RulesView Is Nothing Then Exit Sub

Highest = 0

Set m_ViewCollection = m_RulesView.Allentries

If m_ViewCollection.count > 0 Then

	Set ViewEntry = m_ViewCollection.Getfirstentry

	While Not ViewEntry Is Nothing

		If highest < ViewEntry.Columnvalues( 1 ) Then

			Highest = Cint( ViewEntry.Columnvalues( 1 ) )

		End If

		Set ViewEntry = m_ViewCollection.Getnextentry(  ViewEntry  )

	Wend

End If

If Highest = 0 Then

	Highest = 1

End If

'// Create rules

Dim exclude(1) As String

Dim FilterFormulaItem As NotesItem	

Dim  m_CalProfile As NotesDocument

Set m_CalProfile = m_db.GetProfileDocument("CalendarProfile")

Set FilterFormulaItem = m_CalProfile.getfirstitem("$FilterFormula")



If m_CalProfile Is Nothing Then

	Exit Sub

End If



exclude(0)="A"

exclude(1)="D"



Set m_RuleDoc = m_Db.CreateDocument 

Call m_RuleDoc.ReplaceItemValue("Form","Mailrule")

Call m_RuleDoc.ReplaceItemValue("action","1")

Call m_RuleDoc.ReplaceItemValue("condition","1")

Call m_RuleDoc.ReplaceItemValue("logic","1")

Call m_RuleDoc.ReplaceItemValue("Type","1")

Call m_RuleDoc.ReplaceItemValue("TokActionList","1|1|($JunkMail)")

Call m_RuleDoc.ReplaceItemValue("tokConditionList","2|1|[SPAM]|0")

Call m_RuleDoc.ReplaceItemValue("inportance","1")

Call m_RuleDoc.ReplaceItemValue("importancecond","1")

Call m_RuleDoc.ReplaceItemValue("operator","0")

Call m_RuleDoc.ReplaceItemValue("ExpireDates","D")

Call m_RuleDoc.ReplaceItemValue("ExpireNumber","5")

Call m_RuleDoc.ReplaceItemValue("ConditionList", " Subject contains [SPAM]")

Call m_RuleDoc.ReplaceItemValue("ActionList"," move to folder ($JunkMail)")

Call m_RuleDoc.ReplaceItemValue("ExcludeFromView",exclude)

Call m_RuleDoc.ReplaceItemValue("$FilterFormula","")

Call m_RuleDoc.ReplaceItemValue("$NoPurge","")

Call m_RuleDoc.ReplaceItemValue("Enable", "1")

Call m_RuleDoc.ReplaceItemValue("PROTECTFROMARCHIVE", "1")

Call m_RuleDoc.ReplaceItemValue("OrderNum",Highest)				

Call m_RuleDoc.ComputeWithForm(False,False)	



Dim ws As New NotesUIWorkspace

Dim uidoc As notesuidocument

Set  uidoc = ws.editdocument(True,m_RuleDoc) 

Call SaveRule(m_RuleDoc, uidoc)

End Sub

Script Library - Function SaveRule

Function SaveRule (m_RuleDoc As NotesDocument, m_uidoc As NotesUIDocument) As String

Set note = m_RuleDoc

Set uidoc = m_uidoc	

Call ButtonOKClient()

End function

Subject: Create a mail rule in script

Do you really mean a “mail rule”? Or do you mean an agent? Mail rules are executed only on mail delivery. To hook executable code to any other event you need to write an agent.

Subject: RE: Create a mail rule in script

Hi,

Yes, I want to create a mail rule automaticly for all my users.

Thank you!

Alexandre

Subject: RE: Create a mail rule in script

Then you cannot have it execute on database open because that is not how mail rules are invoked. But you can write an agent to do it.

Subject: RE: Create a mail rule in script

The only thing I want to do is to create the rule when they open the database.

Ours Gateway add [SPAM] in the Subject line when a Spam email is delected. I will need a mail rule in all user mail file to send these email to their Junk Folder.

I want to create a mail rules if the subject contain [SPAM] sent it to the Junk Mail folder.

I don’t want the user to have to create the rules or have to click on a button (they never click). I want to new mail rule to be create automaticly and if they delete it, re-create it. It’s why in the database script, I only create the rule.

Thank you!

Subject: Create a mail rule in script

Hi Alexandre,

You should add a flag to save the UI document with form.

This should solve the problem.

I just can think about a problem if the user have any more rule on his mail file. Every rule haves a sequencial number. This number is pertinent to this database. You should think about a script running on the server, by night when users should be in bed ;-). This script will check if the rule exists, and if the sequential number is correct.

This piece of code will explain a bit:

If Not profile Is Nothing Then

	If rules.AllEntries.Count > 0 Then

		Call profile.ReplaceItemValue("$FilterFormulaCount", Cstr(rules.AllEntries.Count))

	Elseif profile.HasItem("$FilterFormulaCount") Then

		Call profile.RemoveItem("$FilterFormulaCount")

	End If

Then:

Set doc = rules.GetFirstDocument()

While Not doc Is Nothing

	Call doc.ReplaceItemValue("Enable", "0")

	Call doc.ReplaceItemValue("OrderNum",count)

	count = count + 1

	Call doc.Save(True,False,True)

Checking rules by script is painfull, so don’t forget the relation between profile document, rule and the counter.

In my example we are putting all the rules as disabled, moving the counter, and later enabling.

Regards,

Daniel

Subject: RE: Create a mail rule in script

Hi,

The way I did it is that I copy the quick rule form, modify the fiend I wanted to computed with the value I wanted and save the document.

No I have the code bellow to create the rules.

vSpam := @DbColumn(“”;“”;“(Rules)”; 4);

@If(@Contains(vSpam;“[SPAM”); @Return(“”);“”);

@Command([Compose];“StiefelQR”);

@command([FileSave])

Regards,

Alexandre

Subject: RE: Create a mail rule in script

Hi,

I don’t know how to do this using formula.

I believe it’s possible, but other guys will have to help you.

What it’s sure, is that it’s very important to save the fields I told you before.

They are used to keep the rules in the right place, the first being first, second being second and so on.

Besides, try to do this on the server for all the mailfiles just once, and apply this rule to the template, thus

when you create a new user, the rule will be there allready. If you leave the formula or script on the database script, this will affect performance!

Regards,

Daniel

Subject: Create a mail rule in script

Hi,

did you find a solution for your problem yet?

I’m trying to do the same, but didn’t succeed.

Greetings

Benjamin