xpageslotus-noteslotus-dominoxpages-ssjsdomino-designer-eclipse

Xpages: How to get only the first record of a categorised view


I'm doing a while loop to get records in a categorised view. I want to return only the first record that meet the if condition and not the whole category. The code below returns all the records in the category that passed the 'if' condition.

var vView:NotesView = database.getView("Document");
var doc:NotesDocument = vView.getFirstDocument();
while(doc != null){ 
  if(doc.getItemValueString("Status") == 'Live'){
      //get the first in the category here where condition is met
      var pVersion = doc.getItemValueString("Version");
      var pPro = doc.getItemValueString("Pro");
  }
  var tmpDoc:NotesDocument = vView.getNextDocument(doc);
  doc.recycle();
  doc = tmpDoc;
}

View below:

enter image description here

The arrow shows the records that I would like to return.


Solution

  • Try breaking out of the loop once the doc is found:

    var vView:NotesView = database.getView("Document");
    var doc:NotesDocument = vView.getFirstDocument();
    var done = false;
    while(doc != null && !done){ 
      if(doc.getItemValueString("Status") == 'Live'){
          //get the first in the category here where condition is met
          var pVersion = doc.getItemValueString("Version");
          var pPro = doc.getItemValueString("Pro");
          done = true;
      }
      var tmpDoc:NotesDocument = vView.getNextDocument(doc);
      doc.recycle();
      doc = tmpDoc;
    }