0
votes

Case: Create a database (system) for a user to request for an email and update in names.nsf.

Problem found, let say we can't edit names.nsf view or create a new view just to check the uniqueness of Internet address.

sample: InternetAddress (Email address) field of a user is: TestUser1@brooke.com.my

I can't use picker validator just to validate the uniqueness of email address as there has no view and not allow to create a view in names.nsf just to sort it and use it for validation, May I know got another way to do validation~?

Sample value inside arr

array

New Coding Added on 19/07/2017

var setdoc:NotesDocument = database.getProfileDocument("System Setting", "");
var server = setdoc.getItemValueString("DBSvr");
var DName = setdoc.getItemValueString("DbPath");
var db:NotesDatabase = session.getDatabase(server, DName, false);

var vw:NotesView = db.getView("($VIMPeopleByLastName)")
var doc:NotesDocument = vw.getFirstDocument();

var arr = [];       

while (doc != null) {
    var tmpdoc = vw.getNextDocument(doc);
    arr.push(doc.getItemValueString("InternetAddress"));
    doc.recycle();  //  to prevent IBM Notes Crash use recycle  //  The recycle method unconditionally destroys an object // and returns its memory to the system.
    doc = tmpdoc;
}

value=getComponent("mail11").getValue() +"@devsvr1.pcs.com.my"

return @IsMember(value, arr);

Error

Newer Testing result:

enter image description here

-property of the field

enter image description here

-validation property field

enter image description here

-Coding part

<xp:this.expression><![CDATA[#{javascript:
var setdoc:NotesDocument = database.getProfileDocument("System Setting", "");
var server = setdoc.getItemValueString("DBSvr");
var DName = setdoc.getItemValueString("DbPath");
var db:NotesDatabase = session.getDatabase(server, DName, false);

var vw:NotesView = db.getView("($VIMPeopleByLastName)")
var doc:NotesDocument = vw.getFirstDocument();

var arr = [];       

while (doc != null) {
    var tmpdoc = vw.getNextDocument(doc);

        arr.push(doc.getItemValueString("InternetAddress"));

    doc.recycle();
    doc = tmpdoc;
}

value=getComponent("mail11").getValue() +"@devsvr1.pcs.com.my"

return @IsMember(value, arr);
}]]></xp:this.expression>
2
A shame they won't allow addition of a view to names.nsf because a (People By Email) view would make this simpler and not break anything else.David Navarre

2 Answers

2
votes

I see three possibilities to look for internet addresses without changing names.nsf:

  1. "Walk" through hidden view "($Users)" and compare your internet addresses with column InternetAddress
  2. Use database.search()

  3. Use database.FTSearch() if names.nsf is full-text indexed

Example for search:

var formula = 'InternetAddress="TestUser1@brooke.com.my":"TestUser2@brooke.com.my";
var dc:NotesDocumentCollection = database.search(formula);
if (dc.getCount() > 0) {
   // read documents' InternetAddresses and tell user which are used already
}

Example for FTsearch:

var search = '[InternetAddress]="TestUser1@brooke.com.my";
var dc:NotesDocumentCollection = database.FTsearch(search);
if (dc.getCount() > 0) {
   // tell user internet address is used already
}

Put your code into e.g. expression validator:

<xp:inputText
    id="inputMail1">
    <xp:this.validators>
        <xp:validateExpression
            message="Your Error message">
            <xp:this.expression><![CDATA[#{javascript:
                ... your code ...
                return @IsMember(value, arr);
            }]]></xp:this.expression>
        </xp:validateExpression>
    </xp:this.validators>
</xp:inputText>
<xp:message
    id="message1"
    for="inputMail1">
</xp:message>

value contains field's value to validate. Don't forget a message control to show error message to user.

2
votes

Here is a SSJS function which checks if a user with a certain ID exists in any of the available address books:

function isDominoUser(id) {
    try {
        var db,vw,doc,itr;

        itr=sessionAsSigner.getAddressBooks().iterator();
        while (itr.hasNext()) {
            db=itr.next();
            try {
                if (!db.open()) continue;
                vw=db.getView("($Users)");
                doc=vw.getDocumentByKey(id,true);
                if (doc) {
                    doc.recycle();
                    vw.recycle();
                    return true;
                }
                vw.recycle();
            } catch(err1) {}
        }

    } catch(err) {
        java.lang.System.out.println("ERROR: "+err.toString()+" [isDominoUser]");
    }
    return false;
}

Since the ($Users) view also contains the mail addresses you can use the function to find out if a user has already been registered with a certain address.

Based on the code sample you posted, your validation expression would then look like this:

return isDominoUser(getComponent("mail11").getValue()+"@devsvr1.pcs.com.my");

PS: You have to make sure that the signer at least has read access to the address books.