Link to home
Start Free TrialLog in
Avatar of fayeb
fayeb

asked on

new document agent

I have an agent which is set to run on a web query save. When the agent is run it checks to see if the document is a new document by using doc.isnewnote if it is new it runs the rest of the agent if not it exits the agent.

It has been spotted that the agent has run on a document which was not new but was just edited and saved.

What does doc.isnewnote look at to see if it is a new document, what could have caused the agent to view the document as a new document? Is there a better way I can find out if it is a new document?

Please help. Thanks in advance.
Avatar of Arunkumar
Arunkumar

Could you paste the code in here so that we can see it ?

Dont see any problems with IsNewNote though...
And how did you find that the agent has run on an older document ?
IsNewNote returns true as long as the document has never been saved before.
If for example in your agent you would first save the document using the Save method and then checked using IsNewNote, IsNewNote would return false.

Therefore, as Arun stated, your agent should only execute the rest of its code on newly created documents.
Is this a single check or are there other checks you do ?

Post your code here and we'll have a look at it.
Avatar of fayeb

ASKER

Avatar of fayeb

ASKER

Avatar of fayeb

ASKER

no text
Hm, what's this no text here?
Avatar of fayeb

ASKER

no text
really no text???
Looks like you guys are pasting some special characters and EE isn't able to translate your comments to HTML.
Try pasting without special characters or retyping the text.
Give this a try,

Create a computed field hidden in read and edit mode. With the formula as @isNewDoc.

And check this field, if it is 1 then it is a new document, or else old one.

Avatar of fayeb

ASKER

Here is the code.  We noticed it as there was a submission entry for a user which no longer existed in the company.  When we checked to see if any documents had been created by the user within the database within the last month there were no new documents.  I checked the submission log form for the user as to when it was created so I could look at the logs, from the logs I got the document that was modifed and that run this agent, the document was created in 1999 and was not a new submission.

Sub Initialize
     Dim session As New NotesSession
     Dim db As notesDatabase
     Dim note As NotesDocument
     Dim item As NotesItem
     Dim view As NotesView
     Dim TotalSubmissions As Integer
     Dim TempCount As Integer
     Dim SubmissionLogDoc  As NotesDocument
     Dim Key As String
     
     Set db = session.CurrentDatabase
     Set note = session.documentcontext    
     
     If (note.IsNewNote) Then            
          Gosub UpdateSubmissions
     End If
     
     Exit Sub

     
UpdateSubmissions:

     newStaff_Name$ = Cstr(note.Staff_Name(0))
     newStaff_No$ = Cstr(note.Staff_No(0))
     Key$ = Ucase(Trim(newStaff_Name$ +" "+ newStaff_No$ ))
     If Key$ = "" Then
          Key$="-user not specified-"
     End If
     FormName ="SubmissionLog"
     Set view = db.GetView("LuSubmissionLog")
     Set SubmissionLogDoc = view.GetDocumentbykey(Key$, True)
     If (SubmissionLogDoc Is Nothing) Then
          Set SubmissionLogDoc = New NotesDocument( db )
          SubmissionLogDoc.Form = FormName
          SubmissionLogDoc.SubmitterName = Key$
          SubmissionLogDoc.SubmitterCount = 1
          Call SubmissionLogDoc.Save(True, True)
     Else
          Set item = SubmissionLogDoc.getfirstitem("SubmitterCount")
          TempCount = Cint(item.text) + 1
          Set item = SubmissionLogDoc.ReplaceItemValue  ( "SubmitterCount", TempCount )
          Call SubmissionLogDoc.Save(True, True)
     End If

     Call View.Refresh                      
     TotalSubmissions = 0
     Set SubmissionLogDoc = view.GetFirstDocument
     While Not ( SubmissionLogDoc Is Nothing )
          If (SubmissionLogDoc.SubmitterName(0) = "ZZZZZ")  Or (SubmissionLogDoc.SubmitterName(0) = "ZZZZY") Then
             
          Else
               Set item = SubmissionLogDoc.getfirstitem("SubmitterCount")
               If item.text = "" Then
                 
               Else
                    TotalSubmissions = TotalSubmissions + Cint(item.text)
               End If
          End If
          Set SubmissionLogDoc = view.GetNextDocument( SubmissionLogDoc )
     Wend
       
     Set SubmissionLogDoc = view.GetDocumentbykey("ZZZZZ", True)
     If (SubmissionLogDoc Is Nothing) Then
          Set SubmissionLogDoc = New NotesDocument( db )
          SubmissionLogDoc.Form = FormName
          SubmissionLogDoc.SubmitterName ="ZZZZZ"        
          SubmissionLogDoc.SubmitterCount = TotalSubmissions
          Call SubmissionLogDoc.Save(True, True)
     Else
          Set item = SubmissionLogDoc.ReplaceItemValue  ( "SubmitterCount", TotalSubmissions )
          Call SubmissionLogDoc.Save(True, True)
     End If
     Set SubmissionLogDoc = view.GetDocumentbykey("ZZZZY", True)
     If (SubmissionLogDoc Is Nothing) Then
          Set SubmissionLogDoc = New NotesDocument( db )
          SubmissionLogDoc.Form = FormName
          SubmissionLogDoc.SubmitterName ="ZZZZY"        
          SubmissionLogDoc.SubmitterCount = ""
          Call SubmissionLogDoc.Save(True, True)
     End If
     Return
   
