0
votes

The code when run manually (right click and run) it works perfectly, but the issue arises when it is automated using schedule. When automated the code runs fine but right at the end of running the code it fails with the above error message.

The code looks fine, variables are set as they should and the code runs fine when done manually.

Sub processJobs(dbCurrent As NotesDatabase)
Dim vwLookup As NotesView
Dim docReq As NotesDocument
Dim dtMonthAgo As New NotesDateTime(Today)
Dim dtDelDate As NotesDateTime
Dim itmDelDate As NotesItem
Dim sender As NotesName
Dim receiver As NotesName
Dim nmServer As NotesName
Dim lngNoOfDays As Long
Dim mail As Email
Dim intCount As Integer
Dim intCountFailed As Integer
Dim strSendTo As String

On Error GoTo ErrorHandler
On Error 4000 GoTo RecipientNameBlank
On Error 4294 GoTo RecipientNotInNAB

Call AgentLog.LogAction("--------- Process Job ---------")

Call dtMonthAgo.AdjustMonth( -1 )  ' set the dtMonthAgo date to one month ago
Call dtMonthAgo.Setanytime() ' remove the time component from the date

Set vwLookup = dbCurrent.Getview("JobView")
vwLookup.Autoupdate = False

Set docReq = vwLookup.Getfirstdocument()

intCount = 0
intCountFailed = 0
Do Until docReq Is Nothing
    Set itmDelDate = docReq.GetFirstItem("DeliveryDate")
    If itmDelDate.Type = 1024 Then
        Set dtDelDate = itmDelDate.DateTimeValue
        Call dtDelDate.SetAnyTime

        If dtMonthAgo.TimeDifference(dtDelDate) > 0  Then
            intCount = intCount + 1
            Set mail = New Email ' send email...
            mail.Subject = "Processed Job"
            mail.HTML = getCompletionHTML(docReq, mail.WebURL)

            Set sender = New NotesName(docReq.JobBy(0))
            Set receiver = New NotesName(docReq.DespatchTo(0))
            Set nmServer = New NotesName(dbCurrent.Server)
            If receiver.Organization = nmServer.Organization Then
                strSendTo = receiver.Abbreviated
                ' send a copy to..
                If sender.Abbreviated <> receiver.Abbreviated Then
                    mail.CopyTo = docReq.JobBy(0)
                End If
            Else
                strSendTo = sender.Abbreviated
            End If

            mail.Send(strSendTo)
            Call agentLog.LogAction(strSendTo & " - Job No: " & docReq.JobNo(0))
   flagDoc:
            ' flag the job...
            Call docReq.Replaceitemvalue("CompletionJob", "Y")
            Call docReq.Replaceitemvalue("CompletionJobDate", Now)
            Call docReq.Save(True, False)
        End If
    End If
    Set docReq = vwLookup.Getnextdocument(docReq)
Loop

Call AgentLog.LogAction("")
Call AgentLog.LogAction("Attempted to send " & CStr(intCount) & " Job")
Call AgentLog.LogAction("Failed to send " & CStr(intCountFailed) & " Job")
Call AgentLog.LogAction("--------- End of job process ---------")

ErrorHandler:
If Not AgentLog Is Nothing Then
    Call AgentLog.LogError(Err, "errorHandler: " & CStr(Err) & " " & Error$ & " in " & LSI_Info(2))
End If
Resume getOut

23/05/2019 00:00:05 errorHandler: 91 Object variable not set in PROCESSJOBS(Object variable not set)

The agent was supposed to loop through the view, get names of recipients, set the variables and then send the email automatically. By automation, it does loop through the view and get/set names of recipient but fails straight after getting the last name that the object variable is not set. Running the code manually does not pose any problem at all, but this code needs to be run automatically.

2
Your code is incomplete (labels missing, Email class missing, End Sub missing), and without a line number or other indication which line it stops on there's little we can do. One remark: add the command Call on the line with mail.Send(strSendTo)D.Bugger
Is "option declare" activated ? Shouldn't Dim dtMonthAgo As New NotesDateTime(Today) be with a String like Dim dtMonthAgo As New NotesDateTime("Today") ?umeli
Adding Call to mail.Send(strSendTo) resolved the issue. Thanks for your help!Samuel Omopariola

2 Answers

1
votes

You need an Exit Sub statement to prevent your code from falling through into your error handler.

Call AgentLog.LogAction("")
Call AgentLog.LogAction("Attempted to send " & CStr(intCount) & " Job")
Call AgentLog.LogAction("Failed to send " & CStr(intCountFailed) & " Job")
Call AgentLog.LogAction("--------- End of job process ---------")

Exit Sub ' **** You need this

ErrorHandler:
If Not AgentLog Is Nothing Then
    Call AgentLog.LogError(Err, "errorHandler: " & CStr(Err) & " " & Error$ & " in " & LSI_Info(2))
End If
Resume getOut

You also don't appear to be initializing AgentLog, though that might be a global.Is it successfully writing those lines to the agent log when you run it scheduled? If not, perhaps there's a problem with accessing the agent log database on the server where it is scheduled.

2
votes

In your ErrorHandler, log (or print) the line where the error occured.

ErrHandler:
Print "Got error " & Error$ & " on line " & cstr(Erl)

example copied from IBM