A @DocumentUniqueID question

I would like to do an input validation to validate if the current document is the same document in the view. In the view, the first column displays the location, second column displays the region, third column displays the division, and the fourth column is a hidden column that displays the document ID (column formula is @Text(@DocumentUniqueID).

If the document being edited is the same document that is in the view, the validation should fail. Any suggestions on how I should do this? Below is the formula I have for input validation.

lookup := @DbColumn(“” : “NoCache”; @DbName; “Location Lookup”; 4);

@If(Location = “”; @Failure(“Please enter a location”); @DocumentUniqueID = lookup; @Failure("A saved location document already exists with this name " + Location);@Success)

Subject: A @DocumentUniqueID question

Sweet,

I suspect this is one of those times that your business reason might need more explication. Is it true that if the document being edited is in that view already, then you don’t want to let the document be edited? If so, let’s not do this on validation/save, let’s do it when the doc is first opened in edit mode to save the user some grief.

Maybe I missed the boat,

raphael

Subject: RE: A @DocumentUniqueID question

Raphael, you didn’t miss the boat. I unfortunately did not explain myself clearly enough. What I’m trying to do is to prevent the user from creating a duplicate document. For example, if there is a document in the view with Location = Alsip, Division = Wire & Cable, and Region = Federal, and if the user tries to create a document with the same values entered in those 3 fields, then the validation should fail and give an error saying that there’s already a dupicate document in the view. Does this make more sense? Any help would be appreciated.

Subject: RE: A @DocumentUniqueID question

Sweet,

Yes. What I would do, and have done, is to put some code into the QuerySave event that does the lookup. Then, if it finds a match using your location/Div/Region, and if the unid doesn’t match the current doc unid, then I stop the save.

Sounds very similar to what you said originally.

regards,

raphael

Subject: RE: A @DocumentUniqueID question

Raphael (or anyone else), can you see what I’m missing in the code below? I keep receiving “Variable is not set” error and according to the Debugger, the code stops at the searchFormula line. The code is in the QuerySave event of the form.

Dim session As New NotesSession

Dim db As NotesDatabase

Dim docs As NotesDocumentCollection

Dim doc As NotesDocument

Dim searchFormula As String

Set db = session.CurrentDatabase

searchFormula = { Form = “Location Lookup” & Location = “} + doc.Location(0) + {” & Region = “} + doc.Region(0) + {” & Division = “} + doc.Division(0) + {”}

Set docs = db.Search(searchFormula, Nothing, 0)

Set doc = docs.GetFirstDocument

If docs.Count > 1Then

If (doc.UniversalID = source.Document.UniversalID) Then

Msgbox "A saved location document already exists with this name " + doc.Location(0), 0 + 48, “Duplicate Location”

Continue = False

End If

End If

source.document.Date_Edited = Now

Subject: RE: A @DocumentUniqueID question

You don’t have a handle to your doc object yet when trying to build your search criteria. That’s what’s causing your error.

Subject: A @DocumentUniqueID question

The simplest method that I’ve come up with is views.Set up a view using the same SELECT criteria as you would use for the normal view, but make column #1 the UNID of each document. Then, on QuerySave, see if the key (identifying fields) exists. If the key exists, then look for your UNID in the new view. If it exists and there’s only one doc found, then you’re OK – you’re editing the original document.

Subject: RE: A @DocumentUniqueID question

Collin, thanks for your response. I’m not quite clear on how that should work. Do you have an example of how to do this? Any other suggetions?

Subject: RE: A @DocumentUniqueID question

There are multiple ways around the issue and the variations depend on the data you need to check against. But assuming that you’ve got a single key field, try this approach in your QuerySave

  1. Look up all documents with the current key information

– set dc = view.getalldocumentsbykey( keyfielddata )

  1. Quit if there’s more than one (your key criteria is in error)

– if dc.count > 1 then (alert to the problem)

  1. check to see if it’s this document

– set doc = dc.getfirstdocument

– if doc.universalid <> ui.document.universalid then

---- this is a dupliate document. inform the user

– end if

At other times I’ve modified this slightly by adding another view that has column one holding multiple fields concatenated (when it takes multiple fields to identify the document uniquely) and the second column as the DocumentUniqueID.

From this one can GetAllDocumentsByKey and check as shown above.

Collin