LotusScript Help!

I’m trying to figure out what a developer has done. This code is in an agent in a database. What does it do?

Sub initialize

Dim session As New NotesSession

Dim db As NotesDatabase

Dim doc As NotesDocument

Dim dateTime As New NotesDateTime ("1/1/2002")

Dim collection As NotesDocumentCollection

Dim Date1 As Variant

Dim Date2 As Variant

Dim Today1 As  New NotesDateTime("Today")



Dim m As Integer

Dim DateM As Variant

Dim dateY As Variant

Dim d As Integer

Dim ty As Integer     

y = Year(Today1.LSLocalTime)

m = Month(Today1.LSLocalTime)

If m = 1 Then

	dateM = 12

	dateY = y-1

Else 

	dateM = m-1

	dateY = y

End If

d = Day(Today1.LSLocalTime)



Select Case dateM 

Case 1, 3, 5, 7, 8, 10, 12

	d = 31

Case 2

	d = 28

Case Else

	d = 30

End Select



'build date values for search

Date1 =  dateM & "/1/" &  dateY 

Date2 = dateM & "/" & d & "/" & dateY 



'build search string for collection that is docs with due date in prior month

searchFormula$ = "(FIELD Due >= " &  Date1 & " &  FIELD Due <= " & Date2 &")"

Set db = session.CurrentDatabase

Set collection = db.FTSearch (searchFormula$,0)







For i = 1 To collection.Count

	Set doc = collection.GetNthDocument(i)

	

	

	

	Dim docB As NotesDocument

	Set db = session.CurrentDatabase

	Set docB = New NotesDocument (db)

	Call doc.CopyAllItems (docB, True)

	docB.status = "New"

	docB.createdBy = session.CommonUserName

	docB.date = Today()

	docB.file_date = Null

	docB.contact = Null

	docB.filed = "No"

	

	'get year value from due field

	Dim itemB As NotesItem

	Dim datetimeB As NotesDateTime

	Set itemB = docB.getfirstitem ("due")

	Set datetimeB = itemB.datetimevalue

	Call datetimeB.adjustyear (1)

	Set docB.due = datetimeB

	'get year value from ActualDate field if there is a value

	Set itemB = docB.getfirstitem ("ActualDate")

	If Not Isempty (item) Then

		Set datetimeB = itemB.datetimevalue	

		Call datetimeB.adjustyear (1)		

		Set docB.ActualDate = datetimeB

	End If

	

	'Save DocB

	Call docB.Save (True,True)

Next

End Sub

Subject: LotusScript Help!

This is some really poor coding; I’m not surprised you found it confusing…

Dim m As Integer

Dim DateM As Variant

Dim dateY As Variant

Dim d As Integer

Dim ty As Integer     

y = Year(Today1.LSLocalTime)

m = Month(Today1.LSLocalTime)

This section looks at the current month - if it’s January, it sets the date month to December of the previous year; otherwise it sets it to the previous month of this year.

If m = 1 Then

	dateM = 12

	dateY = y-1

Else 

	dateM = m-1

	dateY = y

End If



d = Day(Today1.LSLocalTime) not sure why they even bothered with this line, since it's just being reset in the Select Case.

This section looks at the set month - if it’s January, March, May, July, August, October, or December it assigns it 31 days; if it’s February it gets 28 days (they’re not checking for leap years); otherwise it gets 30 days.

Select Case dateM 

Case 1, 3, 5, 7, 8, 10, 12

	d = 31

Case 2

	d = 28

Case Else

	d = 30

End Select

This section creates a search string based on the date - from the first of the set month/year, to the last of the month (determined by variable d - so either the 31st, 30th or 28th.)

'build date values for search

Date1 =  dateM & "/1/" &  dateY 

Date2 = dateM & "/" & d & "/" & dateY 

It then creates a collection using FTSearch that includes documents whose Due field falls between the first of the set month and the end of the set month - in other words, as the original comment says - items that were due the previous month.

'build search string for collection that is docs with due date in prior month

searchFormula$ = "(FIELD Due >= " &  Date1 & " &  FIELD Due <= " & Date2 &")"

