Link to home
Start Free TrialLog in
Avatar of IanWood
IanWoodFlag for United States of America

asked on

Agent works running on client, but not when server runs on new mail arrival.

Hello Everyone, hope you are all well..

I have an agent that is going to run from a mail-in db to update a different database (documents with a ref no matching the last 4 characters of the memo subject)to create a response 'comments' doc.

The code works fine when I run it 'Manually from actions menu', but not on 'After New Mail has arrived' - I think this is because it is running on the server and not on the client by me..

Anyway here's the code:-

**********************************************************

Sub Initialize
     Dim s As New NotesSession
     Dim db As NotesDatabase
     Dim doc As NotesDocument, doc2 As NotesDocument, NewDoc As NotesDocument
     Dim dc As NotesDocumentCollection
     Dim view As NotesView
     Dim CurrentLog As New NotesLog( "SS53 Process Carrier mails" )
     
     Dim sRefNo As String, sPath As String, sServer As String
     Dim x As Integer
     
     Set db = s.CurrentDatabase
     Set dc = db.UnProcessedDocuments
     Call CurrentLog.OpenNotesLog( db.server,"agentlog.nsf" )    
     Call CurrentLog.LogAction("Started running script") 'Copy, paste and amend this line to change agent log text        
     
     Call CurrentLog.LogAction("Sent e-mail")    
     '****** tmp e-mail msg
     Dim Message As NotesDocument
     Set Message = db.CreateDocument                    
     With message
          .Form = "Memo"
          .SendTo = "Ian Wood"
          '.CopyTo = "Chris Stenton"
          .Subject = "Bingo"
          .Logo = "First Initial"
          .SaveOptions = 0
     End With
     Call Message.Send(False)      
     
     If Not (dc.count > 0) Then
          Call CurrentLog.LogAction("No docs found")
          Exit Sub
     End If
     Call CurrentLog.LogAction("1 or more documents found to run on..")
     
     sServer = "NHTNTDV0"'GetProfileValue("Setup", "Server")
     sPath = "Development\CSC\CSC_SS53_2-0.nsf"'GetProfileValue("Setup", "Path")
     Dim db2 As New NotesDatabase(sServer,sPath)
     
     If (Not db2.Open(sServer,sPath)) Then
         
          'Msgbox "Unable to open the SS53 db.",64,db.title      
          Call CurrentLog.LogAction("unable to open target SS53 db!!!")
         
          Exit Sub
     End If
     Call CurrentLog.LogAction("Opened target SS53 db")
     Set view = db2.GetView("(LookUpRefNo)")
     
     For x = 1 To dc.count
          Call CurrentLog.LogAction("In processing loop no: " & x)
          Set doc = dc.GetNthDocument(x)
          sRefNo = Right(doc.Subject(0),4)
          Set doc2 = view.GetDocumentByKey(sRefNo, True)
          Set NewDoc = New NotesDocument(db2)
          NewDoc.Form = "Comment"
          NewDoc.Subject = doc.Subject(0)
          NewDoc.Body = doc.Body
          Call NewDoc.MakeResponse(doc2)
          Call NewDoc.save(True, False)          
     Next
     Call CurrentLog.Close    
End Sub

**********************************************************

I've only ever written code that is executed on the client.  At the moment it is getting stuck (As far as I can tell it just stops running) on the opening of the target db (db2), but I don't know how else to write it..

Anybody got any ideas?

Cheers

Ian


Avatar of zvonko
zvonko

Support for accessing remote servers from server-based agents is coming in Rnext :-)

Look here for Julie's description:
http://www.notes.net/46dom.nsf/7e6c7e584a76331b85256a46006f083e/574c99ccb345839185256976004e811e?OpenDocument

What does the log say ?

There are some rules by which the scheduled agent runs on server. Basically the security. So you have to see the signature on agent is validated by other server. Probably the agent execution access is not setup propertly on target server.

Here are two excellent articles on agent functionality, check it out. It is worth spending some time to read this articles and in future it will be much handy.

http://notes.net/today.nsf/62f62847467a8f78052568a80055b380/08f2d1ce0329d70e852565a2005227e6?OpenDocument

http://notes.net/today.nsf/62f62847467a8f78052568a80055b380/27f79cb66e381900852565b6006d2eb4?OpenDocument

~Hemanth
Avatar of IanWood

ASKER

The log says what it's supposed to, up until after the db open bit and then it is blank..

I'm just going to have a read of the links now cos I really need to get this working!!

Cheers

Ian
Ian,

Are both databases on the same server? Running the code on your client would work, but due to security the server where the code is running wouldn't be able to access a db on another server.

This is just an idea as you should get an error message in the log if this is the case.

Simon
Avatar of IanWood

ASKER