Okay, I would change the code like this :

Sub Initialize
    Dim session As New NotesSession
    Dim db As notesDatabase
    Dim note As NotesDocument
    Dim item As NotesItem
    Dim view As NotesView
    Dim TotalSubmissions As Integer
    Dim TempCount As Integer
    Dim SubmissionLogDoc  As NotesDocument
    Dim Key As String
   
    Set db = session.CurrentDatabase
    Set note = session.documentcontext    

    REM Check if this is a new doc. If not, exit.
    If Not(note.IsNewNote) Then            
         Exit Sub
    End If

    REM Process document
    newStaff_Name$ = Cstr(note.Staff_Name(0))
    newStaff_No$ = Cstr(note.Staff_No(0))
    Key$ = Ucase(Trim(newStaff_Name$ +" "+ newStaff_No$ ))
    If Key$ = "" Then
         Key$="-user not specified-"
    End If

...

End Sub

This prevents the use of gosub commands (which I don't like, because they tend to make code unreadable).
If you really need to break up code in multiple parts, then use subs or function to break apart the code.
This type of code is easier to read and maintain.

Regards,
JM
fayeb, thats interesting to see it ran on an older doc.
Try Jerriths suggestion in modifying the code. Also, how frequently are you experiencing this trouble ? is it the first and only time ?
Avatar of fayeb

ASKER

This is the first one we have noticed as the user no longer exists.
Is this or any other agent scheduled to run on Newly/modified documents ???
Hello Hemanth,
The agent is called from the WebQuerySave-event of the form as stated in the original question :-)
Yes I noted that jerrith, but I assume that there should be some other agent which is causing the problem. Sometime people forget to disable scheduled agents !!!
Avatar of fayeb

ASKER

There is only one scheduled agent which is set to run once a month to reset the values back to 0.
A scheduled agent still won't change a document's status from old to new, so I don't think this has anything to do with the problem.
The IsNewNote method only checks if the document was never saved (thus is newly created) and that's it.
When used before any save method is used, it should work as documented.

Regards,
JM
I was under impression that other agent would have changed the document. SOmetimes it is a practice among developers to write a scheduled agent for testing and leave it there to run for ever and chasing the goose which is totally unrelated.

Fayeb, I believe it could be an accident. Just one document cannot prove the theory. Has it happened recently. I would ask you to log the activity via agent to agent log and closely monitor the db.
Yes, it would be a good idea to log actions to an agent log, so you can see when on on what documents the agent runs.

In case you don't exactly know how to go about it here's some sample code:

First create an empty Agent log database (or if you have an agent log database, you can use that one) based on the Agent Log template. Let's say it is in "logs/agents.nsf"

Here's how to add the logging to your existing code :

