I have an Agent that imports document from text file. So the user requires me to record the imports in a document, including the time it was imported, number of imported documents, fails and the reason why. I kinda get the part of its logic, but I don't know what methods to use. I've used the NotesLog class, but its not enough on getting the required information.
This here is a part of my Agent:
Sub LoadAPMSSUBdoc(Rname As Variant, directory As Variant, Datef As Variant)
Dim session As New NotesSession
Dim Tdoc As NotesDocumentCollection
Dim dateTime As New NotesDateTime ("01/01/2000")
Dim GDate As Variant
Dim LocView As notesview
Dim LocDoc As notesdocument
Dim subsidiary As String
Print "Loading APMSSUB - Other Staff Information"
Set session = New NotesSession
Set cdb = session.CurrentDatabase
Set LocView = cdb.GetView("LsvLocationProfile")
Set LocDoc = LocView.getfirstdocument
StaffServerName = LocDoc.Z_ExtServer(0)
'SearchFormula$ = "Select Form = ""dfOther"" & @Date(s_Created) != @Date(@Today) "
If (upj = True) And (upg = True) Then
SearchFormula$ = "Select Form = ""dfOther"" "
ElseIf (ibmmy = True) Then
SearchFormula$ = "Select Form = ""dfOther"" & L_Employee = ""UPJ"" "
Else
SearchFormula$ = "Select Form = ""dfOther"" & L_Employee = ""UPG"" "
End If
Set Tdoc = cdb.Search( SearchFormula$, DateTime, 0 )
If Tdoc.Count <> 0 Then
Call Tdoc.RemoveAll(True)
End If
'Get an unused file number
file_no% = Freefile()
Open (Trim(directory + "apmssub.txt")) For Input As file_no%
Set db = Session.CurrentDatabase
Select Case Datef
Case "DMY" : Cdatf = "dd/mm/yyyy"
Case "MDY" : Cdatf = "mm/dd/yyyy"
Case "YMD" : Cdatf = "yyyy/mm/dd"
Case Else :
Print "LoadAPMSSUBdoc - Unknown system date format"
Exit Sub
End Select
Do While Not Eof(file_no%)
Line Input #file_no%, tmp
SerialNo = Trim$(Mid$(tmp,1,6))
Initial = Trim$(Mid$(tmp,239,3))
HQualification = Strconv(Trim$(Mid$(tmp,8,30)),3)
Major = Strconv(Trim$(Mid$(tmp,38,40)),3)
Inst = Strconv(Trim$(Mid$(tmp,78,50)),3)
If Trim$(Mid$(tmp,128,8)) = "" Then
GDate = Null
Else
GD1 = Setdate(Trim$(Mid$(tmp,128,8)), "mm/dd/yy", Datef)
GDate = Cdat(Format(GD1, Cdatf))
'Datenumber ( Val(Trim$(Mid$(tmp,134,2))) , Val(Trim$(Mid$(tmp,131,2))) , Val(Trim$(Mid$(tmp,128,2))) ))
'Print GDate
End If
OB = 0
For i = 4 To 0 Step -1
x1 = 137 + (i * 12)
x2 = 139 + (i * 12)
temp = Trim$(Mid$(tmp,x1,1))
If temp <> "" Then
Redim Preserve Prate(OB)
Redim Preserve Pdate(OB)
Prate(OB) = temp
PD1 = Setdate(Trim$(Mid$(tmp,x2,8)), "mm/dd/yy", Datef)
Pdate(OB) = Cdat(Format(PD1, Cdatf))
'Datenumber ( Val(Trim$(Mid$(tmp,x2+6,2))) , Val(Trim$(Mid$(tmp,x2+3,2))) , Val(Trim$(Mid$(tmp,x2,2))) ))
OB = OB + 1
End If
Next
If OB = 0 And Trim$(Mid$(tmp,185,1)) = "" Then
Redim Preserve Prate(OB)
Redim Preserve Pdate(OB)
Prate(0) = ""
Pdate(0) = Null
End If
AB = 0
For i = 0 To 3
x1 = 198 + (i * 10)
temp = Trim$(Mid$(tmp,x1,10))
If temp <> "" Then
Redim Preserve AAmt(AB)
AAmt(AB) = Val(temp)
AB = AB + 1
End If
Next
If AB = 0 And Trim$(Mid$(tmp,198,10)) = "" Then
Redim Preserve AAmt(AB)
AAmt(0) = Null
End If
subsidiary = Filter(CStr(SerialNo))
If (subsidiary = "UPJ" And upj = True) Or (subsidiary = "UPG" And upg = True) Then
Set doc = New NotesDocument(db)
doc.Form = "dfOther"
doc.L_Employee = subsidiary
doc.E_StaffSerialNo_1 = SerialNo
doc.E_PBCRating = Prate
doc.E_PBCDate = Pdate
doc.E_AdjAmt = AAmt
doc.E_HQualification = HQualification
doc.E_MajorSubject = Major
doc.E_InstituteName = Inst
doc.E_GraduateDate = GDate
doc.E_Initial = Initial
doc.s_created = Now
'doc.G_AuthorDisp = "Management SGP"
doc.G_AuthorDisp = Rname
Call doc.Save (True, True)
End If
Loop
Close file_no%
Print "Other information imported"
End Sub
The solution is to construct a sub function inside the agen that will create a document to store the logs upon import:
Sub createLog
Dim logdoc As NotesDocument
Dim db as notesdatabase
Dim log As String
Dim nitem As NotesItem
Set db = session.CurrentDatabase
Set logdoc = db.CreateDocument
logdoc.Form = "ImpLog"
logdoc.ImpRecords = ""
Set nitem = logdoc.Getfirstitem("ImpRecords")
log = CStr(s_countSave) + " document(s) from textfile1 imported; " + "and" + CStr(s_uncounted) + " document(s) was not imported successfully "
Call nitem.Appendtotextlist(log)
Call logdoc.Save(True, True)
End Sub
On the sub function of import agent, create a variable before the loop and set it to 0
:
s_countAll = 0
s_countSave = 0
s_uncounted = 0
Do While Not Eof(file_no%)
and at the end of the loop add the following code:
Call doc.Save (True, True)
s_countSave = s_countSave + 1
End If
s_countAll = s_countAll + 1
Loop
s_uncounted = s_countAll - s_countSave
Hope this help.