Xpages displaying a message if the field value is already in the list
There is an input line Text (representing the name of the company): txt_NumeCompanie
Suppose there are already 2 documents. saved with the above field value: ABC
and Xpages
.
What I want to do:
- If I create a new document. and I fill in the company name field with
ABC
orXpages
so that a message appears letting me know that the document already has that name. - If I open the document with
Xpages
as the company name and I change it back to theABC
above message.
What I tried: the onChange
field event updates the panel containing the <xp:div styleClass="lotusMessage"
. This div has the following displayable 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; }
}
vwCompanii
has one column listing all values txt_NumeCompanie
and vwCompanii
has 2 columns: @Text(@DocumentUNiqueID))
and txt_NumeCompanie
'. The above code works fine for .isNewNote () documents. But if I opened an existing document. (with txt_NumeCompanie = ABC) the message appears again without changing anything. I also want to cover the 2. case from the above list of items.
Also by opening an existing document. from viewPanel when document. is opened and displayed, the script message appears immediately, even if the value has txt_NumeCompanie
not been changed. How can I make the script message fired only when the onChange event occurs and not when the document is opened?
How can I achieve this?
source to share
@Db Checking columns is not suitable for you here. The method to be taken is to access the NotesView and then look for the document that matches the key and then check if that document has a different UNID for the current document.
This is a general requirement, so for the latest 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 change in future releases.) Keep in mind that this code is based on constructs in the core of the OpenNTF Domino API, such as simple iteration, automatic refactoring, etc.
source to share
The algorithm is simple: find the document by key value and allow it to be saved if and only if:
- no such key for a new document
- there is exactly one key used in the current document for the saved document (unids match).
Excerpt:
try {
var unid = @DbLookup("", "view", "key", 1, '[RETURNDOCUMENTUNIQUEID]');
if (unid && unid instanceof String) {
if (doc.isNewNote()) {
// key was found, that bad
return false; //(do not continue)
} else {
return unid == doc.getUniversalID();
}
} else if (!unid) {
return true; // key not found, it unique, go
} else {
return false; // duplicates found
}
}
source to share