Schedule Agent

I have a “Pending” view and I want that my Status field will automatically change from Pending to Retention using schedule agent.

This is my code. not work please help.

Sub Initialize

Dim s As New NotesSession

Dim db As NotesDatabase

Set db = s.CurrentDatabase

Dim v As NotesView

Set v = db.GetView("Pending2")



Set doc = v.GetFirstDocument





While Not(doc Is Nothing)

	If doc.DaysConsumed(0)>=180 Then

		doc.DocStatus="Retention"

			

	Else

		doc.DocStatus="Pending"



	Else if	

		

	 doc.ClaimedBy(0)<>"" Then

		doc.DocStatus="Claimed"			

		

	End If

	

	

	Call doc.Save(True, False)

	Set doc = v.GetNextDocument(doc)

	



Wend

End Sub

Subject: Schedule Agent

  1. Instead of “while not doc is nothing”, use “do until doc is nothing”, it makes it easier to read the code.

  2. Don’t use extended notation when you access fields on a document. Use the GetItemValue/ReplaceItemValue methods of the NotesDocument class instead, better performance as well as making the application forward compatible.

  3. You are processing all documents and updating every single document. Only update the ones where the value changed.

  4. You have a very weird construction in your loop:

If

Else

Else If (without a statement!)

End If

That won’t work, should not even be accepted by the editor.

  1. To make the agent faster, don’t loop though the view and read every single document. This is a very “expensive” call. Instead you should get a NotesViewEntryCollection and loop through the view entries. Make sure you display the field value you want to compare with (e.g. DaysConsumed and ClaimedBy) in the view, then use the ColumnValues peroperty of the NotesViewEntry class to get read the values. Then only open and update the document if the value has changed.

Subject: RE: Schedule Agent

So something like this:

Dim session As New NotesSession

Dim db As NotesDatabase

Dim view As NotesView

Dim col As NotesViewEntryCollection

Dim entry As NotesViewEntry

Dim doc As NotesDocument

Dim days As Integer

Dim status As String

Dim claimedby As String

Set db = session.CurrentDatabase

'*** Get hidden lookup view, never use

'*** views visible to users for lookups

Set view = db.GetView(“LookupPending”)

'*** Get all entries in view

Set col = view.AllEntries

'*** Loop thorugh all view entries

Set entry = col.GetFirstEntry()

Do Until doc Is Nothing

'*** Read values from columns

status = Cstr(entry.ColumnValues(0)) ’ First column is status

days = Cint(entry.ColumnValues(1)) ’ Second column is days

claimedby = Cstr(entry.ColumnValues(2)) ’ Third column is

'*** Now when we have the vlaues, let’s do stuff…

If days>=18 Then

Set doc = entry.Document

Call doc.ReplaceItemValue(“DocStatus”,“Retention”)

Call doc.Save(True,False)

Else If claimedby<>“” Then

Set doc = entry.Document

Call doc.ReplaceItemValue(“DocStatus”,“Claimed”)

Call doc.Save(True,False)

Else If status<>“Pending” Then

Set doc = entry.Document

Call doc.ReplaceItemValue(“DocStatus”,“Pending”)

Call doc.Save(True,False)

End If

Set entry = col.GetNextEntry(entry)

Loop

This is untested code, and I am making some assumptions. You may want to think through the logic and make sure this is how you want it to work as well.

Subject: RE: Schedule Agent

Many thanks Karl for the tips and commands I really appreciated. I learned a lot.

Thanks

Subject: Schedule Agent

You say “This is my code. not work please help.”
How doesn’t it work? Do you get an error? What is the error?