Trouble with SaveOptions

Having some trouble with my saving on a doc I have the code below in the SaveOptions field. I also have a validation on the exiting of the username field to make sure the user has not already started a version of this doc before. Now when it runs the script and validates that this would be a duplicate, it closes the form but it saves a copy anyways. How can I tweak these 2 to validate properly but not save the duplicate.

Code in Saveoptions field:
@If(@IsNewDoc ; "1" ; "0")

Exiting code in username field:
Sub Exiting(Source As Field)
      Dim workspace As New NotesUIWorkspace
      Dim session As New NotesSession
      Dim username As String
      Dim ndoc As NotesDocument
      Dim cdoc As NotesDocument
      Set uidoc = workspace.CurrentDocument
      Set db = session.CurrentDatabase
      Set ndoc = uidoc.Document
      Set view = db.GetView("(completed)")
      Call uidoc.Save
      username = uidoc.FieldGetText( "username")
      key = username
      Set cdoc = view.GetDocumentbyKey(key)
      If cdoc Is Nothing Or cdoc.NoteID= ndoc.NoteID Then
            Exit Sub
      Else
            Msgbox "You have already started a Self-Assessment, please edit that version."
            Set doc = uidoc.Document
            doc.SaveOptions = "0"
            Call uidoc.close
      End If
End Sub
jforget1Asked:
Who is Participating?
 
mbonaciCommented:
Hi jforget1,
you're getting the error because of this line:

>  If cdoc Is Nothing Or cdoc.NoteID= ndoc.NoteID Then

I presume you thought when there's Or in If statement and the first part (if cdoc Is Nothing) evaluates to False that then the rest of the statement wont be executed.
Wrong, LotusScript isn't "smart" as C, so you must split that in two lines, like this:

    If cdoc Is Nothing Then Exit Sub
    If cdoc.NoteID= ndoc.NoteID Then Exit Sub


PS I'm behind Sjef's advices here 100%.

Hope this helps,
Marko
0
 
p_parthaCommented:
Don't put the code in the exiting event, Try putting the code in query save there you have option of not saving the document by using continue = false and that's the best way to code as the user may avoid the firing of exiting event by not tabbing the field
0
 
jforget1Author Commented:
The only problem with that is I also don't want the user to go through the whole form and then find out it is a duplicate. The entry of the form is long and would be bad to find out after all the data entry.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
p_parthaCommented:
Don;t have any formula on teh saveoptions, but just have the field, and why do u do the uidoc.save , remove that line it should work..

Partha
0
 
p_parthaCommented:
SO ideally your code should be :

Dim workspace As New NotesUIWorkspace
     Dim session As New NotesSession
     Dim username As String
     Dim ndoc As NotesDocument
     Dim cdoc As NotesDocument
     Set uidoc = workspace.CurrentDocument
     Set db = session.CurrentDatabase
     Set ndoc = uidoc.Document
     Set view = db.GetView("(completed)")
     username = uidoc.FieldGetText( "username")
     key = username
     Set cdoc = view.GetDocumentbyKey(key)
     If cdoc Is Nothing Or cdoc.NoteID= ndoc.NoteID Then
          Exit Sub
     Else
          Msgbox "You have already started a Self-Assessment, please edit that version."
          Set doc = uidoc.Document
          doc.SaveOptions = "0"
          Call uidoc.close
     End If
0
 
jforget1Author Commented:
Do I make the SaveOptions Editable or Conputed?
0
 
jforget1Author Commented:
I get an Object variable not set when I rem the Uidoc.Save line.
0
 
p_parthaCommented:
what line you get the error, use debugger and tell us the exact line that is causing the error
0
 
jforget1Author Commented:
It is happening at this line

Set cdoc = view.GetDocumentbyKey(key)
0
 
p_parthaCommented:
that means that view doesn't exist. check whether the view by that name exists, or is it spelled correctly

partha
0
 
jforget1Author Commented:
If i take off the rem on the uidoc.save line I do not get the error.
0
 
p_parthaCommented:
If you put uidoc.save  what happens is, even before u check whether teh document needs to be saved or not, it's already saved. So when you say uidoc.close , the document had been saved 20 lines before and it simply closes the document, I feel the error is somewhere else, what do u mean by take off the rem on uidoc.save , just remove that line

