Manage Learn to apply best practices and optimize your operations.

Change views selection criteria on the fly

I was asked to provide a calendar view to users, where they were able to select on the fly what documents were included in the calendar view based on a designated category.

View member feedback to this tip.

I was asked to provide a calendar view to users, so they could select on the fly what documents were included in the calendar view, based on a designated category. The problem became more difficult when the designated categories were dynamic (i.e., changeable and able to be added to or deleted from).

To solve this problem, I needed to be able to supply the users a way of changing the views selection criteria on the fly. This ability is limited to only Designer access and above, so how could I give this functionality to the end users?

I used the Notes 6 functionality of assigning an agents "run on behalf of" property. I created two agents. The first was called from an outline entry and requested the user to select the category they wished to limit the calendar view selection to. The second agent was called from the first agent (the one where I assigned the "run on behalf of" property to the username we use to sign all of our databases, and, as a default, has manager access to this database).

Here's the code I used in the agents:

Agent1 (Agent to get Category Name 
and to create the new Selection 
Criteria Formula)

Dim s As New NotesSession
Dim ws As New NotesUIWorkspace
Dim db As NotesDatabase
Dim templateview As NotesView
Dim agent As NotesAgent
Dim selection As Variant
Dim selectFormula As String
Dim channels As Variant
Dim uidb As NotesUIDatabase
Dim doc As NotesDocument
Set db = s.CurrentDatabase
Set agent = db.GetAgent("(Open Calendar)")
Set templateview  = db.GetView("Calendar")
Set uidb = ws.CurrentDatabase
Call ws.SetTargetFrame("NotesView")

'Get options for selection in Prompt below.
channels = Evaluate( { "All" : @DbColumn
("" : "NoCache"; "";
"Channel"; 1 ) } )
selection = ws.Prompt(4, "Channel Selection", 
"Please choose the required
Channel", "All", channels)

If Not Isempty(selection) Then
'Capture the desired selection formula 
  If selection = "All" Then
   selectformula = {SELECT ( ( Form = 
"SalesPromo" ) | (
Form = "MarketingPromo" ) )}
   selectformula = {SELECT 
( ( Form = "SalesPromo" ) | (
Form = "MarketingPromo" ) ) & Channel = "}
 & selection & {"}
  End If
'If the selection formula is the same as the
 current selection formula, just
open the view.
'If the selection formula is not the same
 as the views current selection
formula, then call 'Agent2 to change the
 selection formula in the view, prior
to opening it.  
If selectformula = templateview.SelectionFormula 
   Call uidb.OpenView("Calendar") 
'Create a temporary document to store the 
new selection formula, so it can be 
passed to Agent2.
   Set doc = db.CreateDocument
   doc.Form = "Calendar"
   doc.SelectFormula = selection
   Call (True, False)
   'Run the 'Open Calendar' agent on the 
server and pass the agent the note ID 
of the temporary document storing the 
selection formula.
   Call agent.RunOnServer(doc.NoteID)   
   Call uidb.OpenView("Calendar")  
  End If  
 End If

Agent2 (Agent created to amend the views 
selection criteria).
REM This agent must run on the server 
so that the 'Run on behalf of' property 
is used.

Dim session As New NotesSession
Dim templateview As NotesView
Dim selectFormula As String
Dim selection As Variant
Dim db As NotesDatabase
Dim channels As Variant
Dim agent As NotesAgent
Dim doc As NotesDocument
Set db = session.currentdatabase
Set templateview  = db.GetView("Calendar")
Set agent = session.CurrentAgent
'Get the temporary document created to 
store the selection formula
Set doc = db.GetDocumentByID
'Change the selection formual accordingly
 If doc.selectformula( 0 ) = "All" Then
  selectformula = {SELECT ( (Form = 
"SalesPromo") | (Form 
= "MarketingPromo") )}
  templateview.SelectionFormula = 
  selectformula = {SELECT ( (Form =
 "SalesPromo") | (Form 
= "MarketingPromo") ) & Channel = "} 
& doc.selectformula(0) & {"}
  templateview.SelectionFormula = 
 End If
'Remove the temporary document that 
stored the selection formula.
 Call doc.Remove(True)


Is this a single user application? Once the view selection formula is changed on the server, won't it be changed for all users of the application and possibly cause issues if more than one person is using the database at a time?

—Brian A.


No, this is a multi-user database. I had the same question, but fortunately, the continuous change to the view was accepted by the users. The view is used for reference purposes only and each time it is needed, the code forces them to select the criteria and hence, get the view they want. The view will stay the same on their client until they change views or exit the database, even if someone else uses the database on a different client and selects a different criteria.

If this is not acceptable for others, then I can only suggest you try "private on first use" views and see if the same functionality can be applied to these types of views.

—Graham Frey, tip author


Why don't you just use @SetViewInfo?

— Hassan V.


In most circumstances you could definitely use the @SetViewInfo function. But, in my case the selection criteria did not end at just one selection. The application needed to be able to provide further categorization based on the previous selections. I could not utilize @SetViewInfo because I needed to know what the previous selections were.

My code was enabling them to categorize the calendar and then drill down to further categories within the previous selections.

—Graham Frey, tip author

Do you have comments on this tip? Let us know.

This tip was submitted to the tip exchange by member Graham Frey. Please let others know how useful it is via the rating scale below. Do you have a useful Notes/Domino tip or code to share? Submit it to our monthly tip contest and you could win a prize and a spot in our Hall of Fame.

Dig Deeper on Lotus Notes Domino Calendar and Contact Management



  • Favorite iSeries cheat sheets

    Here you'll find a collection of valuable cheat sheets gathered from across the iSeries/ community. These cheat ...

  • HTML cheat sheet

    This is a really cool cheat sheet if you're looking to learn more about HTML. You'll find just about everything you every wanted ...

  • Carol Woodbury: Security

    Carol Woodbury