Link to home
Start Free TrialLog in
Avatar of shuboarder
shuboarderFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Scheduled Agent to create documents via schedule in a view

I've just asked a very similar question to this, and am forever grateful to Sjef for helping out with it.
Unfortunately as always I now need to take this a step further...

Here is the situation...

I have an agent that runs daily.
It collects documents from a view and creates new documents depending on the day of the week.
This works by comparing weekday(today) with the value in a combobox on the form

What I need to achieve now is to set this up so that somehow it can create documents spanning multiple weeks.
i.e. at the moment I can only create the same documents every week. I need to be able to specify for example create document every 4 weeks (28 days) on the form.

I'm stuggling with working out how I can set this up.

I'm thinking along the following lines:

Field: Start_LastRun_Date = user specified date
Field: Repeat_Every = user specified period (days)
Field: Collection_Date = computed date (start_date + repeat_every)

The agent which runs daily should collect documents from the view where Collection_Date = Today
It should then create this document through form2. It should also modify the Start_LastRun_Date to = Now
and save the original document. Thus the original document's Collection_Date will recompute to the next period, and the cycle will begin again.

Hope that makes sense?

Points awarded for:

a) verifying this is all possible and will work
b) help creating an agent to do all this!
Avatar of Sjef Bosman
Sjef Bosman
Flag of France image

a) all possible
b) tomorrow? *yawn*

One tiny correction, think about it:
    ... collect documents from the view where Collection_Date >= Today

Last question: are created documents required the day itself? If so, is it possible to create documents ahead of time?
Avatar of shuboarder

ASKER

Sjef, you are a machine!

Tomorrow is grand. Dankzegging

 ... collect documents from the view where Collection_Date >= Today
I'm thinking, but nothing's happening :)
Why would I want to also collect documents with a date beyond today?

The agent will hopefully only need to run once a day
and will only create documents for the current day.
Why >= Today? Agents might not run. What's worse sometimes: agents might run twice...

The best thing you can do is to implement some sort of activity queue in a view: first column contains date/time the document should be run again, sorted desceding. Your agent can then start at the top, handle those documents that have a rundate<=Today, until the first document is found with a date >Today (or >= Today).

Remember, Today returns the current date with seconds set to 0, so midnight (00:00:00). So if you intend to allow a time, comparing with <= Today effectively means with a date/time of Yesterday.

Does this help already?
I guess so, because I only want to be comparing the date not the time
so really I think I should be using @date(@today)?
In the form for the users, where they type the date, they can also type a time. That's where you need to have an Input Translation formula like @Date(rundate).
something like this....?

Sub Initialize
      Dim session As New NotesSession
      Dim db As NotesDatabase
      Dim view As NotesView
      Dim collection As NotesDocumentCollection
      Dim doc As NotesDocument
      Dim newdoc As NotesDocument
      Dim rdate As String
      
      rdate= Cstr(Date(Today))
      Set db = session.CurrentDatabase
      Set view = db.getview("PPM")
      Set collection = view.GetAllDocumentsByKey(rdate, True)
      Set doc= collection.GetFirstDocument
      
      'Call doc.GetItemValue.next_run_date
      'If rdate = next_run_date
      'Then
      'doc.last_run_date=rdate
      
      While Not(doc Is Nothing)
            Set newdoc = New NotesDocument ( db )
            Call doc.CopyAllItems(newdoc,True)
            Call newdoc.Save( True, False )
            Set doc = collection.GetNextDocument(doc)
      Wend
End Sub
ASKER CERTIFIED SOLUTION
Avatar of Sjef Bosman
Sjef Bosman
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sjef, that's looking great!

Just to confirm, do I want the date decending as in:

Date:

Furthest Away
|
|
V
Soonest

or ascending

Date:

Soonest
|
|
V
Furthest Away

I'm sure you're right, but might need a bit more explanation please
In this case, I think I was mistaken, you need Ascending. :$ It'll have to be a view that shows the documents you need to handle at the top of the list.

Apologies. I suppose you wouldn't believe me if I said it was a test...
No.... lol
but if it was, I did good :)