Set db = session.CurrentDatabase

Set collection = db.FTSearch (searchFormula$,0)





"For i to ..." is a really bad way to loop through a collection; it would be more efficient to say:

Set doc = collection.getfirstdocument

While not doc is nothing

'do your stuff

set doc = collection.getnextdocument(doc)

Wend

Basically what this loop does is goes through the collection and creates a new document for each document in the collection. The new document gets all items from the current document, with status set to New, createdBy set to the current user, date set to today, filed set to No, and file_date and contact set to Null.

For i = 1 To collection.Count

	Set doc = collection.GetNthDocument(i)

	Dim docB As NotesDocument

	Set db = session.CurrentDatabase

	Set docB = New NotesDocument (db)

	Call doc.CopyAllItems (docB, True)

	docB.status = "New"

	docB.createdBy = session.CommonUserName

	docB.date = Today()

	docB.file_date = Null

	docB.contact = Null

	docB.filed = "No"

It then gets the item on the new document called due (which was also copied over from the current document in the collection.) It gets the value of it, then moves it a year in the future. If there’s a field called ActualDate, it gets that value and increments it by a year. It then saves the new document and moves on to the next document in the collection.

	'get year value from due field

	Dim itemB As NotesItem

	Dim datetimeB As NotesDateTime

	Set itemB = docB.getfirstitem ("due")

	Set datetimeB = itemB.datetimevalue

	Call datetimeB.adjustyear (1)

	Set docB.due = datetimeB

	'get year value from ActualDate field if there is a value

	Set itemB = docB.getfirstitem ("ActualDate")

	If Not Isempty (item) Then

		Set datetimeB = itemB.datetimevalue	

		Call datetimeB.adjustyear (1)		

		Set docB.ActualDate = datetimeB

	End If

	

	'Save DocB

Call docB.Save (True,True)

Next

Subject: RE: LotusScript Help!

Thank you, Esther. You’re right, it is a bit cornfusing. Thank you for the great translation work. Much obliged.

:smiley:

Subject: LotusScript Help!

D:

First, the code is checking to see if it is a month with 31, 30 or 28 days in it (the coder may have added code to check for leap years (29 days)).

Second, based on the above, a search criteria is being built (Date1 and Date2), thus resulting in the searchFormula$.

Third, the code is performing a Full-Text search on the data in the db based on the search formula. If documents are found (see the for loop), then a new document is created and all of the values/fields from the searched document is copied into the new document. Also, other fields are being populated (see where the code begins with “docB.status”). Also, the “if” statement beginning with “If Not IsEmpty(item)” is checking to see is a field value is not empty an if so, adjust the year value in that field +1. Then save to document and proceed onto the next document (if there is one from the search results/collection).

One thing that stands out from the code above. The “If Not IsEmpty(item),” what is item and where is id declared? It probably should be “If Not IsEmpty(itemB)”.

HTH,

SPJ

Subject: LotusScript Help!

Oh, good Lord. This builds a search string to look for documents that are due any time “last month”. In the wrongest possible way. Or at least in one of the wronger ways one could choose. I would have done the search string like this:

Dim startTime As NotesDateTime

Dim endTime As NotesDateTime

Dim currentLSTime As Variant

currentLSTime = Today

Set endTime = New NotesDateTime(Format$(currentLSTime,“Short Date”)

Call endTime.AdjustDay(1 - Day(currentLSTime))

Set startTime = New NotesDateTime(Format$(endTime.LSLocalTime,“Short Date”)

Call startTime.AdjustMonth(-1)

searchFormula$ = “(FIELD Due >= [” & startTime.DateOnly & “] & FIELD Due < [” & endTime.DateOnly &“])”

Then it creates a new version of each document returned with a due date one year later than the existing document. Another date field is set ahead a year, but I can’t tell the purpose for that.

Subject: LotusScript Help!

The agent takes today’s date, searches for all documents with a due date that falls within the previous month, creates new copies of those documents and adjusts the due date for those new copies to one year ahead.

Hope that helps.

Subject: LotusScript Help!

Thanks to all who made this translation a bit easier. I’m grateful.

:smiley: