0

The code below works on success, or if there's a serious error (like the name of the list being wrong) but if the item is not found I receive no error and it does nothing. I just want an alert if the search returns no list item.

var EmpNum = $("#searchTxt").val()
EmpNum = $.trim(EmpNum);
if (EmpNum == "")
{
    alert("Please enter an Employee Number!"  );

}
else
{
 var clientContext = new SP.ClientContext();
 var targetList = clientContext.get_web().get_lists().getByTitle('Employees');
 var query = new SP.CamlQuery();

 query.set_viewXml(
     "<View>" +
     "<Query>" +
     "<Where>" +
     "<Eq>" +
     "<FieldRef Name='Title'/>" +
     "<Value Type='Text'>" + EmpNum + "</Value>" +
     "</Eq>" +
     "</Where>" +
     "</Query>" +
     "<ViewFields>" +
     "<FieldRef Name='EmployeeLastName' />" +
     "<FieldRef Name='EmployeeFirstName' />" +
     "</ViewFields>" +
     "</View>");

 listItems = targetList.getItems(query);
 clientContext.load(listItems);
 clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));

Here's the Succeeded:

function onQuerySucceeded() {
 var listEnumerator = listItems.getEnumerator();
 var EmpNum = $("#searchTxt").val()  
 while (listEnumerator.moveNext()) {
     var currentItem = listEnumerator.get_current();
     var itemEmployeeLastName = currentItem.get_item('EmployeeLastName');
     var itemEmployeeFirstName = currentItem.get_item('EmployeeFirstName');
    PopulateFields(EmpNum, itemEmployeeFirstName, itemEmployeeLastName );
    ;
 }

}

| improve this question | | | | |
  • 2
    You have to build that into your success function to see if the result set is empty. What does that look like? – Eric Alexander Jan 5 '17 at 17:35
  • 1
    onQuerySucceeded will be hit if the http request is successful, not if it returned what you were expecting. What is your this.onQuerySucceeded function? This is where you need to inspect the output. – Eric Alexander Jan 5 '17 at 18:27
  • Added. I thought this is where it might be, but didn't know how to inspect the output. – J. Simonsen Jan 5 '17 at 18:30
  • I got it. I just found the count on listItems and if it is zero it throws an alert. I'll update the above code. Thanks for the help on this. – J. Simonsen Jan 5 '17 at 18:43
1

The onQuerySucceeded function will fire if the http request is successful, it knows nothing about the output. You have to put that into the function. You'd have something like this:

function onQuerySucceeded() {
  if (listItems.get_count() == 0) {
    alert("No matching records.");
  }
  else{
    var listEnumerator = listItems.getEnumerator();
    var EmpNum = $("#searchTxt").val()  
    while (listEnumerator.moveNext()) {
      var currentItem = listEnumerator.get_current();
      var itemEmployeeLastName = currentItem.get_item('EmployeeLastName');
      var itemEmployeeFirstName = currentItem.get_item('EmployeeFirstName');
      PopulateFields(EmpNum, itemEmployeeFirstName, itemEmployeeLastName );
    }
  }
}
| improve this answer | | | | |
0

I needed to check the output.

function onQuerySucceeded() {

 var listEnumerator = listItems.getEnumerator();
 var EmpNum = $("#searchTxt").val()  
 while (listEnumerator.moveNext()) {
     var currentItem = listEnumerator.get_current();
     var itemEmployeeLastName = currentItem.get_item('EmployeeLastName');
     var itemEmployeeFirstName = currentItem.get_item('EmployeeFirstName');
    PopulateFields(EmpNum, itemEmployeeFirstName, itemEmployeeLastName );
    ;
 }
   //Added below check
  if (listItems.get_count() == 0)
 {
    NotFound()
 }

}

| improve this answer | | | | |
  • Correct, but do it like Eric shows in his answer, a proper if-then-else construct so you are not wasting CPU cycles when there are no items – Danny '365CSI' Engelman Jan 5 '17 at 20:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.