3
votes

There is an inputText - string ( representing company name ): txt_NumeCompanie Let's suppose there are already 2 docs. saved having the above field value: ABC and Xpages.

What I want to do:

  1. If I create a new doc. and I complete the company name field with ABC or Xpages, a message to appear letting me know a document exists already having that name.
  2. If I open the document having Xpages as the company name and I'm changing it to ABC again the above message.

What I've tried: the onChange event of the field refreshes a panel which contains a <xp:div styleClass="lotusMessage". This div has the following rendered property:

     if ( currentDocument.isNewNote())
{
if ( @IsMember(Cdoc.getItemValueString("txt_NumeCompanie"),@Unique(@DbColumn(@DbName(),"vwComp",0))) )
    { return true;}
     else { return false; }
} 
else
{   
var newe = Cdoc.getItemValueString("txt_NumeCompanie");   
if ( @IsMember(newe,@Unique(@DbLookup(@DbName(),"vwCompanii",Cdoc.getDocument().getUniversalID(),2))) ) 
    { return false;}
     else { return true; }
}

The vwCompanii is having one column listing all the txt_NumeCompanie values and vwCompanii is having 2 columns: @Text(@DocumentUNiqueID)) and txt_NumeCompanie'. The above code works fine for .isNewNote() documents.But, if I opened an existing doc. ( having txt_NumeCompanie = ABC ) the message appears again, without changing anything. I do want to cover also the 2. case from the above item list.

Also, by opening an existing doc. from the viewPanel, when the doc. is opened and displayed, the script message appears immediately even if the value of txt_NumeCompanie hadn't been changed. How can I make the script message to be fired only when the onChange event is taking place, and not when the document is opened?

How can I achieve this?

2

2 Answers

3
votes

@DbColumn checking won't work for you here. The method to take is to access the NotesView, then look for a document matching the key, then check whether that document has a different UNID to the current document.

It's a common requirement, which is why for the latest release of OpenNTF Domino API I added View.checkUnique(key, srcDoc), see https://github.com/OpenNTF/org.openntf.domino/blob/master/org.openntf.domino/src/org/openntf/domino/impl/View.java#L2645-2663. (Line numbers will have changed in future releases). Bear in mind this code relies on constructs in the core of OpenNTF Domino API, like easy iteration, auto-recycling etc.

0
votes

The algorithm is simple: lookup document by key value and allow to save it if and only if:

  • for new document, there is no such key
  • for saved document, there is exactly one key used in current document (unids match).

Snippet:

try {
    var unid = @DbLookup("", "view", "key", 1, '[RETURNDOCUMENTUNIQUEID]');
    if (unid && unid instanceof String) {
        if (doc.isNewNote()) {
            // key was found, that's bad
            return false; //(do not continue)
        } else {
            return unid == doc.getUniversalID();
        }
    } else if (!unid) {
        return true; // key not found, it's unique, go
    } else {
        return false; // duplicates found
    }
}

http://www-01.ibm.com/support/knowledgecenter/SSVRGU_8.5.3/com.ibm.designer.domino.main.doc/H_DBLOOKUP_NOTES_DATABASES.html