Partha
0
 
jforget1Author Commented:
I took that line out completely and I get the Object variable not set error again. I will keep trying.
0
 
p_parthaCommented:
Try this:

Dim workspace As New NotesUIWorkspace
     Dim session As New NotesSession
     Dim username As String
     Dim ndoc As NotesDocument
     Dim cdoc As NotesDocument
     Set uidoc = workspace.CurrentDocument
     Set db = session.CurrentDatabase
     Set ndoc = uidoc.Document
     Set view = db.GetView("(completed)")
if view is nothing then
msgbox " the view is not found please check whether u have spelled me correct, try removing the bracket and check
end if
     username = uidoc.FieldGetText( "username")
key = username
     Set cdoc = view.GetDocumentbyKey(key)
     If cdoc Is Nothing Or cdoc.NoteID= ndoc.NoteID Then
          Exit Sub
     Else
          Msgbox "You have already started a Self-Assessment, please edit that version."
          Set doc = uidoc.Document
          doc.SaveOptions = "0"
          Call uidoc.close
     End If
0
 
jforget1Author Commented:
I pasted yours in directly and get the same Object error at the same Set cdoc line as before.
0
 
p_parthaCommented:
Did you get any message like view not found.
0
 
jforget1Author Commented:
No it went right past that. I know the view is there.
0
 
p_parthaCommented:
Hey
Try removing this condition : Or cdoc.NoteID= ndoc.NoteID , why did you add it , it's never going to be the same for two documents

Partha
0
 
jforget1Author Commented:
I did not write the initial but I believe that is what is doing the comparison if there is a conflict.
0
 
p_parthaCommented:
I assume that you want to check the whole condition only if it is a new document right, for that you can just use
 If uidoc.IsNewDoc Then . I am not sure what's the use of noteid comparison

partha
0
 
Sjef BosmanGroupware ConsultantCommented:
Just barging in... ;)

Can you describe the current situation?

My questions already (backing Partha with what he suggested at the top):
- this code should never be in an Exiting of a field (don't yell, suggestion follows ;)
- why use NotesUIDocument when you can use a NotesDocument?
- why call GetDocumentByKey with only 1 parameter?
- why is key not declared
- why is there a Save before you need it (answered later on)

My suggestion (as promised):
- put the code in a Function called UserExists
- call the same function from the field's Exiting and the form's QuerySave events
- instead of the premature Save, get your logic right!

    find document for username
    if document doesn't exist then
        ok
    elseif new document then ' now we know current document is not valid!
        not ok
    elseif this.noteid=found.noteid then ' same document
        ok
    else
        not ok
    fi
    if not ok then
        Fire!!
    end if
0
 
jforget1Author Commented:
I did not write the code, got this from someone else, I just want to do an evaluation if the person has already started a form for themselves without them filling out the entire thing and then it tells them it is a duplicate. I am open to any new code or suggestions, I will try and work in the changes from Sjef.
0
 
jforget1Author Commented:
mbonaci

Adding the two lines of code you suggested worked, it did all the evaluations properly. Thanks to all for the help.

Joe
0
 
Sjef BosmanGroupware ConsultantCommented:
Just give an A next time...
0
 
mbonaciCommented:
jforget1,
Seriously, why B?
My answer was right on the spot.

http://www.experts-exchange.com/help.jsp#hi73

Marko
0
 
Sjef BosmanGroupware ConsultantCommented:
"My" ???   "Our" !!
0
 
jforget1Author Commented:
I am sorry I did not realize that was a real big deal, can I change it, I will. It was my bad, I really do appreciate the help.
0
 
mbonaciCommented:
sorry Sjef, "Our" of course, I sometimes tend to be a bit egoistic ;)

As I already said, all great tips, you know I didn't mean that ;)

Marko
0
 
Sjef BosmanGroupware ConsultantCommented:
jforget1, just put a question in Community Support asking to reopen this question. Don't forget the link to this one!
0
 
Sjef BosmanGroupware ConsultantCommented:
You can also ask them to just change the grade...
0
 
jforget1Author Commented:
I have requested this be changed.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.