user3659403
user3659403

Reputation: 61

run script when xpages saving document

The xpages contain SAVE button. The xpages also contain InternetAddres field. When user click SAVE button, need to check first on names.nsf - Save success if InternetAddress value NOT found in names.nsf view "($Users)" - Save fail if InternetAddress value found in names.nsf view "($Users)"

How to write the script to do that?

This is the LotusScript version of script:

Set namesview = namesdb.GetView( "($Users)" )
Set namesdoc = namesview.GetDocumentByKey( Lcase(doc.CurrentInternetAddress( 0 ) ), True )
If ( namesdoc Is Nothing ) Then '-- Create New Doc

How to move on xpages?

Upvotes: 2

Views: 205

Answers (2)

stwissel
stwissel

Reputation: 20384

In your datasource is a querySave event. You write JS there. It is almost the same code. Just with { } and ;

Remarks:

  • your app will break when there is more than one address book, so you you would want to use @NameLookup which is quite fast and checks all addressbooks.
  • unless you need the document getEntry is faster than getDocument

In SSJS your function would look like this:

function continueWithValidUser(namesDB, addressCandidate) {
    var success = false;
    try {
       var view = namesDB.getView("($Users)");
       var doc = view.getDocumentByKey(addressCandidate);
       success = (doc != null);
       doc.recycle();
       view.recycle();
    } catch (e) {
      print(e.message);
    }        
    return success;

}

That should do the trick

Upvotes: 0

Paul Stephen Withers
Paul Stephen Withers

Reputation: 15729

The latest release of the OpenNTF Domino API adds a checkUnique() method to the View class. It takes two parameters, the first being a key to check against the view (e.g. a String or List of Strings), the second being the current document. After all, if you're checking for a pre-existing document, you don't want to fail just because it finds this document in the view.

So assuming CurrentInternetAddress is a single value field, the code would be:

function continueWithValidUser(namesDB, doc) {
    var success = false;
    try {
       var view = namesDB.getView("($Users)");
       success = view.checkUnique(doc.getItemValue("CurrentInternetAddress"),doc);
    } catch (e) {
      print(e.message);
    }        
    return success;
}

OpenNTF Domino API recycles all handles to Domino objects, so the recycle() calls aren't needed.

Upvotes: 5

Related Questions