Cannot get queryTask with query to return expected result

4282
7
Jump to solution
01-14-2015 09:28 AM
MichelleRogers1
New Contributor III

I am working with an existing website that I did most of the code on.  It is a Emergency Operations Center website with automatic vehicle tracking as well as history queries.  I was able to query the history for the vehicles with no problem, but I am now trying to query the history of the 911 calls and am coming across some issues.  Any help is greatly appreciated!

Here is the code I am working with:

window.Calls911HistoricalUrl = "http://gis10204:6080/arcgis/rest/services/GeoEvent/AVL_911_Historical_WebOut/FeatureServer/0";
window.Calls911HistoricalOutFields = ["PhoneNumber", "CallType", "TimeStamp"];

registry.byId("execute911bynumber").on("click", execute911bynumber);
  function execute911bynumber(){
  var queryTask = new QueryTask(window.Calls911HistoricalUrl);
  var query = new Query();
  query.returnGeometry = false;
  query.outFields = window.Calls911HistoricalOutFields;
  query.where = "PhoneNumber = " +"'"+number+"'";
  queryTask.execute(query, function(results) {
  var data = [];
  var data = array.map(results.features, function(feature) {
  return {
  //property names used here match those used when creating the dgrid                  
  "Phonenumber": feature.attributes[window.Calls911HistoricalOutFields[0]],                  
  "Calltype": feature.attributes[window.Calls911HistoricalOutFields[1]],
  "Timereceived": feature.attributes[window.Calls911HistoricalOutFields[2]]
  }
  });
  var memStore = new Memory({ data: data });
  //create historical data dgrid
  window.grid2 = new (declare([OnDemandGrid, Selection]))({
  bufferRows: Infinity,
  store:memStore,
  maxRowsPerPage: 5000,
  columns: {         
  "Phonenumber": "Phone Number",
  "Calltype": "Call Type",
  "Timereceived": {"label": "Time Received", "formatter": formatTimestamp}
  },
  loadingMessage: "Loading data...",
  noDataMessage: "No records found."
  }, "grid2");
  grid2.set("sort", [{attribute:"Timereceived", descending:true}]);
  });
  grid2.setStore();
  }

The html code is as follows:

<div data-dojo-type="dijit/layout/ContentPane" id="911historyPane"
  data-dojo-props="title:'911 History'" style="background-color:#bee8ff;">
  <div>
  Phone Number: <input type="text" id="number" size="20">
  </div>
  <div>
  Start Date & Time: <input id="911date" type="text" size="20"><a href="javascript:NewCal('911date', 'mmddyyyy')"><img src="images/cal.gif" width="18" height="18" border="0"></a>
  </div>
  <div>
  End Date & Time: <input id="911date2" type="text" size="20"><a href="javascript:NewCal('911date2', 'mmddyyyy')"><img src="images/cal.gif" width="18" height="18" border="0"></a>
  </div>
  <div>
  <button id ="execute911bydate" data-dojo-type="dijit.form.Button" type="button" data-dojo-attach-point="button" title="Search By Date">Search By Date
  </button>
  <button id ="show911" data-dojo-type="dijit.form.Button" type="button" data-dojo-attach-point="button" title="Show on Map" >Show on Map
  </button>
  <button id ="clear911" data-dojo-type="dijit.form.Button" type="button" data-dojo-attach-point="button" title="Clear" >Clear History
  </button>
  </div>
  <div>
  <button id="execute911bynumber" data-dojo-type="dijit.form.Button" type="button" data-dojo-attach-point="button" title="Search By Number"> Search By Number
  </button>
  <button dojotype="dijit.form.Button" id="911historyHelp" title="Help">
  <img src="images/Help.png" width=15px height=15px/>
  </button>
  </div>
  <div id="grid2" class="claro"></div>
  </div>

I was able to search on date alone and come up with the correct information, but when I try and search on phone number alone, I am getting no results even though I know that there are at least 5 calls for that phone number.  I have also tried using query.text = dom.byId("number").value; but using that throws an error, "unable to complete operation".  Again, any help figuring this out is greatly appreciated.

Michelle

1 Solution

Accepted Solutions
JoelBennett
MVP Regular Contributor

The only problem I see here is in the line:

query.where = "PhoneNumber = " +"'"+number+"'";

The problem is that I don't see where the variable "number" is declared or assigned a value.  Unless you've declared and assigned the value elsewhere, this could be the main issue. Perhaps the following might make a difference:

query.where = "PhoneNumber = '" + dom.byId("number").value + "'";

View solution in original post

7 Replies
RobertScheitlin__GISP
MVP Emeritus

Michelle,

   What is the field type of your PhoneNumber? If it is a number and not a text then your SQL should not wrap the number in quotes like you do for text.

0 Kudos
MichelleRogers1
New Contributor III

Robert,

The field type for PhoneNumber is a string because it has the dashes included, not just the numbers.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Have you done a

console.info(query.where);

to see what the where clause looks like?

0 Kudos
MichelleRogers1
New Contributor III

Robert,

This is what the console log shows when I do a console.info(query.where);

PhoneNumber ='[object HTMLInputElement]'

I don't see anything wrong with it, but the response that I get is this:

{"objectIdFieldName":"OBJECTID","globalIdFieldName":"","features":[]}

So, it is returning an empty set for some reason, even though the query is correct and when I do the query from the rest endpoint, I get 5 records like I am expecting.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Michelle,

PhoneNumber ='[object HTMLInputElement]' is not a valid SQL Statement. Like Joel posted above try

query.where = "PhoneNumber = '" + dom.byId("number").value + "'";

0 Kudos
JoelBennett
MVP Regular Contributor

The only problem I see here is in the line:

query.where = "PhoneNumber = " +"'"+number+"'";

The problem is that I don't see where the variable "number" is declared or assigned a value.  Unless you've declared and assigned the value elsewhere, this could be the main issue. Perhaps the following might make a difference:

query.where = "PhoneNumber = '" + dom.byId("number").value + "'";

MichelleRogers1
New Contributor III

Joel,

That got it!  The only thing I changed was adding in a single quote in front of the value.  The correct code looks like this:

query.where = "PhoneNumber="+"'"+dom.byId("number").value+"'";

0 Kudos