Sub Initialize
   Dim session As New NotesSession
   REM Define variables to log to the agent log
   Dim agent as NotesAgent
   Dim agentLog as NotesLog
   Const LogFile$="logs/agents.nsf"
   Dim LogServer as String
   REM End of variables to log to the agent log
   Dim db As notesDatabase
   Dim note As NotesDocument
   Dim item As NotesItem
   Dim view As NotesView
   Dim TotalSubmissions As Integer
   Dim TempCount As Integer
   Dim SubmissionLogDoc  As NotesDocument
   Dim Key As String
   
   Set db = session.CurrentDatabase

   REM Initialize the agent log
   Set agent=session.currentAgent
   LogServer=db.Server
   set agentLog=New NotesLog(db.Title + " - " + agent.name)
   call agentLog.OpenNotesLog(LogServer, LogFile$)
   REM Define a routine to log errors to the agent log as well
   On Error Goto LogError

   REM Main code
   Set note = session.documentcontext    

   REM Check if this is a new doc. If not, exit.
   If Not(note.IsNewNote) Then
        REM log action to the agent log
        Call agentLog.LogAction("Skipped old document for " & note.Staff_Name(0))
        Exit Sub
   End If

   REM Process document
   REM Log the action the agent log
   Call agentLog.LogAction("Processing new document for " & note.Staff_Name(0))
   newStaff_Name$ = Cstr(note.Staff_Name(0))
   newStaff_No$ = Cstr(note.Staff_No(0))
   Key$ = Ucase(Trim(newStaff_Name$ +" "+ newStaff_No$ ))
   If Key$ = "" Then
        Key$="-user not specified-"
   End If

...

   Exit Sub
LogError:
   REM Error routine
   Call agentLog.LogError(Err, Error$ + " in line " + cstr(Erl))
   Resume Next
End Sub

I hope this is useful, regards,
JM
Hello fayeb,

I think your chief and/or colleagues are confusing you and now you confuse us :-)

First of all, your agent does not save the submitted document; never and never.
The agent looks up a present document with the key Staff_Name+Staff_No and if not found creates a new one. And only this document contained in SubmissionLogDoc is saved.

So please state one more time what your question is.

This starts to become voodoo and is no more technical <|:-)

Avatar of fayeb

ASKER

I have already asked my question and  I feel that everyone else has grasped hold of my question and I am currently doing as suggested by Jeffith to log all info so ican track why the agent was run.  Here is further clarification:

The idea of the "log Submission" agent is to keep track of the number of new documents users have submitted for that month as they get bonuses for the number of documents they submit into the knowledge base.

The database has a main form (Reference)the user uses to input the information.  This form has a webquerysave event which runs the agent "Log Submissions".  

The agent checks to see if the current document is a new one or if it is an existing document that is being edited.  If it is an existing document is exits out and saves the canges made to the main form.
If it is new it runs the rest of the agent to get the user name stored in the cookie fields using these it checks through a view of submission logs (form = SubmissionLog), this form contains two fields user name (SubmitterName) and number of submissions(SubmitterCount).  If it finds a form where the user name is the same as the name on the main form it adds 1 to the value for SubmitterCount if it does not exist it creates a new one and sets the field to 1.  Once this is done it exists out of the agent and saves the main form.

My problems is that the agent was run on an existing document via the webquerysave but instead of it showing it was an existing document it has classed it as a new document and so created a new submission log for the user.  The reason we know it was an existing document is because the user no longer exists and the document was created in 1991, and we could not figure out why there was an entry in the view for the user saying they had submitted 1 document.

This is why I asked the question:

What does doc.isnewnote look at to see if it is a new document, what could have caused the agent to
view the document as a new document? Is there a better way I can find out if it is a new document?

Hope this helps.
Thank you for this description fayeb.

This gives a bit more insight what this all is for.
It is always a good idea to have logging in such situations where nobody can say exactly what is going on.
And this is exact the point I call voodoo: from one assumption are new assumptions resulting and questions concerns the effects of assumption based on another assumption.

You have found a document not matching to your concepts and ask now why does NotesDocument.IsNewNote disappoint you :-)

I hope you find the reason for the discrepancy by jerrith’s logging method. But still my statement is valid: documents with the form Reference are not saved directly by the agent LogSubmissions. The agent lookups again a second time the already opened document. And there can be a ton of exceptions…

Reading your description I have a question to you: do you believe that after the end of WebQuerySave agent the document is saved in any case? This assumption is wrong.

Well... this seems to be an exceptional case of an unknown accident for which no one has any log or trace or clue !  

What is to be done now ?

You know what I mean Guys ?