Hi Simon - No, they are on different servers (the Mail-in is on our mail server and the target db is on our development server (and it'll be moved to an application server once it's completed)!! I was hoping to have the target server and path as parameters in the mail-in db..

Heman, where you on about the agent log from the agents screen in design mode or the Notes Agent log I'm outputting debug msgs to?  The log from the agent screen in the mail-in db tells me that the agent has run and the agent log I'm writing to only has the comments up until the db open line..

Zvonko/anyone, does that mean that I can't access a different server than the one it is running on?  What would I need to do to get access?  If I put them both on the same server (administrators won't like me!!)would it work?  Is the actual code alright for running on a server (I've already removed the rather stupid NotesUIWorkspace declaration I had in to start with!!)?

I've looked through the attached links and I have to admit, I'm a bit confused..  Too much information for me to take in for the time-being!!  i think I'm running a background agent on the server and I'm the 'Owner' , not sure about 'Invoker' (Would this be the server, not me?) and I have manager rights to both db's..

Right, doesn't sound as simple as I thought it'd be('Change line x to read blah blah etc.'), so I'm going to up the points a bits..

Cheers

Ian

Avatar of IanWood

ASKER

Hi Simon - No, they are on different servers (the Mail-in is on our mail server and the target db is on our development server (and it'll be moved to an application server once it's completed)!! I was hoping to have the target server and path as parameters in the mail-in db..

Heman, where you on about the agent log from the agents screen in design mode or the Notes Agent log I'm outputting debug msgs to?  The log from the agent screen in the mail-in db tells me that the agent has run and the agent log I'm writing to only has the comments up until the db open line..

Zvonko/anyone, does that mean that I can't access a different server than the one it is running on?  What would I need to do to get access?  If I put them both on the same server (administrators won't like me!!)would it work?  Is the actual code alright for running on a server (I've already removed the rather stupid NotesUIWorkspace declaration I had in to start with!!)?

I've looked through the attached links and I have to admit, I'm a bit confused..  Too much information for me to take in for the time-being!!  i think I'm running a background agent on the server and I'm the 'Owner' , not sure about 'Invoker' (Would this be the server, not me?) and I have manager rights to both db's..

Right, doesn't sound as simple as I thought it'd be('Change line x to read blah blah etc.'), so I'm going to up the points a bits..

Cheers

Ian

ASKER CERTIFIED SOLUTION
Avatar of srandrews
srandrews

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
Avatar of IanWood

ASKER

Arhhh, that'll be it then (although I don't seem to get any error messsages, it just stops running).. I'm sure it was in the links somewhere, but agents are new to me, so probably missed it in all the detail!!

So, to get it to work I have to have them on the same server, or employ someone to manually run the agent when new mail arrives!!  ;-)

Is there any workarounds possible, or will I have to annoy our administrators and insist that the app goes on the mail server with the Mail-In?
That is how the architecture laid out. So keep it on one server to avoid this problem.

Just to confirm what others have said:
You can only access databases that are on the same server when running the agent on the server.

A tip for debugging agents :

Create scheduled agents using this framework to have complete debugging information :

Sub Initialize
    REM Initialize session and variables for the agent log
    Dim session As New NotesSession
    Dim agent as NotesAgent
    Dim db as NotesDatabase
    Dim agentLog as NotesLog
    'Define the path to the agents log database to log to
    Const LogFile$="logs/agents.nsf"
    Dim LogServer as String

    Set agent=session.CurrentAgent
    Set db=session.CurrentDatabase
    LogServer=db.Server

    REM Open agentlog and define error routine
    Set agentLog=New NotesLog(db.Title + " - " + agent.Name)
    Call agentLog.OpenNotesLog(LogServer, LogFile$)
    On Error Goto LogError
    Call agentLog.LogAction("Agent started at " + Cstr(Time))

    REM Start the regular part of your agent here
    REM Write debugging info to the log using LogAction method

    REM Close agent log and exit
    Call agentLog.LogAction("Agent finished at " & Cstr(Time))
    Call agentLog.Close
    Exit Sub

LogError:
    REM Error catching routine : logs the error to the agent log
    Call agentLog.LogError(err, "Initialize: " & Error$ & " in line " & cstr(Erl))
    Resume Next
End Sub


To log detailed information in subroutines always include an extra on error goto and error catching routine for each sub you want to have debug info on.

An example:

Sub RoutineWithDebugInfo
    REM Define local error routine
    REM You don't need to open the agent log, since this is opend in Initialize
    On Error Goto LogLocalError

    Exit Sub
LogLocalError:
    Call agentLog.LogError(Err, "RoutineWithDebugInfo: " & Error$ & " in line " & Cstr(Erl))
    Resume Next
End Sub

I hope you can use this to debug future problems. If you use this debugging info, you will always be able to find an error message if something goes wrong in your agent.
If you don't write debugging info like this, with a resume command, the agent just stops running if an error is encountered.

The agent log database is created using the Agent Log database.

If you need any further info on this, just let me know.