Please help with Logic. Result is unexpected

Background:

We have a project management database. When a project si created a number is assigned based off a profile document. When the number is taken the profile document is incremented to reflect that.

This all works fine, however sometimes - and I cant figure out why, a duplicate number will get created. Once the duplicatew number is created all entries after that are stuck ont he same number.

To fix this we made a “fix duplicate agent”.

You select the item that needs fixing in the view and runt he agent.

Here is the code:

Sub Initialize

Dim session As New NotesSession

Dim db As NotesDatabase

Dim doc As NotesDocument

Dim currentDoc As notesDocument



Set db = session.CurrentDatabase

Set currentDoc=session.DocumentContext





Set doc = db.GetProfileDocument("Counter")

Call currentDoc.replaceItemValue( "count", Cstr( doc.getItemValue( "counter")( 0)))

Call currentDoc.Save(True,True)



Call doc.replaceItemValue( "count", doc.getItemValue( "counter")( 0) + 1) 

Call doc.Save(True,True)



Exit Sub

errorHandler:

Print "Error #" & Format$(Err) & " at line " & Format$(Erl) & ": " & Error$, 0, "Error" 

End Sub

OK the script runs fine and updates the document with the next available project number. This is great.

However when you go to the next document int he view that needs correction and run the correction tool, it does the same thing and assigns the number it just assigned!

So If you have more than one duplicated document (which is usually the case) the fixer will only work on the first one you choose to fix. It doesnt run on the others.

Is my logic wrong or is this code wrong?

Is it doing what I tell it to do, or am I telling it the wrong thing.

Any insignt is appreeciated.

Feels like christmas lights, one goes out then they all go out.

Subject: Please help with Logic. Result is unexpected.

April,

Log out of notes and then back in, and run the tool again. Did the number change? Probally, because you are using a profile document, which is cached in memory.

So why don’t you make the agent run on several documents that are checked, this way the tool only will run once.

John

Subject: RE: Please help with Logic. Result is unexpected.

Youre onto something there. I logged all the way out of Notes and then back in and now the tool works fine. I changed 5 different docs in various locations throughout the view and its working great.

I hadnt even thought of cache issues.

Subject: RE: Please help with Logic. Result is unexpected.

April - glad to have helped - if you had thought of the cache issue, I don’t think you would have posted it. I would however may the agent be able to run on selected docs, that way you can do more than 1.

good luck, john

Subject: RE: Please help with Logic. Result is unexpected.

Something to keep in mind with profile documents: they’re not good for storing increment numbers because, as you discovered, they are cached. There can also be problems when you have multiple replicas of a database (search this forum and the 4/5 one for “sequential number” for more info.)

The way we handle this is, rather than storing a number on a profile document, we have a view of all documents by number, sorted descending (so the highest number is at the top.) Each time code goes to assign a number, it looks up to the view, does a view.getfirstdocument, and gets the number of that document. It then assigns that number plus one to the new document.

Subject: Please help with Logic. Result is unexpected.

Why are you using Document Context here? Is this application a Notes or Web? I am hoping it is a Notes Application. In order to fix the count on other documents, where is the document collection in your code to loop thru and then update?