:-P
Arun.
As we do usually: all points to Arun!



Haa haa haa !!!

Oh yea i would be overwhelmed if i get this one on me!

:-)
Arun.
Sorry Zvonko, but the document does get saved, even if you don't explicitely save it using a save command in your agent.

Here's a little example you can use to test and see this is true:

Create a form with two fields:
Field1 : a text field you can enter any text in
Field2:  a computed when composed text field that is set by the WebQuerySave agent

On the form include a Save&Close action with the formula:
@Command([FileSave]);
@Command([FileCloseWindow]);

Also include a $$Return field that is composed for display to make the form return to the document in read mode:
"[/" + @Subset(@DbName;-1) + "($All)/" + @Text(@DocumentUniqueID) + "?OpenDocument]"

Now in the WebQuerySave agent have only the following simple code:
Sub Initialize
    Dim session as New NotesSession
    Dim doc as NotesDocument

    Set doc=session.DocumentContext
    If doc.IsNewNote Then
        doc.Field2="from WebQuerySave"
    Else
        doc.Field2="old document"
    End If
End Sub

As you can see there is no save statement in the code, however when you look at the document, you will see that the first time you save it Field2 will be set to
"from WebQuerySave" and when you edit the document and save it again Field2 will be set to "old document".

This little sample also proves that .IsNewNote method works as it should.

(To make testing easier, include an action "Edit" with the formula @Command([EditDocument]) on the form)

Regards,
JM
Sorry Arun,
no points for you in this question, these are mine :-p
Hello JM,

I have tested your scenario and was proven you are absolutely right.
Today I learned something new :-)

I was always in the imagination that the last part of the webquerySAVE event name is only a relict from R4.x times where no built in JavaScript support was offered. I was pretty sure, when the agent does not save the document context, then no document is created. You prove me the opposite. Thanks :-)

Why my web applications all worked well is simply because I never use a button with @Command([FileSave]) and never allow $$Return to decide anything. I put all the logic into an explicitly invoked agent which decides when something is to be saved and where to redirect the browser. This agent has not to be invoked by a button visible on the form. I place the button on a subform and remove the subform after reading buttons unique id. So I can call the button and the agent from any refresh event.
But this trick I told you for sure in former times…

For this question I have no more interests to be involved. I even do not understand what the question is :-)
Now is all up to you.

Cheers,
zvonko

Wow!! Big guru Zvonko learned something from small guru JM today :-)

The question is rather simple, for some mysterious reason an old document was treated as a new document by the WebQuerySave agent.
Why, nobody can tell, so I suggested to add logging in the event something similar happens in the future.

We'll see how it goes, I guess...
P.S.: I quite often use $$Return, because if a form after being saved always has to return you to the same location, this is the simplest way to do it.
On some occasions I use agents or @URLOpen or whatever seems to be the most appropriate in a given situation.

For forms that have to be filled out by a user and where he/she needs a confirmation the form was submitted I use a standard message form that reads the message text from a lookup view using a parameter passed on by the URL in the $$Return field of the submitted form.

If you want me to elaborate and describe this in detail, just let me know.
Thanks JM :-)
but my messages appear in a JavaScript popup window :-)

Better please elaborate this UUOU that I also do not know.
My thanks are sure for this :-)



Please update and finalize this old question.

Moondancer - EE Moderator
Please Zvonkos suggestion that says.....


Comment

From: zvonko  Date: 04/17/2002 12:12PM PST  

As we do usually: all points to Arun!


;-)
Arun.

I vote for granting the points to expert jerrith

Cheers,
zvonko

Hi Bro :-)

I vote for Arunkumar.....

;-)

Hi Bro :-)
Looks like this one's for me, right zvonko?
LOL
Now is the time to see who’s fried is Moondancer: your and mine or Arun's :-)

Hello Moondancer, be so kind and give the points to: jerrith

Let us see Bro ;-)

Avatar of fayeb

ASKER

I still have not found out a solution to this problem.  

I don't know if there was a corruption somewhere but the problems does not seem to of occurred again as far as i can see.

I was hoping someone may have a better solution to the code example I gave but that does not seem to be the case.

Thanks for trying
So we can thank again to Arun and his zero message comments :-)
This bombing of notification waked up fayeb to look into his question(s :-).

Ask Moondancer to speed up your deletion request fayeb.

'till next time,
zvonko

If it ever occurs again (which I doubt, since there's nothing wrong with the code as is), the logging info I suggested should provide you with sufficient data to trap the problem.
Therefore I feel I should get at least some points for effort, right?

P.S.: An attempt to beg for points like Arun :-)
You are learning fast JM :-)

Actually, with the debugging hints and the provided example to show that isNewNote works as it should I feel I did provide a valid answer for this question.
It's not our fault that the problem never occurred again :-)
Hey, we are all for points here :-)

(some tell that they are here because they love people, but...)

Do you know that 100 points have to be paid with one buck. It is a dammed sheet that we get nothing from the cake!!!

Hey Moondancer, you are the only one I believe that you love me when you say it (except my Bro :-)

(what a luck that this question is under deletion ;-)

 
Who is that some people Zvonko, Is it Stamp ???
OK, question to you: how did you get this 150.000 points? Helping people? I say it to you: you earned points! That's all.

That is not that question, I just wanted you to share the information with everyone here ...
We are long enough together here, you and I, and you caught that I mean You (and not Stamp :) talking about altruistic people :-)

(dammed! this question is not going to be deleted; at the end JM gets this 200 points whit an C-- :)

To get this a bit funny look at my last misery:
https://www.experts-exchange.com/questions/20311320/Modify-Script-for-Rollover.html

Special note to questioner's comments :)
But what did I expect from a lawyer office :(

You deserve it man... you know why ? You play game with double names.  Who knows you might be the rest 13 too!!!  

Infact "C" is too good for you. you should have gotten a "Z" if there was one...

LOL !

;-)

Moon....Moon....My Dear MoonDancer....Please please....Give this one to me..... !

How was that Zvo ?  Nice lil tune to cheerup MoonDancer so that she gives it to me... I will buy her a pint...
Hey, keep quiet today I'm watching the WorldCup game between Russia and Belgium. We need to win to go through to the second round.

Anyway, I'm confident these points are mine :-)

So how was your vacation, moondancer?
I've posted a few comments in the question you linked to, zvonko. I think they'll get the message :-)
What a dumb way to ask a question. Link to a text file that will most likely be removed ten minutes after the problem is solved.
So if people return to the question to see if it could be a solution to their problem, they have no idea what the question was about. He should get an F for asking the question that way!!
It is his/her question, but I am still thinking about pasting my version of this idiotic html. The html is full of content concerning this lawyer office web site and therefore I hesitate (you know: lawyer :-)

I know also why I got a C; because he/she recognized that the question is stupid: it does not make sense to flash the menu around at every mouseOver. And this shame is perhaps the reason for no additional comments :-)

Oh, and thank you helping me :)

After our first crash I think you are the right one for teasing Arun when I retire from here ;)

(in the mean while we do it together :-)

Hi Bro :-)

PS: I was on an isle in Croatia when thy loose against Mexico. Hey man, they where angry :)

Belgium just won from Russia 3-2 in a very thrilling game.
As we say here:
Merci, Wesley, merci!!!
And why not Marc and Johan? Only because it does not rhyme? :)

Because he scored the second goal and thus made it possible for us to relax a bit and put the pressure on Russia.
I must admit that Johan's free kick was an absolute beauty. Perfectly curved around the wall and in the upper corner of the goal.
Marc is always important in the Belgian team because he works hard in defense and is dangerous as an offensive player as well. After all, he did score 3 goals in 3 games!
Zvonko be open and be friendly. That is my policy. In my life I helped lots of people but got **** from them, I don't care about that, after looking the ways that you look at it, Be greedy and be selfish the policies that I should learn from you. Will follow the path ... But limit sarcacism, as it will bite you in future.
Sorry, but it was no sarcasm. It was meant as truth valid for you and for me.

And do not be offended; you asked for it.

At the moment I sacrifice many of my valuable time for the church. But I do not see it as a sacrifice for other people. It is in first place for me. So I see the job we are doing here: we do it to other people but to be recognized as helper (as valuable helper). And here comes the points into play. That is all I liked to point out. Do you see it other way?


;-)

ASKER CERTIFIED SOLUTION
Avatar of SpideyMod
SpideyMod

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